Files
FiDA-3D-Trellis/blender_glb_to_obj.py

48 lines
908 B
Python
Raw Normal View History

2026-03-17 11:28:52 +08:00
import bpy
import sys
import os
argv = sys.argv
argv = argv[argv.index("--") + 1:]
glb_input = argv[0]
obj_output = argv[1]
def clean():
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
clean()
bpy.ops.import_scene.gltf(filepath=glb_input)
meshes = [o for o in bpy.context.scene.objects if o.type == "MESH"]
if not meshes:
raise RuntimeError("No mesh objects found in GLB/GLTF.")
bpy.ops.object.select_all(action='DESELECT')
for o in meshes:
o.select_set(True)
bpy.context.view_layer.objects.active = meshes[0]
if len(meshes) > 1:
bpy.ops.object.join()
obj = bpy.context.view_layer.objects.active
obj.select_set(True)
os.makedirs(os.path.dirname(obj_output), exist_ok=True)
bpy.ops.wm.obj_export(
filepath=obj_output,
export_selected_objects=True,
export_normals=True,
export_uv=True,
)
print("OBJ exported:", obj_output)