Nodetree update optimization from Blender 3.1+ breaks my Addon #101378

Closed
opened 2022-09-26 12:30:22 +02:00 by Thomas · 13 comments

System Information
Operating system: -
Graphics card: -

Blender Version
Broken: 3.1+
Worked: 3.0.1

I'm the creator of Material Wizard Addon (https://wp.h0bb1t.de/index.php/about-material-wizard/) and have to admit, that I haven't touched it quite a long time. I now have planned to update it and stumbled across the fact, that it doesn't work as expected starting with Blender 3.1+. After some debugging, I figured out where the problem comes from and assume it is related to the Nodetree update optimization started in 3.1.

Material Wizard has its own Node Tree and reflects any changes to it in different Shader Node Trees. It does it by creating hidden NodeGroups and instanciate them in the respective material and links sockets according the settings in my own node space. Long story short, the problem arises when adding a node group to the shader node tree and try to loop over the inputs of this node group. It seems in Blender 3.1+, the inputs do not reflect the inputs of the node tree embedded to the group:

node-tree-bug.png

In the highlighted area in the lower left corner, len(node.inputs) is 0, while len(node.node_tree.inputs) is 8. In Blender pre 3.1, the first one was also 8, which is what I need. After my script has run, I get the correct node groups in my target material trees, but they don't have inputs or outputs. Subsequent calls to [Node.update()] in Blenders Python console fixes the problem.

node-tree-bug2.png

I assume I need to force an update on the Shader Node Tree from my script, but havn't found any information how to do this. Calling update() on the node in my script (rectangular highlighted area in the code above) doesn't seem to help.

Please help me.
Thanks,
Thomas

**System Information** Operating system: - Graphics card: - **Blender Version** Broken: 3.1+ Worked: 3.0.1 I'm the creator of Material Wizard Addon (https://wp.h0bb1t.de/index.php/about-material-wizard/) and have to admit, that I haven't touched it quite a long time. I now have planned to update it and stumbled across the fact, that it doesn't work as expected starting with Blender 3.1+. After some debugging, I figured out where the problem comes from and assume it is related to the Nodetree update optimization started in 3.1. Material Wizard has its own Node Tree and reflects any changes to it in different Shader Node Trees. It does it by creating hidden NodeGroups and instanciate them in the respective material and links sockets according the settings in my own node space. Long story short, the problem arises when adding a node group to the shader node tree and try to loop over the inputs of this node group. It seems in Blender 3.1+, the inputs do not reflect the inputs of the node tree embedded to the group: ![node-tree-bug.png](https://archive.blender.org/developer/F13574654/node-tree-bug.png) In the highlighted area in the lower left corner, len(node.inputs) is 0, while len(node.node_tree.inputs) is 8. In Blender pre 3.1, the first one was also 8, which is what I need. After my script has run, I get the correct node groups in my target material trees, but they don't have inputs or outputs. Subsequent calls to [Node.update()] in Blenders Python console fixes the problem. ![node-tree-bug2.png](https://archive.blender.org/developer/F13574685/node-tree-bug2.png) I assume I need to force an update on the Shader Node Tree from my script, but havn't found any information how to do this. Calling update() on the node in my script (rectangular highlighted area in the code above) doesn't seem to help. Please help me. Thanks, Thomas
Author

Added subscriber: @h0bB1T

Added subscriber: @h0bB1T
Member

Added subscriber: @JacquesLucke

Added subscriber: @JacquesLucke
Member

Changed status from 'Needs Triage' to: 'Needs User Info'

Changed status from 'Needs Triage' to: 'Needs User Info'
Member

Please try to reproduce the issue outside of your addon to make it easier to investigate for us.

Please try to reproduce the issue outside of your addon to make it easier to investigate for us.
Author

Ok, I've quickly hacked a script based on the custom nodes template that shows the problem. Tested with Blender 3.0 & 3.2, 3.2 is affected (but I assume 3.1+).

In the default scene:

  • Open script & run it.
  • Open custom node tree.
  • Add both available nodes.
  • Link any output to any input socket.
    • This invokes update() on the custom node, which calls rebuild_tree() in the Tree class.
    • This creates a simple node group (TestNode)
    • .. and a new material and adds this node to the material tree.
  • Look at the node tree of TestMaterial. The node group has no sockets.

Adding the group to TestMasterial Shift-A/Group/TestNode fixes the problem.
Running create_node_group() + add_node_group() from script directly works as expected.

I hope this helps.

import bpy

from bpy.types import NodeTree, Node, NodeSocket

just_once = False


def create_node_group():
    ng = bpy.data.node_groups.new('TestNode', 'ShaderNodeTree')
    ng.inputs.new('NodeSocketFloat', 'TestSocket')
    
    
def add_node_group():
    m = bpy.data.materials.new('TestMaterial')
    m.use_nodes = True
    m.node_tree.nodes.clear()
    ng = m.node_tree.nodes.new(type='ShaderNodeGroup')
    ng.node_tree = bpy.data.node_groups['TestNode']
    print(len(ng.inputs))


class MyCustomTree(NodeTree):
    bl_idname = 'CustomTreeType'
    bl_label = "Custom Node Tree"
    bl_icon = 'NODETREE'
    
    
    def rebuild_tree(self):
        global just_once
        if not just_once:
            just_once = True
            create_node_group()
            add_node_group()    


class MyCustomTreeNode:
    @classmethod
    def poll(cls, ntree):
        return ntree.bl_idname == 'CustomTreeType'
    

class MyCustomNode(Node, MyCustomTreeNode):
    bl_idname = 'CustomNodeType'
    bl_label = "Custom Node"
    bl_icon = 'SOUND'

    def init(self, context):
        self.inputs.new('NodeSocketFloat', "Hello 123")
        self.inputs.new('NodeSocketFloat', "World")

    def copy(self, node): print("Copying from node ", node)
    def free(self): print("Removing node ", self, ", Goodbye!")

    def draw_buttons(self, context, layout): pass
    def draw_buttons_ext(self, context, layout): pass

    def update(self):
        self.id_data.rebuild_tree()
        

class MyCustomNode2(Node, MyCustomTreeNode):
    bl_idname = 'CustomNodeType2'
    bl_label = "Custom Node2"
    bl_icon = 'SOUND'

    def init(self, context):
        self.outputs.new('NodeSocketFloat', "Hello")
        self.outputs.new('NodeSocketFloat', "World")

    def copy(self, node): print("Copying from node ", node)
    def free(self): print("Removing node ", self, ", Goodbye!")

    def draw_buttons(self, context, layout): pass
    def draw_buttons_ext(self, context, layout): pass


import nodeitems_utils
from nodeitems_utils import NodeCategory, NodeItem

class MyNodeCategory(NodeCategory):
    @classmethod
    def poll(cls, context):
        return context.space_data.tree_type == 'CustomTreeType'


node_categories = [
    MyNodeCategory('SOMENODES', "Some Nodes", items=[
        NodeItem("CustomNodeType"),
        NodeItem("CustomNodeType2"),
    ])
]

classes = (
    MyCustomTree,
    MyCustomNode,
    MyCustomNode2,
)


def register():
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)

    nodeitems_utils.register_node_categories('CUSTOM_NODES', node_categories)


def unregister():
    nodeitems_utils.unregister_node_categories('CUSTOM_NODES')

    from bpy.utils import unregister_class
    for cls in reversed(classes):
        unregister_class(cls)


if __name__ == "__main__":
    register()

Ok, I've quickly hacked a script based on the custom nodes template that shows the problem. Tested with Blender 3.0 & 3.2, 3.2 is affected (but I assume 3.1+). In the default scene: - Open script & run it. - Open custom node tree. - Add both available nodes. - Link any output to any input socket. - This invokes update() on the custom node, which calls rebuild_tree() in the Tree class. - This creates a simple node group (TestNode) - .. and a new material and adds this node to the material tree. - Look at the node tree of TestMaterial. The node group has no sockets. Adding the group to TestMasterial Shift-A/Group/TestNode fixes the problem. Running create_node_group() + add_node_group() from script directly works as expected. I hope this helps. ``` import bpy from bpy.types import NodeTree, Node, NodeSocket just_once = False def create_node_group(): ng = bpy.data.node_groups.new('TestNode', 'ShaderNodeTree') ng.inputs.new('NodeSocketFloat', 'TestSocket') def add_node_group(): m = bpy.data.materials.new('TestMaterial') m.use_nodes = True m.node_tree.nodes.clear() ng = m.node_tree.nodes.new(type='ShaderNodeGroup') ng.node_tree = bpy.data.node_groups['TestNode'] print(len(ng.inputs)) class MyCustomTree(NodeTree): bl_idname = 'CustomTreeType' bl_label = "Custom Node Tree" bl_icon = 'NODETREE' def rebuild_tree(self): global just_once if not just_once: just_once = True create_node_group() add_node_group() class MyCustomTreeNode: @classmethod def poll(cls, ntree): return ntree.bl_idname == 'CustomTreeType' class MyCustomNode(Node, MyCustomTreeNode): bl_idname = 'CustomNodeType' bl_label = "Custom Node" bl_icon = 'SOUND' def init(self, context): self.inputs.new('NodeSocketFloat', "Hello 123") self.inputs.new('NodeSocketFloat', "World") def copy(self, node): print("Copying from node ", node) def free(self): print("Removing node ", self, ", Goodbye!") def draw_buttons(self, context, layout): pass def draw_buttons_ext(self, context, layout): pass def update(self): self.id_data.rebuild_tree() class MyCustomNode2(Node, MyCustomTreeNode): bl_idname = 'CustomNodeType2' bl_label = "Custom Node2" bl_icon = 'SOUND' def init(self, context): self.outputs.new('NodeSocketFloat', "Hello") self.outputs.new('NodeSocketFloat', "World") def copy(self, node): print("Copying from node ", node) def free(self): print("Removing node ", self, ", Goodbye!") def draw_buttons(self, context, layout): pass def draw_buttons_ext(self, context, layout): pass import nodeitems_utils from nodeitems_utils import NodeCategory, NodeItem class MyNodeCategory(NodeCategory): @classmethod def poll(cls, context): return context.space_data.tree_type == 'CustomTreeType' node_categories = [ MyNodeCategory('SOMENODES', "Some Nodes", items=[ NodeItem("CustomNodeType"), NodeItem("CustomNodeType2"), ]) ] classes = ( MyCustomTree, MyCustomNode, MyCustomNode2, ) def register(): from bpy.utils import register_class for cls in classes: register_class(cls) nodeitems_utils.register_node_categories('CUSTOM_NODES', node_categories) def unregister(): nodeitems_utils.unregister_node_categories('CUSTOM_NODES') from bpy.utils import unregister_class for cls in reversed(classes): unregister_class(cls) if __name__ == "__main__": register() ```
Author

Hello Jacques,

in D16070 you mention there would be better ways to do this. I'm always open to better ways. As it is just a call to rebuild_tree, it's relatively fast to test different ways. What is the preferred way to let the Shader Node Tree stay in sync with my custom tree?
And, in case of your mentioned timer based solution, would it be ok (incrementally) sync tree layout on an e.g. 100ms timed cycle?

Hello Jacques, in [D16070](https://archive.blender.org/developer/D16070) you mention there would be better ways to do this. I'm always open to better ways. As it is just a call to rebuild_tree, it's relatively fast to test different ways. What is the preferred way to let the Shader Node Tree stay in sync with my custom tree? And, in case of your mentioned timer based solution, would it be ok (incrementally) sync tree layout on an e.g. 100ms timed cycle?
Member

It's not perfect but you could set some flag to true in the update function and then just check that flag in a timer so that it only syncs the tree when necessary. I'm not sure if there is a perfect solution for this scenario currently.

It's not perfect but you could set some flag to true in the update function and then just check that flag in a timer so that it only syncs the tree when necessary. I'm not sure if there is a perfect solution for this scenario currently.
Author

Thanks, will try this :)

Thanks, will try this :)

Added subscriber: @ThomasDinges

Added subscriber: @ThomasDinges

Any update here? Did you find a solution @h0bB1T ?

Any update here? Did you find a solution @h0bB1T ?
Author

Hi. Yes. I followed the idea of Jaques to use a dirty flag and a timer based update and it works nicely. At the end, it seems the better solution. So at least to my needs, it is not necessary to fix it (and the new solution works in older Blender versions too).

Hi. Yes. I followed the idea of Jaques to use a dirty flag and a timer based update and it works nicely. At the end, it seems the better solution. So at least to my needs, it is not necessary to fix it (and the new solution works in older Blender versions too).

Changed status from 'Needs User Info' to: 'Archived'

Changed status from 'Needs User Info' to: 'Archived'

Great, thanks for your quick response. I am closing this then. :)

Great, thanks for your quick response. I am closing this then. :)
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#101378
No description provided.