Skip to content

Commit

Permalink
Fix bug when loading glbs with multiple meshes.
Browse files Browse the repository at this point in the history
The previous code was deleting all objects that were not of type "MESH" and then join all 'MESH' types. Doing it that way seemed to cause objects to 'split up' as they seem to have different transforms and their different parts are distributed all over the scene.

The new code selects all objects of type "MESH" without removal of any objects. This seems to fix the issue.

PiperOrigin-RevId: 611289628
  • Loading branch information
Qwlouse authored and henzler committed Mar 8, 2024
1 parent 5311a27 commit 91893d1
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions kubric/renderer/blender.py
Original file line number Diff line number Diff line change
Expand Up @@ -420,20 +420,28 @@ def _add_asset(self, obj: core.FileBasedObject):
**obj.render_import_kwargs)
# gltf files often contain "Empty" objects as placeholders for camera / lights etc.
# here we are interested only in the meshes, we filter these out and join all meshes into one.
bpy.ops.object.select_all(action='DESELECT')
mesh = [m for m in bpy.context.scene.objects if m.type == 'MESH']
mesh = [m for m in bpy.context.selected_objects if m.type == "MESH"]
assert mesh
for ob in mesh:
ob.select_set(state=True)
bpy.context.view_layer.objects.active = ob

# make sure one of the objects is active, otherwise join() fails.
# see https://blender.stackexchange.com/questions/132266/joining-all-meshes-in-any-context-gets-error
bpy.context.view_layer.objects.active = (
bpy.context.selected_objects[0]
)
bpy.context.view_layer.objects.active = mesh[0]
bpy.ops.object.join()
# By default gltf objects are loaded with a different rotation than obj files
# here we compensate for that to ensure alignment between pybullet and blender

# Make sure to delete all remaining non-mesh objects. Note that for
# some reason deleting the non-mesh objets before joining removes
# parts of the meshes in some cases.
non_mesh_objects = [
obj
for obj in bpy.context.selected_objects
if obj.type != "MESH"
]
with bpy.context.temp_override(selected_objects=non_mesh_objects):
bpy.ops.object.delete()

assert len(bpy.context.selected_objects) == 1
blender_obj = bpy.context.selected_objects[0]
blender_obj.rotation_quaternion = (0.707107, -0.707107, 0, 0)
Expand Down

0 comments on commit 91893d1

Please sign in to comment.