Tuesday, May 19, 2009

Alice_Assignment 01B script trial

import maya.cmds as cmds

#create an initial square
initialSquare = cmds.nurbsSquare (sl1=10, sl2=10)
isSides = cmds.filterExpand(sm=9)

#create 3 random points inside the square** (I don't know how to find this random command)
ptA =
ptB =
ptC =

def pointBoundary(iterations)

#Create 3 curves
lineA = cmds.curve(ep=(ptC,ptB),d=1)
lineB = cmds.curve(ep=(ptA,ptC),d=1)
lineC = cmds.curve(ep=(ptB,ptA),d=1)

#Get the distance between 3 points
lenA = cmds.distanceDimension(ptC,ptB)
lenB = cmds.distanceDimension(ptA,ptC)
lenC = cmds.distanceDimension(ptB,ptA)

#Radius of the 3 circles:riA,riB,riC
riA = (lenB + lenC - lenA)/2
riB = (lenA + lenC - lenB)/2
riC = (lenA + lenB - lenC)/2

#Create 3 circles
cirleA = cmds.circle(c=ptA,r=riA)
cirleB = cmds.circle(c=ptB,r=riB)
CirleC = cmds.circle(c=ptC,r=riC)

#Tangent points among the 3 circles
tanA = cmds.curveIntersect(lineA,circleB)
tanB = cmds.curveIntersect(lineB,circleC)
tanC = cmds.curveIntersect(lineC,circleA)

#Offset lenA, lenB, lenC to a long distance and create new lines offA, offB, offC
#(I am not sure if it offset to the right direction!!!)

offA = cmds.offsetCurve(lenA,1000)
offB = cmds.offsetCurve(lenB,1000)
offC = cmds.offsetCurve(lenC,1000)

nodeA = cmds.closestPointOnCurve(offA,tanA)
nodeB = cmds.closestPointOnCurve(offB,tanB)
nodeC = cmds.closestPointOnCurve(offC,tanC)

#Draw perpendicular lines
perA = cmds.curve(ep=(tanA,nodeA), d=1)
perB = cmds.curve(ep=(tanB,nodeB), d=1)
perC = cmds.curve(ep=(tanC,nodeC), d=1)

#Test if perpendicular lines intersect with which sides of the square
#Get coordinates of the 3 intersection points on the square and
#replace the existing values of ptA, ptB, ptC

for i in range(len(isSides)):
isIntA = cmds.curveIntersect(perA,isSides[i])
isIntB = cmds.curveIntersect(perB,isSides[i])
isIntC = cmds.curveIntersect(perC,isSides[i])
if isIntA != None:
ptA = isIntA
if isIntB != None:
ptB = isIntB
if isIntC != None:
ptC =isIntC

#Resize the initial Square

if iterations ==0:
return "Done"
iterations -=1

def pointBoundary(10)


  1. Hi Alice,

    Nice and clear script. Here some comments:

    1) You'll have a problem with the 3 random points (line 7), as you create them outside of the function definition. What will happen is the following: you create it first time. Then in the first iteration of the pointBoundary function, it will use these values, and then generate new values for these variables inside the function (lines 57-70). But then, on the following iteration, the function will still use the values you created on the first time. Why? because of the variable scope: variables created on the root of the script, outside of any functions or loops, are accessible globally. That's what happens with ptA, ptB and ptC in lines 7-10. Variables created inside functions or loops exist only inside these functions or loops: their scope is local. Everytime the function terminates or is executed again, or in every iteration of the loop, these local variables are deleted from memory and do not retain their value. So how could you still grab and use these values? By passing them as arguments to your function. Your function definition should actually be:

    def pointBoundary(iterations, ptA, ptB, ptC):

    and then when you call it again on the recursive call (last line), you have to pass the newly created values from lines 61-70 when you call the function again.

    2) On line 83, when you call the function again, be careful with the identation: it should be idented under the else statement, so that it is executed only if iterations are not equal to 0. Also, when you call a function, you don't use the "def" word, just the function name.

    3) about how to get those random points in the beginning (7-10): it is not hard. If you imagine your square is plotted on a (x,y) plane, you know that the center of it is on (0,0) (as you did not define a center in the nurbsSquare command, it goes to the default 0,0,0). This way you can get the corner coordinates of it if you know the side lengths, which you leaves you with the x-minimum, x-maximum, y-minimun and y-maximum. You can then use these values to get any random point inside the square:

    from random import *

    x = uniform(xmin, xmax)
    y = uniform(ymin, ymax)

    In general I think you are going on a good direction. You just have to test test and test, and according to the results you can find where the problem is. Give it a try and post here!

  2. This comment has been removed by the author.