How do NodeSockets determine if a link between them is possible?

I have two NodeSockets class SockA(NodeSocket) and class SockA(NodeSocket). Now when I define a Node class in a custom NodeTree with both A and B inputs and outputs, I can create links between A and B sockets.

I had instead expected, that it would only be possible, to connect sockets of the same type (or different socket types that define their compatibility somehow).

How can a restriction like this on the linkability of sockets be achieved?

Note: I have looked through the source of animation_nodes for some time. it seems to define this restriction with variables on the sockets like this:

dataType = "Color"
allowedInputTypes = ["Color"]


However, by checking the source for these variable names, I could not find any place where this would be enforced by the AN code, so I am left puzzled.

Here's a rough proof-of-concept: https://gist.github.com/Theverat/f6c30469e22e03202dd429fe19b24c83 See my revision for the changes I did: https://gist.github.com/Theverat/f6c30469e22e03202dd429fe19b24c83/revisions

Basically, the node tree offers an update method which does not tell you what exactly happened, only that something happened.

But each node offers an insert_link method, which tells you about a link before it is inserted. So in this method you can not delete the link, or prevent it being inserted, you can only log it in your nodetree as new (or flag it for deletion).

So when the user creates a new link, this is what happens behind the scenes:

• insert_link is called on the node the connection is coming from
• insert_link is called on the node the connection is going into (Note: the link is the same, so you should prevent a duplicate insertion into the list of new links in the node tree)
• update is called on the parent node tree of these nodes
• Thank you - I'm going to accept the answer, when I've tryed it out. Interestingly though, animationNode does not use the insert_link method at all - I still wonder how they achieve it. – IARI Jul 24 '18 at 6:37
• You should ask the AN developers about it. Are you sure they actually delete links based on the socket types? In my LuxCore addon I have a similar system, but it just warns the user in the UI if socket types are incompatible, it does not prevent linking them. – B.Y.O.B. Jul 24 '18 at 7:14
• Yes, I just double checked it: AN does prevent it. Also there is a problem with your approach: When you drag a node on top of a link, Blender will try to "put the node in between" - apparently this does not call the insert_link method. AN also prevents this – IARI Jul 24 '18 at 7:48
• un-accepted, because technically you can still create A-B links with the described technique, so the problem isn't solved – IARI Jul 25 '18 at 8:03
• I looked a bit more through the AN code, the way Jacques Lucke does it is by keeping a lot of control over the updates in AN, using all sorts of callbacks blender offers, and then performs validity checks on the whole graph. One of the triggered checks originates from the NodeTree.update method. I will again accept this answer, if you just somehow mention, that checking everything in update might be a better solution. – IARI Jul 31 '18 at 17:18