Nov 07

The last time I spoke about how to setup a MMatrix and use it with python. By default there are no vector classes that are part of python. This is not a problem since you can use the Maya API.

The MVector is actualy much easier to work with. You do not need to use the MScriptUtil class with MVector’s you can just simply give it a default value and start working with the new object. For example:

# Import the general maya API classes
import maya.OpenMaya as OpenMaya

# Create two MVectors
vectorA = OpenMaya.MVector(1.0, 1.0, 1.0)
vectorB = OpenMaya.MVector(2.0, 2.0, 2.0)

# Do some operations on them
vectorC = (vectorA + vectorB)
vectorD = (vectorA - vectorB)

# vectorC after adding vectorA and vectorB
print vectorC.x
print vectorC.y
print vectorC.z

# vectorD after subtracting vectorA and vectorB
print vectorD.x
print vectorD.y
print vectorD.z

# get the length of vectorC
print vectorC.length()

# normalize vectorC
vectorC.normalize()

# print the new normalized values
print vectorC.x
print vectorC.y
print vectorC.z

Note that if you want more information on the MVector class you can look it up in the Maya help documents.

Hopefully that helps!
-RyanT

One Response to “Vectors in Maya Python”

  1. Travis Says:

    This is just what I have been needing. Thanks!

Leave a Reply