Saturday, May 23, 2009


It does not work and giving mistake:
UnboundLocalError: local variable 'Squares' referenced before assignment #


import maya.cmds as cmds
x=10
y=10
#Make a Square
initialSqare = cmds.nurbsSquare(sl1=x, sl2=y)
#get outline
isSides = cmds.filterExpand (sm=9)
print isSides
#make an empty list
Squares = []
Squares.append (1)

def squareINsquare (number):
#scale
x=10/(1.5*len(Squares)+1)
y=10/(1.5*len(Squares)+1)
#loop
for j in range (0,len(Squares),1):
cmds.select (Squares[j], r=True)
#get outline
isSides = cmds.filterExpand (sm=9)
#looping in outline
for i in range (0,2,1):
perc1 = 0
perc2 = 1
line1=isSides[1]
line2 = isSides[2]
if i == 0:
poc1 = cmds.pointOnCurve(line1, pr=perc1, top=True, p=True)
print poc1
#change the center of the new square
poc1[0]=poc1[0]-x/2
poc1[1]=poc1[1]+y/2
print poc1
#make a new square
initialSqare1 = cmds.nurbsSquare(c = poc1, sl1=x, sl2=y)
#the same for the oposit corner of the squre
else:
poc2 = cmds.pointOnCurve(line2, pr=perc2, top=True, p=True)
print poc2
poc2[0]=poc2[0]+x/2
poc2[1]=poc2[1]-y/2
print poc2
initialSqare2 = cmds.nurbsSquare(c = poc2, sl1=x, sl2=y)

Squares.append (initialSqare1)
Squares.append (initialSqare2)
Squares=Squares1
Squares1=[]
if number == 0 :
#stop
return "Done."
else:
#did not finish yet
#remove one unit (count down)
number -= 1

squareINsquare (number)

squareINsquare (3)

1 comment:

  1. Hi Grisha!

    1) One thing is for sure in your script: as you know, you are having problem with the Squares list. I know you created the empty list on lines 10-11, but as we can see it did not solve the problem. Also, in the first iteration of the loop you use Squares as defined in 10-11, and then when you do Squares[j] on line 19, you not actually referring to any object, just to the integer 1 from line 11! This will for sure not work.

    2) One idea would be to try to pass the Squares list as an argument to the function. Have you tried that?

    3) The loop on lines 28-44 is not doing your code any good. If you rearrange it, it will sure make more sense: try defining the variables perc1 and perc2 in a list of 2 values, as well as line1 and line2 in another list of 2 values, both outside of the loop. Then you can actually use the loop i value to get first the perc1 and line1 and then perc2 and line2 (by doing perc[i] or lines[i]). Than you create the initialSquare and store it in Squares also inside the loop.

    4) I don't really understand why you do 48-49... was it a try to fix the error?

    In general terms it is going a good way. I hope you managed to fix this mistake. Let me know of any advances by posting in the blog.
    I am having a bit of difficulty guessing the indentation of your code. When you paste it in blogger, try to make sure the indentation is still visible!

    ReplyDelete