Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

Inconsistent NameError

Inconsistent NameError

Anonymous
Not applicable
507 Views
1 Reply
Message 1 of 2

Inconsistent NameError

Anonymous
Not applicable

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?

0 Likes
508 Views
1 Reply
Reply (1)
Message 2 of 2

RFlannery1
Collaborator
Collaborator

The error happens because, as you said "string1 (in addition to the other 3 objects in the strings list) is not yet defined in line 21".  In the cases where it does work, it is because something somewhere has defined "string1".  When you define a variable outside a function, it becomes available to any script that wants to access it.  And once it is defined, it stays defined until Maya is restarted.

 

If you would like to get the error consistently, consider putting all your code into a def block (a function).  Then every time you run the function, it creates a fresh local scope that doesn't remember anything about previous times it was run.  Unless you properly define "string1" in the function, you will get the error every time.  (A warning though:  If at any point you define "string1" outside of any functions, you will run into the same problem as before.  Defining it in a different function is fine.  Functions can't access each others' variables.)