Python maya.OpenMaya.MQuaternion() Examples

The following are 9 code examples of maya.OpenMaya.MQuaternion(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module maya.OpenMaya , or try the search function .
Example #1
Source File: spore_sampler.py    From spore with MIT License 6 votes vote down vote up
def get_rotation(self, direction, weight, min_rot, max_rot):
        """ get rotation from a matrix pointing towards the given direction
        slerped by the given weight into the world up vector and added a random
        rotation between min and max rotation """

        r_x = math.radians(random.uniform(min_rot[0], max_rot[0]))
        r_y = math.radians(random.uniform(min_rot[1], max_rot[1]))
        r_z = math.radians(random.uniform(min_rot[2], max_rot[2]))
        util = om.MScriptUtil()
        util.createFromDouble(r_x, r_y, r_z)
        rotation_ptr = util.asDoublePtr()

        matrix = om.MTransformationMatrix()
        matrix.setRotation(rotation_ptr, om.MTransformationMatrix.kXYZ)
        world_up = om.MVector(0, 1, 0)
        rotation = om.MQuaternion(world_up, direction, weight)
        matrix = matrix.asMatrix() * rotation.asMatrix()
        rotation = om.MTransformationMatrix(matrix).rotation().asEulerRotation()

        return om.MVector(math.degrees(rotation.x),
                          math.degrees(rotation.y),
                          math.degrees(rotation.z)) 
Example #2
Source File: glTFExport.py    From maya-glTF with MIT License 6 votes vote down vote up
def _get_rotation_quaternion(self):
        obj=OpenMaya.MObject()
        #make a object of type MSelectionList
        sel_list=OpenMaya.MSelectionList()
        #add something to it
        #you could retrieve this from function or the user selection
        sel_list.add(self.maya_node)
        #fill in the MObject
        sel_list.getDependNode(0,obj)
        #check if its a transform
        if (obj.hasFn(OpenMaya.MFn.kTransform)):
            quat = OpenMaya.MQuaternion()
            #then we can add it to transfrom Fn
            #Fn is basically the collection of functions for given objects
            xform=OpenMaya.MFnTransform(obj)
            xform.getRotation(quat)
            # glTF requires normalize quat
            quat.normalizeIt()
        
        py_quat = [quat[x] for x in range(4)]
        return py_quat 
Example #3
Source File: oyTrajectoryDrawer.py    From anima with MIT License 6 votes vote down vote up
def getCircleData(self, origin, normal, tangent, scale ):
        """creates the circleData
        """
        quat = OpenMaya.MQuaternion()
        # create the circle data
        circleData = [] * 0
        
        for i in range(self.circleDivision):
            quat.setAxisAngle( normal.normal() , float(i) * self.unitAngle )
            pointOnCircle = tangent.rotateBy( quat )
            
            circleData.append( [pointOnCircle.x * scale + origin.x, pointOnCircle.y * scale + origin.y, pointOnCircle.z * scale + origin.z] )
        
        # close the circle
        # append the first to last
        circleData.append( circleData[0] )
        
        return circleData 
Example #4
Source File: spore_context.py    From spore with MIT License 5 votes vote down vote up
def rotate_into(self, direction, rotation):
        """ slerp the given rotation values into the direction given
        by the brush_state
        @param direction MVector: the target direction
        @param rotation MVector: current euler rotation """

        vector_weight = self.brush_state.settings['strength']
        up_vector = om.MVector(0, 1, 0)
        local_up = up_vector.rotateBy(om.MEulerRotation(math.radians(rotation.x),
                                                        math.radians(rotation.y),
                                                        math.radians(rotation.z)))

        target_rotation = om.MQuaternion(local_up, direction, vector_weight)

        util = om.MScriptUtil()
        x_rot = np.radians(rotation.x)
        y_rot = np.radians(rotation.y)
        z_rot = np.radians(rotation.z)
        util.createFromDouble(x_rot, y_rot, z_rot)
        rotation_ptr = util.asDoublePtr()
        mat = om.MTransformationMatrix()
        mat.setRotation(rotation_ptr, om.MTransformationMatrix.kXYZ)

        mat = mat.asMatrix() * target_rotation.asMatrix()
        rotation = om.MTransformationMatrix(mat).rotation()

        return om.MVector(math.degrees(rotation.asEulerRotation().x),
                        math.degrees(rotation.asEulerRotation().y),
                        math.degrees(rotation.asEulerRotation().z)) 
Example #5
Source File: brush_utils.py    From spore with MIT License 5 votes vote down vote up
def get_rotation(initial_rotation, dir_vector, vector_weight):
    """ Get euler rotation values based on given min/max rotation, a direction
    vector and a weight for the given vector. slerp between direction vector and
    world up vector.
    :param min_rotation tuple(x,y,z): minimum rotation values
    :param max_rotation tuple(x,y,z): maximum rotation values
    :param dir_vector MVector: direction of instance y-up
    :param weight float(0-1): the weigth of the direction
    :return MVector: Mvector containing euler rotation values """

    world_up = om.MVector(0, 1, 0)
    rotation = om.MQuaternion(world_up, dir_vector, vector_weight)

    # get random rotation
    #  r_x = math.radians(random.uniform(min_rotation[0], max_rotation[0]))
    #  r_y = math.radians(random.uniform(min_rotation[1], max_rotation[1]))
    #  r_z = math.radians(random.uniform(min_rotation[2], max_rotation[2]))

    mat = om.MTransformationMatrix()

    util = om.MScriptUtil()
    util.createFromDouble(initial_rotation[0], initial_rotation[1], initial_rotation[2])
    rotation_ptr = util.asDoublePtr()
    mat.setRotation(rotation_ptr, om.MTransformationMatrix.kXYZ)

    mat = mat.asMatrix() * rotation.asMatrix()
    rotation = om.MTransformationMatrix(mat).rotation()

    return om.MVector(math.degrees(rotation.asEulerRotation().x),
                      math.degrees(rotation.asEulerRotation().y),
                      math.degrees(rotation.asEulerRotation().z)) 
Example #6
Source File: vector.py    From mgear_core with MIT License 5 votes vote down vote up
def rotateAlongAxis(v, axis, a):
    """Rotate a vector around a given axis defined by other vector.

    Arguments:
        v (vector): The vector to rotate.
        axis (vector): The axis to rotate around.
        a (float): The rotation angle in radians.

    """
    sa = math.sin(a / 2.0)
    ca = math.cos(a / 2.0)

    q1 = OpenMaya.MQuaternion(v.x, v.y, v.z, 0)
    q2 = OpenMaya.MQuaternion(axis.x * sa, axis.y * sa, axis.z * sa, ca)
    q2n = OpenMaya.MQuaternion(-axis.x * sa, -axis.y * sa, -axis.z * sa, ca)
    q = q2 * q1
    q *= q2n

    out = OpenMaya.MVector(q.x, q.y, q.z)

    return out


##########################################################
# CLASS
########################################################## 
Example #7
Source File: spore_context.py    From spore with MIT License 4 votes vote down vote up
def get_rotation(self, flag, normal, index=0):
        """ generate new rotation values based on the brush state
        if we are in drag mode we maintain old rotation values and adjust
        rotation to the new normal. we can use the index arg to set a
        specific index for the last placed objects """

        dir_vector = self.get_alignment(normal)
        vector_weight = self.brush_state.settings['strength']
        world_up = om.MVector(0, 1, 0)
        rotation = om.MQuaternion(world_up, dir_vector, vector_weight)

        # when we in drag mode we want to maintain old rotation values
        if self.brush_state.shift_mod and flag != SporeToolCmd.k_click:
            initial_rotation = self.initial_rotation[index]

        # otherwise we generate new values
        else:
            # get random rotation
            min_rotation = self.brush_state.settings['min_rot']
            max_rotation = self.brush_state.settings['max_rot']
            r_x = math.radians(random.uniform(min_rotation[0], max_rotation[0]))
            r_y = math.radians(random.uniform(min_rotation[1], max_rotation[1]))
            r_z = math.radians(random.uniform(min_rotation[2], max_rotation[2]))
            self.initial_rotation.set(om.MVector(r_x, r_y, r_z), index)
            initial_rotation = self.initial_rotation[index]
            #  rotation = brush_utils.get_rotation(self.initial_rotation, direction,

        mat = om.MTransformationMatrix()

        util = om.MScriptUtil()
        util.createFromDouble(initial_rotation.x,
                              initial_rotation.y,
                              initial_rotation.z)
        rotation_ptr = util.asDoublePtr()
        mat.setRotation(rotation_ptr, om.MTransformationMatrix.kXYZ)

        mat = mat.asMatrix() * rotation.asMatrix()
        rotation = om.MTransformationMatrix(mat).rotation()

        return om.MVector(math.degrees(rotation.asEulerRotation().x),
                        math.degrees(rotation.asEulerRotation().y),
                        math.degrees(rotation.asEulerRotation().z)) 
Example #8
Source File: brush_state.py    From spore with MIT License 4 votes vote down vote up
def create_brush_shape(self):

        if self.draw:
            # fetch point and normal
            pnt = om.MPoint(self.position[0],
                            self.position[1],
                            self.position[2])
            nrm = om.MVector(self.normal[0],
                            self.normal[1],
                            self.normal[2])
            tan = om.MVector(self.tangent[0],
                            self.tangent[1],
                            self.tangent[2])

            # draw dragger shapes
            if self.shift_mod:
                pos_x, pos_y = self.world_to_view(pnt)

                shapes = []
                shapes.append([(pos_x - 15, pos_y - 15), (pos_x + 15, pos_y + 15)])
                shapes.append([(pos_x - 15, pos_y + 15), (pos_x + 15, pos_y - 15)])
                return shapes


            else:
                # get point at normal and tangent
                #  n_pnt = pnt + (nrm * self._state.radius * 0.75)
                #  t_str = pnt + (tan * self._state.radius * 0.75)
                #  t_end = pnt + (tan * self._state.radius)

                # get circle points
                theta = math.radians(360 / 20)
                shape = []
                for i in xrange(20 + 1):
                    rot = om.MQuaternion(theta * i, nrm)
                    rtan = tan.rotateBy(rot)
                    pos = pnt + (rtan * self._radius)

                    pos_x, pos_y = self.world_to_view(pos)
                    shape.append((pos_x, pos_y))

                return [shape] 
Example #9
Source File: swingtwist.py    From cmt with MIT License 4 votes vote down vote up
def slerp(qa, qb, t):
    """Calculates the quaternion slerp between two quaternions.

    From: http://www.euclideanspace.com/maths/algebra/realNormedAlgebra/quaternions/slerp/index.htm

    :param qa: Start MQuaternion.
    :param qb: End MQuaternion.
    :param t: Parameter between 0.0 and 1.0
    :return: An MQuaternion interpolated between qa and qb.
    """
    qm = OpenMaya.MQuaternion()

    # Calculate angle between them.
    cos_half_theta = qa.w * qb.w + qa.x * qb.x + qa.y * qb.y + qa.z * qb.z
    # if qa == qb or qa == -qb then theta = 0 and we can return qa
    if abs(cos_half_theta) >= 1.0:
        qm.w = qa.w
        qm.x = qa.x
        qm.y = qa.y
        qm.z = qa.z
        return qa

    # Calculate temporary values
    half_theta = math.acos(cos_half_theta)
    sin_half_theta = math.sqrt(1.0 - cos_half_theta * cos_half_theta)
    # if theta = 180 degrees then result is not fully defined
    # we could rotate around any axis normal to qa or qb
    if math.fabs(sin_half_theta) < 0.001:
        qm.w = qa.w * 0.5 + qb.w * 0.5
        qm.x = qa.x * 0.5 + qb.x * 0.5
        qm.y = qa.y * 0.5 + qb.y * 0.5
        qm.z = qa.z * 0.5 + qb.z * 0.5
        return qm

    ratio_a = math.sin((1 - t) * half_theta) / sin_half_theta
    ratio_b = math.sin(t * half_theta) / sin_half_theta
    # Calculate quaternion
    qm.w = qa.w * ratio_a + qb.w * ratio_b
    qm.x = qa.x * ratio_a + qb.x * ratio_b
    qm.y = qa.y * ratio_a + qb.y * ratio_b
    qm.z = qa.z * ratio_a + qb.z * ratio_b
    return qm