本文整理汇总了Python中mathutils.Vector.to_track_quat方法的典型用法代码示例。如果您正苦于以下问题:Python Vector.to_track_quat方法的具体用法?Python Vector.to_track_quat怎么用?Python Vector.to_track_quat使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mathutils.Vector
的用法示例。
在下文中一共展示了Vector.to_track_quat方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: execute
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import to_track_quat [as 别名]
def execute(self, context):
bpy.ops.object.mode_set(mode='EDIT')
obj = bpy.context.edit_object
me = obj.data
bm = bmesh.from_edit_mesh(me)
cornerverts, sharp = corner_vertices(bm)
#print("corner verts:", cornerverts)
#print ("corner sharp:",sharp)
# ------------------------------------------------
# Get angle of edge corners
bpy.ops.object.mode_set(mode='OBJECT')
o = bpy.context.object
mat = o.matrix_world
sel = bpy.context.selected_objects
#bpy.context.Scene.offset = .1
if len(sel) == 2:
sharpcnt = 0
for v in range(0, len(cornerverts), 3):
v1 = mat * o.data.vertices[cornerverts[v]].co
v2 = mat * o.data.vertices[cornerverts[v + 1]].co
v3 = mat * o.data.vertices[cornerverts[v + 2]].co
vec_result = get_angle (v1,v2,v3)
ob = create_object(sel[0], v1)
ob.rotation_mode = 'QUATERNION'
if sharp[sharpcnt] == True:
vec_result = - vec_result
sharpcnt += 1
ob.rotation_quaternion = Vector.to_track_quat(-vec_result, '-Y', 'Z')
bpy.context.scene.update()
# offset
vec = Vector((0, -bpy.context.scene.offset, 0))
inv = ob.matrix_world.copy()
inv.invert()
# vec aligned to local axis
vec_rot = vec * inv
ob.location += vec_rot
# rename
if bpy.context.scene.angle:
ob.name = rename(ob.name,Vector.to_track_quat(-vec_result, '-Y', 'Z'))
return {'FINISHED'}
示例2: ctx_camera_setup
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import to_track_quat [as 别名]
def ctx_camera_setup(context,
location=(0.0, 0.0, 0.0),
lookat=(0.0, 0.0, 0.0),
# most likely the following vars can be left as defaults
up=(0.0, 0.0, 1.0),
lookat_axis='-Z',
up_axis='Y',
):
camera = bpy.data.cameras.new(whoami())
obj = bpy.data.objects.new(whoami(), camera)
scene = context.scene
scene.objects.link(obj)
scene.camera = obj
from mathutils import Vector, Matrix
# setup transform
view_vec = Vector(lookat) - Vector(location)
rot_mat = view_vec.to_track_quat(lookat_axis, up_axis).to_matrix().to_4x4()
tra_mat = Matrix.Translation(location)
obj.matrix_world = tra_mat * rot_mat
ctx_viewport_camera(context)
return obj
示例3: set_camera
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import to_track_quat [as 别名]
def set_camera():
global CAMERA_NAME
# get camera or create one
cam = cam_ob = None
if CAMERA_NAME in bpy.data.cameras:
cam = bpy.data.cameras[CAMERA_NAME]
else:
cam = bpy.data.cameras.new(CAMERA_NAME)
if CAMERA_NAME in bpy.data.objects:
cam_ob = bpy.data.objects[CAMERA_NAME]
else:
cam_ob = bpy.data.objects.new(CAMERA_NAME, cam)
bpy.context.scene.objects.link(cam_ob)
# position
ob = bpy.data.objects[OBJECT_NAME]
cam_ob.location = (0,0,0)
max_dim = max(ob.dimensions.z, max(ob.dimensions.y, ob.dimensions.x))
dim_ratio = 1
try:
dim_ratio = ob.dimensions.y/ob.dimensions.x if ob.dimensions.y>ob.dimensions.x else ob.dimensions.x/ob.dimensions.y
except ZeroDivisionError: pass
cam_ob.location.y += max_dim * 1.1
cam_ob.location.x += max_dim * 1.1
cam_ob.location.z += max_dim / dim_ratio
# rotation
dir = Vector((0,0,0)) - cam_ob.location
cam_ob.rotation_euler = dir.to_track_quat('-Z', 'Y').to_euler()
# other settings
cam.clip_start = 0.002
cam.clip_end = 3*max(abs(cam_ob.location.z), max(abs(cam_ob.location.x), abs(cam_ob.location.y)))
cam.show_limits = True
# set camera as the active one
bpy.context.scene.camera = cam_ob
示例4: point_camera
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import to_track_quat [as 别名]
def point_camera(obj_camera, point):
loc_camera = obj_camera.location
direction = Vector(point) - loc_camera
old_rotation = obj_camera.rotation_euler[:]
# point the cameras '-Z' and use its 'Y' as up
rot_quat = direction.to_track_quat('-Z', 'Y')
# assume we're using euler rotation
new_rotation = rot_quat.to_euler()
# bunch of code to keep rotations nice regardless of quadrant
# treat all angles on the interval [-pi, pi] before adding them
for ii, (old, new) in enumerate(zip(old_rotation, new_rotation)):
oldang = old % (2 * math.pi)
newang = new % (2 * math.pi)
if oldang > math.pi:
oldang = oldang - 2 * math.pi
if newang > math.pi:
newang = newang - 2 * math.pi
diff = newang - oldang
if abs(diff) > math.pi:
sgn = 1 if diff > 0 else -1
diff = sgn * (abs(diff) - math.pi)
# Use the smaller angle to rotate the camera
new = old + diff
obj_camera.rotation_euler[ii] = new
示例5: execute
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import to_track_quat [as 别名]
#.........这里部分代码省略.........
if shape_verts is not None and len(shape_verts) > 0:
if context.space_data.pivot_point == "CURSOR":
center = object.matrix_world.copy() * context.scene.cursor_location.copy()
else:
temp_bm = bmesh.new()
for loop_vert in loop_verts:
temp_bm.verts.new(loop_vert.co.copy())
temp_verts = temp_bm.verts[:]
for i in range(len(temp_verts)):
temp_bm.edges.new((temp_verts[i], temp_verts[(i + 1) % len(temp_verts)]))
temp_bm.faces.new(temp_bm.verts)
temp_bm.faces.ensure_lookup_table()
if context.space_data.pivot_point == 'BOUNDING_BOX_CENTER':
center = temp_bm.faces[0].calc_center_bounds()
else:
center = temp_bm.faces[0].calc_center_median()
del temp_bm
context.scene.perfect_shape.preview_verts_count = loop_verts_len + self.span
if self.projection == "NORMAL":
forward = calculate_normal([v.co.copy() for v in loop_verts])
normal_forward = reduce(
lambda v1, v2: v1.normal.copy() + v2.normal.copy() if isinstance(v1, bmesh.types.BMVert)
else v1.copy() + v2.normal.copy(), loop_verts).normalized()
if normal_forward.angle(forward) - math.pi / 2 > 1e-6:
forward.negate()
else:
forward = Vector([v == self.projection for v in ["X", "Y", "Z"]])
if self.invert_projection:
forward.negate()
matrix_rotation = forward.to_track_quat('Z', 'Y').to_matrix().to_4x4()
matrix_translation = Matrix.Translation(center)
bmesh.ops.scale(shape_bm, vec=Vector((1, 1, 1)) * (1 + self.offset), verts=shape_verts)
bmesh.ops.transform(shape_bm, verts=shape_verts, matrix=matrix_translation * matrix_rotation)
if not is_clockwise(forward, center, loop_verts):
loop_verts.reverse()
loop_edges.reverse()
if not is_clockwise(forward, center, shape_verts):
shape_verts.reverse()
correct_angle_m = 1
shift_m = 1
if context.space_data.pivot_point != "INDIVIDUAL_ORIGINS":
if selection_center.dot(center.cross(forward)) >= 0:
# if (center.cross(forward)).angle(selection_center) - math.pi/2 <= 1e-6:
correct_angle_m = -1
shift_m = -1
loop_verts_co_2d, shape_verts_co_2d = None, None
if loop_verts_co_2d is None:
loop_verts_co_2d = [(matrix_rotation.transposed() * v.co).to_2d() for v in loop_verts]
shape_verts_co_2d = [(matrix_rotation.transposed() * v.co).to_2d() for v in shape_verts]
loop_angle = box_fit_2d(loop_verts_co_2d)
shape_angle = box_fit_2d(shape_verts_co_2d)
# if round(loop_angle, 4) == round(math.pi, 4):
# loop_angle = 0
correct_angle = 0
示例6: execute
# 需要导入模块: from mathutils import Vector [as 别名]
# 或者: from mathutils.Vector import to_track_quat [as 别名]
def execute(self, context):
object = context.object
object.update_from_editmode()
object_bm = bmesh.from_edit_mesh(object.data)
selected_edges = [e for e in object_bm.edges if e.select]
selected_verts = [v for v in object_bm.verts if v.select]
selected_verts_fin = []
if len(selected_edges) == 0:
self.report({'WARNING'}, "Please select edges.")
return {'CANCELLED'}
loops = prepare_loops(selected_edges[:])
if loops is None:
self.report({'WARNING'}, "Please select boundary loop(s) of selected area(s).")
return {'CANCELLED'}
object_bvh = mathutils.bvhtree.BVHTree.FromObject(object, context.scene, deform=False)
refresh_icons()
for (loop_verts, loop_edges), is_loop_cyclic, is_loop_boundary in loops:
if len(loop_edges) < 3:
continue
loop_verts_len = len(loop_verts)
context.window_manager.perfect_shape.preview_verts_count = loop_verts_len
if self.projection == "NORMAL":
if is_loop_boundary:
forward = calculate_normal([v.co for v in loop_verts])
else:
forward = reduce(
lambda v1, v2: v1.normal.copy() + v2.normal.copy() if isinstance(v1, bmesh.types.BMVert)
else v1.copy() + v2.normal.copy(), loop_verts).normalized()
else:
forward = Vector([v == self.projection for v in ["X", "Y", "Z"]])
if self.invert_projection:
forward.negate()
shape_bm = bmesh.new()
if context.space_data.pivot_point == "CURSOR":
center = object.matrix_world.copy() * context.scene.cursor_location.copy()
else:
for loop_vert in loop_verts:
shape_bm.verts.new(loop_vert.co.copy())
shape_bm.faces.new(shape_bm.verts)
shape_bm.faces.ensure_lookup_table()
if context.space_data.pivot_point == 'BOUNDING_BOX_CENTER':
center = shape_bm.faces[0].calc_center_bounds()
else:
center = shape_bm.faces[0].calc_center_median()
shape_bm.clear()
matrix_rotation = forward.to_track_quat('Z', 'X').to_matrix().to_4x4()
matrix_translation = Matrix.Translation(center)
shape_bm = bmesh.new()
shape_verts = None
if self.shape == "CIRCLE":
diameter = sum([e.calc_length() for e in loop_edges]) / (2*math.pi)
diameter += self.offset
shape_segments = loop_verts_len + self.span
shape_verts = bmesh.ops.create_circle(shape_bm, segments=shape_segments,
diameter=diameter, matrix=matrix_translation*matrix_rotation)
shape_verts = shape_verts["verts"]
elif self.shape == "RECTANGLE":
if loop_verts_len % 2 > 0:
self.report({'WARNING'}, "An odd number of edges.")
del shape_bm
return {'FINISHED'}
size = sum([e.calc_length() for e in loop_edges])
size_a = (size / 2) / (self.ratio_a + self.ratio_b) * self.ratio_a
size_b = (size / 2) / (self.ratio_a + self.ratio_b) * self.ratio_b
seg_a = (loop_verts_len / 2) / (self.ratio_a + self.ratio_b) * self.ratio_a
seg_b = int((loop_verts_len / 2) / (self.ratio_a + self.ratio_b) * self.ratio_b)
if seg_a % 1 > 0:
self.report({'WARNING'}, "Incorrect sides ratio.")
seg_a += 1
seg_b += 2
seg_a = int(seg_a)
if self.is_square:
size_a = (size_a + size_b) / 2
size_b = size_a
seg_len_a = size_a / seg_a
seg_len_b = size_b / seg_b
for i in range(seg_a):
shape_bm.verts.new(Vector((size_b/2*-1, seg_len_a*i-(size_a/2), 0)))
for i in range(seg_b):
shape_bm.verts.new(Vector((seg_len_b*i-(size_b/2), size_a/2, 0)))
for i in range(seg_a, 0, -1):
shape_bm.verts.new(Vector((size_b/2, seg_len_a*i-(size_a/2), 0)))
for i in range(seg_b, 0, -1):
shape_bm.verts.new(Vector((seg_len_b*i-(size_b/2), size_a/2*-1, 0)))
#.........这里部分代码省略.........