My exported GLB file is missing part of the object in the scene when I use Blender to open the file. Can you help me?

Hi! I used Blender to combine 2 meshes and create a new GLB file of the scene containing both objects. When I opened the combined file with Meshlab or online viewer, I could see both objects. But when I used Blender one of the objects was missing. For example, I combined a mesh of a chair with a mesh of a desk, now I have a new mesh called - “chair_desk”. when I tried to repeat this process and combine between “chair_desk” and “bed”, the chair was completely missing from the scene (I tried to export the new combined mesh as well as render 2D images of “chsir_desk” and in both cases, the chair was missing).
Please do you have any suggestions for me?
Here is the script I’m using:

    def extrat_transform_renders(self):
        self.reset_scene()
        camera_data = bpy.data.cameras.new("Camera")
        camera = bpy.data.objects.new("Camera", camera_data)

        C = bpy.context
        world = C.scene.world
        world.use_nodes = True
        enode = C.scene.world.node_tree.nodes.new("ShaderNodeTexEnvironment")
        enode.image = bpy.data.images.load("blender_scripts/rural_crossroads_8k.hdr")
        node_tree = C.scene.world.node_tree
        node_tree.links.new(enode.outputs['Color'], node_tree.nodes['Background'].inputs['Color'])
        bpy.context.scene.render.film_transparent = True
        scene = bpy.context.scene
        scene.use_nodes = True
        bpy.context.scene.view_layers["ViewLayer"].use_pass_z = True
        scene.render.engine = 'CYCLES'
        scene.cycles.device = 'GPU'
        scene.render.resolution_x = 512
        scene.render.resolution_y = 512

        names = []
        objects_name = []
        print(f'[254] eval objects: {self.objects}')
        for obj_name, idx, base64_matrix, scale in self.objects:
            binary_string = base64.b64decode(base64_matrix)
            transform_matrix = np.frombuffer(binary_string, dtype=np.float64).reshape((4, 4))

            obj_json_path = os.path.join(root_mesh_dir, obj_name)
            print(f'obj_json_path: {obj_json_path}')
            with open(obj_json_path, 'r') as f:
                concept = json.load(f)

            relative_path = concept['path'][idx]
            obj_path = os.path.join(root_path, relative_path.lstrip(os.sep))
            print(f'obj_name: {obj_name}')
            print(f'obj_path: {obj_path}')
            names.append(obj_name.replace('.json', ''))

            # import model
            with stdout_redirected():
                bpy.ops.import_scene.gltf(filepath=obj_path, merge_vertices=True)

            obj = bpy.context.selected_objects[0]
            obj.matrix_world = np.eye(4)
            obj.scale *= scale
            bpy.context.view_layer.update()
            obj.matrix_world = Matrix(transform_matrix) @ obj.matrix_world
            objects_name.append(obj.name)
            obj.hide_viewport = False

        bpy.context.view_layer.update()
        bbox_min, bbox_max = self.scene_bbox()
        offset = -(bbox_min + bbox_max) / 2
        for name in objects_name:
            bpy.data.objects[name].matrix_world.translation += offset

        # for two objects save scene as glb file
        if len(names) == 2:
            # take only the last part of the path
            hash = self.output_path.split('/')[-1]
            print(f"hash: {hash}")
            save_path = f"{self.root_path}/{names[0]}_{names[1]}"
            os.makedirs(save_path, exist_ok=True)
            bpy.ops.export_scene.gltf(filepath=f"{save_path}/{hash}.glb")

        anglez = 0
        for anglex in range(0, 370, 18):
            scene.camera = camera
            C.window.scene = scene
            C.scene.name = obj_path

            camera.location = (self.camera_distance * np.sin(anglex * np.pi / 180),
                               self.camera_distance * np.tan(anglez * np.pi / 180),
                               self.camera_distance * np.cos(anglex * np.pi / 180))
            camera.rotation_euler = ([radians(a) for a in (180 - anglez, anglex, 0)])

            C.scene.camera = camera
            C.scene.render.filepath = f"{self.output_path}/%d_%d____.png" % (anglex, anglez)

            with stdout_redirected():
                bpy.ops.render.render(write_still=True)

Any help will be much appreciated!

Hm, maybe asking at some Blender forum?

In any case, a rather general (and possibly useless advice) is to try to find whether the problem is in the code or in the objects. Sometimes perfectly good code fails because of bad data, or because the date is not in the expected format. If you try with two simple objects, like two boxes, does your code work well? If it fails, the issue is in the code. If it works fine, you may try a little bit more complex objects.

You shouldn’t need a blender script to join 2 models? Are you trying to automate the process?
You can import 2 models in blender, and export the whole thing as one .glb
I don’t know what that blender script is supposed to do, but it also looks a bit sus, changing nodes and stuff. Perhaps that script is out of date? Not enough info in the post to know whats going on…

1 Like