Эх сурвалжийг харах

update tree when changing overlay

Joseph Brandenburg 1 долоо хоног өмнө
parent
commit
88f0902c72
1 өөрчлөгдсөн 61 нэмэгдсэн , 41 устгасан
  1. 61 41
      socket_definitions.py

+ 61 - 41
socket_definitions.py

@@ -46,22 +46,45 @@ class MantisSocket(parent_classes):
     def draw_color_simple(self):
         return self.color_simple
 
+def socket_update_get_node_tree(self, context):
+    context = bpy.context
+    if not context.space_data:
+        return None
+    if not hasattr(context.space_data, "path"):
+        return None
+    try:
+        node_tree = context.space_data.path[0].node_tree
+    except IndexError: # not in the UI, for example, in a script instead.
+        return None
+    if node_tree is None:
+        return None
+    if node_tree.is_executing or node_tree.is_exporting or not node_tree.do_live_update:
+        return None
+    return node_tree
+
 def overlay_toggle_update(self, context):
-    if self.node.id_data.bl_idname in {'MantisTree', 'SchemaTree'}:
-        parsed_tree = self.node.id_data.parsed_tree
+    node_tree = socket_update_get_node_tree(self, context)
+    if node_tree is None: return
+    parsed_tree = self.node.id_data.parsed_tree
+    if hasattr(self.node, "display_update"):
         self.node.display_update(parsed_tree=parsed_tree, context=context)
-        # and we need to mark the node as un-prepared.
-        # For now I am not gonna try to resolve the overlay value from the UI.
-        from .base_definitions import get_signature_from_edited_tree
-        mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
-        mantis_node.reset_execution_recursive()
+    node_tree.update_tree(context, force = True)
+    # and we need to mark the node as un-prepared.
+    # For now I am not gonna try to resolve the overlay value from the UI.
+    # have to check each one. see default_update NOTE
+    # for mantis_node in node_tree.parsed_tree.values():
+    #     try_update_mantis_node_for_socket(mantis_node, node_tree, self)
     
 def overlay_text_update(self, context):
-    if self.node.id_data.bl_idname in {'MantisTree', 'SchemaTree'}:
-        parsed_tree = self.node.id_data.parsed_tree
-        from .base_definitions import get_signature_from_edited_tree
-        mantis_node = parsed_tree.get(get_signature_from_edited_tree(self, context))
-        mantis_node.ui_modify_socket(self, self.name)
+    node_tree = socket_update_get_node_tree(self, context)
+    if node_tree is None: return
+    parsed_tree = self.node.id_data.parsed_tree
+    # have to check each one. see default_update NOTE
+    handled = True
+    for mantis_node in node_tree.parsed_tree.values():
+        handled = try_update_mantis_node_for_socket(mantis_node, node_tree, self) and handled
+    if not handled:
+        node_tree.update_tree(context, force = True)
 
 class MantisSocketOverlayEnabled(MantisSocket):
     # this class just needs a couple properties
@@ -508,19 +531,10 @@ def socket_update(mantis_node, ui_socket, socket_name=None):
     return node_updated
 
 def default_update(ui_socket, context, do_execute=True):
-    context = bpy.context
-    if not context.space_data:
-        return
-    if not hasattr(context.space_data, "path"):
-        return
-    try:
-        node_tree = context.space_data.path[0].node_tree
-    except IndexError: # not in the UI, for example, in a script instead.
-        return
-    if node_tree is None:
-        return
-    if node_tree.is_executing or node_tree.is_exporting or not node_tree.do_live_update:
-        return
+    if not context:
+        context = bpy.context
+    node_tree = socket_update_get_node_tree(ui_socket, context)
+    if node_tree is None: return
     # if it is a Schema Node, it will fail the checks below -- but we need it to update the tree.
     from .base_definitions import SchemaUINode
     if isinstance(ui_socket.node, SchemaUINode):
@@ -557,23 +571,11 @@ def default_update(ui_socket, context, do_execute=True):
         mantis_updated=False
         node_tree.update_tree(context, force=True)
     elif hasattr(ui_socket, 'default_value'):
-        # we may not have to regenerate the tree; try and update the socket
-        from .utilities.utilities import tree_from_mantis_node
+        # NOTE: there may be many mantis nodes for each UI node... so we need to try all of them.
         for mantis_node in node_tree.parsed_tree.values():
-            # check to see if the mantis node is in the same ui-tree as this ui_socket
-            if mantis_node.ui_signature is None: continue # autogenerated nodes
-            if mantis_node.ui_signature[-1] == ui_socket.node.name and \
-                        tree_from_mantis_node(mantis_node.ui_signature, node_tree) == ui_socket.node.id_data:
-                node_updated = True
-                from .misc_nodes import SimpleInputNode
-                if isinstance(mantis_node, SimpleInputNode):
-                    node_updated = socket_update(mantis_node, ui_socket)
-                    for l in mantis_node.outputs[ui_socket.name].links:
-                        node_updated = node_updated and socket_update(l.to_node, ui_socket, l.to_socket)
-                else:
-                    node_updated = socket_update(mantis_node, ui_socket)
-                # execute the tree if even one node didn't update
-                mantis_updated = node_updated and mantis_updated
+            # we may not have to regenerate the tree; try and update the socket
+            # the 'and' here ensures we trigger an update if even one of the mantis nodes fails
+            mantis_updated = try_update_mantis_node_for_socket(mantis_node, node_tree, ui_socket) and mantis_updated
         # we want to force it if we have made an unhandled change inside of a schema.
         node_tree.update_tree(context, force = (mantis_updated == False))
     node_tree.display_update(context)
@@ -584,6 +586,24 @@ def default_update(ui_socket, context, do_execute=True):
         except Exception as e:
             prRed("Automatic Tree Execution failed because of %s" % e)
 
+def try_update_mantis_node_for_socket(mantis_node, node_tree, ui_socket):
+    # we may not have to regenerate the tree; try and update the socket
+    from .utilities.utilities import tree_from_mantis_node
+    # check to see if the mantis node is in the same ui-tree as this ui_socket
+    if mantis_node.ui_signature is None: return False# autogenerated nodes
+    if mantis_node.ui_signature[-1] == ui_socket.node.name and \
+                tree_from_mantis_node(mantis_node.ui_signature, node_tree) == ui_socket.node.id_data:
+        node_updated = True
+        from .misc_nodes import SimpleInputNode
+        if isinstance(mantis_node, SimpleInputNode):
+            node_updated = socket_update(mantis_node, ui_socket)
+            for l in mantis_node.outputs[ui_socket.name].links:
+                node_updated = node_updated and socket_update(l.to_node, ui_socket, l.to_socket)
+        else:
+            node_updated = socket_update(mantis_node, ui_socket)
+        # execute the tree if even one node didn't update
+        return node_updated
+    return False
 
 def update_socket(self, context,):
     default_update(self,context)