def ___repr__(self):
return "Quaternion({})".format(super(Quaternion, self).__repr__())
python类Quaternion()的实例源码
def sequence_to_quaternion(seq):
return Quaternion((seq[3], seq[0], seq[1], seq[2]))
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
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)
CaptureQuaternionNode.py 文件源码
项目:InSituImmersiveAuthoring
作者: sat-metalab
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
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")
CaptureQuaternionNode.py 文件源码
项目:InSituImmersiveAuthoring
作者: sat-metalab
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
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
QuaternionOperationNode.py 文件源码
项目:InSituImmersiveAuthoring
作者: sat-metalab
项目源码
文件源码
阅读 18
收藏 0
点赞 0
评论 0
def init(self, context):
super().init(context)
self.inputs.new('NodeSocketQuaternion', "Quaternion A")
self.inputs.new('NodeSocketQuaternion', "Quaternion B")
self.outputs.new('NodeSocketQuaternion', "Quaternion")
QuaternionOperationNode.py 文件源码
项目:InSituImmersiveAuthoring
作者: sat-metalab
项目源码
文件源码
阅读 20
收藏 0
点赞 0
评论 0
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))
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
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
#--------------------
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
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)
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
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'}
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'}