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.