def get_render_location(mypoint):
v1 = Vector(mypoint)
scene = bpy.context.scene
co_2d = object_utils.world_to_camera_view(scene, scene.camera, v1)
# Get pixel coords
render_scale = scene.render.resolution_percentage / 100
render_size = (int(scene.render.resolution_x * render_scale),
int(scene.render.resolution_y * render_scale))
return [round(co_2d.x * render_size[0]), round(co_2d.y * render_size[1])]
# ---------------------------------------------------------
# Get center of circle base on 3 points
#
# Point a: (x,y,z) arc start
# Point b: (x,y,z) center
# Point c: (x,y,z) midle point in the arc
# Point d: (x,y,z) arc end
# Return:
# ang: angle (radians)
# len: len of arc
#
# ---------------------------------------------------------
python类radians()的实例源码
def walk_components(board, export):
module = board.GetModules()
while True:
if not module:
return
# Top is for Eagle boards imported to KiCAD
if str(module.GetLayerName()) not in ["Top", "F.Cu"]:
module = module.Next()
continue
lib = str(module.GetFPID().GetLibNickname()).strip()
try:
name = str(module.GetFPID().GetFootprintName()).strip()
except AttributeError:
# it seems we are working on Kicad >4.0.6, which has a changed method name
name = str(module.GetFPID().GetLibItemName()).strip()
value = unicode(module.GetValue()).strip()
ref = unicode(module.GetReference()).strip()
center = module.GetCenter()
orient = math.radians(module.GetOrientation() / 10)
pos = (center.x, center.y, orient)
export(lib, name, value, ref, pos)
module = module.Next()
def spiral(freq=1.0, center_x=0.0, center_y=0.0, range_x=1.0, range_y=1.0, width=1.0, height=1.0, **kwargs):
"""
"""
kink = random.random() * 5.0 - 2.5
x = []
y = []
count = freq * freq
for i in range(count):
fract = i / count
degrees = fract * 360.0 * math.radians(1) * kink
x.append((center_x + math.sin(degrees) * fract * range_x) % width)
y.append((center_y + math.cos(degrees) * fract * range_y) % height)
return x, y
def pointOnCircle(cx, cy, radius, angle):
"""Calculates the coordinates of a point on a circle given the center point, radius, and angle"""
angle = math.radians(angle) - (math.pi / 2)
x = cx + radius * math.cos(angle)
if x < cx:
x = math.ceil(x)
else:
x = math.floor(x)
y = cy + radius * math.sin(angle)
if y < cy:
y = math.ceil(y)
else:
y = math.floor(y)
return (int(x), int(y))
def get_sprite_rotation(self,sprite_name):
obj = bpy.data.objects[sprite_name]
euler_rot = obj.matrix_basis.to_euler()
degrees = math.degrees(euler_rot[1])
return -math.radians(degrees)
### convert windows slashes to linux slashes
def execute(self, context):
sprite_object = None
if context.active_object != None:
sprite_object = get_sprite_object(context.active_object)
scene = context.scene
if self.create:
context.scene.objects.active = None
bpy.ops.object.camera_add(view_align=True, enter_editmode=False, location=(0, -self.resolution[0] * get_addon_prefs(context).sprite_import_export_scale, 0), rotation=(radians(90), 0, 0))
cam = context.active_object
context.scene.objects.active = cam
cam.data.type = "ORTHO"
scene.render.pixel_filter_type = "BOX"
scene.render.alpha_mode = "TRANSPARENT"
if sprite_object != None:
cam.parent = sprite_object
if self.set_resolution:
ortho_scale = max(self.resolution[0],self.resolution[1])
cam.data.ortho_scale = ortho_scale/100
scene.render.resolution_x = self.resolution[0]
scene.render.resolution_y = self.resolution[1]
cam.location[1] = -self.resolution[0] * get_addon_prefs(context).sprite_import_export_scale
scene.render.resolution_percentage = 100
scene.camera = cam
if bpy.context.space_data.region_3d.view_perspective != "CAMERA":
bpy.ops.view3d.viewnumpad(type="CAMERA")
return{"FINISHED"}
def turn_right(self, angle):
"""Turn the turtle right about the axis perpendicular to the direction
it is facing"""
axis = (self.dir.cross(self.right))
axis.normalize()
self.dir.rotate(Quaternion(axis, math.radians(angle)))
self.dir.normalize()
self.right.rotate(Quaternion(axis, math.radians(angle)))
self.right.normalize()
def pitch_up(self, angle):
"""Pitch the turtle up about the right axis"""
self.dir.rotate(Quaternion(self.right, math.radians(angle)))
self.dir.normalize()
def roll_right(self, angle):
"""Roll the turtle right about the direction it is facing"""
self.right.rotate(Quaternion(self.dir, math.radians(angle)))
self.right.normalize()
def calc_helix_points(turtle, rad, pitch):
""" calculates required points to produce helix bezier curve with given radius and pitch in direction of turtle"""
# alpha = radians(90)
# pit = pitch/(2*pi)
# a_x = rad*cos(alpha)
# a_y = rad*sin(alpha)
# a = pit*alpha*(rad - a_x)*(3*rad - a_x)/(a_y*(4*rad - a_x)*tan(alpha))
# b_0 = Vector([a_x, -a_y, -alpha*pit])
# b_1 = Vector([(4*rad - a_x)/3, -(rad - a_x)*(3*rad - a_x)/(3*a_y), -a])
# b_2 = Vector([(4*rad - a_x)/3, (rad - a_x)*(3*rad - a_x)/(3*a_y), a])
# b_3 = Vector([a_x, a_y, alpha*pit])
# axis = Vector([0, 0, 1])
# simplifies greatly for case inc_angle = 90
points = [Vector([0, -rad, -pitch / 4]),
Vector([(4 * rad) / 3, -rad, 0]),
Vector([(4 * rad) / 3, rad, 0]),
Vector([0, rad, pitch / 4])]
# align helix points to turtle direction and randomize rotation around axis
trf = turtle.dir.to_track_quat('Z', 'Y')
spin_ang = rand_in_range(0, 2 * pi)
for p in points:
p.rotate(Quaternion(Vector([0, 0, 1]), spin_ang))
p.rotate(trf)
return points[1] - points[0], points[2] - points[0], points[3] - points[0], turtle.dir.copy()
def apply_tropism(turtle, tropism_vector):
"""Apply tropism_vector to turtle direction"""
h_cross_t = turtle.dir.cross(tropism_vector)
# calc angle to rotate by (from ABoP) multiply to achieve accurate results from WP attractionUp param
alpha = 10 * h_cross_t.magnitude
h_cross_t.normalize()
# rotate by angle about axis perpendicular to turtle direction and tropism vector
turtle.dir.rotate(Quaternion(h_cross_t, radians(alpha)))
turtle.dir.normalize()
turtle.right.rotate(Quaternion(h_cross_t, radians(alpha)))
turtle.right.normalize()
def calculate_cut_off(self):
"""
Calculate cut-off radius as Rc = L/2 from a given MOF object.
"""
width_a = self.ucv / (self.uc_size[1] * self.uc_size[2] / math.sin(math.radians(self.uc_angle[0])))
width_b = self.ucv / (self.uc_size[0] * self.uc_size[2] / math.sin(math.radians(self.uc_angle[1])))
width_c = self.ucv / (self.uc_size[0] * self.uc_size[1] / math.sin(math.radians(self.uc_angle[2])))
self.cut_off = min(width_a / 2, width_b / 2, width_c / 2)
def pbc_parameters(self):
"""
Calculates constants used in periodic boundary conditions.
"""
uc_cos = [math.cos(math.radians(a)) for a in self.uc_angle]
uc_sin = [math.sin(math.radians(a)) for a in self.uc_angle]
a, b, c = self.uc_size
v = self.frac_ucv
xf1 = 1 / a
xf2 = - uc_cos[2] / (a * uc_sin[2])
xf3 = (uc_cos[0] * uc_cos[2] - uc_cos[1]) / (a * v * uc_sin[2])
yf1 = 1 / (b * uc_sin[2])
yf2 = (uc_cos[1] * uc_cos[2] - uc_cos[0]) / (b * v * uc_sin[2])
zf1 = uc_sin[2] / (c * v)
self.to_frac = [xf1, xf2, xf3, yf1, yf2, zf1]
xc1 = a
xc2 = b * uc_cos[2]
xc3 = c * uc_cos[1]
yc1 = b * uc_sin[2]
yc2 = c * (uc_cos[0] - uc_cos[1] * uc_cos[2]) / uc_sin[2]
zc1 = c * v / uc_sin[2]
self.to_car = [xc1, xc2, xc3, yc1, yc2, zc1]
def uc_vectors(cls, uc_size, uc_angle):
"""
Calculate unit cell vectors for given unit cell size and angles
"""
a = uc_size[0]
b = uc_size[1]
c = uc_size[2]
alpha = math.radians(uc_angle[0])
beta = math.radians(uc_angle[1])
gamma = math.radians(uc_angle[2])
x_v = [a, 0, 0]
y_v = [b * math.cos(gamma), b * math.sin(gamma), 0]
z_v = [0.0] * 3
z_v[0] = c * math.cos(beta)
z_v[1] = (c * b * math.cos(alpha) - y_v[0] * z_v[0]) / y_v[1]
z_v[2] = math.sqrt(c * c - z_v[0] * z_v[0] - z_v[1] * z_v[1])
uc_vectors = [x_v, y_v, z_v]
return uc_vectors
def get_distance(location1, location2):
lat1, lng1 = location1
lat2, lng2 = location2
lat1, lng1, lat2, lng2 = map(radians, (lat1, lng1, lat2, lng2))
d = sin(0.5*(lat2 - lat1)) ** 2 + cos(lat1) * cos(lat2) * sin(0.5*(lng2 - lng1)) ** 2
return 2 * earth_Rrect * asin(sqrt(d))
def radial(cls, r, theta):
"""Provide a radial acceleration.
Arguments:
r (float): speed in pixels per second (per second)
theta (float): angle in degrees (0 = +X axis, 90 = +Y axis)
"""
radians = math.radians(theta)
ax = r * math.cos(radians)
ay = r * math.sin(radians)
return cls(ax=ax, ay=ay)
def create_matrix(self,parent_joint,parent_joint_matrix):
# The calculation of the local matrix is an optimized version of
# local_matrix = T*IPS*R*S if ignore_parent_scale else T*R*S
# where S, R and T is the scale, rotation and translation matrix
# respectively and IPS is the inverse parent scale matrix.
cx = cos(radians(self.rotation_x))
sx = sin(radians(self.rotation_x))
cy = cos(radians(self.rotation_y))
sy = sin(radians(self.rotation_y))
cz = cos(radians(self.rotation_z))
sz = sin(radians(self.rotation_z))
if self.ignore_parent_scale:
ips_x = 1/parent_joint.scale_x
ips_y = 1/parent_joint.scale_y
ips_z = 1/parent_joint.scale_z
else:
ips_x = 1
ips_y = 1
ips_z = 1
local_matrix = numpy.empty((3,4),numpy.float32)
local_matrix[0,0] = cy*cz*self.scale_x*ips_x
local_matrix[1,0] = cy*sz*self.scale_x*ips_y
local_matrix[2,0] = -sy*self.scale_x*ips_z
local_matrix[0,1] = (sx*sy*cz - cx*sz)*self.scale_y*ips_x
local_matrix[1,1] = (sx*sy*sz + cx*cz)*self.scale_y*ips_y
local_matrix[2,1] = sx*cy*self.scale_y*ips_z
local_matrix[0,2] = (cx*sy*cz + sx*sz)*self.scale_z*ips_x
local_matrix[1,2] = (cx*sy*sz - sx*cz)*self.scale_z*ips_y
local_matrix[2,2] = cx*cy*self.scale_z*ips_z
local_matrix[0,3] = self.translation_x
local_matrix[1,3] = self.translation_y
local_matrix[2,3] = self.translation_z
return matrix3x4_multiply(parent_joint_matrix,local_matrix)
def create_matrix(self):
c = cos(radians(self.rotation))
s = sin(radians(self.rotation))
R = numpy.matrix([[c,-s,0],[s,c,0],[0,0,1]])
S = numpy.matrix([[self.scale_s,0,0],[0,self.scale_t,0],[0,0,1]])
C = numpy.matrix([[1,0,self.center_s],[0,1,self.center_t],[0,0,1]])
T = numpy.matrix([[1,0,self.translation_s],[0,1,self.translation_t],[0,0,1]])
# Only types 0x00, 0x06, 0x07, 0x08 and 0x09 have been tested
if self.matrix_type in {0x00,0x02,0x0A,0x0B,0x80}:
P = numpy.matrix([[1,0,0,0],[0,1,0,0],[0,0,0,1]])
elif self.matrix_type == 0x06:
P = numpy.matrix([[0.5,0,0,0.5],[0,-0.5,0,0.5],[0,0,0,1]])
elif self.matrix_type == 0x07:
P = numpy.matrix([[0.5,0,0.5,0],[0,-0.5,0.5,0],[0,0,1,0]])
elif self.matrix_type in {0x08,0x09}:
P = numpy.matrix([[0.5,0,0.5,0],[0,-0.5,0.5,0],[0,0,1,0]])*numpy.matrix(self.projection_matrix)
else:
raise ValueError('invalid texture matrix type')
M = T*C*S*R*C.I*P
if self.shape == gx.TG_MTX2x4:
return M[:2,:]
elif self.shape == gx.TG_MTX3x4:
return M
else:
raise ValueError('invalid texture matrix shape')
def update(self,time):
scale_x = self.scale_x.interpolate(time)
scale_y = self.scale_y.interpolate(time)
scale_z = self.scale_z.interpolate(time)
rotation_x = self.rotation_x.interpolate(time)
rotation_y = self.rotation_y.interpolate(time)
rotation_z = self.rotation_z.interpolate(time)
translation_x = self.translation_x.interpolate(time)
translation_y = self.translation_y.interpolate(time)
translation_z = self.translation_z.interpolate(time)
cx = cos(radians(rotation_x))
sx = sin(radians(rotation_x))
cy = cos(radians(rotation_y))
sy = sin(radians(rotation_y))
cz = cos(radians(rotation_z))
sz = sin(radians(rotation_z))
R = numpy.matrix([[0,0,0,0],[0,0,0,0],[0,0,0,0],[0,0,0,1.0]]) #<-?
R[0,0] = cy*cz
R[0,1] = (sx*sy*cz - cx*sz)
R[0,2] = (cx*sy*cz + sx*sz)
R[1,0] = cy*sz
R[1,1] = (sx*sy*sz + cx*cz)
R[1,2] = (cx*sy*sz - sx*cz)
R[2,0] = -sy
R[2,1] = sx*cy
R[2,2] = cx*cy
S = numpy.matrix([[scale_x,0,0,0],[0,scale_y,0,0],[0,0,scale_z,0],[0,0,0,1]])
C = numpy.matrix([[1,0,0,self.center_x],[0,1,0,self.center_y],[0,0,1,self.center_z],[0,0,0,1]])
T = numpy.matrix([[1,0,0,translation_x],[0,1,0,translation_y],[0,0,1,translation_z],[0,0,0,1]])
self.texture_matrix[:] = (T*C*S*R*C.I)[:self.row_count,:]
def update_projection_matrix(self):
u = self.z_near*tan(radians(self.fov))
r = u*self.width()/self.height()
self.projection_matrix = create_frustum_matrix(-r,r,-u,u,self.z_near,self.z_far)
self.projection_matrix_need_update = False