export_chunks_builder.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. SPDX-License-Identifier: Apache-2.0 OR MIT
  5. """
  6. import uuid, os
  7. import azlmbr.scene as sceneApi
  8. import azlmbr.scene.graph
  9. from scene_api import scene_data as sceneData
  10. def get_mesh_node_names(sceneGraph):
  11. meshDataList = []
  12. node = sceneGraph.get_root()
  13. children = []
  14. while node.IsValid():
  15. # store children to process after siblings
  16. if sceneGraph.has_node_child(node):
  17. children.append(sceneGraph.get_node_child(node))
  18. # store any node that has mesh data content
  19. nodeContent = sceneGraph.get_node_content(node)
  20. if nodeContent is not None and nodeContent.CastWithTypeName('MeshData'):
  21. if sceneGraph.is_node_end_point(node) is False:
  22. nodeName = sceneData.SceneGraphName(sceneGraph.get_node_name(node))
  23. nodePath = nodeName.get_path()
  24. if (len(nodeName.get_path())):
  25. meshDataList.append(sceneData.SceneGraphName(sceneGraph.get_node_name(node)))
  26. # advance to next node
  27. if sceneGraph.has_node_sibling(node):
  28. node = sceneGraph.get_node_sibling(node)
  29. elif children:
  30. node = children.pop()
  31. else:
  32. node = azlmbr.scene.graph.NodeIndex()
  33. return meshDataList
  34. def update_manifest(scene):
  35. graph = sceneData.SceneGraph(scene.graph)
  36. meshNameList = get_mesh_node_names(graph)
  37. sceneManifest = sceneData.SceneManifest()
  38. sourceFilenameOnly = os.path.basename(scene.sourceFilename)
  39. sourceFilenameOnly = sourceFilenameOnly.replace('.','_')
  40. for activeMeshIndex in range(len(meshNameList)):
  41. chunkName = meshNameList[activeMeshIndex]
  42. chunkPath = chunkName.get_path()
  43. meshGroupName = '{}_{}'.format(sourceFilenameOnly, chunkName.get_name())
  44. meshGroup = sceneManifest.add_mesh_group(meshGroupName)
  45. meshGroup['id'] = '{' + str(uuid.uuid5(uuid.NAMESPACE_DNS, sourceFilenameOnly + chunkPath)) + '}'
  46. sceneManifest.mesh_group_add_comment(meshGroup, 'auto generated by scene manifest')
  47. sceneManifest.mesh_group_add_advanced_coordinate_system(meshGroup, None, None, None, 1.0)
  48. sceneManifest.mesh_group_select_node(meshGroup, chunkPath)
  49. return sceneManifest.export()
  50. mySceneJobHandler = None
  51. def on_update_manifest(args):
  52. scene = args[0]
  53. result = update_manifest(scene)
  54. global mySceneJobHandler
  55. # do not delete or set sceneJobHandler to None, just disconnect from it.
  56. # this call is occuring while the scene Job Handler itself is in the callstack, so deleting it here
  57. # would cause a crash.
  58. mySceneJobHandler.disconnect()
  59. return result
  60. def main():
  61. global mySceneJobHandler
  62. mySceneJobHandler = sceneApi.ScriptBuildingNotificationBusHandler()
  63. mySceneJobHandler.connect()
  64. mySceneJobHandler.add_callback('OnUpdateManifest', on_update_manifest)
  65. if __name__ == "__main__":
  66. main()