def slerp_quaternion(m_quaternion_1, m_quaternion_2, f_weight):
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
# 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(lerp(m_quaternion_1[i], m_quaternion_2[i], f_weight))
return pmc.datatypes.Quaternion(a_new_values[0], a_new_values[1], a_new_values[2], a_new_values[3])def quaternion_from_matrix(m_matrix):
w = math.sqrt(max(0, 1 + m_matrix(0, 0) + m_matrix(1, 1) + m_matrix(2, 2))) / 2.0 x = math.sqrt(max(0, 1 + m_matrix(0, 0) - m_matrix(1, 1) - m_matrix(2, 2))) / 2.0 y = math.sqrt(max(0, 1 - m_matrix(0, 0) + m_matrix(1, 1) - m_matrix(2, 2))) / 2.0 z = math.sqrt(max(0, 1 - m_matrix(0, 0) - m_matrix(1, 1) + m_matrix(2, 2))) / 2.0
x *= -1 if x * (m_matrix(2, 1) - m_matrix(1, 2)) >= 0.0 else 1 y *= -1 if y * (m_matrix(0, 2) - m_matrix(2, 0)) >= 0.0 else 1 z *= -1 if z * (m_matrix(1, 0) - m_matrix(0, 1)) >= 0.0 else 1
return OpenMaya.MQuaternion(x, y, z, w)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])--
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+unsub...@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.
For more options, visit https://groups.google.com/d/optout.
import maya.api.OpenMaya as omq = om.MQuaternion()print qp = om.MQuaternion(0.7,0,0,0.7)p.normalizeIt()print pprint om.MQuaternion.slerp(p, q, 0.5, 0)(0, 0, 0, 1)(0.707107, 0, 0, 0.707107)(0.382683, 0, 0, 0.92388)type object 'MQuaternion' has no attribute 'slerp'.
--
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+unsub...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/5e22e14d-d95c-4a6a-b20e-cc2694f69c70%40googlegroups.com.
if dot < 0.0:
m_quaternion_2.negateIt()
dot *= -1.0