
collection of class notes, resources and student work

import maya.cmds as cmds
import random
import math
def magnitude (v):
x= v[0]
y= v[1]
z= v[2]
m = math.sqrt ((x*x)+(y*y)+(z*z))
return m
def distance (p1, p2):
#substract both vectors
x1 = p1[0]
y1 = p1[1]
z1 = p1[2]
x2 = p2[0]
y2 = p2[1]
z2 = p2[2]
x = x1-x2
y = y1-y2
z = z1-z2
m = magnitude ([x,y,z])
return m
def unit(v):
#returns unit vector of v
#first get magnitude
m = magnitude(v)
x = v[0]
y = v[1]
z = v[2]
#devide each element by magnitude
x = x/m
y = y/m
z = z/m
vu = [x,y,z]
return vu
def move (v, amount):
"Move by certaine amount in directio v"
x = v[0]
y = v[1]
z = v[2]
x = x+amount
y = y+amount
z = z+amount
newP = [x,y,z]
return amount
def vectorBetweenPoints (p1, p2):
"Returns vector between p1 and p2"
#substract both vectors
x1 = p1[0]
y1 = p1[1]
z1 = p1[2]
x2 = p2[0]
y2 = p2[1]
z2 = p2[2]
x = x1-x2
y = y1-y2
z = z1-z2
newV = [x,y,z]
return newV
def moveVert (allCvs):
selLoc = cmds.filterExpand (sm = 22)
xs = 0
ys = 0
zs = 0
for i in allCvs:
pos = cmds.pointPosition(i)
posLoc = cmds.pointPosition (selLoc)
dd = distance ([centerX, centerY, centerZ], posLoc)
print dd
cmds.polyExtrudeVertex( l =dd, w=3, d=4 )
def moveVertice (allCvs):
selLoc = cmds.filterExpand (sm = 22)
allCvs1 = cmds.ls("nurbsPlane2.cv[:][:]", fl=1)
#d=1000
dist =1000
allDist =[]
print selLoc
for i in allCvs1:
pos = cmds.pointPosition(i)
for j in selLoc:
posLoc = cmds.pointPosition (j)
dd = distance (pos, posLoc)
if dd




#######################
## Ex03B_Move Cvs of a Nurbs according to Locator direction
#######################
import maya.cmds as cmds
import math
#define distance between PolFaces and locator
def magnitude (v):
#v is a list of x,y,z values
x= v[0]
y= v[1]
z= v[2]
m= math.sqrt ( (x*x) + (y*y) + (z*z) )
return m
def distance (posVert, posLoc):
#subtract both vectors
x1= posVert[0]
y1= posVert[1]
z1= posVert[2]
x2= posLoc[0]
y2= posLoc[1]
z2= posLoc[2]
x= x1 - x2
y= y1 - y2
z= z1 - z2
m= magnitude ([x,y,z])
return m
#define moveToLocator function
def moveToLocator():
# A) get all the verticesNames of a plane
allVertices= cmds.ls ("pPlane1.vtx[:]", fl=1)
#create an empty list to store the points
MyVerts=[]
#get selected Locator
selLoc= cmds.filterExpand(sm=22)
#loop through vertices
for v in allVertices:
pos= cmds.pointPosition(v)
x= pos[0]
y= pos[1]
z= pos[2]
#find distance to Locator
posVert= cmds.pointPosition(v)
posLoc= cmds.pointPosition(selLoc)
d= distance(posVert, posLoc)
print d
cmds.move (d,3,d,v,r=True)
#store the cvs in a list
MyVerts.append(allVertices)
#call the function
moveToLocator()


#######################
## Ex03B_Move Cvs of a Nurbs according to Locator direction
#######################
import maya.cmds as cmds
import math
#define distance between cvs and locator
def magnitude (v):
#v is a list of x,y,z values
x= v[0]
y= v[1]
z= v[2]
m= math.sqrt ( (x*x) + (y*y) + (z*z) )
return m
def distance (posPoint, posLoc):
#subtract both vectores
x1= posPoint[0]
y1= posPoint[1]
z1= posPoint[2]
x2= posLoc[0]
y2= posLoc[1]
z2= posLoc[2]
x= x1 - x2
y= y1 - y2
z= z1 - z2
m= magnitude ([x,y,z])
return m
#define moveToLocator function
def moveToLocator():
# A) get all the cvsNames of a NurbsPlane
NurbsCvs = cmds.ls("nurbsSphere1.cv[:]", fl=1)
#create an empty list to store the points
MyCvs=[]
#get selected Locator
selLoc= cmds.filterExpand(sm=22)
#loop through cvs
for i in NurbsCvs:
posPoint= cmds.pointPosition(i)
posLoc= cmds.pointPosition (selLoc)
d= distance(posPoint, posLoc)
print d
cmds.move (d,d,d,i,r=True)
#store the cvs in a list
MyCvs.append(NurbsCvs)
#call the function
moveToLocator()


#####################
## Ex03B_Move Cvs of a Curve according to Locator direction
#######################
import maya.cmds as cmds
import math
#define distance between cvs and locator
def magnitude (v):
#v is a list of x,y,z values
x= v[0]
y= v[1]
z= v[2]
m= math.sqrt ( (x*x) + (y*y) + (z*z) )
return m
def distance (posPoint, posLoc):
#subtract both vectores
x1= posPoint[0]
y1= posPoint[1]
z1= posPoint[2]
x2= posLoc[0]
y2= posLoc[1]
z2= posLoc[2]
x= x1 - x2
y= y1 - y2
z= z1 - z2
m= magnitude ([x,y,z])
return m
#define moveToLocator function
def moveToLocator():
# A) get all the cvsNames of a curve
allCvs = cmds.ls("curve1.cv[:]", fl=1)
#create an empty list to store the points
MyCvs=[]
#get selected Locator
selLoc= cmds.filterExpand(sm=22)
#loop through cvs
for i in allCvs:
posPoint= cmds.pointPosition(i)
posLoc= cmds.pointPosition (selLoc)
d= distance(posPoint, posLoc)
print d
cmds.move (0,0,d,i,r=True)
#store the cvs in a list
MyCvs.append(cvs)
#call the function
moveToLocator()

function fillBoiler:
with cup
fill with coldMilk
get boiler
place boiler right to cup
with cup
move 20cm up
turn -90°
if cup = „empty“
turn 90°
move 20cm down
execute function boilMilk
function boilMilk:
get boiler
put boiler on heater
turn heater.switch to „on“
desiredTime = 5min
when desiredTime:
turn heater.switch to „off“
execute function putCapuccino
function putCapuccino:
get cup
desiredCapuccino = 2
desiredSugar = 2
execute function fillCup
function fillCup:
get cup
place cup right to boiler
with boiler:
move 20cm up
turn -90°
if cup = „full“
with boiler:
turn 90°
move 20cm down
finish
## Ex03B_Move Curve Cvs according to Locator direction
#####
import maya.cmds as cmds
import math
#get names of selected objects on Stage
#first select the objects
selObj= cmds.ls(sl=1)
print selObj
#find position of this two objetcs
obj1= selObj[0] #this is for locator1
obj2= selObj[1] #this is for curve1
print obj1
print obj2
pos1= cmds.pointPosition(obj1)
pos2 = cmds.pointOnCurve(obj2)
print pos1
print pos2
#find distance between two objects
d= distance (pos1,pos2)
print d
#move all vertices of obj2 10 times in direction to obj1
# select all vertices of obj2
allCvs = cmds.ls("curve1.cv[:]", fl=1)
# I dont know how to continue....
## Ex03B_Move Curve according to Locator direction
#####
import maya.cmds as cmds
import math
#get names of selected objects on Stage
#first select the objects
selObj= cmds.ls(sl=1)
print selObj
#find position of this two objetcs
obj1= selObj[0] #this is for locator1
obj2= selObj[1] #this is for curve1
print obj1
print obj2
pos1= cmds.pointPosition(obj1)
pos2= cmds.pointOnCurve(obj2)
print pos1
print pos2
#find distance between two objects
d= distance (pos1,pos2)
print d
#move obj2 4 times in direction to obj1
# first get vectors between obj2 and obj1
v= vectorBetweenPoints(pos1,pos2)
# get the unit of vector v
dir= unit(v)
#move 4 units in dir obj1
newPos= move (dir,4)
#move obj2 to that position
cmds.move (newPos[0], newPos[1], newPos[2], obj2)


### Ex03B ######
## Vector Functions
import maya.cmds as cmds
import math
import random
########### extrude the faces of a polygonal object
# according to its distance to a certain
# locator
def ExtrudeToLocator():
"Extrude based on a distance of a certain Locator"
#get selected Poly and selected Locator
selPoly= cmds.filterExpand(sm=12)
selLoc= cmds.filterExpand(sm=22)
selPoly= selPoly[0]
#get all faces of the Poly
allFaces= cmds.ls(selPoly + ".f[:]", fl=1)
#start a loop through all faces
for face in allFaces:
vertex= cmds.polyListComponentConversion (face, fromFace=1, toVertex=1)
vertex= cmds.ls (vertex, fl=1)
# find the center of the face
xs= 0
ys= 0
zs= 0
#loop through vertices
for v in vertex:
pos= cmds.pointPosition(v)
x= pos[0]
y= pos[1]
z= pos[2]
xs= xs + x
ys= ys + y
zs= zs + z
centerX= xs/len(vertex)
centerY= ys/len(vertex)
centerZ= zs/len(vertex)
#check by placing a Locator
cmds.spaceLocator(p=(centerX, centerY, centerZ))
#find distance to Locator
posLoc= cmds.pointPosition(selLoc)
d= distance([centerX, centerY, centerZ], posLoc)
#extrude based on distance
cmds.polyExtrudeFacet(face, ltz=d)
#extrude to locator
ExtrudeToLocator()









######### Excercise 03A
import maya.cmds as cmds
import random
import math
###### CURVE ######
# A) get all the cvsNames of a curve
allCvs = cmds.ls("curve1.cv[:]", fl=1)
# B) define move cvs randomly
def moveVertRandomly (allCvs, minimum, maximum):
if minimum>= maximum:
print "hello!"
# B.1) start a loop through all cvs
else:
for cv in allCvs:
rx = random.uniform(minimum, maximum)
ry = random.uniform(minimum, maximum)
rz = random.uniform(minimum, maximum)
cmds.move (rx,ry,rz,cv,r=2)
# C) move cvs randomly
moveVertRandomly (allCvs, 0, 17)
####### Nurbs Surface #######
# A) get all the vertices of the Nurbs
allCvs= cmds.ls("nurbsPlane1.cv[:][:]", fl=1)
# B) define move cvs in nurbsPlane randomly
def moveVertiRandomly(allCvs, minimum, maximum,x,y,z):
if minimum>= maximum:
print "hello!"
# B.1) start a loop through all cvs in z axis
else:
for i in allCvs:
x= 0
y= 0
z= random.uniform(minimum, maximum)
cmds.move (x,y,z,i,r=True)
# C) move dvs randomly
moveVertiRandomly (allCvs, 2, 18, 0,0,7)
####### Faces of a Polygon #######
# A) get all faces of a Polygon
allFaces= cmds.ls ("pCone1.f[:]", fl=1)
# B) define extrude faces of a polygon
def ExtrudeFacesRandom (allFaces, minimum, maximum):
if minimum>= maximum:
print "polygon"
# B.1 start a loop through all faces in y axix
else:
for face in allFaces:
ry= random.uniform(minimum, maximum)
cmds.polyExtrudeFacet(face, ltz=ry)
# C) extrude faces
ExtrudeFacesRandom(allFaces, 0,10)
####### Polygon Vertices #######
# A) get all vertices of a Polygon
allVertices= cmds.ls ("pPlane1.vtx[:]", fl=1)
# B) define movePolVertices
def movePolVert (allVertices, minimum, maximum):
if minimum>= maximum:
print "polygon"
# B.1 start a loop through vertices in z axis
else:
for e in allVertices:
rx= random.uniform (minimum, maximum)
ry= random.uniform (minimum, maximum)
rz= random.uniform (minimum, maximum)
cmds.move (rx,ry,rz,e,r=True)
# C) move Polygon Vertices
movePolVert(allVertices,1,7)






import maya.cmds as cmds
import random
import math
def moveVertRandomly (allCvs, minimum, maximum):
if minimum>= maximum:
print "Find new variables!"
else:
for i in allCvs:
rx = random.uniform(minimum, maximum)
ry = random.uniform(minimum, maximum)
rz = random.uniform(minimum, maximum)
cmds.move (rx,ry,rz,i,r=1)
def moveVerticeRandomly (allCvs, minimum, maximum, x,y,z):
if minimum>= maximum:
print "Find new variables!"
else:
for i in allCvs:
z = random.uniform(minimum, maximum)
cmds.move (x,y,z,i,r=1)
def extrudeFacesRandomly(allFaces, minimum, maximum):
if minimum>= maximum:
print "Find new variables!"
else:
for i in allFaces:
rz = random.uniform(minimum, maximum)
cmds.polyExtrudeFacet(i, ltz = rz)
def polygonVertices (allVert, minimum, maximum):
if minimum>= maximum:
print "Find new variables!"
else:
for i in allVert:
rx = random.uniform(minimum, maximum)
ry = random.uniform(minimum, maximum)
rz = random.uniform(minimum, maximum)
cmds.move (rx,ry,rz,i,r=1)
allCvs = cmds.ls("curve1.cv[:]", fl=1)
moveVertRandomly (allCvs, 0, 10)
allCvs1 = cmds.ls("nurbsPlane1.cv[:][:]", fl=1)
moveVerticeRandomly (allCvs, 0, 10, 0,0,5)
extrudeFacesRandomly(allFaces, 0, 5)
allFaces = cmds.ls ("pPlane1.f[:]", fl = 1)
allVert = cmds.ls("pPlane2.vtx[:]", fl=1)
polygonVertices (allVert, 0, 5)
|
|
| Generative Scripting I - SS09 |
| Visit this group |