Inconsistent NameError

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to model random paper lanterns using Python, but, for some reason, my code sometimes runs and sometimes doesn't. At first, the code worked, and then, after closing out of Maya and eventually opening it again, I got a NameError, even though the code ran perfectly before. Then, after maybe an hour or so of having Maya open in the background, the code ran fine again. Now, after reopening Maya once more, I'm getting the Name Error again. I don't understand why the NameError is so inconsistent. Here's the relevant snippet of my code:
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 = lantern paper 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 lantern wick = cmds.polyTorus(ax=(0,1,0), n='myWick') # torus = lantern 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 into proper position at bottom of opening in paper for i in range (0,4): strings = [string1, string2, string3, string4]
# Error: NameError: file <maya console> line 23: 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))
# 4 cylinders = 4 strings/wires connecting paper to wick 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]) # rotates strings into proper orientation cmds.move(xMove[i], -0.75, zMove[i], strings[i]) # moves strings into proper spot for connection lantern = cmds.polyUnite(paper, wick, string1, string2, string3, string4, n='myLantern')
# paper + wick + strings = lantern (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 know that string1 (in addition to the other 3 objects in the strings list) is not yet defined in line 21, where the error is raised, but that didn't seem to be a problem before. Of course, I can find another (potentially longer) way to implement the 4 strings, but I'm really confused about why the error is so inconsistent. Based on the one time that the code ran after initially not working, it seems like maybe I have to have Maya open for a while before it will work, but why is that?