Tuesday, June 16, 2009

w06 - recap and scripts

Yesterday we tried to do a kind of workshop in class. It was good, but a bit slower than I imagined. We started by seeing how to access all cvs of curves and nurbs surfaces, and all faces, vertices and edges of polygonal surfaces, and how to start a loop to make modifications with them.

## how to get all CVs of a curve
## this will result in a list containing all the names of the curve CVs
allCVs = cmds.ls("nameOfCurve.cv[:]", fl=1)

## you can also use the same code for a selected curve on stage
## which would make more sense for the flexibility of your script

# this will return you the name of all nurbs curves selected on stage
curve = cmds.filterExpand(sm=9)

#this will return you the names of all CVs from the first selected curve
#or of the only curve in case there is only one
allCVs = cmds.ls(curve[0] + ".cv[:]", fl=1)

## the same can be done with any selected nurbs surface
srf = cmds.filterExpand(sm=10)
allCVs = cmds.ls(srf[0] + ".cv[:][:]", fl=1)

## and with polygons
poly = cmds.filterExpand(sm=12)
# all faces:
allFaces = cmds.ls(poly[0] + ".f[:]", fl=1)
# all vertices:
allVertices = cmds.ls(poly[0] + ".vtx[:]", fl=1)
# all edges:
allEdges = cmds.ls(poly[0] + ".e[:]", fl=1)

## Then, if you want to make modifications on each one
## of those elements, you have to start a loop through the list.

#In the case of all cvs of a curve, for example:
for cv in allCVs:
#here come the code you want to perform with each cv
print cv

# or with faces of a polygon.
for face in allFaces:
#do something...

# keep in mind that the words cv or face in the line above
# are simply names of variables which you define, and which
# represent the cv or face at each iteration of the loop

## another way to do a loop, would be:
numCVs = len(allCVs) #this will return you the amount of elements you have in allCVs list
for i in range(numCVs):
print i #this will iterate through numbers
cv = allCVs[i] #like this you get the name of the CV
#and here you can put your code to make transformations with each cv



## One thing you can do in a loop is to jump in certain steps
## like for example, only perform action every 5th element, for example
## you do that by using the % operator and an if statement
for i in range(numCVs):
if i % 5 == 0:
#whatever is indented here will oly be executed everytime
#the above if statement is equal to true
print i

No comments:

Post a Comment