บทนำ
3D content rarely stays in a single format for its entire lifetime. A model may originate as an OBJ file exported from a modelling application, travel through a 3D printing pipeline as STL, appear in a web application as glTF, and end up in an additive manufacturing tool as 3MF. Each format serves a different audience and a different downstream tool, and conversion between them is something most 3D pipelines need to handle reliably.
Python เหมาะสมโดยธรรมชาติสำหรับงานนี้: สคริปต์เชลล์มีความเปราะบางเกินไป, และการบูรณาการเครื่องมือ DCC (digital content creation) เต็มรูปแบบก็หนักเกินไปสำหรับการประมวลผลแบบกลุ่ม สิ่งที่คุณต้องการคือไลบรารีที่สามารถโหลดรูปแบบใดก็ได้เหล่านี้, ให้กราฟฉากที่สอดคล้องกัน, และเขียนออกไปยังรูปแบบเป้าหมายใดก็ได้โดยไม่ต้องใช้ GUI หรือไดรเวอร์กราฟิก.
Aspose.3D FOSS สำหรับ Python (aspose-3d-foss, ใบอนุญาต MIT) ครอบคลุมช่องว่างนั้น คู่มือนี้แสดงเส้นทางการแปลงที่พบบ่อยที่สุดพร้อมตัวอย่างโค้ดที่สมบูรณ์และสามารถรันได้.
รูปแบบใดบ้างที่ Aspose.3D FOSS รองรับ?
| รูปแบบ | Extension(s) | โหลด | บันทึก | กรณีการใช้งานทั่วไป |
|---|---|---|---|---|
| Wavefront OBJ | .obj | ใช่ | ใช่ | การแลกเปลี่ยนจากเครื่องมือสร้างโมเดล; .mtl รองรับไฟล์วัสดุ |
| STL | .stl | ใช่ | ใช่ | 3D printing, CAD export; binary and ASCII variants |
| glTF 2.0 / GLB | .gltf, .glb | ใช่ | ใช่ | เว็บวิวเวอร์, เอนจินเกม; GLB เป็นรูปแบบไบนารีที่เป็นอิสระและรวมทุกอย่างไว้ในไฟล์เดียว |
| COLLADA | .dae | ใช่ | ใช่ | นำเข้าจากเครื่องมือแอนิเมชัน; ColladaSaveOptions รองรับการส่งออก |
| 3MF | .3mf | ใช่ | ใช่ | การผลิตแบบเพิ่มชั้น, มีเมตาดาต้าการพิมพ์ที่ละเอียดกว่า STL |
การตรวจจับรูปแบบทำโดยอัตโนมัติจากส่วนขยายของไฟล์. คลาสตัวเลือกเฉพาะรูปแบบ (ObjLoadOptions, StlSaveOptions, GltfSaveOptions) มีให้ใช้เมื่อคุณต้องการการควบคุมในระดับละเอียด.
หมายเหตุเกี่ยวกับ FBX: ไลบรารีนี้รวมตัวแยกโทเคน FBX แบบบางส่วนสำหรับการนำเข้า. ไม่แนะนำให้ใช้ FBX สำหรับกระบวนการแปลงในสภาพแวดล้อมการผลิตในรุ่นนี้; ใช้ OBJ หรือ glTF เป็นรูปแบบกลางแทน.
OBJ ไปยัง STL: การเตรียมโมเดลสำหรับการพิมพ์ 3D
OBJ เป็นรูปแบบการส่งออกที่พบบ่อยที่สุดจากเครื่องมือโมเดลลิงและสเกลป์ท์ STL เป็นภาษากลางของซลายเซอร์พิมพ์ 3D การแปลงระหว่างสองรูปแบบนี้ทำได้ด้วยการเรียกเดียว.
from aspose.threed import Scene
from aspose.threed.formats import StlSaveOptions
scene = Scene.from_file("model.obj")
opts = StlSaveOptions()
scene.save("model.stl", opts)
STL จะเข้ารหัสเฉพาะหน้าตรีเหลี่ยม. หากไฟล์ OBJ ของคุณมีหน้ากว้างสี่เหลี่ยม (quad) หรือ n-gon, ตัวส่งออกจะทำการแปลงเป็นสามเหลี่ยมโดยอัตโนมัติก่อนบันทึก. หากคุณต้องการควบคุมการแปลงเป็นสามเหลี่ยมอย่างชัดเจนก่อนบันทึก, ให้เรียก mesh.triangulate() บนแต่ละ mesh ในฉาก:
from aspose.threed import Scene
from aspose.threed.entities import Mesh
from aspose.threed.formats import StlSaveOptions
scene = Scene.from_file("model.obj")
def triangulate_all(node):
for entity in list(node.entities):
if isinstance(entity, Mesh):
tri_mesh = entity.triangulate()
# Attach the triangulated mesh via add_entity
node.add_entity(tri_mesh)
for child in node.child_nodes:
triangulate_all(child)
triangulate_all(scene.root_node)
scene.save("model_triangulated.stl", StlSaveOptions())
OBJ ไปยัง glTF: การส่งออกสำหรับเว็บและเอนจินเกม
glTF 2.0 เป็นรูปแบบการแลกเปลี่ยนที่แนะนำสำหรับเรนเดอร์เรียลไทม์, ตัวดู WebGL, และเอนจินเกมเช่น Babylon.js, Three.js, และ Unity GLB (เวอร์ชันไบนารี) จะบรรจุเรขาคณิต, เทกซ์เจอร์, และวัสดุไว้ในไฟล์เดียวที่เป็นอิสระ ซึ่งทำให้การให้บริการผ่าน HTTP ง่ายขึ้น.
from aspose.threed import Scene
from aspose.threed.formats import GltfSaveOptions
scene = Scene.from_file("model.obj")
##Save as JSON-based glTF (external buffer)
opts_gltf = GltfSaveOptions()
scene.save("model.gltf", opts_gltf)
##Save as self-contained GLB binary: preferred for web delivery
scene.save("model.glb", GltfSaveOptions())
รูปแบบจะถูกสรุปจากส่วนขยายของไฟล์: .gltf สร้างคู่ JSON+binary-buffer; .glb สร้างไบนารีไฟล์เดียว. ข้อมูลวัสดุ OBJ (LambertMaterial, PhongMaterial) จะถูกส่งต่อไปยังการแสดงผลวัสดุ glTF PBR ที่มีเทียบเท่า.
STL ไปยัง glTF: จากสแกนเนอร์หรือผลลัพธ์ CAD ไปยังเว็บ
ไฟล์ STL จากสแกนเนอร์ 3 มิติและระบบ CAD เป็นอินพุตที่พบบ่อยซึ่งต้องทำให้สามารถดูได้บนเว็บ STL มีเพียงเรขาคณิตแบบสามเหลี่ยมและไม่มีข้อมูลวัสดุ ดังนั้นการแปลงจึงตรงไปตรงมา.
from aspose.threed import Scene
from aspose.threed.formats import GltfSaveOptions
scene = Scene.from_file("scan.stl")
scene.save("scan.glb", GltfSaveOptions())
หาก STL ถูกสร้างโดยเครื่องมือ CAD ที่ใช้ระบบพิกัดแปลก (Z-up เทียบกับ Y-up) คุณสามารถตรวจสอบและแก้ไขการแปลงของโหนดรากก่อนบันทึกได้:
from aspose.threed import Scene
from aspose.threed.formats import GltfSaveOptions
scene = Scene.from_file("cad_export.stl")
##Rotate 90 degrees around X to convert Z-up to Y-up
root = scene.root_node
root.transform.set_euler_angles(-90.0, 0.0, 0.0)
scene.save("cad_export_yup.glb", GltfSaveOptions())
glTF ไปยัง 3MF: การเตรียมพร้อมสำหรับการผลิตแบบเพิ่มชั้น
3MF is a modern 3D printing format backed by the 3MF Consortium. It supports richer metadata than STL (colour, material assignments, build instructions) and is accepted by slicer software such as PrusaSlicer and Bambu Studio. If you are delivering models from a web viewer into a printing workflow, converting GLB to 3MF is a useful step.
from aspose.threed import Scene
from aspose.threed import FileFormat
from aspose.threed.formats import SaveOptions
scene = Scene.from_file("model.glb")
scene.save("model.3mf")
รูปแบบ 3MF จะถูกตรวจจับโดยอัตโนมัติจาก .3mf ส่วนขยาย. หากต้องการควบคุมอย่างชัดเจน ให้ส่งผ่าน SaveOptions อินสแตนซ์ที่กำหนดค่าไว้สำหรับ 3MF.
การแปลงเป็นชุด
โดยปฏิบัติ งานแปลงจะทำงานกับไดเรกทอรีของไฟล์แทนไฟล์เดี่ยว รูปแบบต่อไปนี้จัดการโฟลเดอร์ของไฟล์ OBJ และแปลงแต่ละไฟล์เป็น GLB พร้อมการจัดการข้อผิดพลาดต่อไฟล์เพื่อให้ไฟล์ที่มีปัญหาเพียงไฟล์เดียวไม่ทำให้การทำงานทั้งหมดหยุด.
import os
from aspose.threed import Scene
from aspose.threed.formats import GltfSaveOptions
input_dir = "./obj_files"
output_dir = "./glb_files"
os.makedirs(output_dir, exist_ok=True)
opts = GltfSaveOptions()
results = {"ok": [], "failed": []}
for filename in os.listdir(input_dir):
if not filename.lower().endswith(".obj"):
continue
src = os.path.join(input_dir, filename)
stem = os.path.splitext(filename)[0]
dst = os.path.join(output_dir, stem + ".glb")
try:
scene = Scene.from_file(src)
scene.save(dst, opts)
results["ok"].append(filename)
print(f"OK {filename} -> {stem}.glb")
except Exception as exc:
results["failed"].append((filename, str(exc)))
print(f"ERR {filename}: {exc}")
print(f"\n{len(results['ok'])} converted, {len(results['failed'])} failed.")
if results["failed"]:
for name, reason in results["failed"]:
print(f" {name}: {reason}")
รูปแบบนี้ขยายได้อย่างเป็นธรรมชาติไปยังคู่รูปแบบต้นทาง/ปลายทางอื่น ๆ: เปลี่ยนตัวกรองส่วนขยายและ SaveOptions คลาสสำหรับการผสมใด ๆ ในตารางการสนับสนุนข้างต้น.
สรุป
Aspose.3D FOSS สำหรับ Python ทำให้การแปลงรูปแบบเป็นเรื่องง่าย: โหลดด้วย Scene.from_file, จากนั้นอาจตรวจสอบหรือแก้ไขกราฟฉาก, แล้วบันทึกด้วยรูปแบบที่เหมาะสม SaveOptions. ไลบรารีจัดการการทำสามเหลี่ยม, การทำให้พิกัดเป็นมาตรฐาน, และลักษณะเฉพาะของรูปแบบโดยอัตโนมัติภายใน.
สำหรับรายการคลาสและเมธอดทั้งหมด, ดูที่ API Reference. เพื่อทำความเข้าใจเชิงลึกเกี่ยวกับโมเดลกราฟฉาก (nodes, meshes, transforms), ดู:
- คลาส Scene: การโหลด, การบันทึก, และเมตาดาต้าระดับซีน
- คลาส Node: ลำดับชั้น, การแปลง, การแนบเอนทิตี้
- คลาส Mesh: ข้อมูลเวอร์เท็กซ์, โพลิกอน, องค์ประกอบเวอร์เท็กซ์
- วิธีโหลดโมเดล 3 มิติใน Python
- PyPI: aspose-3d-foss บน PyPI