# Saturday, May 19, 2012

Prior to starting any of these lessons be sure to import the PyMEL library. See here for why and how.

Every now and again, we'll have to do some sort of complicated mathematical computation. Since I often fell asleep during math class, I'm happy to announce that PyMEL ships with a wrap of Maya's powerful math library. Which works out great for us because after all, who wants to reinvent the wheel? Remember, a good programmer is a lazy programmer.

The docs show us the functions and the Classes of PyMel's datatypes module. To access them, we need to invoke the datatypes library. For example:

datatypes.pi # Result: 3.1415926535897931 # dt.pi # Result: 3.1415926535897931 #

Both lines of code are identical. So I'll use the shorter version because, again, I don't like to work very hard.

Now the functions section contains a host of mathematical functions. Most of them look like throwbacks from high school geometry. Let's see them in action.

dt.sin(0) # Result: 0.0 # dt.cos(0) # Result: 1.0 # dt.cos(dt.pi) # Result: -1.0 # dt.sin(dt.pi/2) # Result: 1.0 # dt.floor(6.7) # Result: 6.0 # dt.ceil(6.7) # Result: 7.0 # dt.round(6.7) # Result: 7.0 # dt.round(6.4) # Result: 6.0 #

Notice that the trig functions work with radian values. That is, values that go from 0 to 2*pi. The nature of radians allow them to work naturally with rotations and cycles. But as animators, we're more familiar with working with degrees. So if you want to work with degrees, you have to do something like the following to convert from degrees to radians:

radVal = dt.radians(90) #convert from degrees to radians dt.cos(radVal) # Result: 0.0 #

There are a lot of functions. And I suggest that you explore them all if you can find the time to do so. Don't worry, they won't bite!

The bottom of the docs for the datatypes module contains all the classes. The main ones we use are Vector and Color

Vector Example:

vectorA = dt.Vector(0,0,0) vectorB = dt.Vector(10,10,10) distanceVector = vectorA.distanceTo(vectorB) # Result: 17.320508075688775 #

For a more info on the Vector class, check out the variable tutorial.

The Color class comes in handy when you need to store and manipulate color

myColor = dt.Color(1.0, 0.8, 0.5, 1.0) #creates a new Color with r=1.0, g=0.8, b=0.5, a=1.0 print myColor.g #prints 0.8

The Color class also has useful methods for converting back and forth between RGB and HSV.

myRGBColor = dt.Color(1.0, 0.8, 0.5, 1.0) #creates a new Color with r=1.0, g=0.8, b=0.5, a=1.0 print myRGBColor myHSVColorConversion = dt.Color.rgbtohsv(myRGBColor) #convert RGB color to HSV print myHSVColor myRGBColorConversion = dt.Color.hsvtorgb(myHSVColor) #convert HSV color to RGB print myRGBColorConversion

There you have it. PyMEL's math library. Don't leave home without it.

My comment system is powered by Disqus. And they require you to put in a name and an email. But I'd love to hear what you think. So if you want to comment anonymously, just put any name and