سؤال

I've created a function which creates a grid of circles and I need to collect the circle nodes created into a list so I can later manipulate the nodes. The problem is that I noticed the nodeList is given the nodes name before it's auto-renamed by maya to be unique. You'll notice when you run this script that the collected names are all the same but when you selected them in maya they are incremented to be unique.

I'm returned this

[u'mainShape_00', u'makeNurbCircle1']
[u'|mainShape_00', u'makeNurbCircle2']
[u'|mainShape_00', u'makeNurbCircle3']...

When it should be

[u'mainShape_00', u'makeNurbCircle1']
[u'|mainShape_01', u'makeNurbCircle2']
[u'|mainShape_02', u'makeNurbCircle3']...

Here is the script

# Import Modules
import maya.cmds as cmds
import random

# Scene setup
try:
    cmds.select(all=True)   
    cmds.delete()
except: 
    pass

# create 2D grid of circles
numRows = 4
numColumns = 3
radiusMin = .1
radiusMax = .75

#create empty group for nodes
nodeGroup = cmds.group(em=True, name='main_group_00')
nodeList = []

for r in range(0,numRows):
    for c in range(0,numColumns):

        # Calculate random radius
        radius = random.uniform(radiusMin,radiusMax)

        # Create circle shape and transform it
        node = cmds.circle(n='mainShape_00', ch=True, o=True, nr=(0, 0, 1), c=(0, 0, 0), r=radius)
        cmds.xform(node, t=(r*(radiusMax*2), c*(radiusMax*2), 0) )

        # Parent node under the group node
        cmds.parent(node[0], nodeGroup, relative=False)

        # Append nodes to list
        nodeList.append(node)

for n in nodeList:
    shape = n
    print shape
هل كانت مفيدة؟

المحلول

node is 'mainShape_00' because at that time, that's what it's named. There is no collision until it's parented under nodeGroup. Grab the real name after parenting:

node[0] = cmds.parent(node[0], nodeGroup, relative=False)[0]

This substitutes the original node[0] with the newly parented node[0]

نصائح أخرى

Why not naming yourself your nodes as this :

x = 0
padding = str(x).zfill(2)
mainShapeName = 'mainShape_' + padding
x += 1

# Create circle shape and transform it
node = cmds.circle(n=mainShapeName, ch=True, o=True, nr=(0, 0, 1), c=(0, 0, 0), r=radius)
cmds.xform(node, t=(r*(radiusMax*2), c*(radiusMax*2), 0) )

By incrementing yourself, you avoid maya problems. You should even gave unique name throught every groups.

Cheers.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top