-
Notifications
You must be signed in to change notification settings - Fork 1
/
autofocus_modern.py
314 lines (246 loc) · 9.41 KB
/
autofocus_modern.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
'''
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
'''
import bpy
import bl_math
from bpy.props import (
FloatProperty,
BoolProperty,
CollectionProperty,
StringProperty,
IntProperty
)
from bpy.types import (
Panel,
PropertyGroup,
)
from bpy.app.handlers import persistent
from mathutils import Vector
import time
last_time = time.time()
elapsed = 0.0
lerp_locations = {}
def find_cam(scn, af):
for obj in scn.objects:
if(obj.type=='CAMERA' and obj.data.autofocus != None
and obj.data.autofocus == af):
return obj
return None
def set_enabled(self, value):
self["enabled"] = value
scn = bpy.context.scene
cam = bpy.context.object # find_cam(scn, self)
if value:
uid = cam.name + str(time.time())
cam.data.autofocus.uid = uid
a_cam = scn.autofocus_properties.active_cameras.add()
a_cam.camera = cam
a_cam.name = uid # cam.name
if uid not in lerp_locations:
lerp_locations[uid] = [0, True, 0, 1]
reset_clock()
else:
i = scn.autofocus_properties.active_cameras.find(cam.data.autofocus.uid)
scn.autofocus_properties.active_cameras.remove(i)
# cam.data.autofocus.smooth = False
try:
del lerp_locations[cam.data.autofocus.uid]
except KeyError:
pass
def get_enabled(self):
if self.get("enabled") == None:
return False
else:
return self["enabled"]
def set_smooth_enabled(self, value):
self["smooth"] = value
def get_smooth_enabled(self):
if self.get("smooth") == None:
return False
else:
return self["smooth"]
def set_smooth_offset(self, value):
self["smooth_offset"] = value
def get_smooth_offset(self):
if self.get("smooth_offset") == None:
return 0
else:
return self["smooth_offset"]
def set_timer_enabled(self, value):
self["enabled"] = value
reset_clock()
def get_timer_enabled(self):
if self.get("enabled") == None:
return False
else:
return self["enabled"]
class AutoFocus_Properties(PropertyGroup):
enabled: BoolProperty(
name="Enabled",
default=False,
description="Enable auto focus for this camera.",
get=get_enabled,
set=set_enabled
)
smooth: BoolProperty(
name="Smoothing",
default=False,
description="Enable smoothing for auto focus using slow parent.",
get=get_smooth_enabled,
set=set_smooth_enabled
)
smooth_offset: IntProperty(
name="Offset",
min=1,
default=1,
description="Higher values mean slower focusing. 24 means smooth focus will be complete in 1 second",
get=get_smooth_offset,
set=set_smooth_offset
)
uid: StringProperty(
name="UID",
default="",
description="Unique Identifier"
)
class AutoFocus_Active_Camera(PropertyGroup):
name: bpy.props.StringProperty(name="cameraName",
default="",
description="Active Camera Name")
camera: bpy.props.PointerProperty(type=bpy.types.Object)
class AutoFocus_Scene_Properties(PropertyGroup):
active_cameras: CollectionProperty(
type=AutoFocus_Active_Camera
)
rate_enabled: BoolProperty(
name="Timer",
default=True,
description="Enable timer between AutoFocus updates. (Improves performance)",
get=get_timer_enabled,
set=set_timer_enabled
)
rate_seconds: FloatProperty(
name="Seconds",
default=0.5,
description="The rate in seconds between AutoFocus updates. (Disabled = update on every scene update)"
)
class AutoFocus_Panel(Panel):
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = 'data'
bl_label = "Auto Focus"
@classmethod
def poll(cls, context):
ob = context.object
return ob and ob.type == 'CAMERA' and context.object.data
def draw_header(self, context):
af = context.object.data.autofocus
self.layout.prop(af, "enabled", text="")
def draw(self, context):
af = context.object.data.autofocus
layout = self.layout
row = layout.row()
row.active = af.enabled
row.prop(af, "smooth")
row = layout.row()
row.enabled = af.smooth
row.prop(af, "smooth_offset")
split = layout.split()
split.prop(context.scene.autofocus_properties, "rate_enabled")
split.prop(context.scene.autofocus_properties, "rate_seconds")
@persistent
def scene_update(scn, depsgraph):
if scn.autofocus_properties.rate_enabled and not check_clock(scn):
return
for n, cam in enumerate(bpy.context.scene.autofocus_properties.active_cameras):
try:
if cam.camera.type != "CAMERA": continue
cam = cam.camera
except:
scn.autofocus_properties.active_cameras.remove(n)
cam_matrix = cam.matrix_world
org = cam_matrix @ Vector((0.0, 0.0, 0.0))
dst = cam_matrix @ Vector((0.0, 0.0, -100.0))
dir = dst - org
result, location, normal, index, object, matrix = scn.ray_cast(depsgraph, org, dir)
if result:
current_dist = cam.data.dof.focus_distance
new_dist_vector = cam.location - location
new_dist = new_dist_vector.length
if cam.data.autofocus.enabled:
if cam.data.autofocus.smooth:
global lerp_locations # get lerp locs for smooth
i = scn.autofocus_properties.active_cameras.find(cam.data.autofocus.uid)
if cam.data.autofocus.uid not in lerp_locations:
lerp_locations[cam.data.autofocus.uid] = [0, True, 0, 1]
if isDistAlmostEqual(current_dist, new_dist):
lerp_locations[cam.data.autofocus.uid] = [new_dist, True, current_dist, 1]
else:
lerp_locations[cam.data.autofocus.uid] = [new_dist, False, lerp_locations[cam.data.autofocus.uid][2], 1 ]
if not bpy.app.timers.is_registered(run_24_times):
bpy.app.timers.register(run_24_times)
else:
# print("setting dist")
cam.data.dof.focus_distance = new_dist
@persistent
def run_24_times():
if bpy.context.scene.autofocus_properties.rate_enabled and not check_clock(bpy.context.scene):
return 0.041
# print(len(bpy.context.scene.autofocus_properties.active_cameras))
for n, cam in enumerate(bpy.context.scene.autofocus_properties.active_cameras):
if cam.camera.type != "CAMERA": continue
cam = cam.camera
if cam.data.autofocus.uid not in lerp_locations or not cam.data.autofocus.smooth:
print('continuing')
continue
offset = cam.data.autofocus.smooth_offset
offset = offset if offset else 24
if lerp_locations[cam.data.autofocus.uid][3] >= offset:
lerp_locations[cam.data.autofocus.uid][3] = 0
else:
if lerp_locations[cam.data.autofocus.uid][3] == None:
lerp_locations[cam.data.autofocus.uid][3] = 1
else:
lerp_locations[cam.data.autofocus.uid][3] += 1
foc_dist = cam.data.dof.focus_distance
lerp_dest = lerp_locations[cam.data.autofocus.uid][0]
isDestSame = lerp_locations[cam.data.autofocus.uid][1]
current_dist = foc_dist
if isDestSame:
continue
# if current smooth foc_dist not equal lerp_dest,
# then get initial_loc and reset global frame counter
if not abs(foc_dist - lerp_dest) < 0.01 and isDestSame:
lerp_locations[cam.data.autofocus.uid][3] = 1
step_destination = bl_math.lerp(current_dist, lerp_dest, lerp_locations[cam.data.autofocus.uid][3] / offset)
cam.data.dof.focus_distance = step_destination
return 0.041
def isPosEqual(pos1, pos2):
return pos1.x == pos2.x and pos1.y == pos2.y and pos1.z == pos2.z
def isDistEqual(dist1, dist2):
return dist1 == dist2
def isDistAlmostEqual(dist1, dist2):
return abs(dist1 - dist2) < 0.01
def check_clock(scn):
global last_time
global elapsed
cur_time = time.time()
elapsed += cur_time - last_time
last_time = cur_time
if elapsed >= scn.autofocus_properties.rate_seconds:
elapsed = 0.0
return True
return False
def reset_clock():
global last_time
global elapsed
last_time = time.time()
elapsed = 0.0