XM Magdalena 3D print, GGeorgy (3D)
LC #42 Pipers Alley

View Full Version : random connection

12-02-2011, 11:23 AM
Hello everyone,
Im trying to write a python script that will randomly connect attributes of different objects.
Here is what I have:
connections = e.g. connect 3 pCylinders connect to 10 polySpheres(that's why there is "Sph" in partition function.
inputNodes = []
outputNodes = []
selNodes = cm.ls(selection=True)
for nodes in selNodes:
if nodes.partition("Sph")[1] == "Sph":
if nodes.partition("Sph")[1] != "Sph":

for node in range(len(inputNodes)):
dividedNum = (len(inputNodes)-1)
i = random.randint(0,dividedNum)
dividedNum = (len(outputNodes)-1)
o = random.randint(0,dividedNum)

if cm.isConnected(outputNodes[o]+".rotateX", inputNodes[i]+".rotateX") == 1:

cm.disconnectAttr(outputNodes[o]+".rotateX", inputNodes[i]+".rotateX")

cm.connectAttr(outputNodes[o]+".rotateX", inputNodes[i]+".rotateX")

the problem is that when code detects connection between objects, instead of disconnect the attributes and then reconnect them it prints error , that attributes are already connected.
This means that somewhere I have and error. But where?

12-02-2011, 12:32 PM
your code has lost all formatting so it's a little hard to say where there is or isn't certain errors, however, rather than disconnect if connected, why don't you just connect if not connected?


12-02-2011, 01:41 PM
Thank you Nathan, but still got the same error. In "has lost formatting" did you mean absence of indentation or the code is not readable because I wrote it very bad?
Anyway, can you advice how to write this kind of code? Not command and/or code, just paragraphs.
There is two group of objects. Group A and group B.
Group A have multiple members. The members of the group A in the name have some common string. e.g. pSphere1, pSphere2, pSphere3 etc
Group B have less members and different names.
After I get a list of this objects I might do separate them in different lists. Then I loop through each member of group A and generate a random index number for members of that groups. Them I check if member[index]'s attr of group A have connection with member[index]'s attr of group B. If it not then connect this two group's attribute.
Is this correct?

CGTalk Moderation
12-02-2011, 01:41 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.