مقدمة
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 (إنشاء المحتوى الرقمي) الكاملة ثقيلة للغاية للمعالجة الدفعية. ما تحتاجه هو مكتبة يمكنها تحميل أي من هذه الصيغ، وتزويدك بمخطط مشهد متسق، والكتابة إلى أي صيغة هدف دون الحاجة إلى واجهة مستخدم رسومية أو برنامج تشغيل رسومي.
Aspose.3D FOSS لـ Python (aspose-3d-foss, MIT license) يغطي تلك الفجوة. يوضح هذا الدليل أكثر مسارات التحويل شيوعًا مع أمثلة شفرة كاملة وقابلة للتنفيذ.
ما الصيغ التي يدعمها 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: إعداد نموذج للطباعة ثلاثية الأبعاد
OBJ هو أكثر تنسيق إخراج شائع من أدوات النمذجة والنحت. STL هو اللغة المشتركة لبرامج تقطيع الطباعة ثلاثية الأبعاد. التحويل بينهما يتم باستدعاء واحد.
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 الخاص بك يحتوي على وجوه رباعية أو متعددة الأضلاع، فإن المُصدّر يقوم بتحويلها إلى مثلثات تلقائيًا قبل الكتابة. إذا أردت التحكم في عملية التحويل إلى مثلثات صراحةً قبل الحفظ، استدعِ mesh.triangulate() على كل شبكة في المشهد:
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 من الماسحات الضوئية ثلاثية الأبعاد وأنظمة 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. للتعمق أكثر في نموذج مخطط المشهد (العُقد، الشبكات، التحويلات)، راجع:
- Class Scene: التحميل، الحفظ، وبيانات التعريف على مستوى المشهد
- Class Node: التسلسل الهرمي، التحويلات، إرفاق الكيانات
- Class Mesh: بيانات الرؤوس، المضلعات، عناصر الرؤوس
- كيفية: تحميل الملفات: كيفية تحميل نماذج ثلاثية الأبعاد في Python
- PyPI: aspose-3d-foss على PyPI