How to detect which link was deleted by user in node editor?

I see in API documentation that there is the method which will be triggered if link is connecting to a node. And it works fine. But I found nothing about when a link is disconnecting.

Only what Beldner says is that something was changed. It could be that a bunch of links was deleted or probably neither of them.

It is quite a problem because if it is impossible to say what exactly was changed in node tree the only way reevaluate node tree is to do it from scratch. I think I should not say that it is quite inefficient approach.

If it is impossible indeed how this going be handled in everything node project @jacqueslucke? Or probably there is some tricky way of disconnection detection?

1 Like

Yeah, unfortunately the Python API does not provide callbacks for that.
Blender’s C code can figure out what changed exactly of course.

2 Likes

@jacqueslucke Thanks. Is there any chance of improvements of API in this direction?

It should be possible for sure. However, I don’t see that happening anytime soon. Also just with this API addition you might still not be able to detect all node tree changes accurately. For now, it is probably best to “remember” the old node tree state in a script and compare it to the new state. It’s not nice, but I don’t have a better idea right now.

Why not? Blender already grant good API for tracking nodes life, changes properties and creating new links by user. Any links crating/deleting via Python already known. So the only problem is with deleting links by user. I have made the table of Blender update events of node tree and what can be detected by them.

Creating node tree state is possible solution but with difficulties. It impossible to just copy all links in first stage and compare them with links on the second stage because memory of address of copied links could be changed and Blender will crash. And the order of links in tree.links can change I think.

So it needs to convert links in some way it will be possible keep them in Blender and put them in set data type. It will take significant time and doing this steps every update event will be quite expensive. But if node tree have some nodes with updating time about one second it will be cheaper to handle links changes any way.

Well, I actually did not take in account reroute nodes. It is impossible to subscribe to their creation, deleting or copying. There is no such event as linking reroute node as well.

One trick which probably can help with detection of creation of new reroute node is to check whether number of nodes collection was changed mean while creation of any other normal nodes was not detected. But I would not surprise if len(node_tree.nodes) will take O(n).

I think there must be a not too complicated way of doing this. Each node tree has a list of links, there should be a way to compare the list every time there is a change and show the node that connected or disconnected. I did this test in a few minutes, but I think that with a little more dedication, is possible define a workaround.


Cheers…

It is not as simple as it looks. You can’t use blender objects (links, nodes, sockets) directly because they can change the memory address. You can’t use their names because user can change them, you can’t use their indexes because they can be changed.

Second thing is performance. Using such comparisons in each update event can be very inefficient if you have hundreds of links in a node tree.

Just a thought: you can get the memory address of the underlying C/C++ structure for a Python wrapped object with as_pointer(). Maybe the addresses on that level do not change very often (I wouldn’t expect so, but haven’t looked into it). Although operations like undo will probably screw things up anyway.

1 Like

This draws an apocalyptic scenario… !! So we really can’t do anything, what a mess.
Cheers…

According documentation python instances of Blender objects already keep straight references to address of memory. Pointer property also won’t help because it can’t keep nodes and links.

Probably, solutions could be to use external graph editor, like this:

When you creating new nodes they attached to mouse so you can place them. If to press escape during this placing a node will be deleted. Funny thing is that when you creating node the node tree has signal 'something changed` but when pressing escape there are no any signals. I’m going report this as a bug.

Sure is a bug? Think a bit. If you delete the node before confirming its creation, has anything changed in the node tree that makes it necessary to process the node tree again?
I think not…

A node actually manage to get in the node collection even if it is not placed in node tree layout and pressing escape deletes it from the collection and tree.update method is deaf to this action.

Sure… is deaf for save resources. As I says, make no sense update all node tree, if the node is not ‘really’ added and linked.

I would agree with you if Blender would not add a node to the node collection before that. All actions which change node or link collections should cause update method of node tree. It is purpose of the method according documentation.