Tuesday, May 19, 2009

Assignment 01B


import maya.cmds as cmds
import math
import maya.mel as mm



def getDistance(start, end):
#start and end must be lists xyz
v = [start[0]-end[0], start[1]-end[1], start[2]-end[2]] #list format x,y,z
vector = "<<" + str(v[0]) + "," + str(v[1]) + "," + str(v[2]) + ">>"
mag = mm.eval("mag " + vector + ";")

return mag


#########THIS IS THE NON_RECURSIVE PART ------ JUST SKIP#######################
'''
mySquare = cmds.nurbsSquare(sl1=10, sl2=15)

isSides = cmds.filterExpand (sm=9)

pos = []

for i in range (len(isSides)):
posa = cmds.pointPosition(isSides[i]+".cv[0]")
pos.append (posa)



diag = getDistance(pos[0],pos[2])
a = getDistance(pos[2],pos[3])
b = getDistance(pos[3],pos[0])

h = math.sqrt(a*a - ((a*diag)/(b+a)) * ((a*diag)/(b+a)))


mySquare2 = cmds.nurbsSquare(sl1=h, sl2=diag)
isSides2 = cmds.filterExpand (sm=9)
pos2 = []

for i in range (len(isSides2)):
posb = cmds.pointPosition(isSides2[i]+".cv[0]")
pos2.append (posb)

#get diference between actual location and move
dist = getDistance(pos[2],pos2[2])
p1 = pos[2]
p2 = pos2[2]
x=p2[0] - p1[0]
print x
y=p2[0] - p1[0]
cmds.move(x,-y,0,mySquare)



#get angle of rotation
sinF = getDistance(pos[0],pos[1])
alpha = math.asin(sinF/diag)
Ld= math.degrees (alpha)

cmds.rotate(0,0,Ld,mySquare2, pivot = pos2[2])
'''



#create initial rectangle
mySquare = cmds.nurbsSquare(sl1=a, sl2=b)


#define recursive function
def MyRecursive(iterations,a,b):

isSides = cmds.filterExpand (sm=9)

pos = []
for i in range (len(isSides)):
posa = cmds.pointPosition(isSides[i]+".cv[0]")
pos.append (posa)


#define length of diagonal in order to use it for next rectangle
diag = getDistance(pos[0],pos[2])
#define length of 2 sides to get height
a = getDistance(pos[2],pos[3])
b = getDistance(pos[3],pos[0])
#get height of triangle (diagonal and the two sides)
h = math.sqrt(a*a - ((a*diag)/(b+a)) * ((a*diag)/(b+a)))

#create new rectangle with l2=diagonal length and l1= height of triangle
mySquare2 = cmds.nurbsSquare(sl1=h, sl2=diag)
isSides2 = cmds.filterExpand (sm=9)

pos2 = []

for i in range (len(isSides2)):
posb = cmds.pointPosition(isSides2[i]+".cv[0]")
pos2.append (posb)

#get diference between actual location and move to future pivot point
dist = getDistance(pos[2],pos2[2])
p1 = pos[2]
p2 = pos2[2]
x=p2[0] - p1[0]
y=p2[0] - p1[0]
cmds.move(x,-y,0,mySquare)



#get angle of rotation from diagonal
sinF = getDistance(pos[0],pos[1])
alpha = math.asin(sinF/diag)
Ld= math.degrees (alpha)

#rotate on diagonal
cmds.rotate(0,0,Ld,mySquare2, pivot = pos2[2])

#recursion part
#if else conditions
if iterations == 0:
return "Done"
else:
iterations -= 1
MyRecursive(iterations, a,b)

#call function
MyRecursive(10,10,15)



The problems i have are first of all that the height of the triangle i am calculating is not corresponding to the actual height.

Second would be that the recursion is not working as it should.

What it should do is first create a rectangle, create another one(with length=diagonal, and width=height of the triangle formed by diagonal and two sides). Rotate the last created rectangle on the diagonal, so that one of the sides "becomes" the diagonal. And then repeat everything for the next one.

Irina_Algorithm Sketches






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)
i=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
cmds.rotate(0,0,30,initialSquare)
cmds.scale(1.5,1.5,1,initialSquare)

#Recursion
if iterations ==0:
return "Done"
else:
iterations -=1

def pointBoundary(10)

Monday, May 18, 2009


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)

Andreea Assignment01B


Alice_Assignment 01B

I started by writing a simple algorithm and tried to search for suitable commands in Maya to execute the task, according to my idea. But soon enough, I already feel like I don't know much commands -.-.....

Besides, to answer your comments for my last post, mathematically speaking, there should be only 1 possibility to draw 3 circles touching each of the others. Therefore, given any 3 points, there should be only 1 result. 

I haven't get to the point to solve how to avoid concentrations result of the circles after many iterations. However, if it doesn't make the script much more complicated, I would like to try out if the square(the boundary) is rotated/extended according to a certain rule each time after making 3 new points and in a way to create an intricate network.... : )  


Assignment 01B

assignment 01b-1

Sunday, May 17, 2009

homework

Assignment 01B

Question:
How to find out the point which is tangent to the other circle in this case?

First Assignement ::: Relationship between 2D & 3D

Recurcive square ($iteration=10, $angle=15, $offset=2)
Recurcive square ($iteration=10, $angle=15, $offset=2)

Recurcive square ($iteration=10, $angle=15, $offset=0.2)

Recurcive square ($iteration=15, $angle=33, $offset=0.2)
Recurcive square ($iteration=8, $angle=75, $offset=0.2)



import maya.cmds as cmds

cmds.select(all=True)
cmds.delete()


def recursivesquares(iteration, angle, offset):
rotationpoint = [0,0,0]
for i in range (0, iteration, 1):

cmds.nurbsSquare(n="nurbsSquare%d" %i, sl1=10, sl2=10, c=rotationpoint, nry=1, nrz=0)
cmds.select("topnurbsSquare%d" %i)

cmds.select("nurbsSquare%d" %i)
cmds.rotate(i*angle, i*angle, i*angle, pivot=(rotationpoint))

top = 'topnurbsSquare%d' %i
topposition = cmds.pointPosition('topnurbsSquare%d.cv[0]' %i, world=True)
left = 'leftnurbsSquare%d' %i
leftposition = cmds.pointPosition('leftnurbsSquare%d.cv[0]' %i, world=True)
bottom = 'bottomnurbsSquare%d' %i
bottomposition = cmds.pointPosition('bottomnurbsSquare%d.cv[0]' %i, world=True)
right = 'rightnurbsSquare%d' %i
rightposition = cmds.pointPosition('rightnurbsSquare%d.cv[0]' %i, world=True)

cmds.squareSurface(top, right, bottom, left, n='nurbsSurface%d'%i, ct1=1, ct2=1, ct3=1, ct4=1, po=0)
rotationpoint = cmds.pointOnSurface('nurbsSurface%d'%i, u=offset, v=offset, position=True)
cmds.spaceLocator(p=rotationpoint, n="locator%d" %i)
cmds.delete('nurbsSurface%d'%i)

cmds.delete('nurbsSquare%d' %i)
cmds.curve(n='topviewnurbsSquare%d' %i, 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.curve(n='frontviewnurbsSquare%d' %i, 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.curve(n='sideviewnurbsSquare%d' %i, 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])] )

group = cmds.group('topviewnurbsSquare%d' %i, 'frontviewnurbsSquare%d' %i, 'sideviewnurbsSquare%d' %i, 'locator%d' %i, n='Squares%d' %i)

recursivesquares(10,15,0.9)

Thursday, May 14, 2009

Assignment 01B

Now it is time to start scripting your idea. Don’t worry about making it perfect, just start simple and small and go step-by-step. Simplification is the key. Here some guidelines:

  • Make your diagrams and logic (Assignment 01A) very clear. Divide the script into smaller tasks to check which tasks you know and which you don’t know how to perform
  • Define initial condition for your function to work. If it is very clear, you know that after you reach the initial condition again, you can run your function one more time. That’s where the recursion is.
  • Define a clear final condition
  • Make one iteration of your function work and only then make it recursive
  • If you reach a point where you can’t proceed, either look for help on the web, or post your progress on the blog, so I can make some comments

Please, post your progress by Sunday (17.05) morning on the blog, as I need time to take a look at it and prepare for the class on Monday, to be able to help you.

Don’t forget to post and updated diagram and logic if you changed it or developed further. We need to understand what you want to achieve!

If you manage the whole script by Sunday, great. If not, please make sure you tried really hard!

Assignment 01B – Help Functions

For the completion of Assignment 01, I prepared some small help functions and procedures to help you out in the process of scripting. They are based on things I observed on most submissions. They are not intended to provide ready solutions, but to offer some hints. Therefore they might seem a bit random and out of context. Any help you need to use them, please post on the comments.

gsii_A01_helpFunctions.py

The serpentine() function

We started the script by defining the main logic behind it, according to the diagrams posted last week.

# Basic logic
# 1) create an initial square (4 sides)
# 2) loop thorugh the sides:
# 2.1) get current line (line1)
# 2.2) get next line (line2)
# 2.3) define the percentages
# 2.4) find the point on curve
# 2.5) connect these points
# 3) After finished, select all 4 new lines and run again


As we see by this logic, what we need to run the loop each time is 4 curves through which we will loop and generate a new set of 4 lines. You can notice that as soon as we reach the initial condition, we can run the whole loop again (3): this is where recursion comes in handy.



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


This is how we start everything. With this command we have the 4 initial curve which make our connection loop work. Most important is that, by the way Maya created a nurbsSquare, all curves are selected in the right order on which they compose the square.



#lets connect line after line in a loop
for i in range(0, len(isSides), 1):
print i
#get the name of the current line
line1 = isSides[i]
#get the name of the following line
#but first check if we are already on the last line
#because then we have to pick back the first line
if i == len(isSides)-1:
#this means it is in the last element
#so we have to get the first curve
line2 = isSides[0]
else:
#otherwise, get next curve
line2 = isSides[i+1]

print line1, line2 #to check the line pair
#define the percentages on which to connect the lines
#this could be defined before the loop, because it is fixed
#but in case we wanted to make it variable, we need to
#define them here
perc1 = 1./2
perc2 = 1./3
#get the coordinates on the lines on the percentages
poc1 = cmds.pointOnCurve( line1, pr=perc1, top=True, p=True )
poc2 = cmds.pointOnCurve( line2, pr=perc2, top=True, p=True )
#create the curve to connect both lines
cmds.curve(ep=(poc1, poc2), d=1)


This is the basic main loop we need. Having a set of four consecutive curves selected, it works by going through each one of them and connecting them with a straight line from certain points in each line (define by percentages). By the end of this loop, we have 4 new curves, which we can use to feed the next iteration of the function.



To convert this basic part into a recursive function, we just need some small adjustments. The most important is the if/else statement after the loop which checks for the final condition. This ensures us the loop will not be infinite, and that the function will call itself again in case the end condition is still not met, creating the recursive effect we need.



#turning this into a recursive function
def serpentine(iterations):
#we assume that 4 lines are selected on stage
#and that these lines are in correct order
#this is the initial condition for the loop to work
isSides = cmds.filterExpand(sm=9)
#create a group to store the new lines
#in the end all we need is to select the group
#and we'll have the condition to run the function again
group = cmds.group( n="newCurves", em=True )
#loop through the curves in isSides
for i in range(0, len(isSides),1):
#get first curve name
line1 = isSides[i]
#get second curve name
if i == len(isSides)-1:
#this means it is in the last element
#so I have to get the first
line2 = isSides[0]
else:
#otherwise, get next element
line2 = isSides[i+1]
#print to check the pair
print line1, line2
#define the percentages
perc1 = 1./2
perc2 = 1./3
#get the coordinates on the lines
poc1 = cmds.pointOnCurve( line1, pr=perc1, top=True, p=True )
poc2 = cmds.pointOnCurve( line2, pr=perc2, top=True, p=True )
#create the curve
crv = cmds.curve( ep=(poc1, poc2), d=1 )
#put the curve in the group
cmds.parent( crv, group )

## now the recursive part
#check for the final condition
if iterations == 0:
#finished stop!
return "Done."
else:
#did not finish yet...
#remove one unit form iterations
#(coutdown)
iterations –= 1
#select the group
cmds.select( group, r=True )
#call the function again
serpentine( iterations )

Now, all you need is to create an initial square on stage, select it, and run the function, passing as a parameter the number of iterations you want it to perform.

Week 02 - recap

Last Monday we translated the rules from the Serpentine Pavilion to a full working script. In it, you saw how to find points in a curve, how to turn an initial loop into a recursive function, and how to create workaround for the lack of full 2D drafting support in Maya.

Also, we reviewed your submissions for assignment 01A (all comments are on the blog, under each post).

As a continuation of the assignment, you should give the first go at scripting your idea, keeping in mind the comments made, and what you should improve. Some of you should post new diagrams showing the improved logic of your script, according to the comments.