Example #1
0
def clean_up_bones(obj):
    _to_remove = []

    pose_bones = obj.pose.bones

    bpy.ops.object.mode_set(mode='EDIT')
    updated_context = bpy.data.objects[obj.name]
    edit_bones = updated_context.data.edit_bones

    bpy.ops.object.mode_set(mode='EDIT')
    correct_bone_positions(updated_context.data.edit_bones)

    bpy.ops.object.mode_set(mode='OBJECT')
    for bone in obj.data.bones:
        pose_bone = pose_bones.get(bone.name)

        # Oddly needed to get the edit bone reference
        bpy.ops.object.mode_set(mode='EDIT')
        edit_bone = edit_bones.get(bone.name)

        print(" Bone ", bone.name)

        if pose_bone is not None and edit_bone is not None:
            print(" + Setting Pose Mode for", pose_bone)

            print(" - Removing Constraints from", pose_bone.name)
            for constraint in pose_bone.constraints:
                pose_bone.constraints.remove(constraint)

            print(" # Check Rotations")
            bones.correct_bone_rotations(edit_bone)

            if "Thumb" in edit_bone.name:
                bpy.ops.object.mode_set(mode='EDIT')
                bpy.ops.armature.select_all(action="DESELECT")
                edit_bone.select = True
                bpy.ops.armature.calculate_roll(type="GLOBAL_NEG_Z")
                bpy.ops.armature.select_all(action="DESELECT")

    bpy.ops.object.mode_set(mode='OBJECT')

    return _to_remove
Example #2
0
    def execute(self, context):
        bpy.ops.object.mode_set(mode="EDIT")

        selected = bpy.context.selected_objects
        if selected is None:
            selected = bpy.data.objects

        for obj in selected:
            if obj.type == "ARMATURE":
                print("Lets Do this shit ", obj)
                bpy.ops.object.mode_set(mode="OBJECT")
                correct_scale_rotation(obj, False)
                bpy.ops.object.mode_set(mode="EDIT")
                for ebone in obj.data.edit_bones:
                    bones.correct_bone_rotations(ebone)

        bpy.ops.object.mode_set(mode="OBJECT")
        bones.clear_pose(selected)

        bpy.ops.object.mode_set(mode="OBJECT")
        return {'FINISHED'}
Example #3
0
def rename_bones_and_fix_most_things(self, context):
    print("Rename bones fix most things", self.armature)
    if len(self.armature) < 1:
        print("Armature Update cancelled")
        return {"CANCELLED"}

    bpy.ops.wm.console_toggle()
    mode = bpy.context.area.type

    # Naming Converted
    bpy.ops.object.mode_set(mode="EDIT")
    armature = bpy.data.armatures[self.armature]
    ebones = armature.edit_bones
    
    print("--------")
    print("Updating Bone Names")
    update_bone_name(ebones, self.hips, "Hips")
    update_bone_name(ebones, self.spine, "Spine")
    update_bone_name(ebones, self.spine2, "Spine2")

    spine_was_split = False
    if len(self.spine1) < 1:
        spine1 = ebones.get("Spine2") # Get what is supposed to be the last spine.
        spine1.select = True
        bpy.ops.armature.subdivide()

        # Spine2.001 = new spine 2
        # Spine2 = spine1
        spine2 = ebones.get("Spine2.001")
        spine1.name = "Spine1"
        spine2.name = "Spine2"
        spine_was_split = True

    else:
        update_bone_name(ebones, self.spine1, "Spine1")
    
    update_bone_name(ebones, self.neck, "Neck")
    update_bone_name(ebones, self.head, "Head")

    print("--------")
    print("Updating Bone Names Mirrored")
    update_bone_name_mirrored(ebones, self.eye, "Eye")
    update_bone_name_mirrored(ebones, self.shoulder, "Shoulder")
    update_bone_name_mirrored(ebones, self.arm, "Arm")
    update_bone_name_mirrored(ebones, self.fore_arm, "ForeArm")
    update_bone_name_mirrored(ebones, self.hand, "Hand")

    update_bone_name_mirrored(ebones, self.up_leg, "UpLeg")
    update_bone_name_mirrored(ebones, self.leg, "Leg")
    update_bone_name_mirrored(ebones, self.foot, "Foot")
    update_bone_name_mirrored(ebones, self.toe, "Toe")

    print("--------")
    print("Updating Bone Names Chained Mirrored")
    update_bone_name_chained_mirrored(ebones, self.hand_thumb, "HandThumb")
    update_bone_name_chained_mirrored(ebones, self.hand_index, "HandIndex")
    update_bone_name_chained_mirrored(ebones, self.hand_middle, "HandMiddle")
    update_bone_name_chained_mirrored(ebones, self.hand_ring, "HandRing")
    update_bone_name_chained_mirrored(ebones, self.hand_pinky, "HandPinky")
    
    bpy.ops.object.mode_set(mode="OBJECT")
    armature = bpy.data.armatures[self.armature]

    object_armature = None
    for obj in bpy.data.objects:
        if obj.type == "ARMATURE" and obj.data == armature:
            object_armature = obj
            break

    bones.reset_scale_rotation(object_armature)

    # Fixing Rotations and Scales
    # Now Refresh datablocks

    print("Reset Scale to unit scale")
    print("--------")
    print("Fix Rotations")


    bpy.ops.object.mode_set(mode="EDIT")
    
    ebones = armature.edit_bones

    for bone in ebones:
        bone.hide = False
        bone.name = bones.clean_up_bone_name(bone.name)
        bones.correct_bone_rotations(bone)
        bones.correct_bone(bone, ebones)
        

    bones.correct_bone_parents(armature.edit_bones)
    bpy.ops.object.mode_set(mode="OBJECT")
    bpy.ops.object.select_all(action="DESELECT")
    # TODO: This should be more selective and only affect the armature object's children.

    children = bpy.data.objects 
    for child in children:
        if child.type == "ARMATURE":
            child.select = True
            bones.correct_scale_rotation(child, True)
            bpy.ops.object.mode_set(mode="POSE")
            bpy.ops.pose.select_all(action="SELECT")
            bpy.ops.pose.transforms_clear()
            bpy.ops.pose.select_all(action="DESELECT")
            bpy.ops.object.mode_set(mode="OBJECT")
                
            if self.pin_problems:
                print("PIN PROBLEM")
                bpy.ops.object.mode_set(mode="EDIT")
                bones.pin_common_bones(child, self.fix_rolls)
            
        if child.type == "MESH":
            #        mesh.clean_unused_vertex_groups(child)
            bones.reset_scale_rotation(child)
            if spine_was_split:
                print("Dealing with the Spine split for" + child.name)
                spine1_weights = child.vertex_groups["Spine1"]
                if spine1_weights is not None:
                    spine1_weights.name = "Spine2"
                 
            if self.common_shapekey_correct:
                # common_cats_blend_mapping
                # Reset all shapekeys.
                ## TODO: Perhaps dettach this into a function.
                print("Going Through blendshapes and creating new ones")
                bpy.ops.object.mode_set(mode="OBJECT")
                bpy.ops.object.select_all(action="DESELECT")
                bpy.context.scene.objects.active = child
                child.select = True
                blocks = child.data.shape_keys.key_blocks

                for shape_key in blocks:
                    shape_key.value = 0

               
                for blend_map in common_cats_blend_mapping:
                    blocks.update()
                    block = blocks.get(blend_map.frm)
                    if block is not None:
                        target_block = blocks.get(blend_map.to)
                        if target_block is None:
                            block.value = blend_map.value
                            child.shape_key_add(name=blend_map.to, from_mix=True)
                            block.value = 0
            
            if self.compress_materials:
                materials.clean_materials(child.material_slots)
    
    if self.remove_ends:
        bpy.context.area.type = mode
        bpy.ops.object.mode_set(mode="OBJECT")
        bpy.ops.object.mode_set(mode="EDIT")
        bones.clean_ends(child)


    for material in bpy.data.materials:
        materials.flip_material_specular(material)

    if self.remove_metallic:
        materials.remove_materials_metallic(bpy.data.materials)
    
    if self.mask_textures:
        materials.convert_images_to_mask(bpy.data.images)

    bpy.ops.object.mode_set(mode="OBJECT")

    bpy.context.area.type = mode
    bpy.ops.object.mode_set(mode="OBJECT")

    bpy.ops.wm.console_toggle()
    return {"FINISHED"}
Example #4
0
def clean_up_bones(obj):
    _to_remove = []

    pose_bones = obj.pose.bones

    bpy.ops.object.mode_set(mode='EDIT')
    updated_context = bpy.data.objects[obj.name]
    edit_bones = updated_context.data.edit_bones
    print("Cleaning up Bones")
    bpy.ops.object.mode_set(mode='OBJECT')
    for bone in obj.data.bones:
        pose_bone = pose_bones.get(bone.name)

        # Oddly needed to get the edit bone reference
        bpy.ops.object.mode_set(mode='EDIT')
        edit_bone = edit_bones.get(bone.name)

        print(" Bone ", bone.name)

        if pose_bone is not None and edit_bone is not None:
            if (has_removable(bone.name) or bone.name in bones_to_remove or (bone.name != "Hips" and bone.parent is None)) and (bone.name != "HeadTop" and edit_bone not in _to_remove):
                _to_remove.append(edit_bone)
                print(" - Added to removal list", bone.name)
            elif (bone.parent is not None and bone.parent in _to_remove):
                _to_remove.append(edit_bone)
                print(
                    " - Added to removal list because parent is in deletion", bone.name)
            else:

                print(" + Setting Pose Mode for", pose_bone)

                print(" - Removing Constraints from", pose_bone.name)
                for constraint in pose_bone.constraints:
                    pose_bone.constraints.remove(constraint)

                print(" # Check Rotations")
                bones.correct_bone_rotations(edit_bone)

    print(" Cleaning ", len(_to_remove), " unusable bones")

    for removal_bone in _to_remove:
        if removal_bone is not None:
            print(" Remove Bone:", removal_bone)
            edit_bones.remove(removal_bone)

    bpy.ops.object.mode_set(mode='EDIT')
    edit_bones = updated_context.data.edit_bones

    print("Manipulating", obj.name)

    if edit_bones.get("Spine1") is None and edit_bones.get("Spine2") is None:
        print("Couldnt Detect Spine1, Creating Out of Spine2")
        spine = edit_bones.get("Spine")
        spine.select = True
        bpy.ops.armature.subdivide()
        spine.name = "Spine"
        spine.select = False
        spine = edit_bones.get("Spine.001")
        spine.select = True
        spine.name = "Spine2"

    if edit_bones.get("Spine1") is None:
        print("Couldnt Detect Spine1, Creating Out of Spine2")
        spine = edit_bones.get("Spine2")
        spine.select = True
        bpy.ops.armature.subdivide()
        spine.name = "Spine1"
        spine = edit_bones.get("Spine2.001")
        spine.name = "Spine2"
        spine.select = False

    edit_bones = updated_context.data.edit_bones
    bones.correct_bone_parents(edit_bones)

    bpy.ops.object.mode_set(mode='OBJECT')

    return _to_remove