0
$\begingroup$

Trying to craft a simple script that checks the rig_id of the currently selected Rigify armature, and then snaps FK to IK. This one liner works: bpy.ops.pose.rigify_generic_snap_ja0c9ogre41cfd56(output_bones='["upper_arm_fk.L", "forearm_fk.L", "hand_fk.L"]', input_bones='["upper_arm_ik.L", "MCH-forearm_ik.L", "MCH-upper_arm_ik_target.L"]', ctrl_bones='["upper_arm_ik.L", "upper_arm_ik_target.L", "hand_ik.L"]'), but the rig_id is hardcoded here.

import bpy

# Get the active object
active_object = bpy.context.active_object

# Check if the active object is an armature
if active_object and active_object.type == 'ARMATURE':
    # Check if the armature is a Rigify rig
    if "rig_id" in active_object.data:
        # Get the rig_id
        a_rig_id = active_object.data["rig_id"]
        
        a_output_bones='["upper_arm_fk.L", "forearm_fk.L", "hand_fk.L"]'
        a_input_bones='["upper_arm_ik.L", "MCH-forearm_ik.L", "MCH-upper_arm_ik_target.L"]'     
        a_ctrl_bones='["upper_arm_ik.L", "upper_arm_ik_target.L", "hand_ik.L"]'
        
        # Substitute "CURRENT_RIG" with the rig_id in the command
        command = f'bpy.ops.pose.rigify_generic_snap_{a_rig_id}({a_output_bones}, {a_input_bones}, {a_ctrl_bones})'
        
        # Execute the command
        exec(command)
    else:
        print("The selected object is not a Rigify rig.")
else:
    print("No armature object selected.")

The error I'm getting is:

Python: Traceback (most recent call last):
  File "E:\Blender Assets Out\DAZ Pose Assets7.blend\Text.003", line 21, in <module>
  File "<string>", line 1, in <module>
  File "C:\Program Files\Blender Foundation\Blender 4.0\4.0\scripts\modules\bpy\ops.py", line 106, in __call__
    C_exec, C_undo = _BPyOpsSubModOp._parse_args(args)
  File "C:\Program Files\Blender Foundation\Blender 4.0\4.0\scripts\modules\bpy\ops.py", line 60, in _parse_args
    raise ValueError("1-2 args execution context is supported")
ValueError: 1-2 args execution context is supported
$\endgroup$
1
  • $\begingroup$ I would suggest that you print(command) before the call to exec(command), and edit your question to include the printed string. exec() is not blender specific, so someone might scold you in this forum... $\endgroup$
    – james_t
    Mar 4 at 17:12

1 Answer 1

0
$\begingroup$

Just my noob Python. I had to escape the characters properly to make it work. Funny that ChatGPT 4 didn't escape the characters inside the F string in the original script that it gave me, similar to the working script below.

This is working:

import bpy

# Get the active object
active_object = bpy.context.active_object

# Check if the active object is an armature
if active_object and active_object.type == 'ARMATURE':
    # Check if the armature is a Rigify rig
    if "rig_id" in active_object.data:
        # Get the rig_id
        a_rig_id = active_object.data["rig_id"]
        
        # Substitute "CURRENT_RIG" with the rig_id in the command
        
        command = f'bpy.ops.pose.rigify_generic_snap_{a_rig_id}(output_bones=\'[\"upper_arm_fk.L\", \"forearm_fk.L\", \"hand_fk.L\"]\', input_bones=\'[\"upper_arm_ik.L\", \"MCH-forearm_ik.L\", \"MCH-upper_arm_ik_target.L\"]\', ctrl_bones=\'[\"upper_arm_ik.L\", \"upper_arm_ik_target.L\", \"hand_ik.L\"]\')'
    
        # Execute the command
        exec(command)
    else:
        print("The selected object is not a Rigify rig.")
else:
    print("No armature object selected.")
```
$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .