Thursday, May 14, 2009

w02 – Plotting point in space

We can’t forget to always write in the beginning of our script:

import maya.cmds as cmds

We then saw how to create a variable to define the coordinates x,y,z of a point, and then use it to create a locator on that position.

# points x,y,z
myPoint = (0,0,0)
cmds.spaceLocator() #this will create a locator in the default position (0,0,0)
cmds.spaceLocator(p=myPoint) #this will create a locator on the position define in myPoint
# change the point values
myPoint = (10,2,0) # keep z=0 to work in 2D only
cmds.spaceLocator(p=myPoint)

Next step was to create several points at once by using loops.

#how to create lots of points
#LOOPS

numPoints = 10 #define the number of desired points

for i in range(0, numPoints, 1):
cmds.spaceLocator()

#the loop above created 10 locator, but all of them on the default position
# I can place the locators in a variable position
# if I use the i variable as one of the position values
for i in range(0, numPoints, 1):
myPoint = (i,0,0)
cmds.spaceLocator(p=myPoint)

#other examples:
for i in range(0, numPoints, 1):
myPoint = (i,i,0)
cmds.spaceLocator(p=myPoint)

for i in range(0, numPoints, 1):
myPoint = (i*10,i+5,0)
cmds.spaceLocator(p=myPoint)

Then we used random functions to generate x,y values. For that, we first need to import the random module as we did for the maya.cmds module.

# import all the functions from the random module
from random import *

numPoints = 20
for i in range(0,numPoints,1):
x = randint(0,10) #this generates a random integer between 0 and 10
y = randint(0,10)
z = 0
myPoint = (x,y,z)
cmds.spaceLocator(p=myPoint)

Then we saw an example of how to plot points in a straight lines with evenly spaced distances:

# how to plot points in a line of a certain length
numPoints = 20
lineLength = 30
#divide to get the space between the points
spaces = lineLength/numPoints
for i in range(0, numPoints, 1):
x = spaces*i
y = 0
z = 0
myPoint = (x,y,z)
cmds.spaceLocator(p=myPoint)

We then saw how to create grids of points with rows and columns, by using nested loops.

## how to create grids of points > rows and columns
## NESTED LOOPS!

numRows = 10
numColumns = 10
for i in range(0, numRows, 1):
x = i
print "row ", i
for j in range(0, numColumns, 1):
print "column ", j
y = j
z = 0
myPoint = (x,y,z)
cmds.spaceLocator(p=myPoint)

Finally, we started to use mathematical functions to plot points, such as the sine and cosine functions. Again, we need to import the math module in order to be able to use mathematical functions in Python. 
#how to use mathematical function to plot the points!
#sine function
from math import *

numPoints = 30
for i in range(0, numPoints, 1):
x = sin(i)
y = i
z = 0
myPoint = (x,y,z)
cmds.spaceLocator(p=myPoint)

#add amplitude

numPoints = 60
amplitude = 2
for i in range(0, numPoints, 1):
x = sin(i)*amplitude
y = i
z = 0
myPoint = (x,y,z)
cmds.spaceLocator(p=myPoint)

### spiral
numPoints = 60
amplitude = 2
for i in range(0, numPoints, 1):
x = sin(i)/i*amplitude
y = cos(i)/i*amplitude
z = 0
cmds.spaceLocator()
cmds.scale(.1,.1,.1)
cmds.move(x,y,z)

No comments:

Post a Comment