使用案例
概述
Aspose.3D 是一个开源的 Python 3D 文件格式库,使开发者能够以编程方式创建、操作和转换 3D 场景和模型。它通过提供强大的文件 I/O 和场景图处理,支持现代 3D 工作流。
该库全面支持 glTF(GL 传输格式)并提供 PBR 材质支持,使其适用于 Web 和实时渲染管线。其层次化节点结构允许通过节点之间的父子关系直观地组织 3D 场景,从而实现对复杂模型的可扩展场景管理。
工作原理
Aspose.3D for Python 提供对 3D 场景和模型的编程控制,采用以 Scene、Node 和 Entity 为中心的结构化对象模型。开发者可以加载现有格式,如 STL(广泛用于 3D 打印),或使用核心基元如 Mesh 从头构建场景。该库支持网格和实体管理,能够检查和修改顶点数据、多边形拓扑以及场景层次结构中的空间关系。
from aspose.threed import Scene
from aspose.threed.entities import Mesh
# Create a Mesh instance
mesh = Mesh()
# Access mesh data (control_points and edges are properties, not method calls)
vertices = mesh.control_points
edges = mesh.edges代码示例
本示例演示了如何使用加载选项从 OBJ 文件加载 3D 模型,然后遍历其场景图以检查网格几何。Scene.from_file() 仅接受文件路径;使用 scene.open() 传递加载选项。
from aspose.threed import Scene
from aspose.threed.entities import Mesh
from aspose.threed.formats import ObjLoadOptions
# Import an OBJ file with load options
# Note: Scene.from_file() takes only a file path argument.
# To pass options, use scene.open() instead.
options = ObjLoadOptions()
options.enable_materials = True
options.flip_coordinate_system = False
scene = Scene()
scene.open("model.obj", options)
# Access imported data
for node in scene.root_node.child_nodes:
if node.entity and isinstance(node.entity, Mesh):
mesh = node.entity
print(f"Mesh: {node.name}")
print(f" Vertices: {len(mesh.control_points)}")
print(f" Polygons: {mesh.polygon_count}")另请参阅
Aspose.3D 为可视化工具和交互式应用提供强大的 3D Python 开发能力。开发者可以创建和操作 3D 场景,通过 ObjLoadOptions.enable_materials 导入带材质加载的 OBJ 文件,使用 ObjExporter 导出 OBJ 文件,并使用内置类型如 Vector4 和 Matrix4 执行底层向量数学运算。