Skip to content

Commit

Permalink
Fix bug when loading glbs with multiple meshes. (#311)
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

Co-authored-by: kubric-team <[email protected]>
  • Loading branch information
henzler and Qwlouse authored Mar 12, 2024
1 parent 5311a27 commit 8fe9429
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

1 comment on commit 8fe9429

@bcd8697
Copy link

@bcd8697 bcd8697 commented on 8fe9429 Apr 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dear authors,
It seems that this correction causes a conflict with your current Docker container.

AttributeError: 'Context' object has no attribute 'temp_override'

Docker Kubruntudev has Blender version 2.93 (as well as the whole kubric project, afaik). However, support for 'temp_override' appears only starting with version 3.2, see: https://developer.blender.org/docs/release_notes/3.2/python_api/#additions
😦😦😦

Are there ways to go around this situation?
Many thanks!

Please sign in to comment.