scene_data.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  1. #
  2. # Copyright (c) Contributors to the Open 3D Engine Project.
  3. # For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. #
  5. # SPDX-License-Identifier: Apache-2.0 OR MIT
  6. #
  7. #
  8. import typing
  9. import json
  10. import azlmbr.scene as sceneApi
  11. from enum import IntEnum
  12. # Wraps the AZ.SceneAPI.Containers.SceneGraph.NodeIndex internal class
  13. class SceneGraphNodeIndex:
  14. def __init__(self, scene_graph_node_index) -> None:
  15. self.nodeIndex = scene_graph_node_index
  16. def as_number(self):
  17. return self.nodeIndex.AsNumber()
  18. def distance(self, other):
  19. return self.nodeIndex.Distance(other)
  20. def is_valid(self) -> bool:
  21. return self.nodeIndex.IsValid()
  22. def equal(self, other) -> bool:
  23. return self.nodeIndex.Equal(other)
  24. # Wraps AZ.SceneAPI.Containers.SceneGraph.Name internal class
  25. class SceneGraphName:
  26. def __init__(self, scene_graph_name) -> None:
  27. self.name = scene_graph_name
  28. def get_path(self) -> str:
  29. return self.name.GetPath()
  30. def get_name(self) -> str:
  31. return self.name.GetName()
  32. # Wraps AZ.SceneAPI.Containers.SceneGraph class
  33. class SceneGraph:
  34. def __init__(self, scene_graph_instance) -> None:
  35. self.sceneGraph = scene_graph_instance
  36. @classmethod
  37. def is_valid_name(cls, name):
  38. return sceneApi.SceneGraph_IsValidName(name)
  39. @classmethod
  40. def node_seperation_character(cls):
  41. return sceneApi.SceneGraph_GetNodeSeperationCharacter()
  42. def get_node_name(self, node):
  43. return self.sceneGraph.GetNodeName(node)
  44. def get_root(self):
  45. return self.sceneGraph.GetRoot()
  46. def has_node_content(self, node) -> bool:
  47. return self.sceneGraph.HasNodeContent(node)
  48. def has_node_sibling(self, node) -> bool:
  49. return self.sceneGraph.HasNodeSibling(node)
  50. def has_node_child(self, node) -> bool:
  51. return self.sceneGraph.HasNodeChild(node)
  52. def has_node_parent(self, node) -> bool:
  53. return self.sceneGraph.HasNodeParent(node)
  54. def is_node_end_point(self, node) -> bool:
  55. return self.sceneGraph.IsNodeEndPoint(node)
  56. def get_node_parent(self, node):
  57. return self.sceneGraph.GetNodeParent(node)
  58. def get_node_sibling(self, node):
  59. return self.sceneGraph.GetNodeSibling(node)
  60. def get_node_child(self, node):
  61. return self.sceneGraph.GetNodeChild(node)
  62. def get_node_count(self):
  63. return self.sceneGraph.GetNodeCount()
  64. def find_with_path(self, path):
  65. return self.sceneGraph.FindWithPath(path)
  66. def find_with_root_and_path(self, root, path):
  67. return self.sceneGraph.FindWithRootAndPath(root, path)
  68. def get_node_content(self, node):
  69. return self.sceneGraph.GetNodeContent(node)
  70. class ColorChannel(IntEnum):
  71. RED = 0
  72. """ Red color channel """
  73. GREEN = 1
  74. """ Green color channel """
  75. BLUE = 2
  76. """ Blue color channel """
  77. ALPHA = 3
  78. """ Alpha color channel """
  79. class TangentSpaceSource(IntEnum):
  80. SCENE = 0
  81. """ Extract the tangents and bitangents directly from the source scene file. """
  82. MIKKT_GENERATION = 1
  83. """ Use MikkT algorithm to generate tangents """
  84. class TangentSpaceMethod(IntEnum):
  85. TSPACE = 0
  86. """ Generates the tangents and bitangents with their true magnitudes which can be used for relief mapping effects.
  87. It calculates the 'real' bitangent which may not be perpendicular to the tangent.
  88. However, both, the tangent and bitangent are perpendicular to the vertex normal.
  89. """
  90. TSPACE_BASIC = 1
  91. """ Calculates unit vector tangents and bitangents at pixel/vertex level which are sufficient for basic normal mapping. """
  92. class PrimitiveShape(IntEnum):
  93. BEST_FIT = 0
  94. """ The algorithm will determine which of the shapes fits best. """
  95. SPHERE = 1
  96. """ Sphere shape """
  97. BOX = 2
  98. """ Box shape """
  99. CAPSULE = 3
  100. """ Capsule shape """
  101. class DecompositionMode(IntEnum):
  102. VOXEL = 0
  103. """ Voxel-based approximate convex decomposition """
  104. TETRAHEDRON = 1
  105. """ Tetrahedron-based approximate convex decomposition """
  106. # Contains a dictionary to contain and export AZ.SceneAPI.Containers.SceneManifest
  107. class SceneManifest:
  108. def __init__(self):
  109. self.manifest = {'values': []}
  110. def add_mesh_group(self, name: str) -> dict:
  111. """Adds a Mesh Group to the scene manifest.
  112. Parameters
  113. ----------
  114. name :
  115. Name of the mesh group. This will become a file on disk and be usable as a Mesh in the editor.
  116. Returns
  117. -------
  118. dict
  119. Newly created mesh group.
  120. """
  121. mesh_group = {
  122. '$type': '{07B356B7-3635-40B5-878A-FAC4EFD5AD86} MeshGroup',
  123. 'name': name,
  124. 'nodeSelectionList': {'selectedNodes': [], 'unselectedNodes': []},
  125. 'rules': {'rules': [{'$type': 'MaterialRule'}]}
  126. }
  127. self.manifest['values'].append(mesh_group)
  128. return mesh_group
  129. def add_prefab_group(self, name: str, id: str, json: dict) -> dict:
  130. """Adds a Prefab Group to the scene manifest. This will become a file on disk and be usable as a ProceduralPrefab in the editor.
  131. Parameters
  132. ----------
  133. name :
  134. Name of the prefab.
  135. id :
  136. Unique ID for this prefab group.
  137. json :
  138. The prefab template data.
  139. Returns
  140. -------
  141. dict
  142. The newly created Prefab group
  143. """
  144. prefab_group = {
  145. '$type': '{99FE3C6F-5B55-4D8B-8013-2708010EC715} PrefabGroup',
  146. 'name': name,
  147. 'id': id,
  148. 'prefabDomData': json
  149. }
  150. self.manifest['values'].append(prefab_group)
  151. return prefab_group
  152. def add_actor_group(self, group) -> dict:
  153. groupDict = group.to_dict()
  154. self.manifest['values'].append(groupDict)
  155. return groupDict
  156. def add_motion_group(self, group) -> dict:
  157. groupDict = group.to_dict()
  158. self.manifest['values'].append(groupDict)
  159. return groupDict
  160. def mesh_group_select_node(self, mesh_group: dict, node_name: str) -> None:
  161. """Adds a node as a selected node.
  162. Parameters
  163. ----------
  164. mesh_group :
  165. Mesh group to apply the selection to.
  166. node_name :
  167. Path of the node.
  168. """
  169. mesh_group['nodeSelectionList']['selectedNodes'].append(node_name)
  170. def mesh_group_unselect_node(self, mesh_group: dict, node_name: str) -> None:
  171. """Adds a node as an unselected node.
  172. Parameters
  173. ----------
  174. mesh_group :
  175. Mesh group to apply the selection to.
  176. node_name :
  177. Path of the node.
  178. """
  179. mesh_group['nodeSelectionList']['unselectedNodes'].append(node_name)
  180. def mesh_group_add_advanced_coordinate_system(self, mesh_group: dict,
  181. origin_node_name: str = '',
  182. translation: typing.Optional[object] = None,
  183. rotation: typing.Optional[object] = None,
  184. scale: float = 1.0) -> None:
  185. """Adds an Advanced Coordinate System rule which modifies the target coordinate system,
  186. applying a transformation to all data (transforms and vertex data if it exists).
  187. Parameters
  188. ----------
  189. mesh_group :
  190. Mesh group to add the Advanced Coordinate System rule to.
  191. origin_node_name :
  192. Path of the node to use as the origin.
  193. translation :
  194. Moves the group along the given vector.
  195. rotation :
  196. Sets the orientation offset of the processed mesh in degrees. Rotates the group after translation.
  197. scale :
  198. Sets the scale offset of the processed mesh.
  199. """
  200. origin_rule = {
  201. '$type': 'CoordinateSystemRule',
  202. 'useAdvancedData': True,
  203. 'originNodeName': self.__default_or_value(origin_node_name, '')
  204. }
  205. if translation is not None:
  206. origin_rule['translation'] = translation
  207. if rotation is not None:
  208. origin_rule['rotation'] = rotation
  209. if scale != 1.0:
  210. origin_rule['scale'] = scale
  211. mesh_group['rules']['rules'].append(origin_rule)
  212. def mesh_group_add_comment(self, mesh_group: dict, comment: str) -> None:
  213. """Adds a Comment rule.
  214. Parameters
  215. ----------
  216. mesh_group :
  217. Mesh group to add the comment rule to.
  218. comment :
  219. Text for the comment rule.
  220. """
  221. comment_rule = {
  222. '$type': 'CommentRule',
  223. 'comment': comment
  224. }
  225. mesh_group['rules']['rules'].append(comment_rule)
  226. def __default_or_value(self, val, default):
  227. return default if val is None else val
  228. def mesh_group_add_cloth_rule(self, mesh_group: dict,
  229. cloth_node_name: str,
  230. inverse_masses_stream_name: typing.Optional[str],
  231. inverse_masses_channel: typing.Optional[ColorChannel],
  232. motion_constraints_stream_name: typing.Optional[str],
  233. motion_constraints_channel: typing.Optional[ColorChannel],
  234. backstop_stream_name: typing.Optional[str],
  235. backstop_offset_channel: typing.Optional[ColorChannel],
  236. backstop_radius_channel: typing.Optional[ColorChannel]) -> None:
  237. """Adds a Cloth rule.
  238. Parameters
  239. ----------
  240. mesh_group :
  241. Mesh Group to add the cloth rule to
  242. cloth_node_name :
  243. Name of the node that the rule applies to
  244. inverse_masses_stream_name :
  245. Name of the color stream to use for inverse masses
  246. inverse_masses_channel :
  247. Color channel (index) for inverse masses
  248. motion_constraints_stream_name :
  249. Name of the color stream to use for motion constraints
  250. motion_constraints_channel :
  251. Color channel (index) for motion constraints
  252. backstop_stream_name :
  253. Name of the color stream to use for backstop
  254. backstop_offset_channel :
  255. Color channel (index) for backstop offset value
  256. backstop_radius_channel :
  257. Color channel (index) for backstop radius value
  258. """
  259. cloth_rule = {
  260. '$type': 'ClothRule',
  261. 'meshNodeName': cloth_node_name,
  262. 'inverseMassesStreamName': self.__default_or_value(inverse_masses_stream_name, 'Default: 1.0')
  263. }
  264. if inverse_masses_channel is not None:
  265. cloth_rule['inverseMassesChannel'] = int(inverse_masses_channel)
  266. cloth_rule['motionConstraintsStreamName'] = self.__default_or_value(motion_constraints_stream_name, 'Default: 1.0')
  267. if motion_constraints_channel is not None:
  268. cloth_rule['motionConstraintsChannel'] = int(motion_constraints_channel)
  269. cloth_rule['backstopStreamName'] = self.__default_or_value(backstop_stream_name, 'None')
  270. if backstop_offset_channel is not None:
  271. cloth_rule['backstopOffsetChannel'] = int(backstop_offset_channel)
  272. if backstop_radius_channel is not None:
  273. cloth_rule['backstopRadiusChannel'] = int(backstop_radius_channel)
  274. mesh_group['rules']['rules'].append(cloth_rule)
  275. def mesh_group_add_lod_rule(self, mesh_group: dict) -> dict:
  276. """Adds an LOD rule.
  277. Parameters
  278. ----------
  279. mesh_group :
  280. Mesh Group to add the rule to.
  281. Returns
  282. -------
  283. dict
  284. LOD rule.
  285. """
  286. lod_rule = {
  287. '$type': '{6E796AC8-1484-4909-860A-6D3F22A7346F} LodRule',
  288. 'nodeSelectionList': []
  289. }
  290. mesh_group['rules']['rules'].append(lod_rule)
  291. return lod_rule
  292. def lod_rule_add_lod(self, lod_rule: dict) -> dict:
  293. """Adds an LOD level to the LOD rule. Nodes are added in order. The first node added represents LOD1, 2nd LOD2, etc.
  294. Parameters
  295. ----------
  296. lod_rule :
  297. LOD rule to add the LOD level to.
  298. Returns
  299. -------
  300. dict
  301. LOD level.
  302. """
  303. lod = {'selectedNodes': [], 'unselectedNodes': []}
  304. lod_rule['nodeSelectionList'].append(lod)
  305. return lod
  306. def lod_select_node(self, lod: dict, selected_node: str) -> None:
  307. """Adds a node as a selected node.
  308. Parameters
  309. ----------
  310. lod :
  311. LOD level to add the node to.
  312. selected_node :
  313. Path of the node.
  314. """
  315. lod['selectedNodes'].append(selected_node)
  316. def lod_unselect_node(self, lod: dict, unselected_node: str) -> None:
  317. """Adds a node as an unselected node.
  318. Parameters
  319. ----------
  320. lod :
  321. LOD rule to add the node to.
  322. unselected_node :
  323. Path of the node.
  324. """
  325. lod['unselectedNodes'].append(unselected_node)
  326. def mesh_group_add_advanced_mesh_rule(self, mesh_group: dict,
  327. use_32bit_vertices: bool = False,
  328. merge_meshes: bool = True,
  329. use_custom_normals: bool = True,
  330. vertex_color_stream: typing.Optional[str] = None) -> None:
  331. """Adds an Advanced Mesh rule.
  332. Parameters
  333. ----------
  334. mesh_group :
  335. Mesh Group to add the rule to.
  336. use_32bit_vertices :
  337. False = 16bit vertex position precision. True = 32bit vertex position precision.
  338. merge_meshes :
  339. Merge all meshes into a single mesh.
  340. use_custom_normals :
  341. True = use normals from DCC tool. False = average normals.
  342. vertex_color_stream :
  343. Color stream name to use for Vertex Coloring.
  344. """
  345. rule = {
  346. '$type': 'StaticMeshAdvancedRule',
  347. 'use32bitVertices': use_32bit_vertices,
  348. 'mergeMeshes': merge_meshes,
  349. 'useCustomNormals': use_custom_normals
  350. }
  351. if vertex_color_stream is not None:
  352. rule['vertexColorStreamName'] = vertex_color_stream
  353. mesh_group['rules']['rules'].append(rule)
  354. def mesh_group_add_skin_rule(self, mesh_group: dict, max_weights_per_vertex: int = 4, weight_threshold: float = 0.001) -> None:
  355. """Adds a Skin rule.
  356. Parameters
  357. ----------
  358. mesh_group :
  359. Mesh Group to add the rule to.
  360. max_weights_per_vertex :
  361. Max number of joints that can influence a vertex.
  362. weight_threshold :
  363. Weight values below this value will be treated as 0.
  364. """
  365. rule = {
  366. '$type': 'SkinRule',
  367. 'maxWeightsPerVertex': max_weights_per_vertex,
  368. 'weightThreshold': weight_threshold
  369. }
  370. mesh_group['rules']['rules'].append(rule)
  371. def mesh_group_add_tangent_rule(self, mesh_group: dict,
  372. tangent_space: TangentSpaceSource = TangentSpaceSource.SCENE,
  373. tspace_method: TangentSpaceMethod = TangentSpaceMethod.TSPACE) -> None:
  374. """Adds a Tangent rule to control tangent space generation.
  375. Parameters
  376. ----------
  377. mesh_group :
  378. Mesh Group to add the rule to.
  379. tangent_space :
  380. Tangent space source. 0 = Scene, 1 = MikkT Tangent Generation.
  381. tspace_method :
  382. MikkT Generation method. 0 = TSpace, 1 = TSpaceBasic.
  383. """
  384. rule = {
  385. '$type': 'TangentsRule',
  386. 'tangentSpace': int(tangent_space),
  387. 'tSpaceMethod': int(tspace_method)
  388. }
  389. mesh_group['rules']['rules'].append(rule)
  390. def __add_physx_base_mesh_group(self, name: str, physics_material_asset_hint: typing.Optional[str] = None) -> dict:
  391. import azlmbr.math
  392. group = {
  393. '$type': '{5B03C8E6-8CEE-4DA0-A7FA-CD88689DD45B} MeshGroup',
  394. 'name': name,
  395. 'NodeSelectionList': {
  396. 'selectedNodes': [],
  397. 'unselectedNodes': []
  398. },
  399. "PhysicsMaterialSlots": {
  400. "Slots": [
  401. {
  402. "Name": "",
  403. "MaterialAsset": {
  404. "assetHint": self.__default_or_value(physics_material_asset_hint, "")
  405. }
  406. }
  407. ]
  408. },
  409. "rules": {
  410. "rules": []
  411. }
  412. }
  413. self.manifest['values'].append(group)
  414. return group
  415. def add_physx_triangle_mesh_group(self, name: str,
  416. merge_meshes: bool = True,
  417. weld_vertices: bool = False,
  418. disable_clean_mesh: bool = False,
  419. force_32bit_indices: bool = False,
  420. suppress_triangle_mesh_remap_table: bool = False,
  421. build_triangle_adjacencies: bool = False,
  422. mesh_weld_tolerance: float = 0.0,
  423. num_tris_per_leaf: int = 4,
  424. physics_material_asset_hint: typing.Optional[str] = None) -> dict:
  425. """Adds a Triangle type PhysX Mesh Group to the scene.
  426. Parameters
  427. ----------
  428. name :
  429. Name of the mesh group.
  430. merge_meshes :
  431. When true, all selected nodes will be merged into a single collision mesh.
  432. weld_vertices :
  433. When true, mesh welding is performed. Clean mesh must be enabled.
  434. disable_clean_mesh :
  435. When true, mesh cleaning is disabled. This makes cooking faster.
  436. force_32bit_indices :
  437. When true, 32-bit indices will always be created regardless of triangle count.
  438. suppress_triangle_mesh_remap_table :
  439. When true, the face remap table is not created.
  440. This saves a significant amount of memory, but the SDK will not be able to provide the remap
  441. information for internal mesh triangles returned by collisions, sweeps or raycasts hits.
  442. build_triangle_adjacencies :
  443. When true, the triangle adjacency information is created.
  444. mesh_weld_tolerance :
  445. If mesh welding is enabled, this controls the distance at
  446. which vertices are welded. If mesh welding is not enabled, this value defines the
  447. acceptance distance for mesh validation. Provided no two vertices are within this
  448. distance, the mesh is considered to be clean. If not, a warning will be emitted.
  449. num_tris_per_leaf :
  450. Mesh cooking hint for max triangles per leaf limit. Fewer triangles per leaf
  451. produces larger meshes with better runtime performance and worse cooking performance.
  452. physics_material_asset_hint :
  453. Configure which physics material to use.
  454. Returns
  455. -------
  456. dict
  457. The newly created mesh group.
  458. """
  459. group = self.__add_physx_base_mesh_group(name, physics_material_asset_hint)
  460. group["export method"] = 0
  461. group["TriangleMeshAssetParams"] = {
  462. "MergeMeshes": merge_meshes,
  463. "WeldVertices": weld_vertices,
  464. "DisableCleanMesh": disable_clean_mesh,
  465. "Force32BitIndices": force_32bit_indices,
  466. "SuppressTriangleMeshRemapTable": suppress_triangle_mesh_remap_table,
  467. "BuildTriangleAdjacencies": build_triangle_adjacencies,
  468. "MeshWeldTolerance": mesh_weld_tolerance,
  469. "NumTrisPerLeaf": num_tris_per_leaf
  470. }
  471. return group
  472. def add_physx_convex_mesh_group(self, name: str, area_test_epsilon: float = 0.059, plane_tolerance: float = 0.0006,
  473. use_16bit_indices: bool = False,
  474. check_zero_area_triangles: bool = False,
  475. quantize_input: bool = False,
  476. use_plane_shifting: bool = False,
  477. shift_vertices: bool = False,
  478. gauss_map_limit: int = 32,
  479. build_gpu_data: bool = False,
  480. physics_material_asset_hint: typing.Optional[str] = None) -> dict:
  481. """Adds a Convex type PhysX Mesh Group to the scene.
  482. Parameters
  483. ----------
  484. name :
  485. Name of the mesh group.
  486. area_test_epsilon :
  487. If the area of a triangle of the hull is below this value, the triangle will be
  488. rejected. This test is done only if Check Zero Area Triangles is used.
  489. plane_tolerance :
  490. The value is used during hull construction. When a new point is about to be added
  491. to the hull it gets dropped when the point is closer to the hull than the planeTolerance.
  492. use_16bit_indices :
  493. Denotes the use of 16-bit vertex indices in Convex triangles or polygons.
  494. check_zero_area_triangles :
  495. Checks and removes almost zero-area triangles during convex hull computation.
  496. The rejected area size is specified in Area Test Epsilon.
  497. quantize_input :
  498. Quantizes the input vertices using the k-means clustering.
  499. use_plane_shifting :
  500. Enables plane shifting vertex limit algorithm. Plane shifting is an alternative
  501. algorithm for the case when the computed hull has more vertices than the specified vertex
  502. limit.
  503. shift_vertices :
  504. Convex hull input vertices are shifted to be around origin to provide better
  505. computation stability
  506. gauss_map_limit :
  507. Vertex limit beyond which additional acceleration structures are computed for each
  508. convex mesh. Increase that limit to reduce memory usage. Computing the extra structures
  509. all the time does not guarantee optimal performance.
  510. build_gpu_data :
  511. When true, additional information required for GPU-accelerated rigid body
  512. simulation is created. This can increase memory usage and cooking times for convex meshes
  513. and triangle meshes. Convex hulls are created with respect to GPU simulation limitations.
  514. Vertex limit is set to 64 and vertex limit per face is internally set to 32.
  515. physics_material_asset_hint :
  516. Configure which physics material to use.
  517. Returns
  518. -------
  519. dict
  520. The newly created mesh group.
  521. """
  522. group = self.__add_physx_base_mesh_group(name, physics_material_asset_hint)
  523. group["export method"] = 1
  524. group["ConvexAssetParams"] = {
  525. "AreaTestEpsilon": area_test_epsilon,
  526. "PlaneTolerance": plane_tolerance,
  527. "Use16bitIndices": use_16bit_indices,
  528. "CheckZeroAreaTriangles": check_zero_area_triangles,
  529. "QuantizeInput": quantize_input,
  530. "UsePlaneShifting": use_plane_shifting,
  531. "ShiftVertices": shift_vertices,
  532. "GaussMapLimit": gauss_map_limit,
  533. "BuildGpuData": build_gpu_data
  534. }
  535. return group
  536. def add_physx_primitive_mesh_group(self, name: str,
  537. primitive_shape_target: PrimitiveShape = PrimitiveShape.BEST_FIT,
  538. volume_term_coefficient: float = 0.0,
  539. physics_material_asset_hint: typing.Optional[str] = None) -> dict:
  540. """Adds a Primitive Shape type PhysX Mesh Group to the scene
  541. Parameters
  542. ----------
  543. name :
  544. Name of the mesh group.
  545. primitive_shape_target :
  546. The shape that should be fitted to this mesh. If BEST_FIT is selected, the
  547. algorithm will determine which of the shapes fits best.
  548. volume_term_coefficient :
  549. This parameter controls how aggressively the primitive fitting algorithm will try
  550. to minimize the volume of the fitted primitive. A value of 0 (no volume minimization) is
  551. recommended for most meshes, especially those with moderate to high vertex counts.
  552. physics_material_asset_hint :
  553. Configure which physics material to use.
  554. Returns
  555. -------
  556. dict
  557. The newly created mesh group.
  558. """
  559. group = self.__add_physx_base_mesh_group(name, physics_material_asset_hint)
  560. group["export method"] = 2
  561. group["PrimitiveAssetParams"] = {
  562. "PrimitiveShapeTarget": int(primitive_shape_target),
  563. "VolumeTermCoefficient": volume_term_coefficient
  564. }
  565. return group
  566. def physx_mesh_group_decompose_meshes(self, mesh_group: dict, max_convex_hulls: int = 1024,
  567. max_num_vertices_per_convex_hull: int = 64,
  568. concavity: float = .001,
  569. resolution: float = 100000,
  570. mode: DecompositionMode = DecompositionMode.VOXEL,
  571. alpha: float = .05,
  572. beta: float = .05,
  573. min_volume_per_convex_hull: float = 0.0001,
  574. plane_downsampling: int = 4,
  575. convex_hull_downsampling: int = 4,
  576. pca: bool = False,
  577. project_hull_vertices: bool = True) -> None:
  578. """Enables and configures mesh decomposition for a PhysX Mesh Group.
  579. Only valid for convex or primitive mesh types.
  580. Parameters
  581. ----------
  582. mesh_group :
  583. Mesh group to configure decomposition for.
  584. max_convex_hulls :
  585. Controls the maximum number of hulls to generate.
  586. max_num_vertices_per_convex_hull :
  587. Controls the maximum number of triangles per convex hull.
  588. concavity :
  589. Maximum concavity of each approximate convex hull.
  590. resolution :
  591. Maximum number of voxels generated during the voxelization stage.
  592. mode :
  593. Select voxel-based approximate convex decomposition or tetrahedron-based
  594. approximate convex decomposition.
  595. alpha :
  596. Controls the bias toward clipping along symmetry planes.
  597. beta :
  598. Controls the bias toward clipping along revolution axes.
  599. min_volume_per_convex_hull :
  600. Controls the adaptive sampling of the generated convex hulls.
  601. plane_downsampling :
  602. Controls the granularity of the search for the best clipping plane.
  603. convex_hull_downsampling :
  604. Controls the precision of the convex hull generation process
  605. during the clipping plane selection stage.
  606. pca :
  607. Enable or disable normalizing the mesh before applying the convex decomposition.
  608. project_hull_vertices :
  609. Project the output convex hull vertices onto the original source mesh to increase
  610. the floating point accuracy of the results.
  611. """
  612. mesh_group['DecomposeMeshes'] = True
  613. mesh_group['ConvexDecompositionParams'] = {
  614. "MaxConvexHulls": max_convex_hulls,
  615. "MaxNumVerticesPerConvexHull": max_num_vertices_per_convex_hull,
  616. "Concavity": concavity,
  617. "Resolution": resolution,
  618. "Mode": int(mode),
  619. "Alpha": alpha,
  620. "Beta": beta,
  621. "MinVolumePerConvexHull": min_volume_per_convex_hull,
  622. "PlaneDownsampling": plane_downsampling,
  623. "ConvexHullDownsampling": convex_hull_downsampling,
  624. "PCA": pca,
  625. "ProjectHullVertices": project_hull_vertices
  626. }
  627. def physx_mesh_group_add_selected_node(self, mesh_group: dict, node: str) -> None:
  628. """Adds a node to the selected nodes list
  629. Parameters
  630. ----------
  631. mesh_group :
  632. Mesh group to add to.
  633. node :
  634. Node path to add.
  635. """
  636. mesh_group['NodeSelectionList']['selectedNodes'].append(node)
  637. def physx_mesh_group_add_unselected_node(self, mesh_group: dict, node: str) -> None:
  638. """Adds a node to the unselected nodes list
  639. Parameters
  640. ----------
  641. mesh_group :
  642. Mesh group to add to.
  643. node :
  644. Node path to add.
  645. """
  646. mesh_group['NodeSelectionList']['unselectedNodes'].append(node)
  647. def physx_mesh_group_add_selected_unselected_nodes(self, mesh_group: dict, selected: typing.List[str],
  648. unselected: typing.List[str]) -> None:
  649. """Adds a set of nodes to the selected/unselected node lists
  650. Parameters
  651. ----------
  652. mesh_group :
  653. Mesh group to add to.
  654. selected :
  655. List of node paths to add to the selected list.
  656. unselected :
  657. List of node paths to add to the unselected list.
  658. """
  659. mesh_group['NodeSelectionList']['selectedNodes'].extend(selected)
  660. mesh_group['NodeSelectionList']['unselectedNodes'].extend(unselected)
  661. def physx_mesh_group_add_comment(self, mesh_group: dict, comment: str) -> None:
  662. """Adds a comment rule
  663. Parameters
  664. ----------
  665. mesh_group :
  666. Mesh group to add the rule to.
  667. comment :
  668. Comment string.
  669. """
  670. rule = {
  671. "$type": "CommentRule",
  672. "comment": comment
  673. }
  674. mesh_group['rules']['rules'].append(rule)
  675. def export(self):
  676. return json.dumps(self.manifest)