Wie man mit 3D in Python anfängt
Voraussetzungen für die Durchführung der Maßnahmen
- Python 3.7 oder neuer
- Pip-Paketmanager
Schritt 1 Installieren des Pakets
Installieren Sie Aspose.3D FOSS für Python von PyPI:
pip install aspose-3d-fossDie Installation überprüfen:
import aspose.threed as a3d
scene = a3d.Scene()
print("Aspose.3D FOSS is working, version:", a3d.Scene.VERSION)Schritt 2 Erstellen Sie Ihre erste 3D-Szene
A) Die Scene ist der Wurzelcontainer für alle 3D-Objekte. Der Konstruktor akzeptiert eine optionale Entität, Mutter-Szene und Name:
import aspose.threed as a3d
# Create an empty scene (CLM-3d-35d727)
scene = a3d.Scene()
# Access the root node of the scene (CLM-3d-859ac2)
root = scene.root_node # root_node is a property, not a method — no ()
print("Root node:", root)Schritt 3 Ein Netz in die Szene einfügen
Mesh repräsentiert die Geometrie des Polygons. Node Entitäten an den Szenengraph angehängt:
import aspose.threed as a3d
scene = a3d.Scene()
# Create a named mesh (CLM-3d-529c11)
mesh = a3d.Mesh("my_mesh")
# Create a node and attach the mesh (CLM-3d-0edae8)
node = a3d.Node("my_node", mesh)
# Add the node as a child of the root (CLM-3d-a403f5)
scene.root_node.add_child_node(node) # root_node is a property, not a method — no ()Schritt 4 Laden einer vorhandenen 3D-Datei ein
Scene.open() Lade eine 3D-Datei von der Festplatte oder Stream. Es akzeptiert einen Pfad und optionale Belastungsmöglichkeiten:
import aspose.threed as a3d
# Load an OBJ file (CLM-3d-d84964)
scene = a3d.Scene()
scene.open("model.obj")
# Inspect the loaded scene
root = scene.root_node # root_node is a property, not a method — no ()
print("Child nodes:", len(root.child_nodes)) # child_nodes is a property, not a method — no ()Um das Dateifortrag vor dem Laden zu erkennen:
import aspose.threed as a3d
# Detect the format from a file name (CLM-3d-d2de62)
fmt = a3d.FileFormat.detect(None, "model.fbx")
if fmt:
print("Detected format:", fmt.extension) # extension is a property, not a method — no ()Schritt 5 Speichern der Ausgabe in eine Datei
Scene.save() Die empfohlene Vorgehensweise ist die Autodetection auf der Grundlage von Erweiterungen: Aspose.3D zieht das Format aus der Dateierweitung ab. (CLM-3d-8051bd, CLM-3D-b28d7a, CLM- 3d-de8e1a): Die folgenden Angaben sind nicht erforderlich:
import aspose.threed as a3d
scene = a3d.Scene()
# ... populate scene ...
# Save as Wavefront OBJ — extension auto-detects the format
scene.save("output.obj")
# Save as glTF 2.0 binary — .glb extension triggers binary GLB output
scene.save("output.glb")Sie können auch ein explizites Format mit den Werkstattmethoden geben FileFormat.WAVEFRONT_OBJ() oder FileFormat.GLTF2(), aber die Erweiterung der automatischen Erkennung ist einfacher und tragbarer.
Nächste Schritte
- Wie man 3D-Modelle in Python lädt Formate, Optionen und Durchlauf der Szenengraphik
- Wie man 3D-Modelle in Python konvertiert Arbeitsflüsse zur Batch-Umwandlung
- Wie man eine Mesh-Geometrie in Python erstellt Kontrollpunkte, Polygone und Normale
- FAQ häufige Fragen und Fehlerbehebung