Saturday, May 23, 2009

01B - alec' - Real recursion, soft code, optimized, updated, improved & commented new script...

In this script I mainly correct my mistakes! I changed the loop in a real recursive function, I used "soft code" instead of "hard code" and I tried to optimized my codes.

I also added a recursion "J" which makes vary the result every time we call the function once again. This new "j" parameter (called "recursions" with a "s") increase the rotation angle and the scale of the squares. (= the first 4th screenshots)

In this example, I used a loop at the end of the script to call the function 5times!
and I configured this script to show only the z-axis projection. (= the last screenshot)

I didn't managed to simplify the "projection part" ... from the line 53 to 65.



import maya.cmds as cmds

##########################
### INITIAL PARAMETERS ###
##########################
### drawx = project square on x axis, drawy = project square on y axis, drawz = project square on z axis
### delete3Dsquare = delete the original iterated squares
### deletelocator = delete locator related to the rotationpoint
### deleteSquareSurface = delete the nurbsSquare used for the pointPosition + offset function
drawx = 0 # 0 = Fasle /// 1 = True
drawy = 0 # 0 = Fasle /// 1 = True
drawz = 1 # 0 = Fasle /// 1 = True
delete3Dsquare = 1 # 0 = Fasle /// 1 = True
deletelocator = 1 # 0 = Fasle /// 1 = True
deleteSquareSurface = 1 # 0 = Fasle /// 1 = True
iterationgroup = cmds.group(em=True)
if cmds.objExists('j'):
print "rien"
else:
cmds.spaceLocator(n='j')
iterations = 1
j = []



############################
### DEF RECURSIVE SQUARE ###
############################
### Def the function according to 3 parameters; number of iterations, angle of rotation between each square, offset of the point of rotation
### rotationpoint [0,0,0] is the initial rotation point position /// these position are used only for the first iteration

def recursivesquares(j,iteration, angle, offset, rotationpoint=[0,0,0]):
group = cmds.group(em=True)

### Create the square, and rotate it according to the angle and rotationpoint parameters
nurbsSquare = cmds.nurbsSquare(sl1=iterations*5, sl2=iterations*5, c=rotationpoint, nry=1, nrz=0)
cmds.parent(nurbsSquare[0], group)
cmds.rotate(iterations*iteration*angle, iterations*iteration*angle, iterations*iteration*angle, [nurbsSquare[0]], pivot=(rotationpoint))

### Determining the position of each corner of this square
topposition = cmds.pointPosition('top'+nurbsSquare[0]+'.cv[0]', world=True)
leftposition = cmds.pointPosition('left'+nurbsSquare[0]+'.cv[0]', world=True)
rightposition = cmds.pointPosition('right'+nurbsSquare[0]+'.cv[0]', world=True)
bottomposition = cmds.pointPosition('bottom'+nurbsSquare[0]+'.cv[0]', world=True)

### Create a nurbs surface to use the pointOnSurface command with the offset parameter
### Create a locator in the position of this new rotationpoint
nurbsSquareSurface = cmds.squareSurface('top'+nurbsSquare[0], 'right'+nurbsSquare[0], 'bottom'+nurbsSquare[0], 'left'+nurbsSquare[0], ct1=1, ct2=1, ct3=1, ct4=1, po=0)
rotationpoint = cmds.pointOnSurface(nurbsSquareSurface[0], u=offset/(iteration+1), v=offset/(iteration+1), position=True)
locator = cmds.spaceLocator(p=rotationpoint)
cmds.parent(locator[0], nurbsSquareSurface[0], group)

### Project the squares on the x, y or/and z axis
if drawx == 1:
squaresonx = cmds.curve(d=1, p=[(topposition[0], topposition[1], 0),(leftposition[0], leftposition[1], 0),(bottomposition[0], bottomposition[1], 0),(rightposition[0], rightposition[1], 0), (topposition[0], topposition[1], 0)] )
cmds.parent(squaresonx, group)

if drawy == 1:
squaresony = cmds.curve(d=1, p=[(topposition[0], 0, topposition[2]),(leftposition[0], 0, leftposition[2]),(bottomposition[0], 0, bottomposition[2]),(rightposition[0], 0, rightposition[2]), (topposition[0], 0, topposition[2])] )
cmds.parent(squaresony, group)

if drawz == 1:
squaresonz = cmds.curve(d=1, p=[(0, topposition[1], topposition[2]),(0, leftposition[1], leftposition[2]),(0, bottomposition[1], bottomposition[2]),(0, rightposition[1], rightposition[2]), (0, topposition[1], topposition[2])] )
cmds.parent(squaresonz, group)

else:
print "project nothing"

### Clean the Scene
cmds.parent(group, iterationgroup)

if delete3Dsquare == 1:
cmds.delete(nurbsSquare[0])

if deletelocator == 1:
cmds.delete(locator[0])

if deleteSquareSurface == 1:
cmds.delete(nurbsSquareSurface[0])



### Iterate the function
if iteration == 0:
j.append(1)
return j

else:
iteration -= 1
recursivesquares(j,iteration, angle, offset, rotationpoint)


#############################
### CALL RECURSIVE SQUARE ###
#############################


for i in range (0, 5, 1):
recursivesquares(j,10,15,.9, [0,0,0])
iterations = len(j)+1
print iterations


No comments:

Post a Comment