本文整理汇总了Python中mathutils.Matrix类的典型用法代码示例。如果您正苦于以下问题:Python Matrix类的具体用法?Python Matrix怎么用?Python Matrix使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Matrix类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: copyto
def copyto(scene, source_obj, pos, xdir, zdir, axes, scale=None):
"""
copy the source_obj to pos, so its primary axis points in zdir and its
secondary axis points in xdir
"""
copy_obj = source_obj.copy()
scene.objects.link(copy_obj)
xdir = xdir.normalized()
zdir = zdir.normalized()
#rotation first
z_axis = zdir
x_axis = xdir
y_axis = z_axis.cross(x_axis)
#use axes_dict to assign the axis as chosen in panel
A, B, C = axes_dict[axes]
rot_mat = Matrix()
rot_mat[A].xyz = x_axis
rot_mat[B].xyz = y_axis
rot_mat[C].xyz = z_axis
rot_mat.transpose()
#rotate object
copy_obj.matrix_world = rot_mat
#move object into position
copy_obj.location = pos
#scale object
if scale != None:
copy_obj.scale = scale
return copy_obj
示例2: get_coconuts_mesh
def get_coconuts_mesh(context, prefs):
me = context.blend_data.meshes.new('temp_mesh')
bm = bmesh.new()
bm.from_mesh(me)
mat = Matrix()
trunk_length = prefs.palm_stage_length * prefs.palm_stages
coconutX = (0.29, -0.29, 0, 0)
coconutY = (0, 0, 0.29, -0.29)
coconutZ = trunk_length - 0.2
coconuts = get_random(prefs.lp_Tree_Palm_Top_Coconuts_Min,
prefs.lp_Tree_Palm_Top_Coconuts_Max)
for i in range(0, coconuts):
mat.translation = (coconutX[i], coconutY[i], coconutZ)
bmesh.ops.create_icosphere(
bm,
subdivisions=1,
diameter=0.15,
matrix=mat)
bm.to_mesh(me)
return me
示例3: get_bone_matrix
def get_bone_matrix(armature, bone, relative=True):
pose_bone = armature.pose.bones[bone.name]
m = Matrix() ### inverted posebone origin matrix
m.row[0] = [0, 0, 1, 0]
m.row[1] = [1, 0, 0, 0]
m.row[2] = [0, 1, 0, 0]
m.row[3] = [0, 0, 0, 1]
if bone.parent == None:
mat_bone_space = m * pose_bone.matrix.copy()
else:
if relative:
mat_bone_space = pose_bone.parent.matrix.inverted() * pose_bone.matrix
else:
mat_bone_space = m * pose_bone.matrix
#### remap matrix
loc, rot, scale = mat_bone_space.decompose()
if not bone.use_inherit_scale:
scale = (m * pose_bone.matrix).decompose()[2]
loc_mat = Matrix.Translation(loc)
rot_mat = rot.inverted().to_matrix().to_4x4()
scale_mat = Matrix()
scale_mat[0][0] = scale[1]
scale_mat[1][1] = scale[0]
mat_bone_space = loc_mat * rot_mat * scale_mat
return mat_bone_space
示例4: focus_view_on
def focus_view_on(region_3d, location):
r3d = region_3d
a = r3d.view_location.copy()
b = location
mm = r3d.view_matrix.inverted()
vr = mm.to_3x3()
loc = mm.translation
n = (a-loc).cross(b-loc).normalized()
alp = math.acos( max(-1.0,min(1.0, (a-loc).normalized().dot( (b-loc).normalized() ) )))
zero = Vector()
u0,v0,w0 = vr.transposed()
u = rot_on( zero, n, alp, u0 )
v = rot_on( zero, n, alp, v0 )
w = rot_on( zero, n, alp, w0 )
if bpy.context.user_preferences.inputs.view_rotate_method == 'TURNTABLE':
ez = Vector((0,0,1))
u2 = ez.cross(w)
v2 = w.cross(u2)
u,v = u2,v2
vr2 = Matrix((u,v,w)).transposed()
mm2 = vr2.to_4x4()
mm2[0][3] = loc[0]
mm2[1][3] = loc[1]
mm2[2][3] = loc[2]
dist0 = (loc-location).length
r3d.view_distance = dist0
r3d.view_matrix = mm2.inverted()
示例5: parseTree
def parseTree(tree, parentName):
# print("parsetree")
armName = bpy.context.active_object.name
armatures.createBone(armName, tree.name, parentName)
bpy.ops.roboteditor.select_segment(segment_name=tree.name)
# print(tree.name)
boneProp = bpy.context.active_bone.RobotEditor
m = Matrix()
# print(tree.transformations)
for i in tree.transformations:
# We expect a matrix here!
# Todo accept rotation and translations too!
if type(i[0]) is list:
m = m * Matrix(i)
elif len(i) == 3:
# TODO
pass
elif len(i) == 4:
# TODO
pass
else:
raise Exception("ParsingError")
# print(m)
bpy.context.active_bone.RobotEditor.Euler.x.value = m.translation[0] / 1000
bpy.context.active_bone.RobotEditor.Euler.y.value = m.translation[1] / 1000
bpy.context.active_bone.RobotEditor.Euler.z.value = m.translation[2] / 1000
bpy.context.active_bone.RobotEditor.Euler.gamma.value = degrees(m.to_euler().z)
bpy.context.active_bone.RobotEditor.Euler.beta.value = degrees(m.to_euler().y)
bpy.context.active_bone.RobotEditor.Euler.alpha.value = degrees(m.to_euler().x)
if tree.axis_type == 'revolute':
bpy.context.active_bone.RobotEditor.jointMode = 'REVOLUTE'
# boneProp.theta.value = float(tree.initalValue)
bpy.context.active_bone.RobotEditor.theta.max = float(tree.max)
bpy.context.active_bone.RobotEditor.theta.min = float(tree.min)
else:
bpy.context.active_bone.RobotEditor.jointMode = 'PRISMATIC'
# boneProp.d.value = float(tree.initialValue)
bpy.context.active_bone.RobotEditor.d.max = float(tree.max)
bpy.context.active_bone.RobotEditor.d.min = float(tree.min)
if tree.axis is not None:
for i, axis in enumerate(tree.axis):
if axis == -1.0:
bpy.context.active_bone.RobotEditor.axis_revert = True
tree.axis[i] = 1.0
if tree.axis == [1.0, 0.0, 0.0]:
bpy.context.active_bone.RobotEditor.axis = 'X'
elif tree.axis == [0.0, 1.0, 0.0]:
bpy.context.active_bone.RobotEditor.axis = 'Y'
elif tree.axis == [0.0, 0.0, 1.0]:
bpy.context.active_bone.RobotEditor.axis = 'Z'
# print("parsetree done")
for child in tree.children:
parseTree(child, tree.name)
示例6: make_tube
def make_tube(self, mats, verts):
edges_out = []
verts_out = []
faces_out = []
vID = 0
nring = len(verts[0])
# end face
faces_out.append(list(range(nring)))
for i,m in enumerate(mats):
for j,v in enumerate(verts[0]):
vout = Matrix(m) * Vector(v)
verts_out.append(vout.to_tuple())
vID = j + i*nring
# rings
if j != 0:
edges_out.append([vID, vID - 1])
else:
edges_out.append([vID, vID + nring-1])
# lines
if i != 0:
edges_out.append([vID, vID - nring])
# faces
if j != 0:
faces_out.append([vID, vID - nring, vID - nring - 1, vID-1,])
else:
faces_out.append([vID, vID - nring, vID-1, vID + nring-1])
# end face
# reversing list fixes face normal direction keeps mesh manifold
f = list(range(vID, vID-nring, -1))
faces_out.append(f)
return verts_out, edges_out, faces_out
示例7: print_mat
def print_mat(label, matrix, column=4):
if isinstance(matrix[0], (float, int)):
# buffer用
if len(matrix) == 16:
mat = [matrix[:4], matrix[4:8], matrix[8:12], matrix[12:16]]
matrix = Matrix(mat)
elif len(matrix) == 9:
matrix = Matrix([matrix[:3], matrix[3:6], matrix[6:9]])
elif len(matrix) == 4:
matrix = Matrix([matrix[:2], matrix[2:4]])
print(label)
t2 = 'row{0} [{1:>{5}.{6}f}, {2:>{5}.{6}f}]'
t3 = 'row{0} [{1:>{5}.{6}f}, {2:>{5}.{6}f}, {3:>{5}.{6}f}]'
t4 = 'row{0} [{1:>{5}.{6}f}, {2:>{5}.{6}f}, {3:>{5}.{6}f}, {4:>{5}.{6}f}]'
m = matrix.transposed()
for cnt, row in enumerate(m):
if len(row) == 2:
print(t2.format(cnt, row[0], row[1], 0, 0, column + 3, column))
elif len(row) == 3:
print(t3.format(cnt, row[0], row[1], row[2], 0,
column + 3, column))
else:
print(t4.format(cnt, row[0], row[1], row[2], row[3],
column + 3, column))
示例8: fit_cubicbezier
def fit_cubicbezier(l_v, l_t):
#########################################################
# http://nbviewer.ipython.org/gist/anonymous/5688579
# make the summation functions for A (16 of them)
A_fns = [
lambda l_t: sum([2*t**0*(t-1)**6 for t in l_t]),
lambda l_t: sum([-6*t**1*(t-1)**5 for t in l_t]),
lambda l_t: sum([6*t**2*(t-1)**4 for t in l_t]),
lambda l_t: sum([-2*t**3*(t-1)**3 for t in l_t]),
lambda l_t: sum([-6*t**1*(t-1)**5 for t in l_t]),
lambda l_t: sum([18*t**2*(t-1)**4 for t in l_t]),
lambda l_t: sum([-18*t**3*(t-1)**3 for t in l_t]),
lambda l_t: sum([6*t**4*(t-1)**2 for t in l_t]),
lambda l_t: sum([6*t**2*(t-1)**4 for t in l_t]),
lambda l_t: sum([-18*t**3*(t-1)**3 for t in l_t]),
lambda l_t: sum([18*t**4*(t-1)**2 for t in l_t]),
lambda l_t: sum([-6*t**5*(t-1)**1 for t in l_t]),
lambda l_t: sum([-2*t**3*(t-1)**3 for t in l_t]),
lambda l_t: sum([6*t**4*(t-1)**2 for t in l_t]),
lambda l_t: sum([-6*t**5*(t-1)**1 for t in l_t]),
lambda l_t: sum([2*t**6*(t-1)**0 for t in l_t])
]
# make the summation functions for b (4 of them)
b_fns = [
lambda l_t, l_v: sum(v * (-2 * (t**0) * ((t-1)**3))
for t, v in zip(l_t, l_v)),
lambda l_t, l_v: sum(v * (6 * (t**1) * ((t-1)**2))
for t, v in zip(l_t, l_v)),
lambda l_t, l_v: sum(v * (-6 * (t**2) * ((t-1)**1))
for t, v in zip(l_t, l_v)),
lambda l_t, l_v: sum(v * (2 * (t**3) * ((t-1)**0))
for t, v in zip(l_t, l_v)),
]
# compute the data we will put into matrix A
A_values = [fn(l_t) for fn in A_fns]
# fill the A matrix with data
A_matrix = Matrix(tuple(zip(*[iter(A_values)]*4)))
try:
A_inv = A_matrix.inverted()
except:
return (float('inf'), l_v[0], l_v[0], l_v[0], l_v[0])
# compute the data we will put into the b vector
b_values = [fn(l_t, l_v) for fn in b_fns]
# fill the b vector with data
b_vector = Vector(b_values)
# solve for the unknowns in vector x
v0, v1, v2, v3 = A_inv * b_vector
err = compute_cubic_error(v0, v1, v2, v3, l_v, l_t) / len(l_v)
return (err, v0, v1, v2, v3)
示例9: matchPoseReverse
def matchPoseReverse(pb, src):
gmat = src.matrix
tail = gmat.col[3] + src.length * gmat.col[1]
rmat = Matrix((gmat.col[0], -gmat.col[1], -gmat.col[2], tail))
rmat.transpose()
pmat = getPoseMatrix(rmat, pb)
pb.matrix_basis = pmat
insertRotation(pb, pmat)
示例10: _convertMatrixTo4x4
def _convertMatrixTo4x4(self, value):
matrix = Matrix()
matrix[0] = value[0:4]
matrix[1] = value[4:8]
matrix[2] = value[8:12]
matrix[3] = value[12:16]
return matrix.transposed()
示例11: test_matrix_to_3x3
def test_matrix_to_3x3(self):
# mat =
# [ 1 2 3 4 ]
# [ 2 4 6 8 ]
# [ 3 6 9 12 ]
# [ 4 8 12 16 ]
mat = Matrix(tuple((i, 2 * i, 3 * i, 4 * i) for i in range(1, 5)))
mat_correct = Matrix(((1, 2, 3), (2, 4, 6), (3, 6, 9)))
self.assertEqual(mat.to_3x3(), mat_correct)
示例12: _parseVertices
def _parseVertices( self, mesh ):
'''
Extract the vertices from a blender mesh
'''
transform = Matrix().to_4x4()
transform.identity()
if not self.export_config.export_rot:
transform = self.export_config.global_matrix.to_4x4() * self.matrix_world
self.vertices = [transform * v.co for v in mesh.vertices]
示例13: parse_rotation_channel
def parse_rotation_channel(gltf, node, obj, bone, channel, animation):
"""Manage rotation animation."""
blender_path = "pose.bones[" + json.dumps(bone.name) + "].rotation_quaternion"
group_name = bone.name
keys = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].input)
values = BinaryData.get_data_from_accessor(gltf, animation.samplers[channel.sampler].output)
bind_rotation = node.blender_bone_matrix.to_quaternion()
if animation.samplers[channel.sampler].interpolation == "CUBICSPLINE":
# TODO manage tangent?
quat_keyframes = [
quaternion_gltf_to_blender(values[idx * 3 + 1])
for idx in range(0, len(keys))
]
else:
quat_keyframes = [quaternion_gltf_to_blender(vals) for vals in values]
if node.parent is None:
final_rots = [
bind_rotation.inverted() @ quat_keyframe
for quat_keyframe in quat_keyframes
]
else:
if not gltf.data.nodes[node.parent].is_joint:
parent_mat = Matrix()
else:
parent_mat = gltf.data.nodes[node.parent].blender_bone_matrix
if parent_mat != parent_mat.inverted():
final_rots = [
bind_rotation.rotation_difference(
(parent_mat @ quat_keyframe.to_matrix().to_4x4()).to_quaternion()
)
for quat_keyframe in quat_keyframes
]
else:
final_rots = [
bind_rotation.rotation_difference(quat_keyframe)
for quat_keyframe in quat_keyframes
]
# Manage antipodal quaternions
for i in range(1, len(final_rots)):
if final_rots[i].dot(final_rots[i-1]) < 0:
final_rots[i] = -final_rots[i]
BlenderBoneAnim.fill_fcurves(
obj.animation_data.action,
keys,
final_rots,
group_name,
blender_path,
animation.samplers[channel.sampler].interpolation
)
示例14: sparse
def sparse(values):
"""
constructs a sparse matrix from a list of tuples (col, row, value)
"""
result = Matrix()
result.zero()
result[W][W] = 1
for cell in values:
result.col[cell[0]][cell[1]] = cell[2]
return result
示例15: _transform_mesh_coordinate_system
def _transform_mesh_coordinate_system(mesh):
# This transformation swaps Y and Z axes, turning coordinate system from
# right-handed to left-handed.
transformation = Matrix()
transformation.zero()
transformation[0][0] = 1
transformation[1][2] = 1
transformation[2][1] = 1
transformation[3][3] = 1
mesh.transform(transformation)