Figuring out which way a connection goes in Maya

64 views
Skip to first unread message

Fredrik Averpil

unread,
Mar 18, 2015, 12:34:36 PM3/18/15
to python_in...@googlegroups.com
[cross-posting this as I posted this by mistake on the maya_he3d list too ]

Okay so I want to store connections to and from objects in a python dictionary.

I can check what is connected to an object or a certain attribute of the object using cmds.listConnections(myobject, plugs=True). But I can't see which way the connection was made. So when I recreate the object and want to re-create the connection, I don't know which plug is supposed to be the source and which plug is supposed to be the destination.

This seems like a very fundamental problem... so I'm guessing I'm missing something fundamental too :)

Any ideas?

Cheers,
Fredrik

Justin Israel

unread,
Mar 18, 2015, 12:55:04 PM3/18/15
to python_in...@googlegroups.com

I never understood why the "connections" flag results in the given object always being first in the returned connection pairs. It seems to make the results pointless when doing both a source and destination query since you don't know which is which. Can you make two calls to listConnections which one being s=True/d=False and the other being s=False/d=True?


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/CAD%3DwhWOu67u5Zjx-ukeD6iokVpeecCY66ChfCfhN1b39s0xNWw%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Chad Dombrova

unread,
Mar 18, 2015, 1:35:08 PM3/18/15
to Maya Python Group

I never understood why the "connections" flag results in the given object always being first in the returned connection pairs.

Agreed.  pymel's listConnections has a sourceFirst keyword arg which makes the results more intuitive, in addition to the returning a list of (src, dst) tuples instead of a flat list.

-chad

 

Fredrik Averpil

unread,
Mar 19, 2015, 6:10:32 AM3/19/15
to python_in...@googlegroups.com
Okay, so this is how you need to do it (without using pymel), I guess:

objattr = 'VRayMtl1.color'

# Guess work
src = cmds.listConnections(attr, p=True, s=True, d=False)
dst = cmds.listConnections(attr, p=True, s=False, d=True)
if src != None:
    print src, '->', objattr
if dst != None:
    print objattr, '->', dst
// F


Marcus Ottosson

unread,
Mar 19, 2015, 6:23:10 AM3/19/15
to python_in...@googlegroups.com

Looks good to me.

Depending on your use-case, you might find that about half of the information you store about connections is duplicated due to A being connected to B, but B also being connected to A. It’s a fairly common problem in graph theory, here’s some interesting reads about possible solutions and their pros and cons.

Best,
Marcus

Fredrik Averpil

unread,
Mar 19, 2015, 7:02:19 AM3/19/15
to python_in...@googlegroups.com
Sweet, thanks Marcus!


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

Justin Israel

unread,
Mar 19, 2015, 2:27:49 PM3/19/15
to python_in...@googlegroups.com

Yea it would depend on whether you are interested solely on resolving the nodes, or if you are more concerned about resolving the relationships. NodeA could have and edge to NodeB, and. NodeB could have a different edge to NodeA. They both relate to each other, and while the Node references are the same, the edges in the graph are unique.
As long as you are maintaining an index of nodes and using the same references, you shouldn't end up duplicating data.

You could also check out this library, which can handle letting you just loop over connections and throwing nodes and edges at it, and it will manage the graph

https://networkx.github.io

It comes with a bunch of graph algorithms to traverse and evaluate stuff.


Reply all
Reply to author
Forward
0 new messages