Wie man mit 3D in Python loslegt

Wie man mit 3D in Python loslegt

Voraussetzungen

  • Python 3.8 oder höher
  • pip-Paketmanager

Schritt 1 — Paket installieren

Installieren Sie Aspose.3D FOSS für Python von PyPI:

pip install aspose-3d-foss

Installation überprüfen:

import aspose.threed as a3d
print(a3d.__version__)

Schritt 2 — Erstellen Sie Ihre erste 3D‑Szene

Ein Scene ist der Root-Container für alle 3D-Objekte. Der Konstruktor akzeptiert ein optionales entity, parent scene 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()
print("Root node:", root)

Schritt 3 — Ein Mesh zur Szene hinzufügen

Mesh stellt Polygongeometrie dar. Node fügt Entitäten zum Szenengraphen hinzu:

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)

Schritt 4 — Laden einer vorhandenen 3D‑Datei

Scene.open() lädt eine 3D‑Datei von der Festplatte oder einem Stream. Es akzeptiert einen Pfad und optionale
Ladeoptionen:

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()
print("Child nodes:", len(root.child_nodes()))

Um das Dateiformat 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())

Schritt 5 — Ausgabe in einer Datei speichern

Scene.save() exportiert die Szene in jedes unterstützte Format. Der empfohlene Ansatz ist die erweiterungsbasierte automatische Erkennung: Aspose.3D leitet das Format aus der Dateierweiterung ab (CLM-3d-8051bd, CLM-3d-b28d7a, CLM-3d-de8e1a):

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 über die Fabrikmethoden FileFormat.WAVEFRONT_OBJ() oder FileFormat.GLTF2() übergeben, aber die automatische Erkennung der Erweiterung ist einfacher und portabler.

Nächste Schritte

 Deutsch