WhiteBox.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. # setup path
  7. import azlmbr.legacy.general as general
  8. import azlmbr.bus as bus
  9. import azlmbr.editor as editor
  10. import azlmbr.entity
  11. import azlmbr.object
  12. import azlmbr.math
  13. import azlmbr.whitebox.api as api
  14. from azlmbr.entity import EntityId
  15. # get Component Type for WhiteBoxMesh
  16. whiteBoxMeshComponentTypeId = get_white_box_component_type()
  17. # use old White Box entity to hold White Box component if it exists, otherwise use a new one
  18. newEntityId = None
  19. oldEntityId = general.find_editor_entity('WhiteBox')
  20. if oldEntityId.IsValid():
  21. whiteBoxMeshComponentExists = editor.EditorComponentAPIBus(bus.Broadcast, 'HasComponentOfType', oldEntityId, whiteBoxMeshComponentTypeId)
  22. if (whiteBoxMeshComponentExists):
  23. oldwhiteBoxMeshComponent = editor.EditorComponentAPIBus(bus.Broadcast, 'GetComponentOfType', oldEntityId, whiteBoxMeshComponentTypeId)
  24. editor.EditorComponentAPIBus(bus.Broadcast, 'RemoveComponents', [oldwhiteBoxMeshComponent.GetValue()])
  25. newEntityId = oldEntityId
  26. else:
  27. newEntityId = editor.ToolsApplicationRequestBus(bus.Broadcast, 'CreateNewEntity', EntityId())
  28. editor.EditorEntityAPIBus(bus.Event, 'SetName', newEntityId, "WhiteBox")
  29. # add whiteBoxMeshComponent to entity and enable
  30. whiteBoxMeshComponentOutcome = editor.EditorComponentAPIBus(bus.Broadcast, 'AddComponentsOfType', newEntityId, [whiteBoxMeshComponentTypeId])
  31. if (whiteBoxMeshComponentOutcome.IsSuccess()):
  32. print("White Box Component added to entity.")
  33. whiteBoxMeshComponents = whiteBoxMeshComponentOutcome.GetValue()
  34. whiteBoxMeshComponent = whiteBoxMeshComponents[0]
  35. editor.EditorComponentAPIBus(bus.Broadcast, 'EnableComponents', whiteBoxMeshComponents)
  36. isComponentEnabled = editor.EditorComponentAPIBus(bus.Broadcast, 'IsComponentEnabled', whiteBoxMeshComponent)
  37. if (isComponentEnabled):
  38. print("Enabled Mesh component.")
  39. whiteBoxMesh = azlmbr.whitebox.request.bus.EditorWhiteBoxComponentRequestBus(bus.Event, 'GetWhiteBoxMeshHandle', whiteBoxMeshComponent)
  40. # translate append (extrude) a polygon
  41. if (len(sys.argv) >= 2 and float(sys.argv[2]) != 0.0):
  42. # create face handle from user input (argv[1])
  43. faceHandle = azlmbr.object.construct('FaceHandle', int(sys.argv[1]))
  44. # find the polygon handle that corresponds to the given face
  45. facePolygonHandle = whiteBoxMesh.FacePolygonHandle(faceHandle)
  46. # translate append (extrude) the polygon by a distance specified by the user (argv[2])
  47. whiteBoxMesh.TranslatePolygonAppend(facePolygonHandle, float(sys.argv[2]))
  48. # recalculate uvs as mesh will have changed
  49. whiteBoxMesh.CalculatePlanarUVs()
  50. # notify the white box component the mesh has changed to force it to rebuild the render mesh
  51. azlmbr.whitebox.notification.bus.EditorWhiteBoxComponentNotificationBus(bus.Event, 'OnWhiteBoxMeshModified', whiteBoxMeshComponent)