Could someone help me solve this error

47 views
Skip to first unread message

jettam

unread,
Sep 13, 2017, 1:16:43 PM9/13/17
to Python Programming for Autodesk Maya
Could someone help me with this code. This function is supposed to build a spiral staircase then distribute it randomly X amount of times. I have been able to make it work with another object creation but I cant seem to figure this one out.
Here is the error I am getting. And bellow is my code.
# Error: local variable 'x' referenced before assignment
# Traceback (most recent call last):
#   File "<maya console>", line 42, in <module>
#   File "<maya console>", line 39, in makeRobot
#   File "<maya console>", line 9, in makeSpiralStairs
# UnboundLocalError: local variable 'x' referenced before assignment # 

import maya.cmds as mc

import random


''' Build a spiral staircase and distribute it randomly X amount of times.'''


def makeRobot(numRobots=10):
     
   
def makeSpiralStairs () :
        inc
= str(x +1)


        steps
= {'name':'Step','slide':5,'rotations':10,'spacing':1,'stepDepth':2,'stepHeight':.75}
        stepName
=steps.get('name')


        groupAllSteps
= mc.group(name="Group"+stepName+"s"+inc, empty=True)


       
for x in range (numRobots):


            stepObj
= mc.polyCube(name=stepName+inc, w=5, h=steps.get('stepHeight'), d=steps.get('stepDepth'), ch=0)


            stepGrp
= mc.group(empty=True, name=stepName+inc)


            mc
.parent(stepObj, stepGrp)


            mc
.xform(stepGrp, ro=(0,(steps.get('rotations')*x),0))


            mc
.xform(stepObj[0], t=(steps.get('slide'),steps.get('stepHeight')*steps.get('spacing')*x,0))
           


            mc
.parent(stepObj, groupAllSteps)


            mc
.delete(stepGrp)
           
return groupAllSteps


       
   
for x in range(numRobots):
        AAA
= random.uniform(-20,20)
        BBB
= random.uniform(-20,20)
        CCC
= random.uniform(-20,20)    
       
Grp2 = makeSpiralStairs()
        mc
.setAttr(Grp2[0] + ".translate", AAA,BBB,CCC, type="double3")        


makeRobot
(10)



damon shelton

unread,
Sep 13, 2017, 1:21:19 PM9/13/17
to python_in...@googlegroups.com
You are using x inside of makespiralstairs function but not defining it or passing it in 

def makeSpiralStairs(x):


Then passing x in when you call it

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/548d7be6-b3fc-4785-8a95-cd0df7ea3ae7%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

jettam

unread,
Sep 13, 2017, 1:35:06 PM9/13/17
to Python Programming for Autodesk Maya
I thought I had defined it earlier, here:   Then after that there is a call to the function makeSpiralStairs()     

for x in range(numRobots):
        AAA = random.uniform(-20,20)
        BBB = random.uniform(-20,20)
        CCC = random.uniform(-20,20)     
        Grp2 = makeSpiralStairs()


To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

damon shelton

unread,
Sep 13, 2017, 1:40:10 PM9/13/17
to python_in...@googlegroups.com
Inc = str(x+1)
X is not defined before this line

X is defined when you execute the for x in range  loop. You have to pass x into the makespiralstairs function to use it. X is a local variable

To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/abaa7765-b08b-4f7a-9eab-c544c28216ac%40googlegroups.com.

jettam

unread,
Sep 13, 2017, 2:26:33 PM9/13/17
to Python Programming for Autodesk Maya
Inc = str(x+1)  has been defined in the earlier "for x in" command has it not ? 

I did another version earlier (with out a "for in" command in the second function). And this worked without having to put a x in the makeSpiralStairs() function. 

I made a order of events as I see it. (see the screen grab).   Please let me know how my understanding of the order is wrong. 

damon shelton

unread,
Sep 13, 2017, 2:36:56 PM9/13/17
to python_in...@googlegroups.com
The declaration of x is in the local scope of A.
The variable x inside makespiralstairs has no declaration because x is not declared as global. Not a matter of order, it's a matter of scope. Passing x to the function will fix your issue. That or declare x as a global which I don't suggest. There's a lot of info online about local and global scope in Python and most other languages. It's important to utilize this so as not to accidentally override your values later

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

jettam

unread,
Sep 13, 2017, 2:51:50 PM9/13/17
to Python Programming for Autodesk Maya
Ok thanks.  I will revisit my notes on scope.


On Wednesday, September 13, 2017 at 11:36:56 AM UTC-7, damonshelton wrote:
The declaration of x is in the local scope of A.
The variable x inside makespiralstairs has no declaration because x is not declared as global. Not a matter of order, it's a matter of scope. Passing x to the function will fix your issue. That or declare x as a global which I don't suggest. There's a lot of info online about local and global scope in Python and most other languages. It's important to utilize this so as not to accidentally override your values later

On Wed, Sep 13, 2017 at 11:26 AM jettam <justin...@gmail.com> wrote:
Inc = str(x+1)  has been defined in the earlier "for x in" command has it not ? 

I did another version earlier (with out a "for in" command in the second function). And this worked without having to put a x in the makeSpiralStairs() function. 

I made a order of events as I see it. (see the screen grab).   Please let me know how my understanding of the order is wrong. 




On Wednesday, September 13, 2017 at 10:40:10 AM UTC-7, damonshelton wrote:
Inc = str(x+1)
X is not defined before this line

X is defined when you execute the for x in range  loop. You have to pass x into the makespiralstairs function to use it. X is a local variable

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_maya+unsub...@googlegroups.com.

Neil Roche

unread,
Sep 14, 2017, 9:13:06 AM9/14/17
to Python Programming for Autodesk Maya
Also, is there a reason why you are using the dictionary steps for all your values?  It's hardcoded so you can't change any of the values, so if you wanted a variation you would have to rewrite the code.

You could try using keyword arguments in your function makeSpiralStairs(), for example makeSpiralStairs(name = 'Step', slide = 5, rotations = 10, spacing = 1, stepDepth = 2, stepHeight = 0.75 ), delete the line with the dictionary steps and just replace steps.get() with the relevant keyword, e.g steps.get('name') change to name.

It won't affect your code as you can still run makeSpiralStairs() on line 74 as it will take your keyword argument default values.

jettam

unread,
Sep 14, 2017, 6:45:05 PM9/14/17
to Python Programming for Autodesk Maya
This is probably a good idea,  I am still learning Python, (baby steps for now)
Reply all
Reply to author
Forward
0 new messages