def scale_rot_matrix(self, u, v):
"""
given vector u and v (from and to p0 p1)
apply scale factor to radius and
return a matrix to rotate and scale
the center around u origin so
arc fit v
"""
# signed angle old new vectors (rotation)
a = self.signed_angle(u, v)
# scale factor
scale = v.length / u.length
ca = scale * cos(a)
sa = scale * sin(a)
return scale, Matrix([
[ca, -sa],
[sa, ca]
])
python类Matrix()的实例源码
def rotate(self, a):
"""
Rotate center so we rotate ccw arround p0
"""
ca = cos(a)
sa = sin(a)
rM = Matrix([
[ca, -sa],
[sa, ca]
])
p0 = self.p0
self.c = p0 + rM * (self.c - p0)
dp = p0 - self.c
self.a0 = atan2(dp.y, dp.x)
return self
# make offset for line / arc, arc / arc
def rotate(self, idx_from, a):
"""
apply rotation to all following segs
"""
self.segs[idx_from].rotate(a)
ca = cos(a)
sa = sin(a)
rM = Matrix([
[ca, -sa],
[sa, ca]
])
# rotation center
p0 = self.segs[idx_from].p0
for i in range(idx_from + 1, len(self.segs)):
seg = self.segs[i]
# rotate seg
seg.rotate(a)
# rotate delta from rotation center to segment start
dp = rM * (seg.p0 - p0)
seg.translate(dp)
space_view3d_copy_attributes.py 文件源码
项目:blender-addons
作者: scorpion81
项目源码
文件源码
阅读 16
收藏 0
点赞 0
评论 0
def getmat(bone, active, context, ignoreparent):
"""Helper function for visual transform copy,
gets the active transform in bone space
"""
obj_act = context.active_object
data_bone = obj_act.data.bones[bone.name]
#all matrices are in armature space unless commented otherwise
otherloc = active.matrix # final 4x4 mat of target, location.
bonemat_local = data_bone.matrix_local.copy() # self rest matrix
if data_bone.parent:
parentposemat = obj_act.pose.bones[data_bone.parent.name].matrix.copy()
parentbonemat = data_bone.parent.matrix_local.copy()
else:
parentposemat = parentbonemat = Matrix()
if parentbonemat == parentposemat or ignoreparent:
newmat = bonemat_local.inverted() * otherloc
else:
bonemat = parentbonemat.inverted() * bonemat_local
newmat = bonemat.inverted() * parentposemat.inverted() * otherloc
return newmat
def extrusion_to_matrix(entity):
"""
Converts an extrusion vector to a rotation matrix that denotes the transformation between world coordinate system
and the entity's own coordinate system (described by the extrusion vector).
"""
def arbitrary_x_axis(extrusion_normal):
world_y = Vector((0, 1, 0))
world_z = Vector((0, 0, 1))
if abs(extrusion_normal[0]) < 1 / 64 and abs(extrusion_normal[1]) < 1 / 64:
a_x = world_y.cross(extrusion_normal)
else:
a_x = world_z.cross(extrusion_normal)
a_x.normalize()
return a_x, extrusion_normal.cross(a_x)
az = Vector(entity.extrusion)
ax, ay = arbitrary_x_axis(az)
return Matrix((ax, ay, az)).inverted()
def get_view_projection_matrix(context, settings):
"""
Returns view projection matrix.
Projection matrix is either identity if 3d export is selected or
camera projection if a camera or view is selected.
Currently only orthographic projection is used. (Subject to discussion).
"""
cam = settings['projectionThrough']
if cam == None:
mw = mathutils.Matrix()
mw.identity()
elif cam in projectionMapping.keys():
projection = mathutils.Matrix.OrthoProjection(projectionMapping[cam], 4)
mw = projection
else: # get camera with given name
c = context.scene.objects[cam]
mw = getCameraMatrix(c)
return mw
io_export_paper_model.py 文件源码
项目:blender_addons_collection
作者: manorius
项目源码
文件源码
阅读 29
收藏 0
点赞 0
评论 0
def z_up_matrix(n):
"""Get a rotation matrix that aligns given vector upwards."""
b = n.xy.length
l = n.length
if b > 0:
return M.Matrix((
(n.x*n.z/(b*l), n.y*n.z/(b*l), -b/l),
(-n.y/b, n.x/b, 0),
(0, 0, 0)
))
else:
# no need for rotation
return M.Matrix((
(1, 0, 0),
(0, (-1 if n.z < 0 else 1), 0),
(0, 0, 0)
))
io_export_paper_model.py 文件源码
项目:blender_addons_collection
作者: manorius
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def finalize_islands(self, title_height=0):
for island in self.islands:
if title_height:
island.title = "[{}] {}".format(island.abbreviation, island.label)
points = list(vertex.co for vertex in island.verts) + island.fake_verts
angle = M.geometry.box_fit_2d(points)
rot = M.Matrix.Rotation(angle, 2)
for point in points:
# note: we need an in-place operation, and Vector.rotate() seems to work for 3d vectors only
point[:] = rot * point
for marker in island.markers:
marker.rot = rot * marker.rot
bottom_left = M.Vector((min(v.x for v in points), min(v.y for v in points) - title_height))
for point in points:
point -= bottom_left
island.bounding_box = M.Vector((max(v.x for v in points), max(v.y for v in points)))
def getmat(bone, active, context, ignoreparent):
"""Helper function for visual transform copy,
gets the active transform in bone space
"""
obj_act = context.active_object
data_bone = obj_act.data.bones[bone.name]
#all matrices are in armature space unless commented otherwise
otherloc = active.matrix # final 4x4 mat of target, location.
bonemat_local = data_bone.matrix_local.copy() # self rest matrix
if data_bone.parent:
parentposemat = obj_act.pose.bones[data_bone.parent.name].matrix.copy()
parentbonemat = data_bone.parent.matrix_local.copy()
else:
parentposemat = parentbonemat = Matrix()
if parentbonemat == parentposemat or ignoreparent:
newmat = bonemat_local.inverted() * otherloc
else:
bonemat = parentbonemat.inverted() * bonemat_local
newmat = bonemat.inverted() * parentposemat.inverted() * otherloc
return newmat
def getmat(bone, active, context, ignoreparent):
"""Helper function for visual transform copy,
gets the active transform in bone space
"""
obj_act = context.active_object
data_bone = obj_act.data.bones[bone.name]
#all matrices are in armature space unless commented otherwise
otherloc = active.matrix # final 4x4 mat of target, location.
bonemat_local = data_bone.matrix_local.copy() # self rest matrix
if data_bone.parent:
parentposemat = obj_act.pose.bones[data_bone.parent.name].matrix.copy()
parentbonemat = data_bone.parent.matrix_local.copy()
else:
parentposemat = parentbonemat = Matrix()
if parentbonemat == parentposemat or ignoreparent:
newmat = bonemat_local.inverted() * otherloc
else:
bonemat = parentbonemat.inverted() * bonemat_local
newmat = bonemat.inverted() * parentposemat.inverted() * otherloc
return newmat
def _areDifferent_Mat44(self, mat1, mat2, thresholdLoc = 1.0, thresholdRot = 1.0):
"""
Check if 2 input matrices are different above a certain threshold.
:param mat1: input Matrix
:param mat2: input Matrix
:param thresholdLoc: threshold above which delta translation between the 2 matrix has to be for them to be qualified as different
:param thresholdRot: threshold above which delta rotation between the 2 matrix has to be for them to be qualified as different
:type mat1: mathutils.Matrix
:type mat2: mathutils.Matrix
:type thresholdLoc: Float
:type thresholdRot: Float
:return: a boolean stating wheter the two matrices are different
:rtype: Boolean
"""
areDifferent = False
jnd_vect = mathutils.Vector((thresholdLoc,thresholdLoc,thresholdRot))
t1, t2 = mat1.to_translation(), mat2.to_translation()
r1, r2 = mat1.to_euler(), mat2.to_euler()
for n in range(3):
if (abs(t1[n]-t2[n]) > thresholdLoc) or (abs(math.degrees(r1[n]-r2[n])) > thresholdRot): areDifferent = True
return areDifferent
def compute_rest( self ): # called after rebuild_tree, recursive roots to leaves
if self.parent:
inverseParentMatrix = self.parent.inverse_total_trans
elif self.fixUpAxis:
inverseParentMatrix = self.flipMat
else:
inverseParentMatrix = mathutils.Matrix(((1,0,0,0),(0,1,0,0),(0,0,1,0),(0,0,0,1)))
#self.ogre_rest_matrix = self.skeleton.object_space_transformation * self.matrix # ALLOW ROTATION?
self.ogre_rest_matrix = self.matrix.copy()
# store total inverse transformation
self.inverse_total_trans = self.ogre_rest_matrix.inverted()
# relative to OGRE parent bone origin
self.ogre_rest_matrix = inverseParentMatrix * self.ogre_rest_matrix
self.inverse_ogre_rest_matrix = self.ogre_rest_matrix.inverted()
# recursion
for child in self.children:
child.compute_rest()
def transform_bone_matrix(bone):
if not bone.parent:
return Matrix()
i1 = Vector((1.0, 0.0, 0.0))
i2 = Vector((0.0, 1.0, 0.0))
i3 = Vector((0.0, 0.0, 1.0))
x_axis = bone.y_axis
y_axis = bone.x_axis
z_axis = -bone.z_axis
row_x = Vector((x_axis * i1, x_axis * i2, x_axis * i3))
row_y = Vector((y_axis * i1, y_axis * i2, y_axis * i3))
row_z = Vector((z_axis * i1, z_axis * i2, z_axis * i3))
trans_matrix = Matrix((row_x, row_y, row_z))
location = trans_matrix * bone.matrix.translation
bone_matrix = trans_matrix.to_4x4()
bone_matrix.translation = -location
return bone_matrix
def _create_mesh_object(self, bm, name):
# Transform the coordinate system so that Y is up.
matrix_y_to_z = Matrix(((1, 0, 0), (0, 0, -1), (0, 1, 0)))
bmesh.ops.transform(bm, matrix=matrix_y_to_z, verts=bm.verts)
# Create the mesh into which each bmesh will be written.
mesh = bpy.data.meshes.new(name)
bm.to_mesh(mesh)
bm.free()
# Create, group and link an object representing that mesh.
ob = bpy.data.objects.new(name, mesh)
self._add_to_group(ob, "KCL")
bpy.context.scene.objects.link(ob)
def read_matrix4x3(self):
matrix = mathutils.Matrix()
matrix[0][0] = self.read_single()
matrix[0][1] = self.read_single()
matrix[0][2] = self.read_single()
matrix[1][0] = self.read_single()
matrix[1][1] = self.read_single()
matrix[1][2] = self.read_single()
matrix[2][0] = self.read_single()
matrix[2][1] = self.read_single()
matrix[2][2] = self.read_single()
matrix[3][0] = self.read_single()
matrix[3][1] = self.read_single()
matrix[3][2] = self.read_single()
return matrix
def getWheelOrientationQuaternion(self, wheelIndex):
"""Returns the wheel orientation as a quaternion."""
return mathutils.Matrix()
def getWorldToCamera(self):
"""Returns the world-to-camera transform."""
return mathutils.Matrix()
def __call__(self, bm: BMesh = Required,
mat: Matrix = None):
color = self.node.face_color[:]
if mat is not None:
if self.node.use_ops_trans:
bm = bm.copy()
matrix = mu.Matrix(mat)
bmesh.ops.transform(bm, matrix=matrix, verts=bm.verts)
verts = [v.co[:] for v in bm.verts]
bm.normal_update()
normals = [f.normal[:] for f in bm.faces]
else:
matrix = mu.Matrix(mat)
verts = [matrix * v.co for v in bm.verts]
bm.normal_update()
mat33 = matrix.to_3x3()
normals = [(mat33 * f.normal)[:] for f in bm.faces]
else:
verts = [v.co[:] for v in bm.verts]
bm.normal_update()
normals = [f.normal[:] for f in bm.faces]
tess_faces = bm.calc_tessface()
vert_index = [l.vert.index for l in itertools.chain(*bm.calc_tessface())]
face_index = [t_f[0].face.index for t_f in tess_faces]
edges_index = [(e.verts[0].index, e.verts[1].index) for e in bm.edges]
colors = []
for idx in face_index:
normal = mu.Vector(normals[idx])
normal_nu = normal.angle((0, 0, 1), 0) / np.pi
r = (normal_nu * color[0]) + 0.1
g = (normal_nu * color[1]) + 0.1
b = (normal_nu * color[2]) + 0.1
colors.append((r, g, b))
self.vertices.append(verts)
self.faces.append(vert_index if self.node.display_face else [])
self.colors.append(colors)
self.edges.append(edges_index)
def __call__(self,
verts: Vertices = Required,
edges: Edges = None,
faces: Faces = None,
mat: Matrix = None):
bm = bmesh_from_pydata(verts[:, :3].tolist(), edges, faces)
super().__call__(bm, mat)
def get_object_rotational_matrix():
return mathutils.Matrix(bpy.context.active_object.matrix_world).to_quaternion().to_matrix()