blender导出相机参数

前言:在 Blender 的图形化界面中,没有直接的选项可以导出渲染图片的外参矩阵。你可以通过 Python API 来获取并导出相机的外参矩阵。

blender导出相机参数

1.单张照片

python 复制代码
import numpy as np   
import bpy
# 获取当前活动相机
cam = bpy.context.scene.camera
# 获取相机的外参矩阵
matrix_world = cam.matrix_world
# 将矩阵转换为 numpy 数组
matrix_np = np.array(matrix_world)
# 保存到文件
np.savetxt('E:\\matrix.txt', matrix_np)

2.多张照片(视频)

python 复制代码
import bpy
import numpy as np

# 获取当前场景
scene = bpy.context.scene

# 获取当前活动相机
cam = bpy.context.scene.camera

# 创建一个空的列表来保存所有的矩阵
matrices = []

# 对于场景中的每一帧
for frame in range(scene.frame_start, scene.frame_end + 1):
    # 设置当前帧
    scene.frame_set(frame)
    
    # 获取相机的外参矩阵
    matrix_world = cam.matrix_world
    
    # 将矩阵转换为 numpy 数组并添加到列表中
    matrices.append(np.array(matrix_world))
    
    # 将矩阵转换为 numpy 数组
    matrix_np = np.array(matrix_world)
    
    # 保存到文件,每一帧保存为一个文件
    np.savetxt(f'E:\\briefcase\\matrix_{frame}.txt', matrix_np)

# 将所有的矩阵保存到一个文件中
np.save('E:\\briefcase\\matrices.npy', matrices)
python 复制代码
numpy.load('matrices.npy')

3.坐标系转换

在 Blender 中,相机的外参矩阵是以右手坐标系表示的,而 OpenCV 使用的是左手坐标系。

python 复制代码
import bpy
import numpy as np

# 获取当前场景
scene = bpy.context.scene

# 获取当前活动相机
cam = bpy.context.scene.camera

# 创建一个空的列表来保存所有的矩阵
matrices = []

# 对于场景中的每一帧
for frame in range(scene.frame_start, scene.frame_end + 1):
    # 设置当前帧
    scene.frame_set(frame)
    
    # 获取相机的外参矩阵
    matrix_world = cam.matrix_world
    
    # 将矩阵转换为 numpy 数组
    matrix_np = np.array(matrix_world)
    
    # 创建一个转换矩阵
    transform = np.array([[1, 0, 0, 0],
                          [0, -1, 0, 0],
                          [0, 0, -1, 0],
                          [0, 0, 0, 1]])
    
    # 将 Blender 的外参矩阵转换为 OpenCV 的坐标系
    matrix_opencv = np.dot(transform, matrix_np)
    
    # 保存到文件,每一帧保存为一个文件
    np.savetxt(f'matrix_{frame}.txt', matrix_opencv)
相关推荐
三只坚果1 天前
blender制作动画导入unity两种方式
unity·游戏引擎·blender
devwolf2 天前
【verge3d】如何在项目里调用接口
blender·web3d·verge3d
3D虚拟工厂17 天前
Blender 智能模型库 | 人物·建筑·场景·机械等 近万高精度模型
blender
陶甜也17 天前
MCP-Blender插件的安装和使用
前端·ai·blender
山楂树の20 天前
模型优化——在MacOS 上使用 Python 脚本批量大幅度精简 GLB 模型(通过 Blender 处理)
python·macos·3d·图形渲染·blender
渲染101专业云渲染1 个月前
Blender 云渲染高效流程:渲染 101 集群加速实战
服务器·分布式·blender·maya·houdini
fcm191 个月前
blender 导入的fbx模型位置错乱
blender
阿拉丁的梦1 个月前
【3D大比拼第一集】--max,maya,c4d,blender的命令搜索功能
blender·maya
BJ_bafangonline1 个月前
如何更改Blender插件安装位置呢?
blender
小赖同学啊1 个月前
将Blender、Three.js与Cesium集成构建物联网3D可视化系统
javascript·物联网·blender