-
Notifications
You must be signed in to change notification settings - Fork 0
/
operators.py
276 lines (201 loc) · 8.29 KB
/
operators.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
import json
import bpy
import os
import random
from bpy.types import Operator
from . import utils
from .evertims import ( Evertims )
# ############################################################
# Methods triggered from UI
# ############################################################
# start / stop main auralization
class EvertimsRun(Operator):
# header
bl_label = "run auralization"
bl_idname = 'evertims.run'
bl_options = {'REGISTER'}
# shape input argument
arg: bpy.props.StringProperty(name='arg', default='')
# init locals
_evertims = Evertims()
_handle_timer = None
# add local callback to blender stack
@staticmethod
def handle_add(self, context):
# add local modal to blender callback stack (will call self.modal method from now on)
context.window_manager.modal_handler_add(self)
# setup timer to force modal callback execution more often than blender's default
EvertimsRun._handle_timer = context.window_manager.event_timer_add(0.075, window=context.window)
# debug
if context.scene.evertims.debug_logs: print(__name__, 'added evertims callback to draw_handler')
# remove local callback from blender stack
@staticmethod
def handle_remove(context):
# check that handle_add has been called before
if EvertimsRun._handle_timer is not None:
# remove timer callback setup in handle_add
context.window_manager.event_timer_remove(EvertimsRun._handle_timer)
# reset locals
EvertimsRun._handle_timer = None
# debug
if context.scene.evertims.debug_logs: print(__name__, 'removed evertims callback from draw_handler')
@classmethod
def poll(cls, context):
return context.area.type == 'VIEW_3D'
# method called from UI
def invoke(self, context, event):
# init locals
scene = context.scene
evertims = scene.evertims
addon_prefs = context.preferences.addons[__package__].preferences
# start auralization
if self.arg == 'start':
# check scene integrity
(status, msg) = utils.checkSceneIntegrity(context, evertims)
if status != {'PASS'}:
self.report(status, msg)
return {'CANCELLED'}
# pass parameters to evertims
self._evertims.setup(evertims)
# start evertims
self._evertims.start()
# flag auralization on
evertims.enable_auralization = True
# add local callback to blender stack
self.handle_add(self,context)
return {'RUNNING_MODAL'}
# stop auralization
elif self.arg == 'stop':
# flag auralization off
evertims.enable_auralization = False
return {'FINISHED'}
# crystalize visible rays
elif self.arg == 'solidify':
self._evertims.solidifyVisibleRays()
return {'FINISHED'}
# local modal callback (run always)
def modal(self, context, event):
# init locals
scene = context.scene
evertims = scene.evertims
# auralization stop flagged: kill modal
if not evertims.enable_auralization:
self.cancel(context)
return {'CANCELLED'}
# execute modal on timer event
elif event.type == 'TIMER':
# execute evertims internal callback
self._evertims.update()
# force bgl rays redraw (else only redraw rays on user input event)
if not context.area is None:
context.area.tag_redraw()
return {'PASS_THROUGH'}
# modal cancel method, called (when modal enabled) when blender quit or load new scene
def cancel(self, context):
# remove local callback
self.handle_remove(context)
# remove nested callback
self._evertims.stop()
# force redraw to clean scene of rays
if not context.area is None:
context.area.tag_redraw()
# import materials
class EvertimsImport(Operator):
# header
bl_idname = 'evertims.import'
bl_label = "various import operations"
bl_options = {'REGISTER'}
# shape input arguments
arg: bpy.props.StringProperty(name='arg', default='')
# method called from UI
def execute(self, context):
# init locals
evertims = context.scene.evertims
# import materials
if self.arg == 'materials':
# forced refresh
(status, msg) = utils.loadMaterials(context, evertims, True)
if status != {'PASS'}:
self.report(status, msg)
return {'CANCELLED'}
# convert local string to dict
matDict = utils.str2matDict(evertims.materials)
# create materials if need be
existingMatNameList = [x.name for x in bpy.data.materials]
for matName in matDict:
if( matName not in existingMatNameList ):
bpy.data.materials.new(name=matName)
bpy.data.materials[matName].diffuse_color = (random.random(), random.random(), random.random(), 1.0)
# # define source directivity (example of how to start on a per-source basis)
# if self.arg == 'source':
# # get reference to source
# source = bpy.data.objects[evertims.source_object]
# # init rna (see https://blender.stackexchange.com/questions/43785/adding-other-types-of-custom-properties/43786)
# rna_ui = source.get('_RNA_UI')
# if rna_ui is None:
# source['_RNA_UI'] = {}
# rna_ui = source['_RNA_UI']
# # init directivity type
# source["directivity_type"] = "disabled"
# rna_ui["directivity_type"] = {
# "description": "File or procedural method to define source directivity",
# "items": {
# # ("file", "File", "Loaded SOFA file"),
# ("disabled", "Disable", "No directivity"),
# ("custom", "Custom", "Custom procedural directivity")
# },
# "default": "custom",
# }
# # source["directivity_type"] = bpy.props.EnumProperty(items=test_items, default="custom")
# # define freq
# source["evert_dir_freq"] = (125, 250, 500, 1000, 2000, 4000, 8000, 16000)
# # define selectivity
# for iFreq in range(0, len(source["evert_dir_freq"])):
# key = "evert_dir_select_" + str(iFreq)
# source[key] = 0.0
# rna_ui[key] = { "description": "Float",
# "default": 0.0,
# "min": 0.0,
# "max": 100.0,
# "soft_min": 0.0,
# "soft_max": 100.0 }
return {'FINISHED'}
# export scene to disk
class EvertimsExport(Operator):
# header
bl_label = "export scene"
bl_idname = 'evertims.export'
bl_options = {'REGISTER'}
# method called from UI
def execute(self, context):
# init locals
evertims = context.scene.evertims
_evertims = Evertims()
# check scene integrity
(status, msg) = utils.checkSceneIntegrity(context, evertims)
if status != {'PASS'}:
self.report(status, msg)
return {'CANCELLED'}
# check export file name
filePath = bpy.path.abspath(evertims.export_file_path)
(status, msg) = utils.isValidExportPath(filePath)
if status != {'PASS'}:
self.report(status, msg)
return {'CANCELLED'}
# export scene to disk
_evertims.exportSceneAsOscList(evertims)
return {'FINISHED'}
# ############################################################
# Register / Unregister
# ############################################################
classes = (
EvertimsRun,
EvertimsExport,
EvertimsImport
)
def register():
for cls in classes:
bpy.utils.register_class(cls)
def unregister():
for cls in classes:
bpy.utils.unregister_class(cls)