I just started using Maya, and I'm trying to model random paper lanterns using Python in Maya: an open-bottom cylinder represents the paper, a small torus represents the wick, and 4 very thin cylinders represent strings connecting the wick to the paper. The code worked this morning, and I'm 99% certain that I haven't changed anything (in the part of the code that's raising an error) since then, but now I'm getting a "name not defined" error inside of a list in a for loop. Here's the relevant snippet of my code (it models the initial lantern, before I instance more):
import maya.cmds as cmds import random cmds.select(all=True) cmds.delete() random.seed(1234) paper = cmds.polyCylinder(r=1, h=2, sx=50, sy=1, sz=1, ax=(0,1,0), rcp=0, cuv=3, ch=1, n='myPaper')
# cylinder = paper of lantern cmds.polyColorPerVertex(rgb=(0.941,0.686,0.306), a=1, cdo=1) for i in range (0,50): cmds.select(paper[0] + '.f[50]', r=True) cmds.delete() # opens bottom of cylinder wick = cmds.polyTorus(ax=(0,1,0), n='myWick') # torus = wick cmds.polyColorPerVertex(rgb=(0.2,0.2,0.2), a=1, cdo=1) cmds.scale(0.25, 0.25, 0.25, wick) cmds.move(0, -.75, 0, wick) # moves wick to bottom of opening in paper for i in range (0,4): strings = [string1, string2, string3, string4] # Error: NameError: file <maya console> line 21: name 'string1' is not defined strings[i] = cmds.polyCylinder(r=0.01, h=0.625, sx=50, sy=1, sz=1, ax=(0,1,0), rcp=0, cuv=3, ch=1, n='myString' + str(i+1)) cmds.polyColorPerVertex(rgb=(0.2,0.2,0.2), a=1, cdo=1) xRotate = [90,90,0,0] zRotate = [0,0,90,90] xMove = [0,0,0.625,-0.625] zMove = [0.625,-0.625,0,0] cmds.rotate(xRotate[i], 0, zRotate[i], strings[i]) # gets strings in proper orientation cmds.move(xMove[i], -0.75, zMove[i], strings[i]) # moves strings to position to "connect" paper and wick
lantern = cmds.polyUnite(paper, wick, string1, string2, string3, string4, n='myLantern')
# unites paper, wick, and strings into one polygonal object
cmds.move(random.uniform(-50,50), random.uniform(10,35), random.uniform(-50,50),lantern)
cmds.rotate(random.uniform(-10,10), random.uniform(0,360), random.uniform(-10,10), lantern)
I'm really not sure why the NameError is arising now because I executed this code countless times earlier today, and it always worked. I understand that, when I initialize the list strings, string1 (in addition to the three other variables in strings) has not been defined yet, but, like I keep saying, that didn't seem to be a problem before.
Also, before I started getting this error, I was struggling to change the color of random instances of lantern[0]. After uniting multiple objects using polyUnite, is there any way to select one of the original objects to edit it? I tried to select myPaper and use polyColorPerVertex, but it didn't work. I also tried instancing paper[0], wick[0], and the strings separately, changing the color of paper, and then using polyUnite on them, but that also didn't work. Is it because instances don't count as polygons? If so, is there some way that I can change the color of an instance? I had no problem changing their positions and angles, so I assumed I could also change their colors.
All Python scripts in Maya execute within the same global environment. If earlier in the Maya session you had created Python objects named 'string1', 'string2', etc, perhaps while testing something else, then your code would have worked. But after restarting Maya those objects will no longer be there.
Can't find what you're looking for? Ask the community or share your knowledge.