import bpy import math from . import ctrl_bones_shape_struct from . import ctrl_bones_color_struct from . import ik_bones_struct from . import deform_bones_struct from . import ctrl_ik_struct # Modify Control Bone Shape Function def modifyctrlbone(bone_name, rotx, roty, rotz, shape_scale, shape_obj_name, wire_width, bone_color, armature): edit_ctrl_bone = armature.pose.bones.get(bone_name) shape_object = bpy.data.objects.get(shape_obj_name) if edit_ctrl_bone: # Modify control bone shape and color edit_ctrl_bone.custom_shape = shape_object edit_ctrl_bone.use_custom_shape_bone_size = False edit_ctrl_bone.custom_shape_rotation_euler[0] = math.radians(rotx) edit_ctrl_bone.custom_shape_rotation_euler[1] = math.radians(roty) edit_ctrl_bone.custom_shape_rotation_euler[2] = math.radians(rotz) edit_ctrl_bone.custom_shape_scale_xyz[0] = shape_scale edit_ctrl_bone.custom_shape_scale_xyz[1] = shape_scale edit_ctrl_bone.custom_shape_scale_xyz[2] = shape_scale edit_ctrl_bone.use_custom_shape_bone_size = False edit_ctrl_bone.custom_shape_wire_width = wire_width edit_ctrl_bone.color.palette = bone_color # Modify Bone Color Function def modifyctrlbonecolor(bone_name, bone_color, armature): edit_ctrl_bone = armature.pose.bones.get(bone_name) if edit_ctrl_bone: # Modify control bone color only edit_ctrl_bone.color.palette = bone_color # Add IK Function def addIK(bone_name, ik_target, ik_pole_target, ik_chain, armature): pose_bone = armature.pose.bones.get(bone_name) if pose_bone: for constraint in list(pose_bone.constraints): pose_bone.constraints.remove(constraint) ik_constraint = pose_bone.constraints.new(type='IK') ik_constraint.target = armature ik_constraint.subtarget = ik_target if ik_pole_target != "none": ik_constraint.pole_target = armature ik_constraint.pole_subtarget = ik_pole_target ik_constraint.chain_count = ik_chain print(f"Successfully added IK constraint to {bone_name}") # Add Copy Scale Function def addCopyScale(bone_name, parent_bone, parent_influence, armature): pose_bone = armature.pose.bones.get(bone_name) if pose_bone: for constraint in list(pose_bone.constraints): pose_bone.constraints.remove(constraint) constraint = pose_bone.constraints.new(type='COPY_SCALE') constraint.target = armature constraint.subtarget = parent_bone constraint.influence = parent_influence # print(f"Successfully added Copy Scale constraint to {bone_name}") # Add IK Controller Function def addIKcontroller(bone_name, ctrl_empt, armature): pose_bone = armature.pose.bones.get(bone_name) if pose_bone: for constraint in list(pose_bone.constraints): pose_bone.constraints.remove(constraint) ik_ctrl_constraint = pose_bone.constraints.new(type='CHILD_OF') ik_ctrl_constraint.target = armature ik_ctrl_constraint.subtarget = ctrl_empt print(f"Successfully added ChildOf constraint to {bone_name}") # # Calculate and apply the inverse matrix so the object doesn't jump position # ik_ctrl_constraint.inverse_matrix = ctrl_empt.matrix_world.inverted() # Star Generating Control Rig def generatecontrolrig(): armaturecheck = bpy.data.objects.get("Armature") if armaturecheck: print("Generating Control Rig") new_amt_obj = bpy.data.objects.get("Armature") # =========== Setup Control Rig Bone Shapes =========== custom_bone_shape = ctrl_bones_shape_struct.bone_shape_data for item in custom_bone_shape: b_name = item["bone_name"] rotation_x = float(item["rot_X"]) rotation_y = float(item["rot_Y"]) rotation_z = float(item["rot_Z"]) shape_scale = float(item["scale"]) shape_obj = item["shape"] shape_color = item["color"] shape_width = float(item["width"]) # print(f"Modifying Bone Shape for: {b_name}") modifyctrlbone(b_name, rotation_x, rotation_y, rotation_z, shape_scale, shape_obj, shape_width, shape_color, new_amt_obj) # Call Modify Control Bone Shape Function # =========== Setup Control Rig Bone Colors =========== custom_bone_color = ctrl_bones_color_struct.bone_color_data for item in custom_bone_color: b_name = item["bone_name"] shape_color = item["color"] # print(f"Modifying Bone Color for: {b_name}") modifyctrlbonecolor(b_name, shape_color, new_amt_obj) # Call Modify Bone Function # =========== Setup IK Controls =========== IK_controls = ctrl_ik_struct.ctrl_ik_data for item in IK_controls: b_name = item["bone_name"] ctrl_name = item["ctrl_controller"] print(f"Adding Child constraint for bone: {b_name}") addIKcontroller(b_name, ctrl_name, new_amt_obj) # Call Add IK Controller Function # =========== Setup IK Bones =========== IK_bones = ik_bones_struct.IK_bones_data for item in IK_bones: b_name = item["bone_name"] ik_target = item["target"] ik_pole_target = item["pole_target"] ik_chain = int(item["chain"]) # print(f"Adding IK constraint for bone: {b_name}") addIK(b_name, ik_target, ik_pole_target, ik_chain, new_amt_obj) # Call Add IK Function # =========== Setup Deform Bones =========== deformbones = deform_bones_struct.deform_bones_data for item in deformbones: b_name = item["bone_name"] parent_bone = item["parent"] parent_influence = float(item["influence"]) # print(f"Adding copy scale constraint for bone: {b_name}") addCopyScale(b_name, parent_bone, parent_influence, new_amt_obj) # Call Copy Scale constraints Function # =========== Cleanup =========== # Change Rig Display Type new_amt_obj.data.display_type = 'STICK' new_amt_obj.data.collections_all["control"].is_visible = True new_amt_obj.data.collections_all["skeleton"].is_visible = False new_amt_obj.data.collections_all["webbing"].is_visible = False new_amt_obj.data.collections_all["belly"].is_visible = False new_amt_obj.data.collections_all["gular"].is_visible = False new_amt_obj.data.collections_all["nostril"].is_visible = False new_amt_obj.data.collections_all["metacarpal"].is_visible = False new_amt_obj.data.collections_all["skeleton"].is_visible = False new_amt_obj.data.collections_all["metatarsus"].is_visible = False new_amt_obj.data.collections_all["fingers"].is_visible = False new_amt_obj.data.collections_all["toes"].is_visible = False new_amt_obj.data.show_names = False bpy.data.collections['gular'].hide_viewport = True bpy.data.collections['belly_l'].hide_viewport = True bpy.data.collections['belly_r'].hide_viewport = True bpy.data.collections['nostril_l'].hide_viewport = True bpy.data.collections['nostril_r'].hide_viewport = True bpy.data.collections['helpers'].hide_viewport = True bpy.data.collections['fingers'].hide_viewport = True bpy.data.collections['toes'].hide_viewport = True bpy.data.collections['rig_shapes'].hide_viewport = True bpy.data.objects['belly_mesh'].hide_viewport = True bpy.data.objects['gular_mesh'].hide_viewport = True bpy.data.objects['nostril_mesh'].hide_viewport = True bpy.data.objects['OG_Armature'].hide_viewport = True bpy.data.objects['Armature'].hide_viewport = False else: print("Armature not configured yet")