-
Notifications
You must be signed in to change notification settings - Fork 4
/
lightfield_plane.py
97 lines (78 loc) · 3.04 KB
/
lightfield_plane.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
import math
import random
import bpy
import bmesh
from mathutils import Color
from .lightfield import LightfieldPropertyGroup
from .camera_position import CameraPosition
class LightfieldPlane(LightfieldPropertyGroup):
def construct(self):
visuals = self.default_construct()
self.lf_type = 'PLANE'
self.obj_empty.empty_display_type = 'PLAIN_AXES'
# Update lightfield references
self.obj_visuals.add().obj_visual = visuals[0]
self.obj_visuals.add().obj_visual = visuals[1]
self.obj_visuals.add().obj_visual = visuals[2]
self.obj_grid = visuals[0]
self.set_camera_to_first_view()
def construct_visuals(self, collection):
grid = self.create_grid()
space = self.create_space()
front = self.create_front()
# Add to lightfield collection
collection.objects.link(grid)
collection.objects.link(space)
collection.objects.link(front)
return [grid, space, front]
def create_space(self):
"""
Create visual that represents the space the lightfield is occupying.
:return: Object.
"""
name = self.construct_names()['space']
bpy.ops.mesh.primitive_plane_add(location=(0, 0, 0))
p1 = bpy.context.object
dumped_mesh = p1.data
bpy.ops.mesh.primitive_plane_add(location=(0, 0, 0))
space = bpy.context.object
space.name = name
p1.select_set(True)
bpy.ops.object.join()
space.scale = [0.5] * 3
space.rotation_euler[0] = 0.5 * math.pi
# Remove mesh-data created by p1 which is not necessary anymore
bpy.data.meshes.remove(dumped_mesh)
space.data.name = name
# Unlink the object from its current collection
space.users_collection[0].objects.unlink(space)
space_mat = bpy.data.materials.new(name)
col = Color()
col.hsv = (random.random(), 1.0, 0.8)
space_mat.diffuse_color = col[:] + (0.1,)
space.data.materials.append(space_mat)
space.show_wire = True
space.hide_render = True
return space
@staticmethod
def construct_names():
base = "LFPlane"
return {'lightfield': base,
'camera': "{}_Camera".format(base),
'grid': "{}_Grid".format(base),
'space': "{}_Space".format(base),
'front': "{}_Front".format(base)}
def position_generator(self):
cube = self.cube_camera
for y in range(self.num_cams_y):
for x in range(self.num_cams_x):
# TODO: implement cube_camera in plane lightfield
yield self.get_camera_pos(x, y)
def get_camera_pos(self, x, y):
base_x = 1 / (self.num_cams_x - 1)
base_y = 1 / (self.num_cams_y - 1)
return CameraPosition("view_{:04d}f".format(y * self.num_cams_x + x),
-0.5 + x * base_x,
0.0,
0.5 - y * base_y,
alpha=0.5*math.pi)