First we saw how to store the points coordinates generated in a loop into an empty list , and then use this list to generate a curve:
import maya.cmds as cmds
#generating a straight line with even spacing between points
#define initial variables
numPoints = 20.0 #we use floats instead of integers so that the division below works properly
lineLength = 30.0
spacing = lineLength/numPoints #spacing between points
#create an empty list to store my points
#it is empty and will be "populated" in the following loop
points = []
#loop and get point coordinates
for i in range(0, numPoints, 1): #(start, end, increment)
#define x y and z variables
x = spacing*i #this will vary in each loop
y = 0
z = 0
#put all these variables in a list
myPoint = (x,y,z)
print "point ", i, myPoint #shows in the output window the point coordinates which were generated above (just for your feedback)
#append the point to the list (add it to the end of the list)
points.append(myPoint)
print "points list = ", points #for your feedback
#after the loop, I have all points in my "points" list
#now I can use the curve command to create my curve
#by using the points in "points"
cmds.curve(d=1, p=points) #creates a curve of degree 1 > linear curve
cmds.curve(d=3, p=points) #creates a curve of degree 3 > "curved" curve
Then we looked at some functions to make operations on list objects:
#some list functions
#first we create a new list
myList = ["Dessau", "Germany", "europe"]
print myList
#add element to list
myList.append("world")
print myList
#remove element from list
myList.remove("world")
print myList
#insert element in a specified place (specified by the index value)
myList.insert(1, "DIA")
print myList
#remove the last element
myList.pop()
print myList
#sort the elements of the list alphabetically
myList.sort()
print myList
Then we looked quickly at another function to gather point coordinated and generate a curve, which in this case resembles a helix:
#import all functions of the math module
#we will use the function sin() and cos()
from math import *
#define initial variables
numPoints = 60
amplitude = 2
# create an empty list to store the points
points = [ ]
#loop and gather point information
for i in range( 1, numPoints, 1) :
#function for the spiral curve
x = sin( i ) * amplitude
y = cos( i ) * amplitude
z = i / 10
myPoint = (x,y,z)
#store the point in the list
points.append(myPoint)
#create the curve
cmds.curve( d = 3, p = points ) #degree 3
The we started to look at functions. We saw some simple examples of functions with and without arguments, and how to define and call them:
#FUNCTIONS - INTRO
#first we need to define the function
def myFunction( ):
print "Hello world!"
#the code above doesn't do anything apparently
#but it stores the function definition on memory and
#you can use it by calling the function:
myFunction()
#you'll see the command indented under the function
#be executed and printing the text we wrote
#functions get more interesting when we start
#to add arguments to it:
def printMessage( msg ):
print msg
#the definition above means that this function
#takes as an argument the variable "msg"
#and inside the function we use this argument and print it
#to call this function you have to pass the argument in the function call:
printMessage( "this is my message" )
#you can also pass a pre-defined variable as the argument
#and python will do all the replacing and printing the same way:
a = "Another message"
printMessage( a )
Finally, we took the helix code and converted it into a function:
#converting the helix code into a function
#first import the math module if you still did not do so
from math import *
#then start the function definition
def curves( numPoints, amplitude ):
#it takes as an argument the value of the amplitude of the curve
#and the number of points we want in the curve
#then we just repeat the code of the helix
# create an empty list to store the points
points = [ ]
#loop and gather point information
for i in range( 1, numPoints, 1) :
#function for the spiral curve
x = sin( i ) * amplitude
y = cos( i ) * amplitude
z = i / 10
myPoint = (x,y,z)
#store the point in the list
points.append(myPoint)
#now as we saw, every command in Maya returns me a value
#you can see which values each function returns,
#take a look at Help > Python Command Reference
#and we can store this value in a variable to use it later:
myCurve = cmds.curve( d = 3, p = points ) #degree 3
#also in our own functions we can return values
#in this case, we will return the name of the curve created above
return myCurve
In the end, we saw how to use our new function to generate several curves on the fly. By using the arguments, we can vary each curve, and by using the return value, we can make modifications on the curve after its creation:
#function call in a loop
#first we define how many curves we want
numCurves = 10
#then run the loop
for i in range(0, numCurves, 1):
#then inside the loop we call the function
#and store its return value in the variable crv
crv = curves(60, i ) #arguments (numPoints, amplitude)
#as we stored the return value (in our case, name of the curve)
#in the variable crv, we can later use it to
#do anything we want with the curve
#in this case, we'll move it along the x-axis:
cmds.move(i*10, 0, 0, crv)
No comments:
Post a Comment