كيفية تحويل النماذج 3D إلى Python

كيفية تحويل النماذج 3D إلى Python

تحويل النمط مع Aspose.3D FOSS لـ Python هو عملية خطوتين: تحميل إلى Scene الكائن، ثم حفظ إلى تنسيق النتيجة المطلوبة.لأن جميع الهندسة المعمارية يتم الاحتفاظ بها في تمثيل مشترك في الذاكرة، لا تتطلب خطوات متوسطة محددة للنموذج.تظهر الأقسام أدناه التحويلات الأكثر شيوعا مع رمز العمل.

خطوة بخطوة دليل

الخطوة 1: قم بتثبيت الحزمة

asposefoss/3d is not yet published — build from source until it ships. See the project README for build instructions.

لا تتطلب مكتبات النظام أو المجلدات أو إضافية تعتمد على وقت التشغيل.


الخطوة 2: تحميل نموذج المصدر

استخدام Scene.from_file() في أسهل الحالة: يتم الكشف عن النموذج تلقائيًا من ملحق الملفات:

from aspose.threed import Scene

scene = Scene.from_file("model.obj")

بالنسبة لملفات OBJ حيث تحتاج إلى السيطرة على نظام الإحداثيات أو تحميل المواد، استخدم scene.open() مع ObjLoadOptions:

from aspose.threed import Scene
from aspose.threed.formats import ObjLoadOptions

options = ObjLoadOptions()
options.flip_coordinate_system = True  # Convert to Z-up if needed
options.enable_materials = True        # Load the accompanying .mtl file
options.normalize_normal = True

scene = Scene()
scene.open("model.obj", options)

كلا النهجين يخلقان نفس النوع. Scene وَقَالَ الْمُسْتَبِيح: فَإِنَّهُ لَا يَحْزَن.


الخطوة الثالثة: تحقق من المشهد المضغوط

قبل التزام التحويل ، فمن المفيد التحقق مما إذا كانت الهندسة المعمارية مدفوعة بشكل صحيح.ملف مفقود أو ميزة FBX غير مدعومة أو مشكلة مسار مع .mtl يمكن لكل ملف أن ينتج مشهد فارغ.

from aspose.threed import Scene
from aspose.threed.entities import Mesh

scene = Scene.from_file("model.obj")

mesh_count = 0
total_vertices = 0

def count_meshes(node) -> None:
    global mesh_count, total_vertices
    for entity in node.entities:
        if isinstance(entity, Mesh):
            mesh_count += 1
            total_vertices += len(entity.control_points)
    for child in node.child_nodes:
        count_meshes(child)

count_meshes(scene.root_node)
print(f"Loaded {mesh_count} mesh(es), {total_vertices} total vertices")

if mesh_count == 0:
    raise ValueError("Scene contains no geometry: check the source file path and format")

الخطوة 4: حفظ إلى تنسيق الهدف

مكالمة scene.save() مع مسار الخروج. اجتياز شكل محدد خيارات التخزين الكائن للسيطرة على ثنائي مقابل ASCII الخروجي، ومركبات التنسيق، والضغط.

OBJ إلى STL (بينيري)

from aspose.threed import Scene
from aspose.threed.formats import StlSaveOptions

scene = Scene.from_file("model.obj")

save_opts = StlSaveOptions()
##StlSaveOptions defaults to binary output, which is more compact.

scene.save("model.stl", save_opts)
print("Saved model.stl")

أوبج إلى glTF 2.0

from aspose.threed import Scene
from aspose.threed.formats import GltfSaveOptions

scene = Scene.from_file("model.obj")

save_opts = GltfSaveOptions()

scene.save("model.gltf", save_opts)
print("Saved model.gltf")

لتوفير كحرف ثنائي GLB ذاتية المحتوى بدلا من .gltf + البوفرات الخارجية، تغيير التمديد الإنتاج إلى .glb:

scene.save("model.glb", save_opts)

3MF إلى 3mF

from aspose.threed import Scene
from aspose.threed.formats import ThreeMfSaveOptions

scene = Scene.from_file("model.obj")

save_opts = ThreeMfSaveOptions()

scene.save("model.3mf", save_opts)
print("Saved model.3mf")

STL إلى GLTF 2.0

نفس النموذج ينطبق بغض النظر عن تنسيق المصدر:

from aspose.threed import Scene
from aspose.threed.formats import GltfSaveOptions

scene = Scene.from_file("input.stl")
scene.save("output.gltf", GltfSaveOptions())
print("Saved output.gltf")

الخطوة الخامسة: تحقق من النتيجة.

بعد التخزين، تأكد من وجود ملف الناتج ولديه حجم غير صفر.لتحقق أكثر تفصيلا، إعادة تحميلها ومقارنة عدد الماس:

import os
from aspose.threed import Scene
from aspose.threed.entities import Mesh

output_path = "model.stl"

##Basic file-system check
size = os.path.getsize(output_path)
print(f"Output file size: {size} bytes")
if size == 0:
    raise RuntimeError("Output file is empty: save may have failed silently")

##Round-trip verification: reload and count geometry
def _iter_nodes(node):
    yield node
    for child in node.child_nodes:
        yield from _iter_nodes(child)

reloaded = Scene.from_file(output_path)
mesh_count = sum(
    1
    for node in _iter_nodes(reloaded.root_node)
    for entity in node.entities
    if isinstance(entity, Mesh)
)
print(f"Round-trip check: {mesh_count} mesh(es) in output")

مشكلات و تصحيحات مشتركة

يتم إنشاء ملف الإخراج ولكن لا يحتوي على الهندسة الجيولوجية

قد يكون ملف المصدر محمولًا مع صفر الماس.إضافة خطوة التحقق من الخطوة 3 قبل حفظها.تأكد أيضًّا أن امتداد الملف يتطابق مع النموذج الحقيقي؛ يستخدم Aspose.3D التمديد لاختيار الشريحة.

النتائج المفقودة لـ GTTF هي النسيج المختفي

Aspose.3D FOSS يحمل الجيومترية والخصائص المادية من خلال التحويل.إذا كانت المصدر OBJ يشير إلى ملفات الصورة الخارجية في .mtl,هذه الملفات الصورة لا يتم نسخها تلقائيًا إلى جانب .gltf.نسخ الصور النصية إلى دليل الناتج يدويا بعد حفظ.

STL الخروج يبدو داخل وخارج (الوجه الطبيعي المطاط)

STL لا يحمل بيانات الأوامر المتعددة.إذا تم تحويل عادات الإنتاج، إعداد StlSaveOptions خيارات إذا كانت متاحة، أو التمرير نظام الإحداثيات أثناء التحميل: ObjLoadOptions.flip_coordinate_system = True.

ValueError: unsupported format عندما ينقذ

تحقق من إصدار ملف التمديد هو واحد من .obj, .stl, .gltf, .glb, .dae, .3mf.التمديدات حساسة للكأس على لينكس.

الكمبيوتر الكبير يؤدي إلى تحويل بطيئ

Aspose.3D FOSS يعالج الهندسة الجيومترية في ذاكرة.للملفات التي تحتوي على ملايين البوليغونات، توفر ما يكفي من م.لا يوجد حاليا أي API المكتوبة عبر الإنترنت.


الأسئلة المتكررة (FAQ)

هل يمكنني تحويل ملف دون كتابته إلى القرص أولاً؟?

نعم كلاهما scene.open() و scene.save() قبول الكائنات المماثلة للملفين بالإضافة إلى مسارات الملف. read() للتحميل أو write() من أجل الادخار :

import io
from aspose.threed import Scene

# Load from an in-memory buffer
data = open('model.obj', 'rb').read()
scene = Scene()
scene.open(io.BytesIO(data))

# Save to an in-memory buffer
buf = io.BytesIO()
scene.save(buf)

هل يتم دعم FBX كشكل مصدر للتحويل؟?

يتم تنفيذ توكينات FBX جزئياً، ولكن لا يكون المقطع كاملاً.يمكن أن تنتج إدخال FB X مشاهد غير كاملة.استخدم OBJ أو STL أو glTF أو COLLADA أو 3MF كمصادر مصدر موثوقة.

هل ستبقى المواد على قيد الحياة من تحويل OBJ-to-glTF؟?

يتم نقل خصائص المواد الأساسية فونغ/لامبرت (اللون المتفرق) من خلال Scene نموذج وكتب في كتلة المواد glTF. يتم إزالة المعلمات الإجراءات أو الرموز المخصصة غير قابلة للتعبير في نمط المادة gl TF .

هل يمكنني تحويل ملفات متعددة إلى قوس واحد؟?

نعم، كل واحد منهم Scene.from_file() يخلق المكالمة كائنًا مستقلًّا ، لذلك يكون الدوران عبر قائمة بالطرق بسيطًَّا:

from pathlib import Path
from aspose.threed import Scene
from aspose.threed.formats import StlSaveOptions

source_dir = Path("input")
output_dir = Path("output")
output_dir.mkdir(exist_ok=True)

opts = StlSaveOptions()
for obj_file in source_dir.glob("*.obj"):
    scene = Scene.from_file(str(obj_file))
    out_path = output_dir / obj_file.with_suffix(".stl").name
    scene.save(str(out_path), opts)
    print(f"Converted {obj_file.name} -> {out_path.name}")

هل يحافظ التحويل على الهرم الحرفي (العناصر الأبوية / الطفل)؟?

نعم. يتم الحفاظ على شجرة العقد إلى أقصى حد يسمح به تنسيق الهدف. تتخزين تناسقات مثل STL فقط الهندسة المسطحة دون هيكل العمود؛ يتم تخزينة الهييرارقة على التخزن. تحافظ تنسقات كالـ glTF و COLLADA على الهرم الكامل.

 العربية