-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconfig.py
196 lines (169 loc) · 8.77 KB
/
config.py
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import bpy
import csv
import os
import json
from . import utils, file_utils
# Export configuration of current setup for later use.
class EXPORT_OT_lightfield_config(bpy.types.Operator):
bl_idname = "lightfield.export_config"
bl_label = """Export lightfield configuration"""
bl_options = {'REGISTER'}
frame_number = bpy.props.IntProperty()
def execute(self, context):
lf = context.scene.lightfield[context.scene.lightfield_index]
lf = (utils.get_lightfield_class(lf.lf_type))(lf)
os.makedirs(lf.get_output_directory(frame_number=self.frame_number), exist_ok=True)
with open(lf.get_path_config_file_json(self.frame_number), mode='w', newline='') as json_file:
cam = lf.data_camera
sensor_size = []
if cam.sensor_fit == 'AUTO':
size = cam.sensor_width
res = max(lf.res_x, lf.res_y)
sensor_size = [size * lf.res_x / res, size * lf.res_y / res]
elif cam.sensor_fit == 'HORIZONTAL':
size = cam.sensor_width
sensor_size = [size, size * lf.res_y / lf.res_x]
elif cam.sensor_fit == 'VERTICAL':
size = cam.sensor_height
sensor_size = [size * lf.res_x / lf.res_y, size]
else:
raise Exception("Unknown sensor fit")
cfg = {
'camera': {
'type': cam.type,
},
'lf_type': lf.lf_type,
'resolution': [lf.res_x, lf.res_y],
'sensor_size': sensor_size,
}
if cam.type == 'PANO':
engine = context.engine
if engine == 'CYCLES':
ccam = cam.cycles
cfg['camera']['panorama_type'] = ccam.panorama_type
if ccam.panorama_type == 'FISHEYE_EQUIDISTANT':
cfg['camera']['fisheye_fov'] = ccam.fisheye_fov
elif ccam.panorama_type == 'FISHEYE_EQUISOLID':
cfg['camera']['fisheye_lens'] = ccam.fisheye_lens
cfg['camera']['fisheye_fov'] = ccam.fisheye_fov
elif ccam.panorama_type == 'EQUIRECTANGULAR':
cfg['camera']['latitude_min'] = ccam.latitude_min
cfg['camera']['latitude_max'] = ccam.latitude_max
cfg['camera']['longitude_min'] = ccam.longitude_min
cfg['camera']['longitude_max'] = ccam.longitude_max
else:
raise Exception("Panoramic lenses only supported in Cycles")
elif cam.type == 'PERSP':
cfg['camera']['lens_unit'] = cam.lens_unit
if cam.lens_unit == 'MILLIMETERS':
cfg['camera']['focal_length'] = cam.lens
elif cam.lens_unit == 'FOV':
# TODO this is ambiguous.
cfg['camera']['angle'] = cam.angle
projection_matrix = lf.obj_camera.calc_matrix_camera(
context.evaluated_depsgraph_get(),
x=context.scene.render.resolution_x,
y=context.scene.render.resolution_y,
scale_x=context.scene.render.pixel_aspect_x,
scale_y=context.scene.render.pixel_aspect_y)
cfg['camera']['projection_matrix'] = [[projection_matrix[r][c] for c in range(4)] for r in range(4)]
cfg['frames'] = []
json.dump(cfg, json_file, indent=2)
with open(lf.get_path_config_file(self.frame_number), mode='w', newline='') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
cam = lf.data_camera
camera_meta_fields = ["type"]
camera_meta = [cam.type]
if cam.type == 'PANO':
engine = context.engine
if engine == 'CYCLES':
ccam = cam.cycles
camera_meta_fields.append("panorama_type")
camera_meta.append(ccam.panorama_type)
if ccam.panorama_type == 'FISHEYE_EQUIDISTANT':
camera_meta_fields.append("fisheye_fov")
camera_meta.append(ccam.fisheye_fov)
elif ccam.panorama_type == 'FISHEYE_EQUISOLID':
camera_meta_fields.append("fisheye_lens")
camera_meta_fields.append("fisheye_fov")
camera_meta.append(ccam.fisheye_lens)
camera_meta.append(ccam.fisheye_fov)
elif ccam.panorama_type == 'EQUIRECTANGULAR':
camera_meta_fields.append("latitude_min")
camera_meta_fields.append("latitude_max")
camera_meta_fields.append("longitude_min")
camera_meta_fields.append("longitude_max")
camera_meta.append(ccam.latitude_min)
camera_meta.append(ccam.latitude_max)
camera_meta.append(ccam.longitude_min)
camera_meta.append(ccam.longitude_max)
else:
raise Exception("Panoramic lenses only supported in Cycles")
else:
camera_meta_fields.append("lens_unit")
camera_meta.append(cam.lens_unit)
if cam.lens_unit == 'MILLIMETERS':
camera_meta_fields.append("lens")
camera_meta.append(cam.lens)
elif cam.lens_unit == 'FOV':
camera_meta_fields.append("angle")
camera_meta.append(cam.angle)
writer.writerow(camera_meta_fields)
writer.writerow(camera_meta)
projection_matrix = lf.obj_camera.calc_matrix_camera(
context.evaluated_depsgraph_get(),
x=context.scene.render.resolution_x,
y=context.scene.render.resolution_y,
scale_x=context.scene.render.pixel_aspect_x,
scale_y=context.scene.render.pixel_aspect_y)
writer.writerow([lf.lf_type])
writer.writerow([lf.res_x, lf.res_y])
writer.writerow(["sensor_width", "sensor_height"])
if cam.sensor_fit == 'AUTO':
size = cam.sensor_width
res = max(lf.res_x, lf.res_y)
writer.writerow([size * lf.res_x / res, size * lf.res_y / res])
elif cam.sensor_fit == 'HORIZONTAL':
size = cam.sensor_width
writer.writerow([size, size * lf.res_y / lf.res_x])
elif cam.sensor_fit == 'VERTICAL':
size = cam.sensor_height
writer.writerow([size * lf.res_x / lf.res_y, size])
else:
raise Exception("Unknown sensor fit")
writer.writerow(["projection_matrix"])
for i in range(0, 4):
writer.writerow([projection_matrix[i][0],
projection_matrix[i][1],
projection_matrix[i][2],
projection_matrix[i][3]])
writer.writerow(["name", "x", "y", "z", "rot_x", "rot_y", "rot_z"])
return {'FINISHED'}
# Export part of a configuration to be appended, for later use.
class EXPORT_OT_lightfield_config_append(bpy.types.Operator):
bl_idname = "lightfield.export_config_append"
bl_label = """Append a camera position to the lightfield configuration"""
bl_options = {'REGISTER'}
frame_number = bpy.props.IntProperty()
filename = bpy.props.StringProperty()
def execute(self, context):
lf = context.scene.lightfield[context.scene.lightfield_index]
lf = (utils.get_lightfield_class(lf.lf_type))(lf)
with open(lf.get_path_config_file(self.frame_number), mode='a', newline='') as csv_file:
writer = csv.writer(csv_file, delimiter=',')
x, y, z = lf.obj_camera.matrix_world.to_translation()
rx, ry, rz = lf.obj_camera.matrix_world.to_euler()
writer.writerow([self.filename, x, y, z, rx, ry, rz])
with open(lf.get_path_config_file_json(self.frame_number), mode='r', newline='') as json_file:
cfg = json.load(json_file)
with open(lf.get_path_config_file_json(self.frame_number), mode='w', newline='') as json_file:
x, y, z = lf.obj_camera.matrix_world.to_translation()
rx, ry, rz = lf.obj_camera.matrix_world.to_euler()
cfg['frames'].append({
'name': self.filename,
'position': [x, y, z],
'rotation': [rx, ry, rz],
'world_matrix': [[lf.obj_camera.matrix_world[r][c] for c in range(4)] for r in range(4)],
})
json.dump(cfg, json_file, indent=2)
return {'FINISHED'}