From 8a2a1ab497ea607775a775115c611b58c91b58e2 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 24 Jun 2019 20:20:30 +0300 Subject: [PATCH] Fixed Custom Binder --- hifi_tools/__init__.py | 2 +- hifi_tools/utils/bones/bones_builder.py | 51 ++++++++++--------- hifi_tools/utils/bones/custom.py | 68 ++++++++++--------------- hifi_tools/utils/bones/makehuman.py | 3 +- hifi_tools/utils/ui/panels.py | 13 ++--- 5 files changed, 61 insertions(+), 76 deletions(-) diff --git a/hifi_tools/__init__.py b/hifi_tools/__init__.py index a0ba69a..3369e60 100644 --- a/hifi_tools/__init__.py +++ b/hifi_tools/__init__.py @@ -20,7 +20,7 @@ bl_info = { "name": "HiFi Blender Add-on", "author": "Matti 'Menithal' Lahtinen", - "version": (1, 4, 4), + "version": (1, 4, 5), "blender": (2, 80, 0), "location": "File > Import-Export, Materials, Armature", "description": "Blender tools to allow for easier Content creation for Metaverses, such as High Fidelity", diff --git a/hifi_tools/utils/bones/bones_builder.py b/hifi_tools/utils/bones/bones_builder.py index 27d10fa..767582e 100644 --- a/hifi_tools/utils/bones/bones_builder.py +++ b/hifi_tools/utils/bones/bones_builder.py @@ -204,6 +204,15 @@ def find_armatures(selection): return armatures +def find_armature(selection): + for selected in selection: + print(selected, selected.parent) + if selected.type == "ARMATURE": + return selected + if selected.type == "MESH" and selected.parent is not None and selected.parent.type == "ARMATURE": + return selected.parent + return None + def find_armatures_parent(selection): armatures = [] @@ -214,7 +223,7 @@ def find_armatures_parent(selection): def find_armature_or_armature_parent(selection): - armature = find_armatures(selection) + armature = find_armature(selection) if armature is None: armatures = find_armatures_parent(selection) if len(armatures) > 0: @@ -223,25 +232,18 @@ def find_armature_or_armature_parent(selection): return armature -def find_armature(selection): - for selected in selection: - if selected.type == "ARMATURE": - return selected - return None - def find_select_armature_and_children(selection): - roots = find_armature_or_armature_parent(selection) - if len(roots) == 0: + root = find_armature_or_armature_parent(selection) + if root is None: return bpy.ops.object.select_all(action='DESELECT') - for root in roots: - root.select_set(True) + root.select_set(True) - if len(root.children) > 0: - for child in root.children: - child.select_set(True) + if len(root.children) > 0: + for child in root.children: + child.select_set(True) def camel_case_split(name): @@ -560,22 +562,24 @@ def build_skeleton(): def reset_scale_rotation(obj): - #current_context = bpy.context.area.type - #bpy.context.area.type = "VIEW_3D" - # set context to 3D View and set Cursor - bpy.context.scene.cursor.location[0] = 0.0 - bpy.context.scene.cursor.location[1] = 0.0 - bpy.context.scene.cursor.location[2] = 0.0 - #bpy.context.area.type = current_context + mode = bpy.context.area.type + bpy.context.area.type = "VIEW_3D" bpy.ops.object.mode_set(mode="OBJECT") + bpy.ops.view3d.snap_cursor_to_center('INVOKE_DEFAULT') + bpy.context.area.type = mode + bpy.ops.object.select_all(action="DESELECT") - bpy.ops.object.origin_set(type="ORIGIN_CURSOR") + obj.select_set(True) bpy.ops.object.transform_apply(location=False, rotation=True, scale=True) + + mode = mode + def correct_scale_rotation(obj, rotation): reset_scale_rotation(obj) - obj.scale = Vector((100, 100, 100)) + obj.scale = Vector((100.0, 100.0, 100.0)) + print(obj.scale) str_angle = -90 * pi/180 if rotation: obj.rotation_euler = Euler((str_angle, 0, 0), "XYZ") @@ -587,6 +591,7 @@ def correct_scale_rotation(obj, rotation): obj.rotation_euler = Euler((-str_angle, 0, 0), "XYZ") + def navigate_armature(data, current_rest_node, world_matrix, parent, parent_node): name = current_rest_node["name"] bone = data.get(name) diff --git a/hifi_tools/utils/bones/custom.py b/hifi_tools/utils/bones/custom.py index 2a6f4f2..7c4a441 100644 --- a/hifi_tools/utils/bones/custom.py +++ b/hifi_tools/utils/bones/custom.py @@ -111,7 +111,8 @@ def automatic_bind_bones(self, avatar_bones): knee_check = False cleaned_bones = dict() for bone in avatar_bones: - cleaned_bones[bones_builder.clean_up_bone_name(bone.name).lower()] = bone + cleaned_bones[bones_builder.clean_up_bone_name( + bone.name).lower()] = bone keys = list(cleaned_bones.keys()) for cleaned_name in keys: @@ -236,12 +237,15 @@ def rename_bones_and_fix_most_things(self, context): bpy.ops.wm.console_toggle() except: print("Console Toggle") - - mode = bpy.context.area.type + armature_obj = context.scene.objects[self.armatures] + mode = bpy.context.area.type + # Naming Converted - bpy.ops.object.mode_set(mode="EDIT") + armature = bpy.data.armatures[self.armature] + + bpy.ops.object.mode_set(mode="EDIT") ebones = armature.edit_bones print("--------") @@ -254,7 +258,7 @@ def rename_bones_and_fix_most_things(self, context): if len(self.spine1) < 1: # Get what is supposed to be the last spine. spine1 = ebones.get("Spine2") - spine1.select_set(state=True) + spine1.select = True bpy.ops.armature.subdivide() # Spine2.001 = new spine 2 @@ -294,14 +298,6 @@ def rename_bones_and_fix_most_things(self, context): 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_builder.reset_scale_rotation(object_armature) - # Fixing Rotations and Scales # Now Refresh datablocks @@ -316,7 +312,6 @@ def rename_bones_and_fix_most_things(self, context): for bone in ebones: bone.hide = False bone.name = bones_builder.clean_up_bone_name(bone.name) - bones_builder.correct_bone_rotations(bone) bones_builder.correct_bone(bone, ebones) bones_builder.correct_bone_parents(armature.edit_bones) @@ -326,24 +321,17 @@ def rename_bones_and_fix_most_things(self, context): children = bpy.data.objects for child in children: - + child.select_set(state=True) if child.type == "ARMATURE": - bones_builder.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_builder.pin_common_bones(child, self.fix_rolls) - if child.type == "MESH": # mesh.clean_unused_vertex_groups(child) - bones_builder.reset_scale_rotation(child) if spine_was_split: print("Dealing with the Spine split for" + child.name) spine1_weights = child.vertex_groups["Spine1"] @@ -375,30 +363,31 @@ def rename_bones_and_fix_most_things(self, context): block.value = 0 if self.remove_ends: - bpy.context.area.type = mode - bpy.ops.object.mode_set(mode="OBJECT") bpy.ops.object.mode_set(mode="EDIT") bones_builder.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") - try: bpy.ops.wm.console_toggle() except: print("Console was toggled") + if self.fix_rolls: + bpy.ops.metaverse_toolset.hf_fix_bone_rolls('INVOKE_DEFAULT') + + if self.pin_problems: + bpy.ops.metaverse_toolset.hf_pin_problem_bones('INVOKE_DEFAULT') + + print(armature_obj) + armature_obj.select_set(True) + + bpy.ops.metaverse_toolset.hf_objects_fix_scale_and_rotation('INVOKE_DEFAULT') + return {"FINISHED"} @@ -547,23 +536,16 @@ def draw(self, context): column.prop_search(self, 'toe', data, 'bones', icon='BONE_DATA', text='Toe') - column.prop(self, "cats_convert") + #column.prop(self, "cats_convert") column.label(text="Bones") row = column.row() row.prop(self, "pin_problems") row.prop(self, "fix_rolls") - column.label(text="Materials") - - row = column.row() - row.prop(self, "remove_metallic") - row.prop(self, "mask_textures") - else: print(" No Armatures") - print("custom_armature_name", self.custom_armature_name) # After selecting armature, iterate through bones. # https://blender.stackexchange.com/questions/19293/prop-search-armature-bones @@ -581,8 +563,10 @@ def scene_delete(): def custom_register(): - bpy.utils.register_class(AVATAR_OT_METAV_TOOLSET_Custom_Avatar_Binder_Operator) + bpy.utils.register_class( + AVATAR_OT_METAV_TOOLSET_Custom_Avatar_Binder_Operator) def custom_unregister(): - bpy.utils.unregister_class(AVATAR_OT_METAV_TOOLSET_Custom_Avatar_Binder_Operator) + bpy.utils.unregister_class( + AVATAR_OT_METAV_TOOLSET_Custom_Avatar_Binder_Operator) diff --git a/hifi_tools/utils/bones/makehuman.py b/hifi_tools/utils/bones/makehuman.py index 4e6d958..5824d9e 100644 --- a/hifi_tools/utils/bones/makehuman.py +++ b/hifi_tools/utils/bones/makehuman.py @@ -156,6 +156,7 @@ def create_blink_shapes(shapekey, armature1, armature2): armature = None old_meshes = {} meshes = {} + selected = bpy.context.selected_objects # duplicate avatar bpy.ops.object.mode_set(mode='OBJECT') @@ -171,7 +172,7 @@ def create_blink_shapes(shapekey, armature1, armature2): bpy.ops.object.duplicate() # pose eyelids - for obj in bpy.context.selected_objects: + for obj in selected: if obj.type == 'ARMATURE': armature = obj bpy.context.view_layer.objects.active = obj diff --git a/hifi_tools/utils/ui/panels.py b/hifi_tools/utils/ui/panels.py index 5399772..26f6e5c 100644 --- a/hifi_tools/utils/ui/panels.py +++ b/hifi_tools/utils/ui/panels.py @@ -310,14 +310,8 @@ class OBJECT_OT_METAV_TOOLSET_Fix_Scale_Operator(bpy.types.Operator): bl_category = "High Fidelity" def execute(self, context): - bones_builder.find_select_armature_and_children(bpy.context.selected_objects) - selected_objects = bpy.context.selected_objects - for selected in selected_objects: - bones_builder.correct_scale_rotation(selected, True) - - - - + armature = bones_builder.find_armature_or_armature_parent(bpy.context.selected_objects) + bones_builder.correct_scale_rotation(armature, True) return {'FINISHED'} @@ -352,6 +346,7 @@ def execute(self, context): if selected is None: selected = bpy.data.objects + bones_builder.find_select_armature_and_children(selected) selected = bpy.context.selected_objects @@ -781,7 +776,7 @@ def execute(self, context): BONES_OT_METAV_TOOLSET_Combine_Disconnected, BONES_OT_METAV_TOOLSET_Connect_Selected, BONES_OT_METAV_TOOLSET_Fix_Rolls, - + BONES_OT_METAV_TOOLSET_Disconnect_Selected, BONES_OT_METAV_TOOLSET_Pin_Problem_Bones, OBJECT_PT_METAV_TOOLSET, OBJECT_PT_METAV_TOOLSET_Assets_Display,