Skip to content

Commit

Permalink
unit scale fix
Browse files Browse the repository at this point in the history
- separated options for applying scale and rotation
- fixed unit scale issue by setting blender scene to right unit scale before batch converting
  • Loading branch information
enziop committed Jun 28, 2017
1 parent 4dd12ea commit b804492
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 14 deletions.
20 changes: 14 additions & 6 deletions __init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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'}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
Expand Down
23 changes: 15 additions & 8 deletions mixamoconv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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')
Expand Down Expand Up @@ -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

Expand All @@ -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":
Expand All @@ -234,15 +237,19 @@ 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
for action in bpy.data.actions:
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)
Expand Down

0 comments on commit b804492

Please sign in to comment.