本文整理汇总了Python中mathutils.Vector.normalize方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.normalize方法的具体用法?Python Vector.normalize怎么用?Python Vector.normalize使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mathutils.Vector
的用法示例。
在下文中一共展示了Vector.normalize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: write_camera
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def write_camera(self, camera, name="Active Camera"):
pos, target, up = camera.GetOrientation()
bpy.ops.object.add(type='CAMERA', location=pos)
ob = self.context.object
ob.name = name
z = (Vector(pos) - Vector(target))
x = Vector(up).cross(z)
y = z.cross(x)
x.normalize()
y.normalize()
z.normalize()
ob.matrix_world.col[0] = x.resized(4)
ob.matrix_world.col[1] = y.resized(4)
ob.matrix_world.col[2] = z.resized(4)
cam = ob.data
aspect_ratio = camera.aspect_ratio
fov = camera.fov
if aspect_ratio == False: # we seem to be using dynamic / screen aspect ratio
sketchupLog("CAMERA {} uses dynamic / screen aspect ratio ".format(name))
aspect_ratio = self.aspect_ratio
if fov == False:
sketchupLog("CAMERA {} is ortho ".format(name))
cam.type = 'ORTHO'
else:
cam.angle = (pi * fov / 180 ) * aspect_ratio
cam.clip_end = self.prefs.camera_far_plane
cam.name = name
示例2: generate
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def generate(self):
""" Generate the rig.
Do NOT modify any of the original bones, except for adding constraints.
The main armature should be selected and active before this is called.
"""
ctrl_bones = self.fk_limb.generate()
thigh = ctrl_bones[0]
shin = ctrl_bones[1]
foot = ctrl_bones[2]
foot_mch = ctrl_bones[3]
# Position foot control
bpy.ops.object.mode_set(mode='EDIT')
eb = self.obj.data.edit_bones
foot_e = eb[foot]
vec = Vector(eb[self.org_bones[3]].vector)
vec.normalize()
foot_e.tail = foot_e.head + (vec * foot_e.length)
foot_e.roll = eb[self.org_bones[3]].roll
bpy.ops.object.mode_set(mode='OBJECT')
# Create foot widget
ob = create_widget(self.obj, foot)
if ob is not None:
verts = [(0.7, 1.5, 0.0), (0.7, -0.25, 0.0), (-0.7, -0.25, 0.0), (-0.7, 1.5, 0.0), (0.7, 0.723, 0.0), (-0.7, 0.723, 0.0), (0.7, 0.0, 0.0), (-0.7, 0.0, 0.0)]
edges = [(1, 2), (0, 3), (0, 4), (3, 5), (4, 6), (1, 6), (5, 7), (2, 7)]
mesh = ob.data
mesh.from_pydata(verts, edges, [])
mesh.update()
mod = ob.modifiers.new("subsurf", 'SUBSURF')
mod.levels = 2
return [thigh, shin, foot, foot_mch]
示例3: scale_bone_from_origin
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def scale_bone_from_origin(bone_name, length):
"""
returns the bone tail Vector(x,y,z)
"""
direction = Vector(amt.edit_bones[bone_name].tail) - Vector(amt.edit_bones[bone_name].head)
direction.normalize()
direction *= length
return Vector(amt.edit_bones[bone_name].head) + direction
示例4: normalAt
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def normalAt(self, t):
tan = self.tangentAt(t)
# Rotate the normal along the Z-up axis
normal = Vector((tan.y, -tan.x, tan.z))
normal.normalize()
return normal
示例5: triangle_relative_to_verts
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def triangle_relative_to_verts(top, base_relative, base_length):
v1 = Vector(top)
v = Vector([-base_relative[1] / 2, base_relative[0] / 2])
v.normalize()
v *= base_length / 2
vb = Vector(base_relative)
v2 = v1 + vb + v
v3 = v1 + vb - v
return v1, v2, v3
示例6: depth_cast
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def depth_cast(self, xy, radius=0, cached=True, coords='REGION'):
xy = self.convert_ui_coord(xy, coords, 'REGION', False)
radius = int(radius)
search = (radius > 0)
radius = max(radius, 1)
sz = radius * 2 + 1 # kernel size
w, h = sz, sz
zbuf = self.read_zbuffer(xy, (sz, sz), centered=True, cached=cached)
def get_pos(x, y):
wnd_x = min(max(x+radius, 0), w-1)
wnd_y = min(max(y+radius, 0), h-1)
z = zbuf[wnd_x + wnd_y * w]
if (z >= 1.0) or (z < 0.0): return None
d = self.zbuf_to_depth(z)
return self.unproject((xy[0]+x, xy[1]+y), d)
cx, cy = 0, 0
center = None
if search:
rr = radius * radius
for dxy in self.__radial_search_pattern:
if dxy[2] > rr: break
p = get_pos(dxy[0], dxy[1])
if p is not None:
cx, cy = dxy[0], dxy[1]
center = p
break
else:
center = get_pos(0, 0)
if center is None: return (False, None, Matrix(), Vector(), Vector())
normal_count = 0
normal = Vector()
last_i = -10 # just some big number
last_p = None
neighbors = ((-1, -1), (0, -1), (1, -1), (1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1))
for i, nbc in enumerate(neighbors):
nbx, nby = nbc
p = get_pos(cx + nbx, cy + nby)
if p is None: continue
if (i - last_i) < 4:
d0 = last_p - center
d1 = p - center
normal += d0.cross(d1).normalized()
normal_count += 1
last_p = p
last_i = i
if normal_count > 1: normal.normalize()
return (True, None, Matrix(), center, normal)
示例7: create_geometry
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def create_geometry(self, bm, e_loops):
geom_extruded = bmesh.ops.extrude_edge_only(bm, edges=e_loops)["geom"]
self.offset_verts = offset_verts = [e for e in geom_extruded if isinstance(e, bmesh.types.BMVert)]
self.offset_edges = offset_edges = [e for e in geom_extruded if isinstance(e, bmesh.types.BMEdge)]
self.side_faces = side_faces = [f for f in geom_extruded if isinstance(f, bmesh.types.BMFace)]
bmesh.ops.recalc_face_normals(bm, faces=side_faces)
self.side_edges = side_edges = [e.link_loops[0].link_loop_next.edge for e in offset_edges]
for f in side_faces:
f.select = True
extended_verts = self.extended_verts
self.v_v_pairs = v_v_pairs = dict() # keys is offset vert,
# values is original vert.
for e in side_edges:
v1, v2 = e.verts
if v1 in offset_verts:
v_offset, v_orig = v1, v2
else:
v_offset, v_orig = v2, v1
v_v_pairs[v_offset] = v_orig
if v_orig in extended_verts:
extended_verts.add(v_offset)
self.faces = faces = bmesh.ops.edgeloop_fill(bm, edges=offset_edges, mat_nr=0, use_smooth=False)["faces"]
self.l_fn_pairs = l_fn_pairs = dict() # loop - face normal pairs.
for face in faces:
face.loops.index_update()
if face.normal.dot(v_v_pairs[face.verts[0]].normal) < 0.0:
face.normal_flip()
for fl in face.loops:
edge = fl.link_loop_radial_next.link_loop_next.link_loop_next.edge
co = 0
normal = Vector((0.0, 0.0, 0.0))
for f in edge.link_faces:
if f not in side_faces and not f.hide and f.normal.length:
normal += f.normal
co += 1
if f.select:
l_fn_pairs[fl] = f.normal.copy()
break
else:
if co:
normal.normalize()
l_fn_pairs[fl] = normal
# Be careful, if you flip face normal after
# this line, l_fn_pairs won't work as you expect
# because face.normal_flip() changes loop order in
# the face.
return faces
示例8: polygon_normal_angle_D
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def polygon_normal_angle_D(verts, poly, D):
''' The angle between the polygon normal and the given direction '''
N = polygon_normal(verts, poly)
v1 = Vector(N)
v2 = Vector(D)
v1.normalize()
v2.normalize()
angle = acos(v1.dot(v2)) # the angle in radians
return angle
示例9: shape_square
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def shape_square(context, orientation):
center = context.scene.cursor_location
active = context.active_object
zed = active.location[2]
base_dir = active.location.xy - center.xy
diagonal = base_dir.length
if orientation == 'XY':
zero_dir = get_xy_corner(base_dir).resized(3)
else:
zero_dir = base_dir.xy.resized(3)
num_objects = len(context.selected_objects)
num_side_objects = (num_objects // 4) - 1
ortho_angle = math.pi * 0.5
ortho_dir = Vector(zero_dir)
ortho_dir.rotate(Euler((0, 0, ortho_angle * 0.5)))
ortho_dir.normalize()
# sort objects based on angle to center
sorted_objects = sorted(context.selected_objects, key=lambda ob: get_angle(base_dir, ob, center))
corners = []
for i in range(0, num_objects, num_side_objects + 1):
corners.append(sorted_objects[i])
assert(len(corners) == 4)
sides = [ob for ob in sorted_objects if ob not in corners]
side_step = math.sqrt(2 * (diagonal ** 2)) / (num_side_objects + 1)
for i in range(4):
# corner
angle = ortho_angle * i
euler = Euler((0, 0, -angle))
direction = Vector(zero_dir)
direction.rotate(euler)
corners[i].location = center + direction
corners[i].location[2] = zed
side_dir = Vector(ortho_dir)
side_dir.rotate(euler)
for j in range(num_side_objects):
ob = sides[(i * num_side_objects) + j]
step = (j + 1) * side_step
ob.location = center + direction
ob.location.xy -= side_dir.xy * step
ob.location[2] = zed
示例10: setCursor
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def setCursor(self, v):
if self.stepLengthEnable:
c = CursorAccess.getCursor()
if (Vector(c) - Vector(v)).length > 0:
if self.stepLengthMode == "Absolute":
v = Vector(v) - Vector(c)
v.normalize()
v = v * self.stepLengthValue + Vector(c)
if self.stepLengthMode == "Proportional":
v = (Vector(v) - Vector(c)) * self.stepLengthValue + Vector(c)
CursorAccess.setCursor(Vector(v))
示例11: nextpoint
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def nextpoint(poi,powers):
verts_ = [poi-pow for pow in powers]
vect = Vector()
for i in verts_:
vect+=i*(1/i.length**2)
vect.normalize()
# additional power:
#cos(x)
#sin
#3*exp(-(x**2+3**2)**2)
vertnext = poi + vect*(1/lent)
return vertnext
示例12: react
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def react(self, position, direction, population):
res = Vector((0, 0, 0))
for individue in population:
diff = individue.position - position
diff = -diff
if True :
print(" agora ")
diff.normalize()
res += diff
res.normalize()
res = res * 5
return res
示例13: polygon_normal_angle_P
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def polygon_normal_angle_P(verts, poly, P):
''' The angle between the polygon normal and the vector from polygon center to given point '''
N = polygon_normal(verts, poly)
C = polygon_center(verts, poly)
V = [P[0] - C[0], P[1] - C[1], P[2] - C[2]]
v1 = Vector(N)
v2 = Vector(V)
v1.normalize()
v2.normalize()
angle = acos(v1.dot(v2)) # the angle in radians
return angle
示例14: _createBone
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def _createBone(self, bone, armature):
name = bone.name()
print(" Creation of the bone '%s'..." % name)
bl_bone = armature.edit_bones.new(name)
bl_bone.head = bone.pivot_point.to_tuple()
if bone.usedChildrenCount() == 1:
bl_bone.tail = bone.children[0].pivot_point.to_tuple()
elif bone.usedChildrenCount() > 1:
done = False
if self.smart_bone_parenting:
max_depth = 0
max_depth_bones = []
for child in bone.usedChildren():
depth = child.depth(self.only_used_bones)
if depth > max_depth:
max_depth = depth
max_depth_bones = [child]
elif depth == max_depth:
max_depth_bones.append(child)
print("%s - %d - %d" % (bone.name(), max_depth, len(max_depth_bones)))
if len(max_depth_bones) == 1:
bl_bone.tail = max_depth_bones[0].pivot_point.to_tuple()
done = True
if not(done):
v = Vector( (0, 0, 0) )
for child in bone.usedChildren():
v += child.pivot_point
bl_bone.tail = (v / bone.usedChildrenCount()).to_tuple()
elif bone.parent is not None:
if bone.parent.bl_bone is None:
self._createBone(bone.parent, armature)
v = (bone.pivot_point - bone.parent.pivot_point)
if v.length >= 0.001:
v.normalize()
bl_bone.tail = (bone.parent.bl_bone.length * v + bone.pivot_point).to_tuple()
else:
bl_bone.tail = (bone.parent.bl_bone.length * bone.pivot_point.normalized() + bone.pivot_point).to_tuple()
else:
bl_bone.tail = (bone.pivot_point * 2).to_tuple()
if bone.parent is not None:
bl_bone.parent = bone.parent.bl_bone
bl_bone.use_connect = (bone.parent.usedChildrenCount() == 1)
bone.bl_bone = bl_bone
示例15: spherize
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import normalize [as 别名]
def spherize(context):
print ("in spherize")
#influence
influence = context.scene['unt_spherifyratio']
#gets the location of the center of the center object
if context.scene.centerobject:
center = bpy.data.objects[context.scene.centerobject].location
else:
center = bpy.context.scene.cursor_location
#get world matrix for the object
worldmatrix = bpy.context.object.matrix_world
ob = bpy.context.object
obdata = ob.data
#mandatory stupid step, calculate normals split, in object mode
bpy.ops.object.mode_set(mode="OBJECT")
obdata.calc_normals_split()
#prepare a list for all the normals. One per "loop"(vertice-per-faace)
normals = [Vector()]*len(obdata.loops)
#loop all the loops (subvertices) in the mesh
for loop in obdata.loops:
#obdata.calc_normals_split()
vertexindex = loop.vertex_index
normals[loop.index] = loop.normal
print ("normal: %s"% normals[loop.index])
#if the vertex is selected, normalize the normal
if obdata.vertices[vertexindex].select:
#get local coordinate of the related vertex
localco = obdata.vertices[vertexindex].co
#calculate the globla coordinates of the vertex
globalco = worldmatrix * obdata.vertices[vertexindex].co
#delta betwen the global location of the vertex and the center of the center object
v= Vector([y-x for x,y in zip(globalco,center)])
v.negate()
v.normalize()
#resulting vector (v*influence + normal*(1-influence))
normals[loop.index] = v * float(influence) + normals[loop.index] * (float(1)-float(influence))
#normals[loop.index].negate()
normals[loop.index].normalize()
print ("new normal: %s"% normals[loop.index])
obdata.normals_split_custom_set(normals)
bpy.ops.object.mode_set(mode="EDIT")