Files
FiDA-3D-Trellis/0_glb_to_obj.py
2026-04-13 11:20:56 +08:00

69 lines
1.8 KiB
Python
Executable File
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import bpy
import sys
import os
import argparse
glb_input = "trellis_out/sample.ply"
obj_output = "obj_output/sample.obj"
def parse_args():
argv = sys.argv
argv = argv[argv.index("--") + 1:] if "--" in argv else []
p = argparse.ArgumentParser()
p.add_argument("-i", "--input", default=True)
p.add_argument("-o", "--output", required=True)
return p.parse_args(argv)
def clean():
bpy.ops.object.select_all(action='SELECT')
bpy.ops.object.delete(use_global=False)
def main():
args = parse_args()
clean()
bpy.ops.import_scene.gltf(filepath=args.input) # 支持 .glb/.gltf
# 只保留 mesh并合并成一个符合你“单资产”假设
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)
out_dir = os.path.dirname(args.output)
if out_dir:
os.makedirs(out_dir, exist_ok=True)
# bpy.ops.wm.obj_export(
# filepath=args.output,
# export_selected_objects=True,
# export_normals=True,
# export_uv=True,
# )
bpy.ops.export_scene.obj(
filepath=args.output,
use_selection=True, # 只导出选中的对象
use_normals=True,
use_uvs=True,
use_materials=True, # 根据需要改(你原来没导出材质)
axis_forward='-Z', # glTF 通常是 -Z forwardY up可根据需要调整
axis_up='Y',
# keep_vertex_order=True, # 可选:保持顶点顺序
)
if __name__ == "__main__":
main()