用blender球形环绕物体拍摄
相机位置
相机位置使用球面函数计算。
相机位置 V:
相机旋转根据欧拉角计算
blender中相机坐标系
xyz的欧拉角旋转是绕世界系的xyz旋转的。
欧拉角计算:
旋转根据当前相机位置:
θz = acos(y*(x,y,0))
如果x >0
θz = 180-θz
反之
θz = θz - 180
θx = acos(z*V)
θy = 恒等于0
def creatCamera(T_location,Rotation_euler,scale):
scene = bpy.context.scene
sensor_width_in_mm = K[1][1]*K[0][2] / (K[0][0]*K[1][2])
sensor_height_in_mm = 1 # doesn't matter
resolution_x_in_px = K[0][2]*2 # principal point assumed at the center
resolution_y_in_px = K[1][2]*2 # principal point assumed at the center
s_u = resolution_x_in_px / sensor_width_in_mm
s_v = resolution_y_in_px / sensor_height_in_mm
# TODO include aspect ratio
f_in_mm = K[0][0] / s_u
# recover original resolution
scene.render.resolution_x = resolution_x_in_px / scale
scene.render.resolution_y = resolution_y_in_px / scale
scene.render.resolution_percentage = scale * 100
# create a new camera
bpy.ops.object.add(
type='CAMERA',
location=T_location)
ob = bpy.context.object
ob.name = 'CamFrom3x4PObj'
cam = ob.data
cam.name = 'CamFrom3x4P'
# Lens
cam.type = 'PERSP'
cam.lens = f_in_mm
cam.lens_unit = 'MILLIMETERS'
cam.sensor_width = sensor_width_in_mm
pi = math.pi
ob.rotation_mode = 'XYZ'
ob.rotation_euler[0] = Rotation_euler[0] * (pi / 180.0)
ob.rotation_euler[1] = Rotation_euler[1] * (pi / 180.0)
ob.rotation_euler[2] = Rotation_euler[2] * (pi / 180.0)
# Display
cam.show_name = True
# Make this the current camera
scene.camera = ob
#bpy.context.scene.update()
def getRotationFromLocation(T_location):
V = T_location
Y_axis = Vector((.0 , 1.0 ,.0))
Z_axis = Vector((.0 , .0 ,1.0))
tempV = Vector((T_location[0] , T_location[1] ,.0))
pi = math.pi
sitaZ = tempV.angle(Y_axis)
sitaZ = (180.0 * sitaZ) / pi
sitaX = V.angle(Z_axis)
sitaX = (180.0 * sitaX) / pi
if(T_location[0] < 0):
Rotation_euler = Vector((sitaX , 1.0 ,sitaZ-180.0))
else:
Rotation_euler = Vector((sitaX , 1.0 ,180.0-sitaZ))
print(Rotation_euler)
return Rotation_euler
if __name__ == "__main__":
pi = math.pi
nRows = 5
nCols = 14
weidu_angles = [30,60,90,120,150]
r = 10.0
pi = math.pi
for j in range(nRows):
weidu = weidu_angles[j] / 180.0 * pi
for i in range(nCols):
jingdu = (i*360.0/nCols - 180.0) / 180.0 * pi
print(math.sin(weidu))
x = r*math.sin(weidu)*math.cos(jingdu)
y = r*math.sin(weidu)*math.sin(jingdu)
z = r*math.cos(weidu)
K = Matrix(
([2666.666748046875 , 0 , 960.0],
[0 , 2666.666748046875 , 540.0],
[0 , 0 , 1])
)
T_location = Vector((x , y , z))
print(T_location)
#T_location = Vector((-13.9922 , -9.63222 , 4.01505))
Rotation_euler = getRotationFromLocation(T_location)
#Rotation_euler = Vector((76.8264 , 0 , -55.3262))
creatCamera(T_location,Rotation_euler,1)
结果: