Skip to content

Commit

Permalink
improve get_parent logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Steven Garcia committed Mar 31, 2024
1 parent 351f762 commit b42bc61
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 47 deletions.
2 changes: 1 addition & 1 deletion io_scene_halo/file_jms/process_scene.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def process_scene(context, version, game_version, generate_checksum, fix_rotatio
region_idx = -1

parent_idx = global_functions.get_parent(blend_scene.armature, marker, joined_list, 0)
marker_matrix = global_functions.get_matrix(marker, marker, True, blend_scene.armature, joined_list, False, version, 'JMS', False, custom_scale, fix_rotations)
marker_matrix = global_functions.get_matrix(marker, marker, True, blend_scene.armature, joined_list, False, version, 'JMS', False, custom_scale, fix_rotations, 0)
mesh_dimensions = global_functions.get_dimensions(marker_matrix, marker, version, False, 'JMS', custom_scale)

rotation = (mesh_dimensions.quaternion[0], mesh_dimensions.quaternion[1], mesh_dimensions.quaternion[2], mesh_dimensions.quaternion[3])
Expand Down
91 changes: 45 additions & 46 deletions io_scene_halo/global_functions/global_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ def get_fcurve_matrix(node, node_idx, fcurve_matrices, jma_version):

return object_matrix

def get_matrix(obj_a, obj_b, is_local, armature, joined_list, is_node, version, file_type, constraint, custom_scale, fix_rotation):
def get_matrix(obj_a, obj_b, is_local, armature, joined_list, is_node, version, file_type, constraint, custom_scale, fix_rotation, default_parent=-1):
object_matrix = Matrix.Translation((0, 0, 0))
rotation_matrix = Matrix.Rotation(0, 4, 'Z')
if file_type == 'ASS':
Expand Down Expand Up @@ -583,18 +583,16 @@ def get_matrix(obj_a, obj_b, is_local, armature, joined_list, is_node, version,
else:
if armature:
object_matrix = obj_a.matrix_world @ scale_matrix
bone_test = armature.data.bones.get(obj_b.parent_bone)
if obj_b.parent_bone and is_local and bone_test:
parent_object = get_parent(armature, obj_b, joined_list, -1)
parent_object = get_parent(armature, obj_b, joined_list, default_parent)
if parent_object[1] and is_local:
pose_bone = armature.pose.bones['%s' % (parent_object[1].name)]
object_matrix = ((pose_bone.matrix @ rotation_matrix).inverted() @ obj_a.matrix_world) @ scale_matrix

else:
object_matrix = obj_a.matrix_world @ scale_matrix
if obj_b.parent and is_local:
parent_object = get_parent(armature, obj_b, joined_list, -1)
if not parent_object[1] == None:
object_matrix = (parent_object[1].matrix_world.inverted() @ obj_a.matrix_world) @ scale_matrix
parent_object = get_parent(armature, obj_b, joined_list, default_parent)
if parent_object[1] and is_local:
object_matrix = (parent_object[1].matrix_world.inverted() @ obj_a.matrix_world) @ scale_matrix

return object_matrix

Expand Down Expand Up @@ -665,55 +663,56 @@ def get_children(world_node):

return children_hierarchy

def get_parent(armature, mesh, joined_list, default_parent):
parent_object = None
parent_index = default_parent
parent = None
if mesh:
if armature:
if type(mesh).__name__ == 'Bone':
parent = mesh.parent

else:
parent = mesh.parent_bone

else:
parent = mesh.parent
def get_parent(armature, node, joined_list, default_parent):
valid_parent_idx = default_parent
valid_parent = None
if valid_parent_idx >= 0:
valid_parent = joined_list[default_parent]

if node and node.parent:
node_parent = node.parent
if node_parent.type == 'ARMATURE':
node_parent = node_parent.parent_bone
while valid_parent == None:
if type(node).__name__ == 'Bone':
if node_parent != None:
if node_parent in joined_list and node_parent.use_deform:
valid_parent = node_parent
valid_parent_idx = joined_list.index(node_parent)

else:
node_parent = node_parent.parent

while parent:
if armature:
if type(mesh).__name__ == 'Bone':
bone_test = armature.data.bones.get(mesh.parent.name)
else:
break

else:
bone_test = armature.data.bones.get(mesh.parent_bone)
if not string_empty_check(node_parent):
node_parent = armature.data.bones[node_parent]
if node_parent in joined_list and node_parent.use_deform:
valid_parent = node_parent
valid_parent_idx = joined_list.index(node_parent)

if bone_test:
if type(mesh).__name__ == 'Bone':
mesh = armature.data.bones[mesh.parent.name]
else:
node_parent = node_parent.parent

else:
mesh = armature.data.bones[mesh.parent_bone]

if mesh in joined_list:
parent_object = mesh
break

else:
break
else:
while valid_parent == None:
if node_parent != None:
if node_parent in joined_list and node_parent.hide_viewport == False and node_parent.hide_get() == False:
valid_parent = node_parent
valid_parent_idx = joined_list.index(node_parent)

else:
if mesh == None:
break
mesh = mesh.parent
if mesh in joined_list and mesh.hide_viewport == False and mesh.hide_get() == False:
parent_object = mesh
break
else:
node_parent = node_parent.parent

if parent_object:
parent_index = joined_list.index(parent_object)
else:
break

return (parent_index, parent_object)
return (valid_parent_idx, valid_parent)

def set_scale(scale_enum, scale_float):
scale = 1
Expand Down

0 comments on commit b42bc61

Please sign in to comment.