using om.MVector.rotateBy

905 views
Skip to first unread message

sam williams

unread,
Oct 28, 2014, 2:47:26 PM10/28/14
to python_in...@googlegroups.com
Hello !

im trying to use a rotateBy method of the MVector class. I want to use the one that allows you to rotate a vector based on a given axis and angle. I have an axis and an angle but im really clueless about how to actually write out the code


om.MVector.rotateBy( axis, angle)   but the reference book says i need to use, kXaxis,kYaxisk,Zaxis,kWaxis to specify the axis.


could anyone help me out;)

thanks alot guys, 
Sam :scream:

Pedro Bellini

unread,
Oct 28, 2014, 10:00:12 PM10/28/14
to python_in...@googlegroups.com
Hi,
on the axis,angle function you should use one of MVectors defaults, as the doc says..
so I think what you want would be something like:

import maya.OpenMaya as om

vecX = om.MVector(1,0,0)
print vecX.x

# if we want to rotate the vecX on the Y axis by 180 degrees
rotVec = vecX.rotateBy(om.MVector.kYaxis, 3.1415926)
print rotVec.x

Keep in mind that you can also use Euler and Quaternion data to rotate the vector

sam williams

unread,
Oct 29, 2014, 4:28:36 AM10/29/14
to python_in...@googlegroups.com
thanks alot mate, very useful, 

Sam;)

sam williams

unread,
Oct 29, 2014, 9:20:39 AM10/29/14
to python_in...@googlegroups.com
hey Pedro, as you can only specify x,y and z axis. Is there a way of using a specific vector to rotate around? so if instead of y being (0,1,0) can (-0.12542495647, 0.971342830439, -0.201895235331) be used to represent the y axis to use in the rotateBy method.

thanks, 
Sam

Marcus Ottosson

unread,
Oct 29, 2014, 9:39:43 AM10/29/14
to python_in...@googlegroups.com

Back in the days, I wrote a Point and Matrix implementation for these purposes, maybe it can help you understand the math behind things. Today, I believe PyMEL provides similar functionality out of the box.

Especially see the rotate() method of Point.

Best,
Marcus


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/2cbe8989-10ea-4f60-9ba7-4d17685f5704%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Marcus Ottosson
konstr...@gmail.com

sam williams

unread,
Oct 29, 2014, 3:46:00 PM10/29/14
to python_in...@googlegroups.com

sweet, thanks alot Marcus 
Sam 

Pedro Bellini

unread,
Nov 1, 2014, 4:17:57 PM11/1/14
to python_in...@googlegroups.com
Hi Sam, to answer your last question
if you want a different axis to rotate the vector around I probably would use MQuaternions, like

import maya.OpenMaya as om
from maya import cmds
import math

cube = cmds.polyCube(w=10)[0]
dag = om.MDagPath()
sel = om.MSelectionList()
sel.add(cube)
sel.getDagPath(0,dag)

#
transformFn = om.MFnTransform(dag)
vec1 = om.MVector(0,1,0) # rotation in Y vec
angle = 30
radAngle = math.radians(angle)
quat = om.MQuaternion(radAngle, vec1)
transformFn.setRotation(quat)

vec2=om.MVector(1,1,0)
quat2=om.MQuaternion(radAngle, vec2)
transformFn.setRotation(quat2)
# totates 30 degress about the [1,1,0] vector

vec3 = vec2.rotateBy(quat)
print vec3.x, vec3.y, vec3.z
# vec 3 is the vec2 [1,1,0] rotated 30 degrees in vec1 [0,1,0] axis
# therefore vec3 y value should maitain unchanged while
# vec3 x value becomes cos(30)
# vec3 z value becomes -sin(30)

vec4 = vec1.rotateBy(quat2)
print vec4.x, vec4.y, vec4.z
# vec 4 is the vec1 [0,1,0] rotated 30 degrees in vec2 [1,1,0] axis

## note that if you try to rotate vec1 on quat, or vec2 on quat2 the resulting vector will be unchanged
## since you are rotating a vector 30 degrees on its own axis...
## like:
vec5 =vec1.rotateBy(quat)
print vec5.x, vec5.y, vec5.z
vec6 = vec2.rotateBy(quat2)
print vec6.x, vec6.y, vec6.z

# cheers
Reply all
Reply to author
Forward
0 new messages