1
This commit is contained in:
68
0_glb_to_obj.py
Normal file
68
0_glb_to_obj.py
Normal file
@@ -0,0 +1,68 @@
|
||||
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 forward,Y up,可根据需要调整
|
||||
axis_up='Y',
|
||||
# keep_vertex_order=True, # 可选:保持顶点顺序
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user