二、高阶案例学习
Camera Random Trajectories
在这个示例中,展示了如何使用随机游走抽样器来生成摄像机轨迹。主要步骤如下:
-
先确定一个兴趣点poi(相机“看”的目标)。
-
给poi叠加一个小幅随机漂移 poi_drift[ i ]目标在抖动/移动)。
-
相机沿着四分之一圆的轨迹移动(位置 location_cam 随帧 i 变化)。
-
让相机朝向 poi + poi_drift[ i ];再在此基础上叠加一个小幅角度抖动(axis-angle 随帧随机),形成每帧的最终外参 cam2world 并加入渲染队列。
-
降低采样数以便快速渲(set_max_amount_of_samples(50))。
全部代码如下:
import blenderproc as bproc
import numpy as np
import argparse
import mathutils
from blenderproc.python.utility.SetupUtility import SetupUtility
parser = argparse.ArgumentParser()
parser.add_argument('scene', nargs='?', default="advanced/resources/scene.obj", help="Path to the scene.obj file")
parser.add_argument('output_dir', nargs='?', default="advanced/camera_random_trajectories/output", help="Path to where the final files, will be saved")
args = parser.parse_args()
bproc.init()
# load the objects into the scene
objs = bproc.loader.load_obj(args.scene)
# define a light and set its location and energy level
light = bproc.types.Light()
light.set_type("POINT")
light.set_location([5, -5, 5])
light.set_energy(1000)
#通常计算传入对象集合的兴趣点(常为几何中心/质心)
poi = np.array(bproc.object.compute_poi(objs), dtype=np.float32)
#为 POI 生成一个“三维随机游走”的位移序列(逐帧的位置漂移)
poi_drift = bproc.sampler.random_walk(
total_length = 25, # 共生成 25 帧/步
dims = 3, # 3 维(x,y,z 都漂移)
step_magnitude = 0.005,# 单步步长(幅度因子)
window_size = 5, # 滑动窗口平滑(越大越平滑)
interval = [-0.03, 0.03], # 限制漂移分量的取值范围(夹在这个区间)
distribution = 'uniform' # 采样分布:均匀
)
# 为“角度”做 1 维随机游走,作为相机抖动的角度大小(弧度)
camera_shaking_rot_angle = bproc.sampler.random_walk(
total_length = 25,
dims = 1,
step_magnitude = np.pi/32, # 单步角度变化上限(约 5.6°)
window_size = 5,
interval = [-np.pi/6, np.pi/6], # 角度被限制在 ±30° 内
distribution = 'uniform',
order = 2 # 高阶滤波/平滑阶数(2 表示更平滑的变化)
)
# 为“旋转轴”做 3 维随机游走,然后逐帧单位化(变成单位向量)
camera_shaking_rot_axis = bproc.sampler.random_walk(
total_length = 25, dims = 3,
window_size = 10, distribution = 'normal'
)
camera_shaking_rot_axis /= np.clip(np.linalg.norm(camera_shaking_rot_axis, axis=1, keepdims=True), 1e-8, None)
# 对每一帧得到一个 3D 轴向量,然后归一化成单位轴。分布用 normal(高斯),window_size=10 让轴缓慢变化,避免每帧乱跳
for i in range(25):
# 定义相机的轨迹:x,y 绕原点旋转,z 高度不变
location_cam = np.array([
10*np.cos(i/25 * np.pi), # x: 半径 10 的圆
10*np.sin(i/25 * np.pi), # y
8 # z 固定在 8
])
# 让相机“看向”目标(兴趣点 + 漂移),求一个“看向旋转矩阵”
rotation_matrix = bproc.camera.rotation_from_forward_vec(poi + poi_drift[i] - location_cam)
# 把“轴-角”变成旋转矩阵:构造一个小抖动旋转 R_rand
R_rand = np.array(
mathutils.Matrix.Rotation(
camera_shaking_rot_angle[i], # 本帧抖动角(标量,弧度)
3, # 3x3 旋转
camera_shaking_rot_axis[i] # 本帧抖动轴(单位向量)
)
)
# 把抖动叠加到相机朝向上
rotation_matrix = R_rand @ rotation_matrix
# 把位置 + 旋转组成 cam->world 齐次外参,并加入相机轨迹
cam2world_matrix = bproc.math.build_transformation_mat(location_cam, rotation_matrix)
bproc.camera.add_camera_pose(cam2world_matrix)
# 降低每像素采样数,快速渲染(噪点会多,但出图快)
bproc.renderer.set_max_amount_of_samples(50)
# render the whole pipeline
data = bproc.renderer.render()
# write the data to a .hdf5 container
bproc.writer.write_hdf5(args.output_dir, data)
# write the animations into .gif files
bproc.writer.write_gif_animation(args.output_dir, data, frame_duration_in_ms=80)
结果如下:

补充说明:
random_walk 通用参数
total_length:生成多少帧(要和你的循环帧数一致)。dims:维度(角度取 1,轴/位移取 3)。step_magnitude:单步变化幅度。interval:把数值钳制在某区间(防止漂太远/角度太大)。window_size:时间平滑窗口,越大越平滑。distribution:‘uniform’ 或 ‘normal’。order:高阶平滑(对角度很有用)。
rotation_from_forward_vec(target - location)
- 计算相机“look-at”朝向。你只要提供从相机到目标的向量即可。
- 若想固定地平线(不roll),有的版本支持参数
inplane_rot=0;若无,可后处理约束滚转。
乘法顺序
R_rand @ R_look:更像“世界系扰动”。R_look @ R_rand:更像“相机手抖”(局部扰动)。- 二者皆可,按你想要的视觉效果选。
590

被折叠的 条评论
为什么被折叠?



