Linked sockets by Python has is_linked attribute as False #82390

Open
opened 2020-11-04 05:39:40 +01:00 by Sergey · 22 comments

System Information
Operating system: Windows-10-10.0.18362-SP0 64 Bits

Blender Version
Broken: version: 2.91.0 Beta, branch: master, commit date: 2020-10-29 21:28, hash: 9e85812acc

Short description of error
If add link to a node tree in its update method linked sockets will still have is_linked attribute as False. I would expect that it should be True because what else should be expected if I explicitly saying that I what two sockets to be linked to each other.

Exact steps for others to reproduce the error
untitled.blend

  1. Download file
  2. Execute text script in the text editor
  3. Look at prints in console

It is important to me because as I was told I have to replace sockets of reroute nodes in tree update method. https://developer.blender.org/T81701
reroutes.gif
I'm deleting old sockets with their links and create new one with new links. Then I'm passing running to update system which assumes that if socket has False in is_linked attribute then the socket is really unlinked what is not true anymore.

**System Information** Operating system: Windows-10-10.0.18362-SP0 64 Bits **Blender Version** Broken: version: 2.91.0 Beta, branch: master, commit date: 2020-10-29 21:28, hash: `9e85812acc` **Short description of error** If add link to a node tree in its update method linked sockets will still have `is_linked` attribute as False. I would expect that it should be True because what else should be expected if I explicitly saying that I what two sockets to be linked to each other. **Exact steps for others to reproduce the error** [untitled.blend](https://archive.blender.org/developer/F9189404/untitled.blend) 1. Download file 2. Execute text script in the text editor 3. Look at prints in console It is important to me because as I was told I have to replace sockets of reroute nodes in tree update method. https://developer.blender.org/T81701 ![reroutes.gif](https://archive.blender.org/developer/F9189483/reroutes.gif) I'm deleting old sockets with their links and create new one with new links. Then I'm passing running to update system which assumes that if socket has False in `is_linked` attribute then the socket is really unlinked what is not true anymore.
Author

Added subscriber: @randum

Added subscriber: @randum
Author

Funny thing is that if I'm creating link outside of update tree event (via timer call) sockets are getting their actual status immediately. How this behaviour can be explained?

    tree = create_tree()

    def add_link():
        n1, n2 = tree.nodes[0], tree.nodes[1]
        in_s, out_s = n1.inputs[0], n2.outputs[0]
        tree.links.new(in_s, out_s)
        print('New link was created')
        print(f'Input socket is linked: {in_s.is_linked}')
        print(f'Output socket is linked: {out_s.is_linked}')
    
        
    bpy.app.timers.register(add_link, first_interval=0.1)
    print('TREE WAS ADDED')
TREE WAS ADDED
New link was created
Input socket is linked: True
Output socket is linked: True

untitled (1).blend

Funny thing is that if I'm creating link outside of update tree event (via timer call) sockets are getting their actual status immediately. How this behaviour can be explained? ``` tree = create_tree() def add_link(): n1, n2 = tree.nodes[0], tree.nodes[1] in_s, out_s = n1.inputs[0], n2.outputs[0] tree.links.new(in_s, out_s) print('New link was created') print(f'Input socket is linked: {in_s.is_linked}') print(f'Output socket is linked: {out_s.is_linked}') bpy.app.timers.register(add_link, first_interval=0.1) print('TREE WAS ADDED') ``` ``` TREE WAS ADDED New link was created Input socket is linked: True Output socket is linked: True ``` [untitled (1).blend](https://archive.blender.org/developer/F9195927/untitled__1_.blend)

Added subscriber: @iss

Added subscriber: @iss

Changed status from 'Needs Triage' to: 'Archived'

Changed status from 'Needs Triage' to: 'Archived'
Richard Antalik self-assigned this 2020-11-09 23:03:22 +01:00

Looking at code, adding link will run update function on to_node and on tree itself. So this is potentially quite dangerous to do inside update function. I am not quite sure if these update function calls will be nested or queued. After adding debug print inside update function, it looks like they are queued...

It is not quite clear to me why you need to do this though.

I will just repeat, please use https://www.blender.org/community/ for technical support.

Looking at code, adding link will run update function on `to_node` and on tree itself. So this is potentially quite dangerous to do inside update function. I am not quite sure if these update function calls will be nested or queued. After adding debug print inside update function, it looks like they are queued... It is not quite clear to me why you need to do this though. I will just repeat, please use https://www.blender.org/community/ for technical support.
Author

Did you read your own message posted several weeks ago?

Color type socket is created by default by operator. In case of shader nodes, socket type is changed in node tree update, which is called by operator. So if you want this functionality, you will have to implement this in your node tree

And now you are talking that it is not the way and :

It is not quite clear to me why you need to do this though.

What is not clear? Why I would like to see my own sockets in reroute node, why I would like to see it linked when it linked or what?

Did you read your own message posted several weeks ago? > Color type socket is created by default by operator. In case of shader nodes, socket type is changed in node tree update, which is called by operator. So if you want this functionality, you will have to implement this in your node tree And now you are talking that it is not the way and : > It is not quite clear to me why you need to do this though. What is not clear? Why I would like to see my own sockets in reroute node, why I would like to see it linked when it linked or what?

In #82390#1051306, @randum wrote:
Did you read your own message posted several weeks ago?

Color type socket is created by default by operator. In case of shader nodes, socket type is changed in node tree update, which is called by operator. So if you want this functionality, you will have to implement this in your node tree

And now you are talking that it is not the way and

I am not talking about sockets here, I am saying what happens when you create a link. rna_NodeTree_link_new() is function that does this.

It is not quite clear to me why you need to do this though.

What is not clear? Why I would like to see my own sockets in reroute node, why I would like to see it linked when it linked or what?

Why do you need to add links from within update function. Perhaps custom operator woud be better here, I am not sure.

> In #82390#1051306, @randum wrote: > Did you read your own message posted several weeks ago? > >> Color type socket is created by default by operator. In case of shader nodes, socket type is changed in node tree update, which is called by operator. So if you want this functionality, you will have to implement this in your node tree > > And now you are talking that it is not the way and I am not talking about sockets here, I am saying what happens when you create a link. `rna_NodeTree_link_new()` is function that does this. >> It is not quite clear to me why you need to do this though. > > What is not clear? Why I would like to see my own sockets in reroute node, why I would like to see it linked when it linked or what? Why do you need to add links from within update function. Perhaps custom operator woud be better here, I am not sure.
Author

How custom operator will be notify that reroute node was connected then? What I need is that reroute always has sockets the same type as socket to which it is connected. In another topic you gave me advice to do this in update function.

How custom operator will be notify that reroute node was connected then? What I need is that reroute always has sockets the same type as socket to which it is connected. In another topic you gave me advice to do this in update function.

What I need is that reroute always has sockets the same type as socket to which it is connected.

Then just change type? I don't see why you would need to create new links.

> What I need is that reroute always has sockets the same type as socket to which it is connected. Then just change type? I don't see why you would need to create new links.
Author

The type of a socket is its class. It impossible to change class of an instance. Change socket type means to create new socket instance from another class.

The type of a socket is its class. It impossible to change class of an instance. Change socket type means to create new socket instance from another class.

I was almost convinced, but I thought that before I reopen I will need some evidence. However this function seems to work pretty well:

    def update(self):
        nodes = self.nodes
        for node in nodes:
            if isinstance(node, bpy.types.NodeReroute):
                in_sock = node.inputs[0]
                - in_sock.display_shape = 'SQUARE_DOT'
                - in_sock.type = 'VECTOR'
                from_sock = in_sock.links[0].from_socket
                in_sock.type = from_sock.type

I see some glitches like when you add more reroutes, this doesn't work well. That looks like a bug on the surface, but you can probably bubble up chain up to the actual node as workaround.

I was almost convinced, but I thought that before I reopen I will need some evidence. However this function seems to work pretty well: ``` def update(self): nodes = self.nodes for node in nodes: if isinstance(node, bpy.types.NodeReroute): in_sock = node.inputs[0] - in_sock.display_shape = 'SQUARE_DOT' - in_sock.type = 'VECTOR' from_sock = in_sock.links[0].from_socket in_sock.type = from_sock.type ``` I see some glitches like when you add more reroutes, this doesn't work well. That looks like a bug on the surface, but you can probably bubble up chain up to the actual node as workaround.
Author

Here is the list of standard socket types:
2020-11-12_09-36-16.png

To see type of socket you should use socket.bl_idname

Value of type attribute of a socket is not type of socket. It is type of its default value.

Here is the list of standard socket types: ![2020-11-12_09-36-16.png](https://archive.blender.org/developer/F9263472/2020-11-12_09-36-16.png) To see type of socket you should use `socket.bl_idname` Value of type attribute of a socket is not type of socket. It is type of its default value.

I added debug prints of in_sock.bl_idname to update function

    def update(self):
        nodes = self.nodes
        for node in nodes:
            if isinstance(node, bpy.types.NodeReroute):
                in_sock = node.inputs[0]
                from_sock = in_sock.links[0].from_socket
                print(in_sock.bl_idname)
                in_sock.type = from_sock.type
                print(in_sock.bl_idname)

After making reroute, the output was:

NodeSocketColor
NodeSocketVector

So you should rather change type member of NodeSocket class to change its instance. This looks like it's working as expected.
Maybe a bit weird from OOP perspective, but these data structures are in C and there you can change class without having to make new intance.

I added debug prints of `in_sock.bl_idname` to update function ``` def update(self): nodes = self.nodes for node in nodes: if isinstance(node, bpy.types.NodeReroute): in_sock = node.inputs[0] from_sock = in_sock.links[0].from_socket print(in_sock.bl_idname) in_sock.type = from_sock.type print(in_sock.bl_idname) ``` After making reroute, the output was: ``` NodeSocketColor NodeSocketVector ``` So you should rather change `type` member of `NodeSocket` class to change its instance. This looks like it's working as expected. Maybe a bit weird from OOP perspective, but these data structures are in C and there you can change class without having to make new intance.
Author

Okay. Here is MY socket. How can I switch type of socket of any other type to socket of mine?

class MyCustomSocket(NodeSocket):
    bl_idname = 'MyCustomSocket'
    bl_label = "My Custom Socket"

    def draw_color(self, context, node):
        return (0.0, 0.0, 0.0, 1)
    
    def draw(self, context, layout, node, text):
        layout.label(text=text)
Okay. Here is MY socket. How can I switch type of socket of any other type to socket of mine? ``` class MyCustomSocket(NodeSocket): bl_idname = 'MyCustomSocket' bl_label = "My Custom Socket" def draw_color(self, context, node): return (0.0, 0.0, 0.0, 1) def draw(self, context, layout, node, text): layout.label(text=text) ```

Changed status from 'Archived' to: 'Needs Triage'

Changed status from 'Archived' to: 'Needs Triage'

Added subscriber: @JacquesLucke

Added subscriber: @JacquesLucke

That seems to be problematic. So to recapitulate this:

Issue is that Add Reroute operator always creates color socket.
Node socket type should be changed during node tree update function, which works fine for predefined types.
But for custom socket type defined in python, it seems that it is necessary to make new socket instance and rebuild links to and from socket.
However is_linked property of socket is not changed immediately, but in node update function if I remember this correctly. Also this will cause node tree function to be called. I am not sure if this is making this problem unsolvable, but id definitely doesn't sound nice.

@JacquesLucke Can you look at this comment and give your opinion if this could be a bug? or are we missing something?

That seems to be problematic. So to recapitulate this: Issue is that Add Reroute operator always creates color socket. Node socket type should be changed during node tree update function, which works fine for predefined types. But for custom socket type defined in python, it seems that it is necessary to make new socket instance and rebuild links to and from socket. However is_linked property of socket is not changed immediately, but in node update function if I remember this correctly. Also this will cause node tree function to be called. I am not sure if this is making this problem unsolvable, but id definitely doesn't sound nice. @JacquesLucke Can you look at this comment and give your opinion if this could be a bug? or are we missing something?
Member

Changed status from 'Needs Triage' to: 'Confirmed'

Changed status from 'Needs Triage' to: 'Confirmed'
Richard Antalik was unassigned by Philipp Oeser 2021-06-02 10:10:08 +02:00

Added subscriber: @JoshuaKnauber

Added subscriber: @JoshuaKnauber

Added subscriber: @Zeastin

Added subscriber: @Zeastin

Added subscriber: @evantryan

Added subscriber: @evantryan

Added subscriber: @timodriaan

Added subscriber: @timodriaan
Philipp Oeser removed the
Interest
Nodes & Physics
label 2023-02-10 08:46:27 +01:00
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
7 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#82390
No description provided.