Python has no built-in vector math calls :( You can see my notes on this here. In Maya however, you have a few solutions to continue to do vector math:
Method A: Write your own wrappers around the mel vector commands:
Method B: Call to
Method C: Write your own Python vector module, or get someone else's:
Method D: Get
http://numpy.scipy.org
You'll need to make sure that the lib is installed where Maya can see it, like here:
Method A: Write your own wrappers around the mel vector commands:
import maya.mel as mm def mvCross(vec1, vec2): mVec1 = vec1.__str__().replace("[", "<<").replace("]",">>") mVec2 = vec2.__str__().replace("[", "<<").replace("]",">>") res = mm.eval("cross(" + mVec1 + ", " + mVec2 + ")") return list(res) vec1 = [0,1,0] vec2 = [1,0,0] cross = mvCross(vec1,vec2) print cross # [0.0, 0.0, -1.0]
Method B: Call to
maya.OpenMaya
, and use API MVector
(docs) goodness:import maya.OpenMaya as om vec1 = om.MVector(0,1,0) vec2 = om.MVector(1,0,0) vec3 = vec1^vec2 # calculate cross product print vec3.x, vec3.y, vec3.z # 0.0 0.0 -1.0
*
operator: The dot product of twoMVector
s.^
operator: The cross product of twoMVector
s.
Method C: Write your own Python vector module, or get someone else's:
- I document the math behind some of the basic vector functions here
- Vector : A fairly robust looking 2d vector solution.
- The 2d and 3d vector classes in the gameobjects package (over at Google Code).
- PyGame have contributed Vec2d and Vec3d classes.
Method D: Get
NumPy
http://numpy.scipy.org
You'll need to make sure that the lib is installed where Maya can see it, like here:
C:\Program Files\Autodesk\Maya<version>\Python\lib\site-packagesExample:
import numpy v1 = (1,2,3) v2 = (3,2,1) v3 = numpy.cross (u, v)