Skip to content
24 changes: 18 additions & 6 deletions addons/io_scene_gltf2/blender/exp/gltf2_blender_gather_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,6 @@ def recursive_node_traverse(self, blender_object, blender_bone, parent_uuid, par
node.parent_uuid = parent_uuid
node.set_blender_data(blender_object, blender_bone)

# add to parent if needed
if parent_uuid is not None:
self.add_children(parent_uuid, node.uuid)
else:
self.roots.append(node.uuid)

# Set blender type
if blender_bone is not None:
node.blender_type = VExportNode.BONE
Expand All @@ -145,6 +139,8 @@ def recursive_node_traverse(self, blender_object, blender_bone, parent_uuid, par
else:
node.blender_type = VExportNode.OBJECT

force_node_at_world_center = False

# For meshes with armature modifier (parent is armature), keep armature uuid
if node.blender_type == VExportNode.OBJECT:
modifiers = {m.type: m for m in blender_object.modifiers}
Expand All @@ -160,6 +156,16 @@ def recursive_node_traverse(self, blender_object, blender_bone, parent_uuid, par
else:
node.armature = parent_uuid

# In glTF, meshes should be at root, so clearing parent, and setting as root
node.parent_uuid = None
force_node_at_world_center = True

# add to parent if needed
if node.parent_uuid is not None:
self.add_children(parent_uuid, node.uuid)
else:
self.roots.append(node.uuid)

# For bones, store uuid of armature
if blender_bone is not None:
node.armature = armature_uuid
Expand Down Expand Up @@ -231,6 +237,12 @@ def recursive_node_traverse(self, blender_object, blender_bone, parent_uuid, par
if blender_object.is_instancer is True and blender_object.show_instancer_for_render is False:
node.force_as_empty = True

# Because no local transforms are set in glTF for skinned meshes, we need to
# artificially set the mesh at world center
# This way, any children local transforms will be correclty calculated
if force_node_at_world_center is True:
node.matrix_world = Matrix.Identity(4)

# Storing this node
self.add_node(node)

Expand Down