automation of object posing with an armature

I have an interactive procedure that I’m trying to automate via, but I’m just getting started, and context issues are driving me crazy! I think I’ve got all of the pieces I need, but I just can’t get it to work. The script loads an armature (bonus points if anybody can help me save/load just the armature!), imports an obj file, parents the object to a bone, poses the armature (and attached object), then re-exports the object in its new location. It generally fails in a call to either bpy.ops.object.parent_set() or bpy.ops.object.mode_set() and complains about the context. Can anybody help me debug it?


import bpy
    # open the session file, which includes the armature and interface config
    blend_file = '/home/slice/Dropbox/MyMiniMatic/blender/mmm_poser_prime_interface.blend'
    bpy.ops.wm.open_mainfile(filepath=blend_file)

    # posing parameters
    input_path = '/home/slice/mmm/JRifle.OBJ'
    output_path = '/home/slice/mmm/JRifle2.OBJ'
    parent_bone = 'Bone.lhand'
    pose_name = 'jrifle_chest'

    # change the current (script window) to 3d view (this helps sometimes)
    bpy.context.area.type = "VIEW_3D"

    # select anything not called armature and delete it 
    # this is cleanup, for multiple calls
    for i in bpy.data.objects:
        if i.name == 'Armature':
            i.select = False
        else:
            i.select = True
    bpy.ops.object.delete() 

    # load up the piece
    # get a list of objects before and after so we can find the name of what came in
    names = [x.name for x in bpy.data.objects]
    bpy.ops.import_scene.obj(filepath=input_path)
    imported = [x.name for x in bpy.data.objects if x.name not in names]

    # assuming a single piece, it is selected already, as the most recent object.
    # make the left hand bone "active"
    bpy.data.objects['Armature'].data.bones.active = bpy.data.objects['Armature'].data.bones[parent_bone]

    # SOMETIMES IT FAILS HERE (in parent_set)
    if rigid:
        bpy.ops.object.parent_set(type='BONE')
    else:
        bpy.ops.object.parent_set(type='ARMATURE_AUTO')

    # find a pose in the pose library that matches pose_name and pose
    pose_library = bpy.data.actions['Tpose'].pose_markers
    ix = [x.name for x in pose_library].index(pose_name)
    
    # SOMETIMES IT FAILS HERE INSTEAD (in mode_set)
    bpy.ops.object.mode_set(mode='POSE')
    bpy.ops.poselib.apply_pose(pose_index=ix)

    # re-export the object in its new position to the output file
    # select all to clear, then select objects by name
    for i in bpy.data.objects:
        if i.name in imported:
            i.select = True
        else:
            i.select = False

    bpy.ops.export_scene.obj(filepath=output_path, use_materials=False, use_selection=True)


mmm_poser_prime_interface.blend (457 KB)

JRifle.OBJ.zip (245 KB)

Forgive my ignorance, as I’m new to blender. I think these files are linked together, and so the file that actually contains the armature was not availible. Here, I think, is the parent file:
mmm_poser_prime.blend (760 KB)