Advertisement
Velepexon

VexUpdatedRig IK/FK Script

Apr 7th, 2025
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.04 KB | None | 0 0
  1. # THIS SCRIPT WAS GENERATED BY CHATGPT
  2. #
  3. import bpy
  4.  
  5. # Mapping for IK to FK
  6. ik_to_fk_map = {
  7.     "FK_Arm_1_L": "IK-TO-FK-SNAP_ARM_1_L",
  8.     "FK_Arm_2_L": "IK-TO-FK-SNAP_ARM_2_L",
  9.     "FK_Wrist_L": "IK-TO-FK-SNAP_WRIST_L",
  10.     "FK_Leg_1_L": "IK-TO-FK-SNAP_LEG_1_L",
  11.     "FK_Leg_2_L": "IK-TO-FK-SNAP_LEG_2_L",
  12.     "FK_Ankle_L": "IK-TO-FK-SNAP_ANKLE_L",
  13.  
  14.     "FK_Arm_1_R": "IK-TO-FK-SNAP_ARM_1_R",
  15.     "FK_Arm_2_R": "IK-TO-FK-SNAP_ARM_2_R",
  16.     "FK_Wrist_R": "IK-TO-FK-SNAP_WRIST_R",
  17.     "FK_Leg_1_R": "IK-TO-FK-SNAP_LEG_1_R",
  18.     "FK_Leg_2_R": "IK-TO-FK-SNAP_LEG_2_R",
  19.     "FK_Ankle_R": "IK-TO-FK-SNAP_ANKLE_R",
  20. }
  21.  
  22. # Mapping for FK to IK
  23. fk_to_ik_map = {
  24.     "Wrist_IK_L": "FK-TO-IK-SNAP_WRIST_IK_L",
  25.     "POLE_Elbow_L": "FK-TO-IK-SNAP_POLE_ELBOW_L",
  26.     "CTRL_FOOT_L": "FK-TO-IK-SNAP_CTRL_FOOT_L",
  27.     "POLE_Knee_L": "FK-TO-IK-SNAP_POLE_KNEE_L",
  28.  
  29.     "Wrist_IK_R": "FK-TO-IK-SNAP_WRIST_IK_R",
  30.     "POLE_Elbow_R": "FK-TO-IK-SNAP_POLE_ELBOW_R",
  31.     "CTRL_FOOT_R": "FK-TO-IK-SNAP_CTRL_FOOT_R",
  32.     "POLE_Knee_R": "FK-TO-IK-SNAP_POLE_KNEE_R",
  33. }
  34.  
  35. def snap_and_bake(snap_map):
  36.     obj = bpy.context.object
  37.     if obj.mode != 'POSE':
  38.         bpy.ops.object.mode_set(mode='POSE')
  39.  
  40.     frame = bpy.context.scene.frame_current
  41.  
  42.     for target_bone, source_bone in snap_map.items():
  43.         try:
  44.             pbone = obj.pose.bones[target_bone]
  45.             source = obj.pose.bones[source_bone]
  46.  
  47.             # Add Copy Transforms
  48.             constraint = pbone.constraints.new(type='COPY_TRANSFORMS')
  49.             constraint.name = "TEMP_Snap"
  50.             constraint.target = obj
  51.             constraint.subtarget = source.name
  52.  
  53.             # Select the bone
  54.             for b in obj.data.bones:
  55.                 b.select = False
  56.             obj.data.bones[target_bone].select = True
  57.             bpy.context.view_layer.objects.active = obj
  58.  
  59.             # Bake the frame
  60.             bpy.ops.nla.bake(
  61.                 frame_start=frame,
  62.                 frame_end=frame,
  63.                 only_selected=True,
  64.                 visual_keying=True,
  65.                 clear_constraints=False,
  66.                 clear_parents=False,
  67.                 use_current_action=True,
  68.                 bake_types={'POSE'}
  69.             )
  70.  
  71.             # Remove ONLY the Copy Transforms constraint we just added
  72.             for con in reversed(pbone.constraints):
  73.                 if con.type == 'COPY_TRANSFORMS' and con.name == "TEMP_Snap":
  74.                     pbone.constraints.remove(con)
  75.                     break
  76.  
  77.         except KeyError:
  78.             print(f"Bone {target_bone} or {source_bone} not found, skipping.")
  79.  
  80. # --- Run based on selection
  81. def main():
  82.     selected = [b.name for b in bpy.context.selected_pose_bones]
  83.     to_run = {}
  84.  
  85.     # Handle IK to FK
  86.     for tgt, src in ik_to_fk_map.items():
  87.         if tgt in selected or src in selected:
  88.             to_run[tgt] = src
  89.  
  90.     # Handle FK to IK
  91.     for tgt, src in fk_to_ik_map.items():
  92.         if tgt in selected or src in selected:
  93.             to_run[tgt] = src
  94.  
  95.     snap_and_bake(to_run)
  96.  
  97. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement