I just tried this in Maya 2016 and 2018 and it worked in both, so perhaps this is all moot. import maya.api.OpenMaya as om q = om.MQuaternion() print q p = om.MQuaternion(0.7,0,0,0.7) p.normalizeIt() print p print om.MQuaternion.slerp(p, q, 0.5, 0) prints (0, 0, 0, 1) (0.707107, 0, 0, 0.707107) (0.382683, 0, 0, 0.92388) which is correct.
On Tuesday, 19 September 2017 07:57:55 UTC+10, Michael Boon wrote: > > If your dot is >0 in that code, and there are no mistakes, then you > shouldn't get a flip. If you're seeing a flip, my feeling is that you > should be looking for mistakes rather than worrying about the source of the > quaternions. You could print out some numbers. > - I'd expect you to get a dot < 0.0 from quaternions-from-matrices > sometimes, even though neither matrix can be >180, for example, two > matrices that represent rotations of +100 and -100 degrees, their > difference would be 200. > - f_theta should be half of the angle between your matrices (in radians). > Try some tests with simple rotations and see if it looks correct. It should > always be between 0 and pi/2 (90 degrees). > > You call negateIt() on m_quaternion_2. That changes the quat in-place. So > even outside the function, m_quaternion_2 will be different, which possibly > causes trouble. You could try inverting it differently, perhaps invert > f_scale_2 instead since that will have the same effect. > > Apart from that, I can't see anything wrong with your code. > > > > > On 18 September 2017 at 18:08, Rémi Deletrain <remi.deletr...@gmail.com> > wrote: > >> Ho I'm sorry ... I just see that the code I sent was not the right one >> ... I did some testing and suddenly the code had become anything ... >> >> def slerp_quaternion(m_quaternion_1, m_quaternion_2, f_weight): >> >> """ >> !@Brief Apply Spherical interpolation between two quaternions. >> >> @type m_quaternion_1: OpenMaya.MQuaternion >> @param m_quaternion_1: First Quaternion. >> @type m_quaternion_2: OpenMaya.MQuaternion >> @param m_quaternion_2: Second Quaternion. >> @type f_weight: float >> @param f_weight: Value for blending. >> """ >> >> # Normalize quaternions >> m_quaternion_1 = m_quaternion_1.normal() >> m_quaternion_2 = m_quaternion_2.normal() >> >> # If is equial return first quaternion >> if m_quaternion_1.isEquivalent(m_quaternion_2): >> return m_quaternion_1 >> >> # TODO: fixlater >> # If the inputs are too close for comfort, >> # linearly interpolate and normalize the result. >> # if abs(dot) > 0.9995: >> # pass >> >> # If the dot product is negative, the quaternions >> # have opposite handed-ness and slerp won't take >> # the shorter path. Fix by reversing one quaternion. >> # dot = dot_product(m_quaternion_1, m_quaternion_2) >> dot = dot_product(m_quaternion_1, m_quaternion_2) >> if dot < 0.0: >> m_quaternion_2.negateIt() >> dot *= -1.0 >> >> # Weight Blend >> f_scale_1 = 1.0 - f_weight >> f_scale_2 = f_weight >> >> # Get Quaternion median >> dot = max(min(dot, 1.0), -1.0) >> f_theta = math.acos(dot) >> f_sin_theta = math.sin(f_theta) >> >> f_scale_1 = math.sin(f_scale_1 * f_theta) / f_sin_theta >> f_scale_2 = math.sin(f_scale_2 * f_theta) / f_sin_theta >> >> # New Quaternion >> a_new_values = [] >> for i in xrange(4): >> a_new_values.append(f_scale_1 * m_quaternion_1[i] + f_scale_2 * >> m_quaternion_2[i]) >> >> return OpenMaya.MQuaternion(a_new_values[0], a_new_values[1], >> a_new_values[2], a_new_values[3]) >> >> >> I do not get a better result. I wanted to use the function slerp of maya >> but it is accessible only in c ++ not in python. So I make a slerp function >> in python. The current problem is that as I can not know if my dot is >> positive or negative. So I have a flip of 180 that is done on my rotation. >> I understand better now with your explanation. I use the matrices to >> retrieve quaternions. So I would have to retrieve the quaternions since the >> euler rotations? >> >> -- >> You received this message because you are subscribed to a topic in the >> Google Groups "Python Programming for Autodesk Maya" group. >> To unsubscribe from this topic, visit >> https://groups.google.com/d/topic/python_inside_maya/Ot9WVMEZRYA/unsubscribe >> . >> To unsubscribe from this group and all its topics, send an email to >> python_inside_maya+unsubscr...@googlegroups.com. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/python_inside_maya/54eec6b1-e0f0-42e1-af3b-0ee6d4e69d94%40googlegroups.com >> >> <https://groups.google.com/d/msgid/python_inside_maya/54eec6b1-e0f0-42e1-af3b-0ee6d4e69d94%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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_maya+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/81da49a3-5eca-4415-bbaa-11b9e5dd28b3%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.