MQuaternion slerp

501 views
Skip to first unread message

a_adam

unread,
Oct 4, 2011, 12:26:42 PM10/4/11
to python_inside_maya
Hi there,

just joined in here, and I'm looking forward to some good discussions!
And of course I stumbled into a problem today which brought me here in
the first place: I can't for the life of me trigger the slerp function
from the MQuaternion class.

The API docs tell me that there are two slerps (odd enough), one
considering spin and being marked as not exposed to scripting, and a
simple one without a spin argument which should be accessable to
scripting. It is this simpler version which I'm trying to get to work.

I've seen in this group's archive that slerp indeed does not seem to
be not implemented in the Python wrapper. However, that post is over a
year old, and I asked myself if Autodesk did anything for Maya 2012 to
make this work? Or anyone found a workaround in the meantime?

Many thanks in advance for a quick hint, cheers!

-Andre

David Moulder

unread,
Oct 4, 2011, 5:47:17 PM10/4/11
to python_in...@googlegroups.com
I don't know if slerp is correctly exposed but I can tell you that there is a new api binding available in 2012.  Maya API 2.  Its a faster and more friendly python api and the docs suggest that slerp is exposed correctly.




--
David Moulder
http://www.google.com/profiles/squish3d

Chad Vernon

unread,
Oct 4, 2011, 5:50:54 PM10/4/11
to python_in...@googlegroups.com
You could implement slerp function:

import math
def quaternionSlerp(a, b, t):
    cos = quaternionDot(a, b)
    if cos < 0.0:
        cos = quaternionDot(a, b.negateIt())
    theta = math.acos(cos)
    sin = math.sin(theta)

    if sin > 0.001:
        w1 = math.sin((1.0 - t) * theta) / sin
        w2 = math.sin(t * theta) / sin
    else:
        w1 = 1.0 - t
        w2 = t
    aa = OpenMaya.MQuaternion(a)
    bb = OpenMaya.MQuaternion(b)
    aa.scaleIt(w1)
    bb.scaleIt(w2)

    return aa + bb

def quaternionDot(q1, q2):
    return (q1.x * q2.x) + (q1.y * q2.y) + (q1.z * q2.z) + (q1.w * q2.w)

Renato Polimeno

unread,
Oct 4, 2011, 9:44:28 PM10/4/11
to python_in...@googlegroups.com
Hey Chad, that is a beatifull peace of code!

Could you please tell me what are those functions <value>.negateIt() and <value>.scaleIt() ? 
Properties of python numbers I guess ?! Could you point me a doc link ?

Again, thanks for sharing !
______________________________
 
-> Renato Polimeno


André Adam

unread,
Oct 5, 2011, 2:58:07 AM10/5/11
to python_inside_maya
Hi Renato, perhaps I can quickly help out here, negateIt() and
scaleIt() are functions of the MQuaternion class, which you can find
documented in the Maya API:

http://download.autodesk.com/global/docs/mayasdk2012/en_us/index.html

Cheers!

-André
> renatopolimeno.com <http://www.renatopolimeno.com>
>  <http://www.renatopolimeno.com/>facebook.com/renato.polimeno<http://www.facebook.com/renato.polimeno>
> @renatopolimeno <http://www.twitter.com/renatopolimeno>

André Adam

unread,
Oct 5, 2011, 3:01:14 AM10/5/11
to python_inside_maya
Thanks David, I had hoped for something like this, API 2 looks
promising so far! Will try to implement my stuff going this route,
will keep you posted how it goes.

Thanks again!

-André

André Adam

unread,
Oct 5, 2011, 3:04:48 AM10/5/11
to python_inside_maya
Nice piece of code, thanks for sharing. Writing my own slerp would be
my fallback, though, in the end I'm going to call my function per
frame, and therefore would look into having this C++ wrapped and
compiled. But for now, the API 2 access David suggested looks to be
exactly what I have been searching for.

Cheers!

-André
Reply all
Reply to author
Forward
0 new messages