Wednesday, July 22, 2009

Claudia_Ex04B_Nurbs Surfaces









########## EX04B ###########
# 1) Create a curve from points
# 2) Tubulize the curve
# 3) Create 3 more tubes displaced in a radial way
# 4) Move the Cvs of each tube
# according to a locator distance
# 5) Populate each tube with cylinders
# 6) finish!

###########################
import maya.cmds as cmds
from math import*
from random import*

# 1) Create a list of points to generate a curve
# define initial variables
numPoints= 15
# create an empty list to store the points
myPoints= []

# def Curve
def Curve(amplitude):
#loop and gather point information
for i in range (0, numPoints, 1):
# function of the helix to generate a curve
x= cos(7*i)
y= sin(7*i)
z= i*amplitude
myPoint= (x,y,z)
# store the points in the list
myPoints.append(myPoint)
#create the curve
Curve= cmds.curve(d=3, p=myPoints, n="myCurve") #curve of degree 3

#call function
myCurve=Curve(5)

# 2) Create a tube
# first select the curve
myCurve= cmds.filterExpand(sm=9)

# define the function
def tubulize(path,radius,tubeSections):
# function to create tubes out of a curve
#position on end of the curve where I will put the circle profile
pos= cmds.pointOnCurve(path, position=1, pr=0.001, top=1)
#get tangent of this point
tan= cmds.pointOnCurve(path,tangent=1,pr=0.001,top=1)
#create the profile circle
profile=cmds.circle(c=pos,r=radius,normal=tan,s=tubeSections,ch=1, n="circle")
#center the circle pivot
cmds.xform(cp=1)

#extrude the circle along the path
tube=cmds.extrude(profile[0], path, ucp=1, upn=1, et=2, rb=1, dl=3, ch=1,n="spiralTube")

# call function
myTube=tubulize(myCurve,6,10)

# 3) duplicate tubes rotating them according to a Locator
# create a locator
pivot= (-12,15,0)
myPoint= cmds.spaceLocator( p=pivot, n="myPoint")

# create a list to store the tubes
myTubes=[]
# select the tube to copy
selObjects=cmds.select("spiralTube")

# define variables
numTubes=3
rot = 0
# duplicate the tubes
for e in range (0,numTubes,1):
rot=rot+70
mynewTubes= cmds.duplicate("spiralTube", n="myNewTubes_%s" %e)
cmds.rotate(0,0,rot, "myNewTubes_%s" %e, p=pivot)
myTubes.append("myNewTubes_%s" %e)

#4) Move the vertices of each tube according
# a locator distance
# 4.1) For the Spiral Tube
#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,pivot):
#subtract both vectors
x1= posPoint[0]
y1= posPoint[1]
z1= posPoint[2]
x2= pivot[0]
y2= pivot[1]
z2= pivot[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 myTubes
spiralTubeCvs = cmds.ls("spiralTube.cv[:]", fl=1)

#create an empty list to store the points
mySpiralCvs=[]

#get selected Locator
#myPoint= cmds.filterExpand(sm=22)
myPointPos= cmds.ls("myPoint", fl=1)

#loop through cvs
for i in spiralTubeCvs:
posPoint= cmds.pointPosition(i)
posPivot= cmds.pointPosition(myPoint)
d= distance(posPoint, pivot)
cmds.move (d,d,d,i,r=True)

#store the cvs in a list
mySpiralCvs.append(spiralTubeCvs)

#call the function
moveToLocator()

#####################3
# 4.2) For myNewTubes_0

#define moveToLocator function
def moveToLocator():
# A) get all the cvsNames of myTubes
newTubeCvs = cmds.ls("myNewTubes_0.cv[:]", fl=1)

#create an empty list to store the points
myNewTubeCvs=[]

#get selected Locator
#myPoint= cmds.filterExpand(sm=22)
myPointPos= cmds.ls("myPoint", fl=1)

#loop through cvs
for i in newTubeCvs:
posPoint= cmds.pointPosition(i)
posPivot= cmds.pointPosition(myPoint)
d= distance(posPoint, pivot)
cmds.move (d,d,d,i,r=True)

#store the cvs in a list
myNewTubeCvs.append(newTubeCvs)

#call the function
moveToLocator()

###################
#4.3) For myNewTubes_1
#define moveToLocator function
def moveToLocator():
# A) get all the cvsNames of myTubes
newTube1Cvs = cmds.ls("myNewTubes_1.cv[:]", fl=1)

#create an empty list to store the points
myNewTube1Cvs=[]

#get selected Locator
#myPoint= cmds.filterExpand(sm=22)
myPointPos= cmds.ls("myPoint", fl=1)

#loop through cvs
for i in newTube1Cvs:
posPoint= cmds.pointPosition(i)
posPivot= cmds.pointPosition(myPoint)
d= distance(posPoint, pivot)
cmds.move (d,d,d,i,r=True)

#store the cvs in a list
myNewTube1Cvs.append(newTube1Cvs)

#call the function
moveToLocator()

###################
#4.3) For myNewTubes_2

#define moveToLocator function
def moveToLocator():
# A) get all the cvsNames of myTubes
newTube2Cvs = cmds.ls("myNewTubes_2.cv[:]", fl=1)

#create an empty list to store the points
myNewTube2Cvs=[]

#get selected Locator
#myPoint= cmds.filterExpand(sm=22)
myPointPos= cmds.ls("myPoint", fl=1)

#loop through cvs
for i in newTube2Cvs:
posPoint= cmds.pointPosition(i)
posPivot= cmds.pointPosition(myPoint)
d= distance(posPoint, pivot)
print d
cmds.move (d,d,d,i,r=True)

#store the cvs in a list
myNewTube2Cvs.append(newTube2Cvs)

#call the function
moveToLocator()

###############################
# 5) Populate each Tube with cylinders
def cylindersOnSurface(numU, numV):

#5.1)Select objects on stage
SelTubes= cmds.select("spiralTube", "myNewTubes_0", "myNewTubes_1", "myNewTubes_2")
#5.2) get selected objects
tubs= cmds.filterExpand(sm=10)
#5.3) create cylinders on the surface
for srf in tubs:
# first find the maximum number in U and V
U= cmds.getAttr(srf + ".minMaxRangeU")
V= cmds.getAttr(srf + ".minMaxRangeV")
U=U[0]
V=V[0]
rangeU= U[1]-U[0]
stepU= rangeU/numU
rangeV= V[1]-V[0]
stepV= rangeV/numV

#loop in U direction
for i in range (numU):
u = i*stepU
#loop in V direction
for j in range (numV):
v= j*stepV
coord= cmds.pointOnSurface(srf, p=1, u=u, v=v)
cmds.spaceLocator(p=coord)
normal = cmds.pointOnSurface(srf, u=u, v=v, normalizedNormal=1)
#create cilinders perpendicular to the normal of the tube
cmds.cylinder(p=coord, ax=normal, radius=4)
### call the function
cylindersOnSurface(10,10)

# erase the cilinder profile, the initial curve and the locator
cmds.delete("myCurve", "myPoint", "circle")
# 6) Finish!

No comments:

Post a Comment