PolygonModifier — Aspose.3D Python API Reference
Package: aspose.threed.entities (aspose-3d-foss 26.1.0)
PolygonModifier is a static utility class that provides polygon tessellation operations on Mesh and Scene objects. All methods are class methods; PolygonModifier is never instantiated directly.
from aspose.threed.entities import PolygonModifierThe primary operation is triangulation: converting quads and n-gons into triangles using ear-clipping. This is required before exporting to formats that accept only triangle geometry (STL, some glTF pipelines).
Methods
triangulate(arg) — triangulate a Mesh
Return a new Mesh in which every polygon has been converted to triangles. The original mesh is not modified. Vertex element layers (normals, UVs, colours) are copied to the new mesh.
@staticmethod
def triangulate(mesh: Mesh) -> Mesh| Parameter | Type | Description |
|---|---|---|
mesh | Mesh | The source mesh to triangulate. |
Returns: Mesh — a new mesh containing only triangle polygons.
from aspose.threed.entities import Mesh, PolygonModifier
from aspose.threed.utilities import Vector4
mesh = Mesh()
for x, z in [(0, 0), (1, 0), (1, 1), (0, 1)]:
mesh.control_points.append(Vector4(float(x), 0.0, float(z), 1.0))
mesh.create_polygon(0, 1, 2, 3) # one quad
tri_mesh = PolygonModifier.triangulate(mesh)
print(tri_mesh.polygon_count) # 2triangulate(scene) — triangulate all meshes in a Scene
Traverse the entire scene graph and triangulate every Mesh entity in place. After this call, no non-triangle polygons remain in the scene.
@staticmethod
def triangulate(scene: Scene) -> None| Parameter | Type | Description |
|---|---|---|
scene | Scene | The scene whose mesh entities will be triangulated. |
Returns: None
from aspose.threed import Scene
from aspose.threed.entities import PolygonModifier
scene = Scene.from_file("model_with_quads.obj")
PolygonModifier.triangulate(scene)
scene.save("model_triangulated.stl")triangulate(control_points, polygon) — triangulate a single polygon
Low-level overload: given a list of control points and a list of vertex indices forming one polygon, return a list of triangle index triplets produced by fan triangulation.
@staticmethod
def triangulate(
control_points: list[Vector4],
polygon: list[int],
) -> list[list[int]]| Parameter | Type | Description |
|---|---|---|
control_points | list[Vector4] | The full vertex position array. |
polygon | list[int] | Indices into control_points defining the polygon to split. |
Returns: list[list[int]] — a list of [i, j, k] triplets, each representing one output triangle.
from aspose.threed.entities import PolygonModifier
from aspose.threed.utilities import Vector4
pts = [
Vector4(0.0, 0.0, 0.0, 1.0),
Vector4(1.0, 0.0, 0.0, 1.0),
Vector4(1.0, 1.0, 0.0, 1.0),
Vector4(0.0, 1.0, 0.0, 1.0),
]
quad_indices = [0, 1, 2, 3]
triangles = PolygonModifier.triangulate(pts, quad_indices)
print(triangles) # e.g. [[0, 1, 2], [0, 2, 3]]triangulate(control_points, polygons, generate_normals, normals_out) — batch triangulate with optional normal generation
Batch-triangulate a list of polygon index lists. Optionally compute face normals for each output triangle and append them to normals_out.
@staticmethod
def triangulate(
control_points: list[Vector4],
polygons: list[list[int]],
generate_normals: bool = False,
normals_out: list[Vector3] | None = None,
) -> list[list[int]]| Parameter | Type | Description |
|---|---|---|
control_points | list[Vector4] | The vertex position array. |
polygons | list[list[int]] | A list of polygon definitions, each a list of vertex indices. |
generate_normals | bool | When True, compute a face normal for each output triangle. |
normals_out | list[Vector3] | None | Output list; face normals are appended here when generate_normals=True. |
Returns: list[list[int]] — all output triangle triplets in order.
from aspose.threed.entities import PolygonModifier
from aspose.threed.utilities import Vector4, Vector3
pts = [
Vector4(0, 0, 0, 1), Vector4(1, 0, 0, 1),
Vector4(1, 1, 0, 1), Vector4(0, 1, 0, 1),
]
polys = [[0, 1, 2, 3]]
normals: list[Vector3] = []
tris = PolygonModifier.triangulate(pts, polys, True, normals)
print(len(tris)) # 2
print(len(normals)) # 2