Python mathutils 模块,Quaternion() 实例源码

我们从Python开源项目中,提取了以下45个代码示例,用于说明如何使用mathutils.Quaternion()

项目:Urho3D-Blender-Mod    作者:Mike3D    | 项目源码 | 文件源码
def __init__(self, tKeyframe):
        # Bit mask of elements present
        self.mask = 0
        # Time position in seconds: float
        self.time = tKeyframe.time
        # Position: Vector((0.0, 0.0, 0.0))
        self.position = tKeyframe.position
        if tKeyframe.position:
            self.mask |= TRACK_POSITION
        # Rotation: Quaternion()
        self.rotation = tKeyframe.rotation
        if tKeyframe.rotation:
            self.mask |= TRACK_ROTATION
        # Scale: Vector((0.0, 0.0, 0.0))
        self.scale = tKeyframe.scale
        if tKeyframe.scale:
            self.mask |= TRACK_SCALE
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def get_rotated_pt(piv_co, ang_diff_rad, rot_dat, mov_co):
    axis_lk = rot_dat.axis_lk
    mov_aligned = mov_co - piv_co
    rot_val = []
    if   axis_lk == '':  # arbitrary axis / spherical rotations
        rot_val = Quaternion(rot_dat.piv_norm, ang_diff_rad)
    elif axis_lk == 'X':
        rot_val = Euler((ang_diff_rad, 0.0, 0.0), 'XYZ')
    elif axis_lk == 'Y':
        rot_val = Euler((0.0, ang_diff_rad, 0.0), 'XYZ')
    elif axis_lk == 'Z':
        rot_val = Euler((0.0, 0.0, ang_diff_rad), 'XYZ')
    mov_aligned.rotate(rot_val)
    return mov_aligned + piv_co


# Takes a ref_pts (ReferencePoints class) argument and modifies its member
# variable lp_ls (lock pt list). The lp_ls variable is assigned a modified list
# of 3D coordinates (if an axis lock was provided), the contents of the
# ref_pts' rp_ls var (if no axis lock was provided), or an empty list (if there
# wasn't enough ref_pts or there was a problem creating the modified list).
# todo : move inside ReferencePoints class ?
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def RBenVe(Object, Dir):
    ObjectV = Object.normalized()
    DirV = Dir.normalized()
    cosTheta = ObjectV.dot(DirV)
    rotationAxis = mathutils.Vector((0.0, 0.0, 0.0))
    if (cosTheta < -1 + 0.001):
        v = mathutils.Vector((0.0, 1.0, 0.0))
        rotationAxis = ObjectV.cross(v)
        rotationAxis = rotationAxis.normalized()
        q = mathutils.Quaternion()
        q.w = 0.0
        q.x = rotationAxis.x
        q.y = rotationAxis.y
        q.z = rotationAxis.z
        return q
    rotationAxis = ObjectV.cross(DirV)
    s = math.sqrt((1.0 + cosTheta) * 2.0)
    invs = 1 / s
    q = mathutils.Quaternion()
    q.w = s * 0.5
    q.x = rotationAxis.x * invs
    q.y = rotationAxis.y * invs
    q.z = rotationAxis.z * invs
    return q
项目:spqrel_tools    作者:LCAS    | 项目源码 | 文件源码
def swap(vec):
    if CONFIG['SWAP_AXIS'] == 'xyz': return vec
    elif CONFIG['SWAP_AXIS'] == 'xzy':
        if len(vec) == 3: return mathutils.Vector( [vec.x, vec.z, vec.y] )
        elif len(vec) == 4: return mathutils.Quaternion( [ vec.w, vec.x, vec.z, vec.y] )
    elif CONFIG['SWAP_AXIS'] == '-xzy':
        if len(vec) == 3: return mathutils.Vector( [-vec.x, vec.z, vec.y] )
        elif len(vec) == 4: return mathutils.Quaternion( [ vec.w, -vec.x, vec.z, vec.y] )
    elif CONFIG['SWAP_AXIS'] == 'xz-y':
        if len(vec) == 3: return mathutils.Vector( [vec.x, vec.z, -vec.y] )
        elif len(vec) == 4: return mathutils.Quaternion( [ vec.w, vec.x, vec.z, -vec.y] )
    elif CONFIG['SWAP_AXIS'] == 'aldeb':
        if len(vec) == 3: return mathutils.Vector( [vec.x, -vec.z, vec.y] )
        elif len(vec) == 4: return mathutils.Quaternion( [ vec.w, vec.x, -vec.z, vec.y] )
    else:
        print( 'unknown swap axis mode', CONFIG['SWAP_AXIS'] )
        assert 0

## Config
项目:bge-logic-nodes-add-on    作者:thepgi    | 项目源码 | 文件源码
def evaluate(self):
        armature = self.get_parameter_value(self.armature)
        bone_name = self.get_parameter_value(self.bone_name)
        if armature is LogicNetworkCell.STATUS_WAITING: return
        if bone_name is LogicNetworkCell.STATUS_WAITING: return
        self._set_ready()
        if none_or_invalid(armature): return
        if not bone_name: return
        channel = None
        if (armature is self._prev_armature) and (bone_name == self._prev_bone):
            channel = self._channel
        else:
            self._prev_armature = armature
            self._prev_bone = bone_name
            self._channel = armature.channels[bone_name]
            channel = self._channel
        if channel.rotation_mode is bge.logic.ROT_MODE_QUAT:
            self._rot[:] = mathutils.Quaternion(channel.rotation_quaternion).to_euler()
        else:
            self._rot[:] = channel.rotation_euler
        self._pos[:] = channel.location
        self._sca[:] = channel.scale
项目:blender-addons    作者:scorpion81    | 项目源码 | 文件源码
def create_step(width, base_level, step_height, num_sides):

        axis = [0,0,-1]
        PI2 = pi * 2
        rad = width / 2

        quat_angles = [(cur_side/num_sides) * PI2 
                            for cur_side in range(num_sides)]

        quaternions = [Quaternion(axis, quat_angle) 
                            for quat_angle in quat_angles]

        init_vectors = [Vector([rad, 0, base_level])] * len(quaternions)

        quat_vector_pairs = list(zip(quaternions, init_vectors))
        vectors = [quaternion * vec for quaternion, vec in quat_vector_pairs]
        bottom_list = [(vec.x, vec.y, vec.z) for vec in vectors]
        top_list = [(vec.x, vec.y, vec.z+step_height) for vec in vectors]
        full_list = bottom_list + top_list
        return full_list
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def onTrackerPosition(self,userdata,data):
        sensor = data["sensor"]

        # Position
        name = "Location " + str(sensor)
        socket = self.outputs.get(name)
        if socket is None:
            socket = self.outputs.new('NodeSocketVectorXYZ', name)
        rawPosition = data["position"]
        socket.default_value = Vector((rawPosition[0], -rawPosition[2], rawPosition[1]))

        # Quaternion
        name = "Rotation " + str(sensor)
        socket = self.outputs.get(name)
        if socket is None:
            socket = self.outputs.new('NodeSocketQuaternion', name)
        rawQuaternion = data["quaternion"]
        socket.default_value = Quaternion((rawQuaternion[3], rawQuaternion[0], -rawQuaternion[2], rawQuaternion[1]))
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def run(self):
        reset = self.getInputValue("Reset")
        if reset:
            self.lastValue = Quaternion((1.0, 0.0, 0.0, 0.0))
            self.outputs["Quaternion"].default_value = Quaternion((1.0, 0.0, 0.0, 0.0))

        accumulate = self.getInputValue("Accumulate")
        if accumulate and not self.accumulate:
            self.lastValue = self.getInputValue("Quaternion").inverted()
            self.outputs["Accumulating"].default_value = True
            self.accumulate = True

        elif not accumulate and self.accumulate:
            self.outputs["Accumulating"].default_value = False
            self.accumulate = False

        elif accumulate:
            value = self.getInputValue("Quaternion") # No need to copy we're not using directly

            self.outputs["Quaternion"].default_value *= self.lastValue * value
            self.lastValue = value.inverted()
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def getRotatedPoint(PivC,angleDiffRad,rotDat,movCo):
    axisLk = rotDat.axisLk
    vecTmp = movCo - PivC
    rotVal = []
    if   axisLk == '': # arbitrary axis / spherical rotations
        rotVal = Quaternion(rotDat.pivNorm, angleDiffRad)
    elif axisLk == 'X':
        rotVal = Euler((angleDiffRad,0.0,0.0), 'XYZ')
    elif axisLk == 'Y':
        rotVal = Euler((0.0,angleDiffRad,0.0), 'XYZ')
    elif axisLk == 'Z':
        rotVal = Euler((0.0,0.0,angleDiffRad), 'XYZ')
    vecTmp.rotate(rotVal)
    return vecTmp + PivC


# Finds out whether rotDat.newAngR or negative rotDat.newAngR will
# result in desired rotation angle.
# angleEq_0_180 for 0 and 180 degree starting rotations
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def RBenVe(Object, Dir):
    ObjectV = Object.normalized()
    DirV = Dir.normalized()
    cosTheta = ObjectV.dot(DirV)
    rotationAxis = mathutils.Vector((0.0, 0.0, 0.0))
    if (cosTheta < -1 + 0.001):
        v = mathutils.Vector((0.0, 1.0, 0.0))
        rotationAxis = ObjectV.cross(v)
        rotationAxis = rotationAxis.normalized()
        q = mathutils.Quaternion()
        q.w = 0.0
        q.x = rotationAxis.x
        q.y = rotationAxis.y
        q.z = rotationAxis.z
        return q
    rotationAxis = ObjectV.cross(DirV)
    s = math.sqrt((1.0 + cosTheta) * 2.0)
    invs = 1 / s
    q = mathutils.Quaternion()
    q.w = s * 0.5
    q.x = rotationAxis.x * invs
    q.y = rotationAxis.y * invs
    q.z = rotationAxis.z * invs
    return q
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def getRotatedPoint(PivC,angleDiffRad,rotDat,movCo):
    axisLk = rotDat.axisLk
    vecTmp = movCo - PivC
    rotVal = []
    if   axisLk == '': # arbitrary axis / spherical rotations
        rotVal = Quaternion(rotDat.pivNorm, angleDiffRad)
    elif axisLk == 'X':
        rotVal = Euler((angleDiffRad,0.0,0.0), 'XYZ')
    elif axisLk == 'Y':
        rotVal = Euler((0.0,angleDiffRad,0.0), 'XYZ')
    elif axisLk == 'Z':
        rotVal = Euler((0.0,0.0,angleDiffRad), 'XYZ')
    vecTmp.rotate(rotVal)
    return vecTmp + PivC


# Finds out whether rotDat.newAngR or negative rotDat.newAngR will
# result in desired rotation angle.
# angleEq_0_180 for 0 and 180 degree starting rotations
项目:ToolPlus    作者:mkbreuer    | 项目源码 | 文件源码
def RBenVe(Object, Dir):
    ObjectV = Object.normalized()
    DirV = Dir.normalized()
    cosTheta = ObjectV.dot(DirV)
    rotationAxis = mathutils.Vector((0.0, 0.0, 0.0))
    if (cosTheta < -1 + 0.001):
        v = mathutils.Vector((0.0, 1.0, 0.0))
        rotationAxis = ObjectV.cross(v)
        rotationAxis = rotationAxis.normalized()
        q = mathutils.Quaternion()
        q.w = 0.0
        q.x = rotationAxis.x
        q.y = rotationAxis.y
        q.z = rotationAxis.z
        return q
    rotationAxis = ObjectV.cross(DirV)
    s = math.sqrt((1.0 + cosTheta) * 2.0)
    invs = 1 / s
    q = mathutils.Quaternion()
    q.w = s * 0.5
    q.x = rotationAxis.x * invs
    q.y = rotationAxis.y * invs
    q.z = rotationAxis.z * invs
    return q
项目:Blender-AnimeHairSupporter    作者:saidenka    | 项目源码 | 文件源码
def relocation_taper_and_bevel(main_ob, sub_ob, is_taper):
    # ????
    if len(main_ob.data.splines):
        if len(main_ob.data.splines[0].points):
            end_co = main_ob.matrix_world * mathutils.Vector(main_ob.data.splines[0].points[-1].co[:3])
            sub_ob.location = end_co.copy()

    # ????
    if len(main_ob.data.splines):
        spline = main_ob.data.splines[0]
        if 2 <= len(spline.points):
            # ?????????
            sub_ob.rotation_mode = 'QUATERNION'
            last_direction = main_ob.matrix_world * mathutils.Vector(spline.points[-2].co[:3]) - main_ob.matrix_world * mathutils.Vector(spline.points[-1].co[:3])
            up_direction = mathutils.Vector((0, 0, 1))
            sub_ob.rotation_quaternion = up_direction.rotation_difference(last_direction)
            # Z??
            diff_co = main_ob.matrix_world * mathutils.Vector(spline.points[-1].co[:3]) - main_ob.matrix_world * mathutils.Vector(spline.points[0].co[:3])
            rotation_z = math.atan2(diff_co.y, diff_co.x) - spline.points[-1].tilt
            if is_taper: sub_ob.rotation_quaternion *= mathutils.Quaternion((0, 0, 1), rotation_z)
            else       : sub_ob.rotation_quaternion *= mathutils.Quaternion((0, 0, 1), rotation_z - math.radians(90))
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def set_view_front(self,context):
        for area in context.screen.areas:
            if area.type == "VIEW_3D":
                for space in area.spaces:
                    if space.type == "VIEW_3D":
                        region = space.region_3d
                        region.view_rotation = Quaternion((0.7071,0.7071,-0.0,-0.0))
项目:coa_tools    作者:ndee85    | 项目源码 | 文件源码
def lock_view(screen,lock):
    for area in screen.areas:
        if area.type == "VIEW_3D":
            for space in area.spaces:
                if space.type == "VIEW_3D":
                    region = space.region_3d
                    if lock:
                        region.view_rotation = Quaternion((0.7071,0.7071,-0.0,-0.0))                        
                        region.lock_rotation = True
                    else:
                        region.lock_rotation = False
项目:tree-gen    作者:friggog    | 项目源码 | 文件源码
def get_mesh(self, bend, base_shape, index):
        """produce leaf mesh at position of this leaf given base mesh as input"""
        # calculate angles to transform mesh to align with desired direction
        trf = self.direction.to_track_quat('Z', 'Y')
        right_t = self.right.rotated(trf.inverted())
        spin_ang = pi - right_t.angle(Vector([1, 0, 0]))

        # calculate bend transform if needed
        if bend > 0:
            bend_trf_1, bend_trf_2 = self.calc_bend_trf(bend)
        else:
            bend_trf_1 = bend_trf_2 = None

        vertices = []
        for vertex in base_shape[0]:
            # rotate to correct direction
            vertex = vertex.copy()
            vertex.rotate(Quaternion(Vector([0, 0, 1]), spin_ang))
            vertex.rotate(trf)
            # apply bend if needed
            if bend > 0:
                vertex.rotate(bend_trf_1)
                vertex.rotate(bend_trf_2)
            # move to right position
            vertex += self.position
            # add to vertex array
            vertices.append(vertex)
        # set face to refer to vertices at correct offset in big vertex list
        index *= len(vertices)
        faces = deepcopy(base_shape[1])
        for face in faces:
            for ind, elem in enumerate(face):
                face[ind] = elem + index
        return vertices, faces
项目:tree-gen    作者:friggog    | 项目源码 | 文件源码
def calc_bend_trf(self, bend):
        """calculate the transformations required to 'bend' the leaf out/up from WP"""
        normal = self.direction.cross(self.right)
        theta_pos = atan2(self.position.y, self.position.x)
        theta_bend = theta_pos - atan2(normal.y, normal.x)
        bend_trf_1 = Quaternion(Vector([0, 0, 1]), theta_bend * bend)
        self.direction.rotate(bend_trf_1)
        self.right.rotate(bend_trf_1)
        normal = self.direction.cross(self.right)
        phi_bend = normal.declination()
        if phi_bend > pi / 2:
            phi_bend = phi_bend - pi
        bend_trf_2 = Quaternion(self.right, phi_bend * bend)
        return bend_trf_1, bend_trf_2
项目:tree-gen    作者:friggog    | 项目源码 | 文件源码
def turn_right(self, angle):
        """Turn the turtle right about the axis perpendicular to the direction
        it is facing"""
        axis = (self.dir.cross(self.right))
        axis.normalize()
        self.dir.rotate(Quaternion(axis, math.radians(angle)))
        self.dir.normalize()
        self.right.rotate(Quaternion(axis, math.radians(angle)))
        self.right.normalize()
项目:tree-gen    作者:friggog    | 项目源码 | 文件源码
def pitch_up(self, angle):
        """Pitch the turtle up about the right axis"""
        self.dir.rotate(Quaternion(self.right, math.radians(angle)))
        self.dir.normalize()
项目:tree-gen    作者:friggog    | 项目源码 | 文件源码
def roll_right(self, angle):
        """Roll the turtle right about the direction it is facing"""
        self.right.rotate(Quaternion(self.dir, math.radians(angle)))
        self.right.normalize()
项目:tree-gen    作者:friggog    | 项目源码 | 文件源码
def apply_tropism(turtle, tropism_vector):
    """Apply tropism_vector to turtle direction"""
    h_cross_t = turtle.dir.cross(tropism_vector)
    # calc angle to rotate by (from ABoP) multiply to achieve accurate results from WP attractionUp param
    alpha = 10 * h_cross_t.magnitude
    h_cross_t.normalize()
    # rotate by angle about axis perpendicular to turtle direction and tropism vector
    turtle.dir.rotate(Quaternion(h_cross_t, radians(alpha)))
    turtle.dir.normalize()
    turtle.right.rotate(Quaternion(h_cross_t, radians(alpha)))
    turtle.right.normalize()
项目:tree-gen    作者:friggog    | 项目源码 | 文件源码
def add_points_to_bez(self, line, point1, point2, width, trunk=False):
        """add point to specific bezier spline"""
        direction = (point2 - point1)
        handle_f = 0.3

        # if exists get middle point in branch
        if len(line.bezier_points) > 1:
            start_point = line.bezier_points[-1]
            # linearly interpolate branch width between start and end of branch
            start_point.radius = line.bezier_points[-2].radius * 0.5 + width * 0.5
            # add bendiness to branch by rotating direction about random axis by random angle
            if self.bendiness > 0:
                acc_dir = direction.rotated(Quaternion(Vector.random(), radians(self.bendiness * (random() * 35 - 20))))
            else:
                acc_dir = direction
            start_point.handle_right = point1 + handle_f * acc_dir
            start_point.handle_left = point1 - handle_f * acc_dir
        else:
            # scale initial handle to match branch length
            start_point = line.bezier_points[-1]
            if trunk:
                # if trunk we also need to set the start width
                start_point.radius = width
                start_point.handle_right = start_point.co + Vector([0, 0, direction.magnitude * handle_f])
            else:
                start_point.handle_right = start_point.co + (start_point.handle_right -
                                                             start_point.co) * direction.magnitude * handle_f
                start_point.handle_left = start_point.co + (start_point.handle_left -
                                                            start_point.co) * direction.magnitude * handle_f

        # add new point to line and set position, direction and width
        line.bezier_points.add()
        end_point = line.bezier_points[-1]
        end_point.co = point2
        end_point.handle_right = point2 + handle_f * direction
        end_point.handle_left = point2 - handle_f * direction
        end_point.radius = width
项目:io_scene_kcl    作者:Syroot    | 项目源码 | 文件源码
def read_quaternion(self):
        return mathutils.Quaternion((self.read_single(), self.read_single(), self.read_single(), self.read_single()))
项目:Smooth-Normal    作者:dskjal    | 项目源码 | 文件源码
def get_view_rotational_matrix(reverse=False):
    qt = mathutils.Quaternion(bpy.context.scene.ne_view_orientation)
    if reverse:
        qt.conjugate()

    return qt.to_matrix()
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def get_rotation(self):
        return Quaternion()
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def angle_axis_to_quat(angle, axis):
    w = math.cos(angle / 2.0)
    xyz = axis.normalized() * math.sin(angle / 2.0)
    return Quaternion((w, xyz.x, xyz.y, xyz.z))
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def rot(point, axis, angle):
    q = Quaternion(axis, angle)
    P = point.copy()
    P.rotate(q)
    #print(point, P)
    return P
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def extend(p0, p1, p2, loopa, verts):
    # will extend this with a tri centered at p0
    #print('extend')
    #print(p0,p1,p2,[verts[i] for i in loopa])

    # both difference point upward, we extend to the second
    d1 = p1 - p0
    d2 = p0 - p2
    p = (verts[loopa[0]] + verts[loopa[1]] + verts[loopa[2]] + verts[loopa[3]]) / 4
    a = d1.angle(d2, 0)
    if abs(a) < 0.05:
        #print('small angle')
        loopb = vertcopy(loopa, verts, p0 - d2 / 2 - p)
        # all verts in loopb are displaced the same amount so no need to find the minimum distance
        n = 4
        return ([(loopa[(i) % n], loopa[(i + 1) % n], loopb[(i + 1) % n], loopb[(i) % n]) for i in range(n)], loopa, loopb)

    r = d2.cross(d1)
    q = Quaternion(r, -a)
    dverts = [verts[i] - p for i in loopa]
    #print('large angle',dverts,'axis',r)
    for dv in dverts:
        dv.rotate(q)
    #print('rotated',dverts)
    for dv in dverts:
        dv += (p0 - d2 / 2)
    #print('moved',dverts)
    loopb = vertextend(verts, dverts)
    # none of the verts in loopb are rotated so no need to find the minimum distance
    n = 4
    return ([(loopa[(i) % n], loopa[(i + 1) % n], loopb[(i + 1) % n], loopb[(i) % n]) for i in range(n)], loopa, loopb)
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def console_math_data():
    from mathutils import Matrix, Vector, Quaternion, Euler

    data_matrix = {}
    data_quat = {}
    data_euler = {}
    data_vector = {}
    data_vector_array = {}

    for key, var in console_namespace().items():
        if key[0] == "_":
            continue

        state_prop = bpy.context.window_manager.MathVisStatePropList.get(key)
        if state_prop:
            disp, lock = state_prop.state
            if not disp:
                continue

        var_type = type(var)

        if var_type is Matrix:
            if len(var.col) != 4 or len(var.row) != 4:
                if len(var.col) == len(var.row):
                    var = var.to_4x4()
                else:  # todo, support 4x3 matrix
                    continue
            data_matrix[key] = var
        elif var_type is Vector:
            if len(var) < 3:
                var = var.to_3d()
            data_vector[key] = var
        elif var_type is Quaternion:
            data_quat[key] = var
        elif var_type is Euler:
            data_euler[key] = var
        elif var_type in {list, tuple} and is_display_list(var):
            data_vector_array[key] = var

    return data_matrix, data_quat, data_euler, data_vector, data_vector_array
项目:bge-logic-nodes-add-on    作者:thepgi    | 项目源码 | 文件源码
def _rotate(self, ch, xyzrot):
        orientation = self._convert_orientation(ch, xyzrot)
        if ch.rotation_mode is bge.logic.ROT_MODE_QUAT:
            ch.rotation_quaternion = mathutils.Quaternion(ch.rotation_quaternion) * orientation
        else:
            ch.rotation_euler = ch.rotation_euler.rotate(orientation)
项目:Blender_ioEDM    作者:ndevenish    | 项目源码 | 文件源码
def ___repr__(self):
      return "Quaternion({})".format(super(Quaternion, self).__repr__())
项目:Blender_ioEDM    作者:ndevenish    | 项目源码 | 文件源码
def sequence_to_quaternion(seq):
  return Quaternion((seq[3], seq[0], seq[1], seq[2]))
项目:blender-addons    作者:scorpion81    | 项目源码 | 文件源码
def console_math_data():
    from mathutils import Matrix, Vector, Quaternion, Euler

    data_matrix = {}
    data_quat = {}
    data_euler = {}
    data_vector = {}
    data_vector_array = {}

    for key, var in console_namespace().items():
        if key[0] == "_":
            continue

        var_type = type(var)

        if var_type is Matrix:
            if len(var.col) != 4 or len(var.row) != 4:
                if len(var.col) == len(var.row):
                    var = var.to_4x4() 
                else:  # todo, support 4x3 matrix
                    continue
            data_matrix[key] = var
        elif var_type is Vector:
            if len(var) < 3:
                var = var.to_3d()
            data_vector[key] = var
        elif var_type is Quaternion:
            data_quat[key] = var
        elif var_type is Euler:
            data_euler[key] = var
        elif var_type in {list, tuple}:
            if var:
                ok = True
                for item in var:
                    if type(item) is not Vector:
                        ok = False
                        break
                if ok:
                    data_vector_array[key] = var

    return data_matrix, data_quat, data_euler, data_vector, data_vector_array
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def nothing(self, resetDomeValues):
        if resetDomeValues:
            self.outputs["Dome Intersection"].default_value = (0.00, 0.00, 0.00)
            self.outputs["Ray Orientation"].default_value = Quaternion((1.00, 0.00, 0.00, 0.00))

        self.outputs["Result"].default_value = False
        self.outputs["Object"].default_value = ""
        self.outputs["Location"].default_value = (0.00, 0.00, 0.00)
        self.outputs["Normal"].default_value = (0.00, 0.00, 0.00)
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def init(self, context):
        super().init(context)

        self.inputs.new('NodeSocketBool', "Momentary")
        self.inputs.new('NodeSocketBool', "Capture")
        self.inputs.new('NodeSocketQuaternion', "Quaternion")

        self.outputs.new('NodeSocketBool', "Captured")
        self.outputs.new('NodeSocketQuaternion', "Quaternion")
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def run(self):
        capture = self.getInputValue("Capture")

        if capture and not self.captured:
            self.outputs["Quaternion"].default_value = self.getInputValue("Quaternion").copy()
            self.outputs["Captured"].default_value = True
            self.captured = True

        elif not capture and self.captured:
            if self.getInputValue("Momentary"):
                self.outputs["Quaternion"].default_value = Quaternion((1.0, 0.0, 0.0, 0.0))
                self.outputs["Captured"].default_value = False
            self.captured = False
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def init(self, context):
        super().init(context)

        self.inputs.new('NodeSocketQuaternion', "Quaternion A")
        self.inputs.new('NodeSocketQuaternion', "Quaternion B")

        self.outputs.new('NodeSocketQuaternion', "Quaternion")
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def run(self):
        out = None

        # A-Only Operations
        a = self.getInputValue("Quaternion A")

        if self.op == "NORM":
            out = a.normalized()
        elif self.op == "INVERT":
            out = a.inverted()
        else:
            # A - B Operations
            b = self.getInputValue("Quaternion B")

            if self.op == "ADD":
                out = a + b
            elif self.op == "SUB":
                out = a - b
            elif self.op == "MUL":
                out = a * b
            elif self.op == "CROSS":
                out = a.cross(b)
            elif self.op == "DOT":
                out = a.dot(b)
            elif self.op == "ROTATE":
                out = a.copy().rotate(b)
            elif self.op == "ROTATION_DIFFERENCE":
                out = a.rotation_difference(b)

        self.outputs["Quaternion"].default_value = out if out is not None else Quaternion((1.0, 0.0, 0.0, 0.0))
项目:blender2ogre    作者:OGRECave    | 项目源码 | 文件源码
def swap(vec):
    if config.get('SWAP_AXIS') == 'xyz': return vec
    elif config.get('SWAP_AXIS') == 'xzy':
        if len(vec) == 3: return mathutils.Vector( [vec.x, vec.z, vec.y] )
        elif len(vec) == 4: return mathutils.Quaternion( [ vec.w, vec.x, vec.z, vec.y] )
    elif config.get('SWAP_AXIS') == '-xzy':
        if len(vec) == 3: return mathutils.Vector( [-vec.x, vec.z, vec.y] )
        elif len(vec) == 4: return mathutils.Quaternion( [ vec.w, -vec.x, vec.z, vec.y] )
    elif config.get('SWAP_AXIS') == 'xz-y':
        if len(vec) == 3: return mathutils.Vector( [vec.x, vec.z, -vec.y] )
        elif len(vec) == 4: return mathutils.Quaternion( [ vec.w, vec.x, vec.z, -vec.y] )
    else:
        logging.warn( 'unknown swap axis mode %s', config.get('SWAP_AXIS') )
        assert 0
项目:Urho3D-Blender-Mod    作者:Mike3D    | 项目源码 | 文件源码
def __init__(self):
        self.lodUpdatedGeometryIndices = set()
        self.lodDistance = None
        self.doForceElements = False
        self.mergeObjects = False
        self.mergeNotMaterials = False
        self.useLods = False
        self.onlySelected = False
        self.orientation = Quaternion()
        self.scale = 1.0
        self.globalOrigin = True
        self.bonesGlobalOrigin = False  #useless
        self.actionsGlobalOrigin = False
        self.applyModifiers = False
        self.applySettings = 'PREVIEW'
        self.doBones = True
        self.doOnlyKeyedBones = False
        self.doOnlyDeformBones = False
        self.doOnlyVisibleBones = False
        self.actionsByFcurves = False
        self.skinBoneParent = False
        self.derigifyArmature = False
        self.doAnimations = True
        self.doAllActions = True
        self.doUsedActions = False
        self.doSelectedActions = False
        self.doSelectedStrips = False
        self.doSelectedTracks = False
        self.doStrips = False
        self.doTracks = False
        self.doTimeline = False
        self.doTriggers = False
        self.doAnimationZero = True
        self.doAnimationPos = True
        self.doAnimationRot = True
        self.doAnimationSca = True
        self.filterSingleKeyFrames = False
        self.doGeometries = True
        self.doGeometryPos = True
        self.doGeometryNor = True
        self.doGeometryCol = True
        self.doGeometryColAlpha = False
        self.doGeometryUV  = True
        self.doGeometryUV2 = False
        self.doGeometryTan = True
        self.doGeometryWei = True
        self.doMorphs = True
        self.doMorphNor = True
        self.doMorphTan = True
        self.doMorphUV = True
        self.doOptimizeIndices = True
        self.doMaterials = True


#--------------------
# “Computing Tangent Space Basis Vectors for an Arbitrary Mesh” by Lengyel, Eric.
# Terathon Software 3D Graphics Library, 2001.
# http://www.terathon.com/code/tangent.html
#--------------------
项目:bpy_lambda    作者:bcongdon    | 项目源码 | 文件源码
def add_twisted_torus(major_rad, minor_rad, major_seg, minor_seg, twists):
    PI_2 = pi * 2.0
    z_axis = (0.0, 0.0, 1.0)

    verts = []
    faces = []

    edgeloop_prev = []
    for major_index in range(major_seg):
        quat = Quaternion(z_axis, (major_index / major_seg) * PI_2)
        rot_twists = PI_2 * major_index / major_seg * twists

        edgeloop = []

        # Create section ring
        for minor_index in range(minor_seg):
            angle = (PI_2 * minor_index / minor_seg) + rot_twists

            vec = Vector((
                major_rad + (cos(angle) * minor_rad),
                0.0,
                sin(angle) * minor_rad))
            vec = quat * vec

            edgeloop.append(len(verts))
            verts.append(vec)

        # Remember very first edgeloop
        if major_index == 0:
            edgeloop_first = edgeloop

        # Bridge last with current ring
        if edgeloop_prev:
            f = createFaces(edgeloop_prev, edgeloop, closed=True)
            faces.extend(f)

        edgeloop_prev = edgeloop

    # Bridge first and last ring
    f = createFaces(edgeloop_prev, edgeloop_first, closed=True)
    faces.extend(f)

    return verts, faces
项目:blender-addons    作者:scorpion81    | 项目源码 | 文件源码
def loadMhpFile(rig, scn, filepath):
    unit = Matrix()
    for pb in rig.pose.bones:
        pb.matrix_basis = unit

    (pname, ext) = os.path.splitext(filepath)
    mhppath = pname + ".mhp"

    fp = open(mhppath, "rU")
    for line in fp:
        words = line.split()
        if len(words) < 4:
            continue

        try:
            pb = rig.pose.bones[words[0]]
        except KeyError:
            print("Warning: Did not find bone %s" % words[0])
            continue

        if isMuscleBone(pb):
            pass
        elif words[1] == "quat":
            q = Quaternion((float(words[2]), float(words[3]), float(words[4]), float(words[5])))
            mat = q.to_matrix().to_4x4()
            pb.matrix_basis = mat
        elif words[1] == "gquat":
            q = Quaternion((float(words[2]), float(words[3]), float(words[4]), float(words[5])))
            mat = q.to_matrix().to_4x4()
            maty = mat[1].copy()
            matz = mat[2].copy()
            mat[1] = -matz
            mat[2] = maty
            pb.matrix_basis = pb.bone.matrix_local.inverted() * mat
        elif words[1] == "matrix":
            rows = []
            n = 2
            for i in range(4):
                rows.append((float(words[n]), float(words[n+1]), float(words[n+2]), float(words[n+3])))
                n += 4
            mat = Matrix(rows)
            if pb.parent:
                pb.matrix_basis = mat
            else:
                maty = mat[1].copy()
                matz = mat[2].copy()
                mat[1] = -matz
                mat[2] = maty
                pb.matrix_basis = pb.bone.matrix_local.inverted() * mat
        elif words[1] == "scale":
            pass
        else:
            print("WARNING: Unknown line in mcp file:\n%s" % line)
    fp.close()
    print("Mhp file %s loaded" % mhppath)
项目:InSituImmersiveAuthoring    作者:sat-metalab    | 项目源码 | 文件源码
def run(self):
        domeLocation = self.getInputValue("Dome Location")
        domeRadius = self.getInputValue("Dome Radius")
        rayLocation = self.getInputValue("Location")
        rayRotation = self.getInputValue("Rotation")

        rayVector = Vector((0.0, 1.0, 0.0))
        rayVector.rotate(rayRotation)

        # Intersect ray with dome
        rayDomeVec = rayLocation - domeLocation
        a = rayVector.dot(rayVector)
        b = 2 * rayVector.dot(rayDomeVec)
        c = rayDomeVec.dot(rayDomeVec) - (domeRadius * domeRadius)
        result, t0, t1 = self.solveQuadratic(a, b, c)
        if not result:
            return self.nothing(True)

        if t0 < 0:
            # if t0 is negative, let's use t1 instead
            t0 = t1
        if t0 < 0:
            # both t0 and t1 are negative
            return self.nothing(True)

        # Find intersection point with dome
        rayVector.length = t0
        domeRayLocation = rayLocation + rayVector
        self.outputs["Dome Intersection"].default_value = domeRayLocation

        # Find vector/rotation pointing from dome center to that intersection point
        domeLoc = domeRayLocation.normalized()
        dirVector = Vector((0.0, 1.0, 0.0))
        axis = dirVector.cross(domeLoc)
        angle = math.acos(dirVector.dot(domeLoc))
        rayRotation = Quaternion(axis, angle)
        self.outputs["Ray Orientation"].default_value = rayRotation

        rayVector = Vector((0.0, 1.0, 0.0))
        rayVector.rotate(rayRotation)

        result, location, normal, index, object, matrix = bpy.context.scene.ray_cast(domeLocation, rayVector)
        if not result:
            return self.nothing(False)

        self.outputs["Result"].default_value = result
        self.outputs["Object"].default_value = object.name
        self.outputs["Location"].default_value = location
        self.outputs["Normal"].default_value = normal
项目:blender-scripts    作者:vincentgires    | 项目源码 | 文件源码
def execute(self, context):
        node = context.node
        selected_object = context.object

        # get attribute value and type
        data_path = "bpy.data."+node.data_enum + "['"+node.data_item+"']"
        data_path = eval(data_path)
        try:
            attribute = eval("data_path"+"."+node.attribute_property)
        except:
            attribute = None

        if attribute is not None:

            if isinstance(attribute, str):
                node.outputs.new('NodeSocketString', node.attribute_property)

            elif isinstance(attribute, bool):
                node.outputs.new('NodeSocketBool', node.attribute_property)

            elif isinstance(attribute, int):
                node.outputs.new('NodeSocketInt', node.attribute_property)

            elif isinstance(attribute, float):
                node.outputs.new('NodeSocketFloat', node.attribute_property)

            elif isinstance(attribute, mathutils.Color):
                node.outputs.new('NodeSocketColor', node.attribute_property)

            elif isinstance(attribute, mathutils.Vector):
                node.outputs.new('NodeSocketVector', node.attribute_property)

            elif isinstance(attribute, mathutils.Euler):
                node.outputs.new('NodeSocketVector', node.attribute_property)

            elif isinstance(attribute, mathutils.Quaternion):
                node.outputs.new('NodeSocketVector', node.attribute_property)

            elif len(attribute) == 4: # RGBA
                node.outputs.new('NodeSocketColor', node.attribute_property)

        return{'FINISHED'}
项目:blender-scripts    作者:vincentgires    | 项目源码 | 文件源码
def execute(self, context):
        node = context.node
        selected_object = context.object

        # get attribute value and type
        data_path = "bpy.data."+node.data_enum + "['"+node.data_item+"']"
        data_path = eval(data_path)
        try:
            attribute = eval("data_path"+"."+node.attribute_property)
        except:
            attribute = None

        if attribute is not None:

            if isinstance(attribute, str):
                node.inputs.new('NodeSocketString', node.attribute_property)

            elif isinstance(attribute, bool):
                node.inputs.new('NodeSocketBool', node.attribute_property)

            elif isinstance(attribute, int):
                node.inputs.new('NodeSocketInt', node.attribute_property)

            elif isinstance(attribute, float):
                node.inputs.new('NodeSocketFloat', node.attribute_property)

            elif isinstance(attribute, mathutils.Color):
                node.inputs.new('NodeSocketColor', node.attribute_property)

            elif isinstance(attribute, mathutils.Vector):
                node.inputs.new('NodeSocketVector', node.attribute_property)

            elif isinstance(attribute, mathutils.Euler):
                node.inputs.new('NodeSocketVector', node.attribute_property)

            elif isinstance(attribute, mathutils.Quaternion):
                node.inputs.new('NodeSocketVector', node.attribute_property)

            elif len(attribute) == 4: # RGBA
                node.inputs.new('NodeSocketColor', node.attribute_property)

        return{'FINISHED'}