Fix: Python node_utils.connect_sockets broken after API change #113630

Merged
Hans Goudey merged 2 commits from pioverfour/blender:dp_node_utils_fix_connect into blender-v4.0-release 2023-10-15 20:38:50 +02:00
Member

After the recent introduction of node panels, the utility that
allowed scripters to connect sockets to or from a virtual socket was
broken. This happens when creating an interface before connecting
sockets. Sockets can have a subtype, while interfaces cannot.

For instance, the NodeSocketFloatFactor type cannot be used
directly, it has to be converted to NodeSocketFloat.

This commit chooses an appropriate type for the new interface before
creating it, based on the socket's type property.


I think what I’m doing is stupid: using a regex to manipulate the name of the socket type, instead of programmatically finding its base type. However I couldn’t find a way to do that, bl_rna.base is useless here and so is the mro.
@LukasTonne I think you implemented the interface panels? Is there a more reliable way to find a socket’s base type?

Attached is an example use case for this PR. The script uses bpy_extras.node_utils.connect_sockets to create group inputs from the Principled BSDF node.

Also, this fixes Node Wrangler’s lazy connect.

After the recent introduction of node panels, the utility that allowed scripters to connect sockets to or from a virtual socket was broken. This happens when creating an interface before connecting sockets. Sockets can have a subtype, while interfaces cannot. For instance, the `NodeSocketFloatFactor` type cannot be used directly, it has to be converted to `NodeSocketFloat`. This commit chooses an appropriate type for the new interface before creating it, based on the socket's `type` property. ----- ~~I _think_ what I’m doing is stupid: using a regex to manipulate the name of the socket type, instead of programmatically finding its base type. However I couldn’t find a way to do that, `bl_rna.base` is useless here and so is the `mro`. @LukasTonne I think you implemented the interface panels? Is there a more reliable way to find a socket’s base type?~~ Attached is an example use case for this PR. The script uses `bpy_extras.node_utils.connect_sockets` to create group inputs from the Principled BSDF node. Also, this fixes Node Wrangler’s lazy connect.
Damien Picard added the
Module
Python API
Interest
Nodes & Physics
labels 2023-10-12 22:50:02 +02:00
Iliya Katushenock added this to the Nodes & Physics project 2023-10-13 09:02:42 +02:00
Iliya Katushenock removed the
Interest
Nodes & Physics
label 2023-10-13 09:02:45 +02:00
Hans Goudey requested review from Lukas Tönne 2023-10-13 09:13:19 +02:00
Member

I'd like to have a more robust way to find a valid base type from subclass on the C++ side. The new_socket method could do this internally, i think it's the only method in group interface API that takes a socket type name directly. Could also have a utility function but i don't think it's really necessary outside of this case. I'll take a look.

I'd like to have a more robust way to find a valid base type from subclass on the C++ side. The `new_socket` method could do this internally, i think it's the only method in group interface API that takes a socket type name directly. Could also have a utility function but i don't think it's really necessary outside of this case. I'll take a look.
Member

Ok, some complication: The interface sockets have to use the restricted list of socket base types for their enum items, otherwise we'd have to do internal checks and custom validation, which gets messy and confusing. A utility function to return a valid type like the one in this patch would be a cleaner solution, but then implemented based on actual type inheritance.

Ok, some complication: The interface sockets have to use the restricted list of socket base types for their enum items, otherwise we'd have to do internal checks and custom validation, which gets messy and confusing. A utility function to return a valid type like the one in this patch would be a cleaner solution, but then implemented based on actual type inheritance.
Member

I can't find a good way to do this in RNA, there are just too many limitations. I propose to do this the simple way, otherwise the function will not handle custom socket types correctly which do not necessarily follow the CamelCase pattern of the built-in socket types. I've tried making this smarter based on python class inheritance or concatenating the enum name with NodeSocket+name, but there are too many inconsistencies to make this work (e.g. "RGBA" vs "Color").

diff --git a/scripts/modules/bpy_extras/node_utils.py b/scripts/modules/bpy_extras/node_utils.py
index aee535499f4..4ab265fcdc5 100644
--- a/scripts/modules/bpy_extras/node_utils.py
+++ b/scripts/modules/bpy_extras/node_utils.py
@@ -7,6 +7,48 @@ __all__ = (
 )
 
 
+def find_base_socket_type(socket):
+    """
+    Find the base class of the socket.
+
+    Sockets can have a subtype such as NodeSocketFloatFactor,
+    but only the base type is allowed, e. g. NodeSocketFloat
+    """
+    import bpy
+
+    if socket.type == 'CUSTOM':
+        # Custom socket types are used directly
+        return socket.bl_idname
+    if socket.type == 'VALUE':
+        return 'NodeSocketFloat'
+    if socket.type == 'INT':
+        return 'NodeSocketInt'
+    if socket.type == 'BOOLEAN':
+        return 'NodeSocketBoolean'
+    if socket.type == 'VECTOR':
+        return 'NodeSocketVector'
+    if socket.type == 'ROTATION':
+        return 'NodeSocketRotation'
+    if socket.type == 'STRING':
+        return 'NodeSocketString'
+    if socket.type == 'RGBA':
+        return 'NodeSocketColor'
+    if socket.type == 'SHADER':
+        return 'NodeSocketShader'
+    if socket.type == 'OBJECT':
+        return 'NodeSocketObject'
+    if socket.type == 'IMAGE':
+        return 'NodeSocketImage'
+    if socket.type == 'GEOMETRY':
+        return 'NodeSocketGeometry'
+    if socket.type == 'COLLECTION':
+        return 'NodeSocketCollection'
+    if socket.type == 'TEXTURE':
+        return 'NodeSocketTexture'
+    if socket.type == 'MATERIAL':
+        return 'NodeSocketMaterial'
+
+
 def connect_sockets(input, output):
     """
     Connect sockets in a node tree.
@@ -34,17 +76,6 @@ def connect_sockets(input, output):
         print("Cannot connect two virtual sockets together")
         return
 
-    def find_base_socket_type(socket):
-        """
-        Find the base class of the socket.
-
-        Sockets can have a subtype such as NodeSocketFloatFactor,
-        but only the base type is allowed, e. g. NodeSocketFloat
-        """
-        import re
-        output_type = socket.bl_idname
-        return re.match(r"^([A-Z][a-z]+){3}", output_type).group()
-
     if output_node.type == 'GROUP_OUTPUT' and type(input) == bpy.types.NodeSocketVirtual:
         output_type = find_base_socket_type(output)
         socket_interface = output_node.id_data.interface.new_socket(
I can't find a good way to do this in RNA, there are just too many limitations. I propose to do this the simple way, otherwise the function will not handle custom socket types correctly which do not necessarily follow the CamelCase pattern of the built-in socket types. I've tried making this smarter based on python class inheritance or concatenating the enum name with `NodeSocket`+name, but there are too many inconsistencies to make this work (e.g. "RGBA" vs "Color"). ```diff diff --git a/scripts/modules/bpy_extras/node_utils.py b/scripts/modules/bpy_extras/node_utils.py index aee535499f4..4ab265fcdc5 100644 --- a/scripts/modules/bpy_extras/node_utils.py +++ b/scripts/modules/bpy_extras/node_utils.py @@ -7,6 +7,48 @@ __all__ = ( ) +def find_base_socket_type(socket): + """ + Find the base class of the socket. + + Sockets can have a subtype such as NodeSocketFloatFactor, + but only the base type is allowed, e. g. NodeSocketFloat + """ + import bpy + + if socket.type == 'CUSTOM': + # Custom socket types are used directly + return socket.bl_idname + if socket.type == 'VALUE': + return 'NodeSocketFloat' + if socket.type == 'INT': + return 'NodeSocketInt' + if socket.type == 'BOOLEAN': + return 'NodeSocketBoolean' + if socket.type == 'VECTOR': + return 'NodeSocketVector' + if socket.type == 'ROTATION': + return 'NodeSocketRotation' + if socket.type == 'STRING': + return 'NodeSocketString' + if socket.type == 'RGBA': + return 'NodeSocketColor' + if socket.type == 'SHADER': + return 'NodeSocketShader' + if socket.type == 'OBJECT': + return 'NodeSocketObject' + if socket.type == 'IMAGE': + return 'NodeSocketImage' + if socket.type == 'GEOMETRY': + return 'NodeSocketGeometry' + if socket.type == 'COLLECTION': + return 'NodeSocketCollection' + if socket.type == 'TEXTURE': + return 'NodeSocketTexture' + if socket.type == 'MATERIAL': + return 'NodeSocketMaterial' + + def connect_sockets(input, output): """ Connect sockets in a node tree. @@ -34,17 +76,6 @@ def connect_sockets(input, output): print("Cannot connect two virtual sockets together") return - def find_base_socket_type(socket): - """ - Find the base class of the socket. - - Sockets can have a subtype such as NodeSocketFloatFactor, - but only the base type is allowed, e. g. NodeSocketFloat - """ - import re - output_type = socket.bl_idname - return re.match(r"^([A-Z][a-z]+){3}", output_type).group() - if output_node.type == 'GROUP_OUTPUT' and type(input) == bpy.types.NodeSocketVirtual: output_type = find_base_socket_type(output) socket_interface = output_node.id_data.interface.new_socket( ```
Member

Writing an automated test for this would be nice, but i don't have time right now.

Writing an automated test for this would be nice, but i don't have time right now.
Author
Member

@LukasTonne Thanks for looking into it. I applied your diff, simply removed the unused import bpy.

@LukasTonne Thanks for looking into it. I applied your diff, simply removed the unused `import bpy`.
Lukas Tönne approved these changes 2023-10-13 16:15:24 +02:00
Member

Should this go to 4.0 instead of main?

Should this go to 4.0 instead of main?
Damien Picard changed title from Python API: fix node_utils.connect_sockets after API change to Python API: fix node_utils.connect_sockets after API change 2023-10-15 20:33:14 +02:00
pioverfour changed target branch from main to blender-v4.0-release 2023-10-15 20:33:16 +02:00
Damien Picard force-pushed dp_node_utils_fix_connect from 21dbdd8cb1 to 8c58f03b60 2023-10-15 20:35:11 +02:00 Compare
Author
Member

Should this go to 4.0 instead of main?

Yes, I rebased just now. Thanks

> Should this go to 4.0 instead of main? Yes, I rebased just now. Thanks
Hans Goudey changed title from Python API: fix node_utils.connect_sockets after API change to Fix: Python node_utils.connect_sockets broken after API change 2023-10-15 20:37:55 +02:00
Hans Goudey merged commit fa5bb53f5c into blender-v4.0-release 2023-10-15 20:38:50 +02:00
Damien Picard deleted branch dp_node_utils_fix_connect 2023-10-16 12:56:15 +02:00
Sign in to join this conversation.
No reviewers
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 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#113630
No description provided.