0

I want to merge several obj format 3d models and output as a gltf format model.

The directory structure looks like this.

folder_1/1.obj, 1.mlt, 1.jpg

folder_2/2.obj, 2.mlt, 2_1.jpg, 2_2.jpg

folder_3/3obj, 3.mlt, 3_1.jpg, 3_2.jpg, 3_3.jpg

folder_4/4.obj, 4.mlt, 4.jpg

And when I run this code:

folder_paths = ["folder_1", "folder_2", "folder_3", "folder_4"]

bpy.ops.object.select_all(action='SELECT')

bpy.ops.object.delete(True)

for folder_path in folder_paths: obj_file = glob.glob(os.path.join(folder_path, "*.obj"))

bpy.ops.import_scene.obj(filepath=obj_file[0])

bpy.ops.object.select_all(action='SELECT')

bpy.ops.object.join()

bpy.ops.export_scene.gltf("merged.gltf", export_format='GLTF_EMBEDDED', ui_tab='GENERAL')

I got this error:

RuntimeError: Operator bpy.ops.object.join.poll() failed, context is incorrect

What should I do?

I appreciate and thank you for your answer.

Duarte Farrajota Ramos
  • 59,425
  • 39
  • 130
  • 187
  • How and in what context do you run the script? – Martynas Žiemys Oct 13 '23 at 06:57
  • See: https://blender.stackexchange.com/questions/13986/how-to-join-objects-with-python – Martynas Žiemys Oct 13 '23 at 07:01
  • looks like you need to set an object as active to join the rest upon it, see this older post, it might help you: https://blender.stackexchange.com/questions/132266/joining-all-meshes-in-any-context-gets-error – cnisidis Oct 13 '23 at 08:21
  • Also to understand using context read: https://blender.stackexchange.com/questions/6101/poll-failed-context-incorrect-example-bpy-ops-view3d-background-image-add – Harry McKenzie Oct 14 '23 at 13:44

1 Answers1

0

Thank you for many answers.

I fixed the code like this.

folder_paths = ["folder_1", "folder_2", "folder_3", "folder_4"]

bpy.ops.object.select_all(action="SELECT") bpy.ops.object.delete(True)

scene = bpy.context.scene

for folder_path in folder_paths: obj_file = glob.glob(os.path.join(folder_path, "*.obj"))

bpy.ops.import_scene.obj(filepath=obj_file[0])

mesh_objects = [obj for obj in scene.objects if obj.type == 'MESH']

for obj in mesh_objects: obj.select_set(True)

bpy.context.view_layer.objects.active = mesh_objects[0]

bpy.ops.object.join()

bpy.ops.export_scene.gltf(filepath="merged.gltf", export_format='GLTF_EMBEDDED', ui_tab='GENERAL')

I could merge several obj format 3d models by bpy.

But the size of merged file is very big.

Total size of several obj format 3d models is about 200MB, and the size of merged file is about 500MB.

Is this normal?

Why is this size big?

I appreciate and thank you for your answer.