วิธีการสร้าง 3D Mesh ด้วย Aspose.3D ใน Python

วิธีการสร้าง 3D Mesh ด้วย Aspose.3D ใน Python

Aspose.3D FOSS สําหรับ Python ช่วยให้คุณสร้าง geometry 3D ทั้งหมดในรหัส: ไม่จําเป็นต้องใช้เครื่องมือการจําลองภายนอก คุณจะสร้างเครื่องหมายที่กําหนดเอง Mesh, โพลิเคชันที่กําหนดเองด้วยตําแหน่งแนวตั้ง (control_points) และใบหน้าคํานวณ (polygons), เพิ่มคุณสมบัติแนวตั้งตามที่กําหนดเองเช่นปกติแล้วบันทึกภาพในรูปแบบใด ๆ ที่ได้รับการสนับสนุน.

คู่มือขั้นตอน

ขั้นตอนที่ 1: ติดตั้งแพคเกจ

ติดตั้ง Aspose.3D FOSS จาก PyPI ไม่จําเป็นต้องมีการขยายตัวหรือเครื่องมือคอมเพลย์ในประเทศ.

asposefoss/3d is not yet published — build from source until it ships. See the project README for build instructions.

ตรวจสอบการติดตั้ง:

from aspose.threed import Scene
print("Aspose.3D FOSS ready")

รุ่น Python ที่ได้รับการสนับสนุน: 3.7, 3.8, 3.9, 3.10, 3.11, 3.12.


ขั้นตอนที่ 2: สร้างสถานการณ์และนิวส์

ทุกม้าต้องอยู่ภายในภาพวาดของสถานการณ์ สร้างภาพที่ Scene เพิ่มชื่อและชื่อ Node เพื่อให้เก็บของเมซะ:

from aspose.threed import Scene

scene = Scene()
node = scene.root_node.create_child_node("triangle")

ชื่อคอยที่เก็บไว้ในไฟล์ที่ส่งออกและมีประโยชน์สําหรับการลบและการรับคืนหลังจากนั้นผ่านทางไฟล์ node.get_child("triangle").


ขั้นตอนที่ 3: สร้างวัตถุ Mesh

อัตราการใช้งาน A Mesh ด้วยชื่ออธิบายที่เลือก:

from aspose.threed.entities import Mesh

mesh = Mesh("triangle")

ใบมีดเป็นต้นที่ว่างเปล่า: ไม่มีแนวตั้งไม่มีโพลีกอน คุณจะพับมันในขั้นตอนต่อไปนี้.


ขั้นตอน 4: เพิ่มจุดควบคุม (แนวตั้ง)

จุดควบคุมคือตําแหน่งแนวตั้ง แต่ละแนวโน้มจะถูกเก็บไว้เป็นจุดการควบคุม Vector4(x, y, z, w) ที่ไหน w=1 แสดงจุดในพื้นที่ 3D:

from aspose.threed.utilities import Vector4

##Vertex 0: origin
# Note: control_points returns a copy of the internal vertex list.
# Appending to the returned copy discards the vertex silently.
# Use _control_points to mutate the backing list directly.
# This is a known library limitation — a public add_control_point() API is not yet available.
mesh._control_points.append(Vector4(0.0, 0.0, 0.0, 1.0))

##Vertex 1: 1 unit along X
mesh._control_points.append(Vector4(1.0, 0.0, 0.0, 1.0))

##Vertex 2: apex
mesh._control_points.append(Vector4(0.5, 1.0, 0.0, 1.0))

print(f"Vertices added: {len(mesh.control_points)}")

สิ่งสําคัญ: ภาษาไทยmesh.control_points กลับ A คอมโพสิต จากรายการ vertex ภายใน (Getter executes) list(self._control_points)) โทรศัพท์ mesh.control_points.append(v) แพ็คกับสําเนาไม่ใช่เมซะเพื่อให้แนวตั้งถูกหลุดอย่างเงียบเสมอ ใช้ mesh._control_points.append(v) เพิ่มแนวตั้ง การเข้าถึงรัฐส่วนตัวผ่านทาง _control_points เป็นงานที่รู้จักกันดี การเชื่อมต่อสามารถเปลี่ยนแปลงได้ในรุ่นของห้องสมุดในอนาคต.


ขั้นตอน 5: สร้างใบหน้าโพลีกอน

กําหนด topology หน้าโดยใช้ตัวอักษร vertex Pass ตัวอธิบาย vertec to create_polygon().หมายเลข 3 ดัชนีผลิตสามเหลี่ยม 4 ดําเนินการเป็น quad:

##Triangle: connect vertices 0 → 1 → 2
mesh.create_polygon(0, 1, 2)

print(f"Polygon count: {mesh.polygon_count}")

สําหรับ quad mesh คุณจะผ่านสี่ตัวอักษร: mesh.create_polygon(0, 1, 2, 3).

ตัวชี้วัดจะต้องมีตําแหน่งที่ถูกต้องใน control_points (0-based, within range) สั่งวิ่งเป็น counter-hourwise สําหรับปกติด้านนอก.


ขั้นตอน 6: เพิ่ม Vertex Normals

Vertex ปกติจะถูกเก็บไว้เป็น A VertexElement ที่เชื่อมต่อกับเมซะ ใช้ mesh.create_element() ด้วย VertexElementType.NORMAL, MappingMode.CONTROL_POINT,และ ReferenceMode.DIRECT:

from aspose.threed.entities import VertexElementType, MappingMode, ReferenceMode, VertexElementNormal
from aspose.threed.utilities import Vector4, FVector4

##Create the normal element (returns VertexElementNormal, a VertexElementFVector subclass)
normals: VertexElementNormal = mesh.create_element(
    VertexElementType.NORMAL,
    MappingMode.CONTROL_POINT,
    ReferenceMode.DIRECT
)

##One normal per vertex: all pointing out of the XY plane (0, 0, 1)
normals.set_data([
    FVector4(0, 0, 1, 0),   # vertex 0
    FVector4(0, 0, 1, 0),   # vertex 1
    FVector4(0, 0, 1, 0),   # vertex 2
])

print("Normal layer attached.")

MappingMode.CONTROL_POINT หมายความ 1 ปกติต่อแนวตั้ง. ReferenceMode.DIRECT หมายความว่าข้อมูลปกติจะอ่านในลําดับเดียวกันกับจุดควบคุม (ไม่มีอัตราส่วนพิเศษ).

การใช้ vectors ปกติ FVector4(x, y, z, w) ด้วย w=0 เพื่อแสดงแนวทางมากกว่าตําแหน่ง. FVector4 เป็น vector float ความแม่นยําเดียว; vertex attribute data in VertexElementFVector อะไหล่ใช้ประเภทนี้.


ขั้นตอน 7: เชื่อมต่อกับปุ่มและบันทึก

เพิ่มแป้งไปยังคอยล์แล้วบันทึกภาพ:

node.add_entity(mesh)

scene.save("triangle.gltf")
print("Saved triangle.gltf")

สกรีนการทํางานที่สมบูรณ์ (ขั้นตอนทั้งหมดรวม):

from aspose.threed import Scene
from aspose.threed.entities import Mesh, VertexElementType, MappingMode, ReferenceMode, VertexElementNormal
from aspose.threed.utilities import Vector3, Vector4, FVector4

scene = Scene()
node = scene.root_node.create_child_node("triangle")

mesh = Mesh("triangle")

##Add 3 vertices (x, y, z, w)
# Use _control_points to mutate the backing list directly (control_points returns a copy)
mesh._control_points.append(Vector4(0.0, 0.0, 0.0, 1.0))
mesh._control_points.append(Vector4(1.0, 0.0, 0.0, 1.0))
mesh._control_points.append(Vector4(0.5, 1.0, 0.0, 1.0))

##Create a triangle polygon
mesh.create_polygon(0, 1, 2)

##Add normals (create_element returns VertexElementNormal, a VertexElementFVector subclass)
normals: VertexElementNormal = mesh.create_element(VertexElementType.NORMAL, MappingMode.CONTROL_POINT, ReferenceMode.DIRECT)
normals.set_data([
    FVector4(0, 0, 1, 0),
    FVector4(0, 0, 1, 0),
    FVector4(0, 0, 1, 0),
])

node.add_entity(mesh)
scene.save("triangle.gltf")

ปัญหาทั่วไป

หลักสูตรการตัดสินใจ
IndexError ใน create_polygonตรวจสอบว่าทุกตัวเลขอยู่ในภายใน range(len(mesh.control_points)). ตัวอักษรเป็น 0 ตาม.
การส่งออกของ Mesh กับแนวตั้งศูนย์mesh.control_points.append(...) ลบแนวตั้งอย่างเงียบสงบเพราะทรัพย์สินจะส่งคืนสําเนา ใช้ mesh._control_points.append(...) แทนนั้น.
ค่าปกติไม่ตรงกับคํานวณแนวตั้งเมื่อใช้ MappingMode.CONTROL_POINT + ReferenceMode.DIRECT, normals.data ต้องมีอย่างถูกต้อง len(control_points) การเข้าสู่ระบบ.
Mesh ปิดจากไฟล์ที่บันทึกไว้ตรวจสอบว่า node.add_entity(mesh) ได้รับการโทรก่อนหน้านี้ scene.save(). ใบมีดที่ไม่ได้เชื่อมต่อกับจุดใด ๆ ไม่ถูกส่งออก.
การปรับตัวที่ผิดพลาด (ใบหน้าดูไม่เห็นได้)การสั่งซื้อแนวตั้งแบบ Counter-hourwise ผลิตไปตามปกติด้านนอก แปลงคําสั่งของ index ใน create_polygon เพื่อ flip มัน.
polygon_count กลับ 0polygon_count อ่านรายการเดียวกันเช่นเดียวกับ polygons.ถ้า create_polygon ไม่ได้ถูกเรียก, รายการว่างเปล่า.
สัญญาณปกติดูผิดในผู้ชมให้แน่ใจว่า vectors ทั้งหมดปกติมีความยาวหน่วย คอมโพสิตกับ n / abs(n) หรือผ่านค่าเริ่มต้นที่กําหนดไว้.

คําถามที่พบบ่อย

ความแตกต่างระหว่างอะไรคือ Vector3 และ Vector4 สําหรับจุดควบคุม?

control_points ร้านค้า Vector4 วัตถุ - The w ส่วนประกอบคือการ koordinate homogeneous: ใช้ w=1 สําหรับตําแหน่ง Vertex และ w=0 สําหรับวิศวกรแนวทางเช่นปกติ. Vector3 ใช้สําหรับการแปลง (การแปล, ระดับ) แต่ไม่สําหรับเก็บข้อมูลทางภูมิศาสตร์.

ฉันสามารถสร้างม้าด้วยแควร์ได้หรือไม่แทนที่สามเหลี่ยม?

ใช่ โทรศัพท์ mesh.create_polygon(0, 1, 2, 3) มีสี่ตัวเลขเพื่อกําหนด quad บางจุดประสงค์บันทึก (STL, 3MF) ต้องสามเหลี่ยมและจะ triangulate quads โดยอัตโนมัติ glTF และ COLLADA รักษา quad.

ฉันจะเพิ่มการสอดคล้อง UV ได้อย่างไร?

ใช้ mesh.create_element_uv(TextureMapping.DIFFUSE, MappingMode.POLYGON_VERTEX) เพื่อสร้าง A VertexElementUV สําหรับช่อง diffuse แล้ว populate ของมัน data รายการที่มี Vector4 การเข้าสู่ระบบ. ใช้สอดคล้อง UV x และ y; z และ w เป็นปกติ 0 หลักฐานแรกต้องเป็น A TextureMapping ระยะเวลา (เช่น., TextureMapping.DIFFUSE)ระบุสิ่งทอที่สล็อตของชั้น UV ที่เป็นเจ้าของ.

หมายเลขรุ่น: ต้องการมาตรฐานเพื่อส่งออกอย่างถูกต้องหรือไม่?

ไม่ Normals เป็นตัวเลือก หากลืมผู้ชมส่วนใหญ่จะคํานวณ normals per-face จากการปรับอากาศของ polygon การเพิ่ม norms pervertex ที่ชัดเจนทําให้เกิดความสว่างมากขึ้น.

ฉันสามารถเพิ่มหลายช่องไปยังหนึ่งคิวส์ได้หรือไม่?

ใช่ โทรศัพท์ node.add_entity(mesh) มากมายครั้ง. ทุกโทรหาเพิ่มหน่วยใหม่เพื่อ node.entities. บางรูปแบบอาจทําให้หลาย entities ในหนึ่งในส่งออก.

ฉันจะสามเหลี่ยมผืนผ้าด้วยโพลีกรองผสมอย่างไร?

โทรศัพท์ mesh.triangulate() เพื่อแปลง quads และ N-gons ทั้งหมดไปเป็นสามเหลี่ยมที่ตั้งอยู่ นี้จะช่วยให้ก่อนที่จะบันทึกไว้ในรูปแบบที่สนับสนุนสามคนเท่านั้น.

 ภาษาไทย