From b804492aad96e227269b09eb1690dd6027d6b88e Mon Sep 17 00:00:00 2001 From: Enzio Date: Sun, 25 Jun 2017 22:16:53 +0200 Subject: [PATCH] unit scale fix - separated options for applying scale and rotation - fixed unit scale issue by setting blender scene to right unit scale before batch converting --- __init__.py | 20 ++++++++++++++------ mixamoconv.py | 23 +++++++++++++++-------- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/__init__.py b/__init__.py index 134a14c..486f117 100644 --- a/__init__.py +++ b/__init__.py @@ -96,10 +96,14 @@ class MixamoPropertyGroup(bpy.types.PropertyGroup): name="Add Leaf Bones", description="If enabled, adds leaf bones on export when batchconverting", default=False) - apply_transform = bpy.props.BoolProperty( - name="Apply Transform", - description="Applies transform during conversion to prevent rotation and scaling issues", + apply_rotation = bpy.props.BoolProperty( + name="Apply Rotation", + description="Applies rotation during conversion to prevent rotation and scaling issues", default=True) + apply_scale = bpy.props.BoolProperty( + name="Apply Scale", + description="Applies scale during conversion to prevent rotation and scaling issues", + default=False) b_remove_namespace = bpy.props.BoolProperty( name="Remove Namespace", description="Removes Naespaces from objects and bones", @@ -148,7 +152,8 @@ def execute(self, context): restoffset = context.scene.mixamo.restoffset, hipname = bpy.context.scene.mixamo.hipname, fixbind = bpy.context.scene.mixamo.fixbind, - apply_transform = bpy.context.scene.mixamo.apply_transform) + apply_rotation = bpy.context.scene.mixamo.apply_rotation, + apply_scale = bpy.context.scene.mixamo.apply_scale) if status == -1: self.report({'ERROR_INVALID_INPUT'}, 'Error: Hips not found') return{'CANCELLED'} @@ -208,7 +213,8 @@ def execute(self, context): restoffset = context.scene.mixamo.restoffset, hipname = bpy.context.scene.mixamo.hipname, fixbind = bpy.context.scene.mixamo.fixbind, - apply_transform = bpy.context.scene.mixamo.apply_transform, + apply_rotation = bpy.context.scene.mixamo.apply_rotation, + apply_scale = bpy.context.scene.mixamo.apply_scale, b_remove_namespace = bpy.context.scene.mixamo.b_remove_namespace, add_leaf_bones = bpy.context.scene.mixamo.add_leaf_bones) if numfiles == -1: @@ -268,7 +274,9 @@ def draw(self, context): row = box.row() row.prop(scene.mixamo, "fixbind") row.prop(scene.mixamo, "add_leaf_bones") - row.prop(scene.mixamo, "apply_transform") + row = box.row() + row.prop(scene.mixamo, "apply_rotation") + row.prop(scene.mixamo, "apply_scale") row = box.row() row.prop(scene.mixamo, "scale") row = box.row() diff --git a/mixamoconv.py b/mixamoconv.py index f80f760..e2c2480 100644 --- a/mixamoconv.py +++ b/mixamoconv.py @@ -59,7 +59,7 @@ def apply_restoffset(armature, hipbone, restoffset): ''' function to bake hipmotion to RootMotion in MixamoRigs ''' -def hip_to_root(armature, use_x = True, use_y = True, use_z = True, on_ground = True, scale = 1.0, restoffset = (0,0,0), hipname='', fixbind = True, apply_transform = True): +def hip_to_root(armature, use_x = True, use_y = True, use_z = True, on_ground = True, scale = 1.0, restoffset = (0,0,0), hipname='', fixbind = True, apply_rotation = True, apply_scale = False): root = armature root.name = "root" @@ -142,8 +142,8 @@ def hip_to_root(armature, use_x = True, use_y = True, use_z = True, on_ground = root.select = True bpy.context.scene.objects.active = root - if apply_transform: - bpy.ops.object.transform_apply(location=False, rotation=True, scale=True) + if apply_rotation or apply_scale: + bpy.ops.object.transform_apply(location=False, rotation=apply_rotation, scale=apply_scale) #Bake Root motion to Armature (root) bpy.ops.object.constraint_add(type='COPY_LOCATION') @@ -195,10 +195,10 @@ def hip_to_root(armature, use_x = True, use_y = True, use_z = True, on_ground = root.select = True bpy.context.scene.objects.active = root bpy.ops.object.parent_set(type='ARMATURE') - elif apply_transform: + elif apply_rotation or apply_scale: bindmesh.select = True bpy.context.scene.objects.active = bindmesh - bpy.ops.object.transform_apply(location=False, rotation=True, scale=True) + bpy.ops.object.transform_apply(location=False, rotation=apply_rotation, scale=apply_scale) return 1 @@ -207,7 +207,10 @@ def hip_to_root(armature, use_x = True, use_y = True, use_z = True, on_ground = ''' Batch Convert MixamoRigs ''' -def batch_hip_to_root(source_dir, dest_dir, use_x = True, use_y = True, use_z = True, on_ground = True, scale = 1.0, restoffset = (0,0,0), hipname = '', fixbind = True, apply_transform = True, b_remove_namespace = True, add_leaf_bones = False): +def batch_hip_to_root(source_dir, dest_dir, use_x = True, use_y = True, use_z = True, on_ground = True, scale = 1.0, restoffset = (0,0,0), hipname = '', fixbind = True, apply_rotation = True, apply_scale = False, b_remove_namespace = True, add_leaf_bones = False): + bpy.context.scene.unit_settings.system = 'METRIC' + bpy.context.scene.unit_settings.scale_length = 0.01 + numfiles = 0 for file in os.scandir(source_dir): if file.name[-4::] == ".fbx": @@ -234,7 +237,7 @@ def getArmature(objects): return a armature = getArmature(bpy.context.selected_objects) #do hip to Root conversion - if hip_to_root(armature, use_x = use_x, use_y = use_y, use_z = use_z, on_ground = on_ground, scale = scale, restoffset = restoffset, hipname = hipname, fixbind = fixbind, apply_transform = apply_transform) == -1: + if hip_to_root(armature, use_x = use_x, use_y = use_y, use_z = use_z, on_ground = on_ground, scale = scale, restoffset = restoffset, hipname = hipname, fixbind = fixbind, apply_rotation = apply_rotation, apply_scale = apply_scale) == -1: return -1 #remove newly created orphan actions @@ -242,7 +245,11 @@ def getArmature(objects): if action != armature.animation_data.action: bpy.data.actions.remove(action, do_unlink=True) - bpy.ops.export_scene.fbx(filepath=dest_dir + file.name, use_selection=False, add_leaf_bones=add_leaf_bones) + bpy.ops.export_scene.fbx(filepath=dest_dir + file.name, + version = 'BIN7400', + use_selection=False, + apply_unit_scale=False, + add_leaf_bones=add_leaf_bones) bpy.ops.object.select_all(action='SELECT') bpy.ops.object.delete(use_global=False) print("%d files converted" % numfiles)