material_canvas_utils.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 azlmbr.atomtools as atomtools
  7. import azlmbr.editor.graph as graph
  8. import azlmbr.math as math
  9. import azlmbr.object
  10. import azlmbr.bus as bus
  11. from Atom.atom_utils.atom_constants import (
  12. DynamicNodeManagerRequestBusEvents, GraphControllerRequestBusEvents, GraphDocumentRequestBusEvents)
  13. class Node(object):
  14. """
  15. Represents a node inside of a graph and contains node related functions.
  16. """
  17. def __init__(self, node_python_proxy: azlmbr.object.PythonProxyObject):
  18. """
  19. :param node_python_proxy: azlmbr.object.PythonProxyObject representing a node object.
  20. """
  21. self.node_python_proxy = node_python_proxy
  22. def get_slots(self) -> dict:
  23. """
  24. Returns a dictionary mapping of all slots on this Node.
  25. :return: a dict containing slot name keys with graph.GraphModelSlotId slot values.
  26. """
  27. mapped_node_slots = {}
  28. raw_node_slots_dict = self.node_python_proxy.invoke('GetSlots')
  29. for k, v in raw_node_slots_dict.items():
  30. mapped_node_slots[k.name] = graph.GraphModelSlotId(k.name)
  31. return mapped_node_slots
  32. class Graph(object):
  33. """
  34. Binds this Graph object to the opened graph matching the document_id passed to construct this object.
  35. """
  36. def __init__(self, document_id: math.Uuid):
  37. self.document_id = document_id
  38. def get_graph_id(self) -> math.Uuid:
  39. """
  40. Returns the graph ID value for this Graph object.
  41. :return: math.Uuid which represents the graph ID value.
  42. """
  43. return atomtools.GraphDocumentRequestBus(
  44. bus.Event, GraphDocumentRequestBusEvents.GET_GRAPH_ID, self.document_id)
  45. def get_graph_name(self) -> str:
  46. """
  47. Returns the graph name for this Graph object.
  48. :return: string representing the name of this Graph object.
  49. """
  50. return atomtools.GraphDocumentRequestBus(
  51. bus.Event, GraphDocumentRequestBusEvents.GET_GRAPH_NAME, self.document_id)
  52. def get_graph(self) -> azlmbr.object.PythonProxyObject:
  53. """
  54. Returns the AZStd::shared_ptr<Graph> object for this Graph object.
  55. :return: azlmbr.object.PythonProxyObject containing a AZStd::shared_ptr<Graph> object.
  56. """
  57. return atomtools.GraphDocumentRequestBus(bus.Event, GraphDocumentRequestBusEvents.GET_GRAPH, self.document_id)
  58. def get_nodes(self) -> list[azlmbr.object.PythonProxyObject]:
  59. """
  60. Gets the current Nodes in this Graph and returns them as a list of azlmbr.object.PythonProxyObject objects.
  61. :return: list of azlmbr.object.PythonProxyObject objects each representing a Node in this Graph.
  62. """
  63. return graph.GraphControllerRequestBus(
  64. bus.Event, GraphControllerRequestBusEvents.GET_NODES, self.get_graph_id())
  65. def create_node_by_name(self, node_name: str) -> azlmbr.object.PythonProxyObject:
  66. """
  67. Creates a new node in memory matching the node_name string on the specified graph object.
  68. i.e. "World Position" for node_name would create a World Position node.
  69. We create them in memory for use with the GraphControllerRequestBusEvents.ADD_NODE bus call on this Graph.
  70. :param node_name: String representing the type of node to add to the graph.
  71. :return: azlmbr.object.PythonProxyObject representing a C++ AZStd::shared_ptr<Node> object.
  72. """
  73. return atomtools.DynamicNodeManagerRequestBus(
  74. bus.Broadcast, DynamicNodeManagerRequestBusEvents.CREATE_NODE_BY_NAME, self.get_graph(), node_name)
  75. def add_node(self, node: azlmbr.object.PythonProxyObject, position: math.Vector2) -> Node:
  76. """
  77. Adds a node generated from the NodeManager to this Graph, then returns a Node object for that node.
  78. :param node: A azlmbr.object.PythonProxyObject containing the node we wish to add to the graph.
  79. :param position: math.Vector2(x,y) value that determines where to place the node on the Graph's grid.
  80. :return: A Node object containing node object and node id via azlmbr.object.PythonProxyObject objects.
  81. """
  82. graph.GraphControllerRequestBus(
  83. bus.Event, GraphControllerRequestBusEvents.ADD_NODE, self.get_graph_id(), node, position)
  84. return Node(node)
  85. def add_connection_by_slot_id(self,
  86. source_node: Node,
  87. source_slot: graph.GraphModelSlotId,
  88. target_node: Node,
  89. target_slot: graph.GraphModelSlotId) -> azlmbr.object.PythonProxyObject:
  90. """
  91. Connects a starting source_slot to a destination target_slot.
  92. :param source_node: A Node() object for the source node we are connecting from.
  93. :param source_slot: A graph.GraphModelSlotId representing a slot on the source_node.
  94. :param target_node: A Node() object for the target node we are connecting to.
  95. :param target_slot: A graph.GraphModelSlotId representing a slot on the target_node.
  96. :return: azlmbr.object.PythonProxyObject representing a C++ AZStd::shared_ptr<Connection> object.
  97. """
  98. return graph.GraphControllerRequestBus(
  99. bus.Event, GraphControllerRequestBusEvents.ADD_CONNECTION_BY_SLOT_ID, self.get_graph_id(),
  100. source_node.node_python_proxy, source_slot, target_node.node_python_proxy, target_slot)
  101. def are_slots_connected(self,
  102. source_node: Node,
  103. source_slot: graph.GraphModelSlotId,
  104. target_node: Node,
  105. target_slot: graph.GraphModelSlotId) -> bool:
  106. """
  107. Determines if the source_slot is connected to the target_slot on the target_node.
  108. :param source_node: A Node() object for the source node that should be connected to the target node.
  109. :param source_slot: A graph.GraphModelSlotId representing a slot on the source_node.
  110. :param target_node: A Node() object for the target node that should be connected to the source node.
  111. :param target_slot: A graph.GraphModelSlotId representing a slot on the target_node..
  112. :return: True if the source_slot and target_slot are connected, False otherwise.
  113. """
  114. return graph.GraphControllerRequestBus(
  115. bus.Event, GraphControllerRequestBusEvents.ARE_SLOTS_CONNECTED, self.get_graph_id(),
  116. source_node.node_python_proxy, source_slot, target_node.node_python_proxy, target_slot)