परिचय

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 (डिजिटल कंटेंट क्रिएशन) टूल इंटीग्रेशन बैच प्रोसेसिंग के लिए बहुत भारी होते हैं। आपको एक लाइब्रेरी चाहिए जो इन सभी फ़ॉर्मेट्स को लोड कर सके, आपको एक सुसंगत सीन ग्राफ़ दे, और किसी भी लक्ष्य फ़ॉर्मेट में बिना 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हाँहाँAdditive manufacturing, STL से अधिक समृद्ध प्रिंट मेटाडेटा

फ़ॉर्मेट का पता लगाना फ़ाइल एक्सटेंशन से स्वचालित है। Format-specific option classes (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 फ़ाइल में क्वाड या n‑गॉन चेहरें हैं, तो एक्सपोर्टर उन्हें लिखने से पहले स्वचालित रूप से त्रिकोणीय बनाता है। यदि आप सहेजने से पहले त्रिकोणीयकरण को स्पष्ट रूप से नियंत्रित करना चाहते हैं, तो कॉल करें। 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 आउटपुट से वेब तक

3D स्कैनर और CAD सिस्टम से आने वाली STL फ़ाइलें सामान्य इनपुट हैं जिन्हें वेब‑देखने योग्य बनाना आवश्यक है। 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 for Python फ़ॉर्मेट रूपांतरण को सरल बनाता है: लोड करें Scene.from_file, वैकल्पिक रूप से सीन ग्राफ़ की जाँच या संशोधन करें, फिर उपयुक्त के साथ सहेजें SaveOptions.लाइब्रेरी त्रिकोणीयकरण, निर्देशांक सामान्यीकरण, और फ़ॉर्मेट-विशिष्ट विचित्रताओं को आंतरिक रूप से संभालती है।.

क्लासों और मेथड्स की पूरी सूची के लिए, देखें API रेफ़रेंस. सीन ग्राफ़ मॉडल (नोड्स, मेषेज़, ट्रांसफ़ॉर्म्स) पर गहराई से जानने के लिए, देखें: