EXCEPTION_ACCESS_VIOLATION Blender 2.8 Python I'm running a script that appends a rig and parents it to a certain mesh but every time I run it I get an error: EXCEPTION_ACCESS_VIOLATION. I believe it has to do with too many bpy.ops.object.posemode_toggle()s because when I delete some of them from the code, everything works fine except for the places where I deleted them. I have been directed here: https://docs.blender.org/api/2.78b/info_gotcha.html#help-my-script-crashes-blender but it didn't seem to help. Does anyone know how to fix this?

Additionally, if I deleted the append lines, the code works fine.

Here is my code (it's very very messy): https://pastebin.com/SEd67Q9G
Do not use operators from Python, where possible. You will probably need to remake your code to just operate on bpy.types.Armature.pose.bones.

The problem I think comes from the fact that are you changing things like bone parenting from the pose mode context, while Blender expects that to be done in edit mode. But it is a bad idea to switch modes and do this with operators as they are slower than just assigning data with Python directly.