File size: 2,400 Bytes
9f3781c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import bpy
import csv
from math import pi

# 设置导出文件的路径
export_file = "/home/falcary/workstation/blender_env_indoors_dataset/outputs/Capsule/output_in_blender.csv"

# 假设动画以24帧每秒的速度播放
# 每帧之间的时间间隔(秒)
frame_duration = 1 / 10

# 获取当前激活的相机对象
camera = bpy.context.scene.camera

# 如果存在相机且相机类型是'CAMERA'
if camera is not None and camera.type == 'CAMERA':
    # 获取当前场景的起始和结束帧
    start_frame = bpy.context.scene.frame_start
    end_frame = bpy.context.scene.frame_end
    
    # 准备存储相机变换数据的列表
    camera_transforms = []
    
    # 遍历指定的帧范围内的每一帧
    for frame in range(start_frame, end_frame + 1):
        # 设置当前帧
        bpy.context.scene.frame_set(frame)
        # 更新场景以获取最新数据
        bpy.context.view_layer.update()
        bpy.context.evaluated_depsgraph_get().update()
        
        # 获取相机的世界坐标位置
        loc = camera.matrix_world.to_translation()
        # 获取相机的世界旋转(欧拉角)
        rot = camera.matrix_world.to_euler('XYZ')
        
        # 计算时间戳,假设帧率为24fps,将帧转换为微秒
        timestamp = (frame - start_frame) * frame_duration * 1e7
        scale = 1.0
        # 添加位置和旋转数据到列表
        camera_transforms.append({
            'timestamp': int(timestamp),  # 时间戳为整数微秒
            'x': loc.x * scale,
            'y': loc.y * scale,
            'z': loc.z * scale,
            # 将旋转角度转换为弧度,确保在[0, 2*pi)范围内
            'rx': (rot.x + 2 * pi) % (2 * pi),
            'ry': (rot.y + 2 * pi) % (2 * pi),
            'rz': (rot.z + 2 * pi) % (2 * pi),
        })
    
    # 导出到CSV文件
    with open(export_file, 'w', newline='') as csvfile:
        fieldnames = ['timestamp', 'x', 'y', 'z', 'rx', 'ry', 'rz']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
        
        # 写入表头
        csvfile.write('# ' + ', '.join(fieldnames) + '\n')
        
        # 写入变换数据
        for transform in camera_transforms:
            writer.writerow(transform)
    
    print(f"Camera transforms exported to {export_file}")
else:
    print("No camera selected or active object is not a camera.")