-
Notifications
You must be signed in to change notification settings - Fork 5
/
__init__.py
405 lines (328 loc) · 13.9 KB
/
__init__.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
bl_info = {
"name": "BlendQuery",
"blender": (3, 0, 0),
"category": "Parametric",
}
RELOAD_DEBOUNCE_S = 1.0
import os
import re
import sys
import traceback
import importlib
import importlib.util
import bpy
from bpy.app.handlers import persistent
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
from setup_venv import setup_venv
venv_dir = setup_venv()
from poll import watch_for_text_changes
cadquery = None
build123d = None
# TODO: Find a better way to store/reset
are_dependencies_installed = False
def statusbar_progress_bar(self, context):
if bpy.context.window_manager.blendquery.is_regenerating:
layout = self.layout
row = layout.row()
row.progress(
text="Regenerating BlendQuery Objects",
factor=bpy.context.window_manager.blendquery.regeneration_progress,
type="BAR",
)
row.scale_x = 4
def register():
bpy.utils.register_class(ObjectPropertyGroup)
bpy.utils.register_class(BlendQueryPropertyGroup)
bpy.utils.register_class(BlendQueryImportDependenciesOperator)
bpy.utils.register_class(BlendQueryInstallOperator)
bpy.utils.register_class(BlendQueryRegenerateOperator)
bpy.utils.register_class(BlendQueryPanel)
bpy.utils.register_class(BlendQueryWindowPropertyGroup)
bpy.types.WindowManager.blendquery = bpy.props.PointerProperty(
type=BlendQueryWindowPropertyGroup
)
bpy.types.Object.blendquery = bpy.props.PointerProperty(
type=BlendQueryPropertyGroup
)
bpy.app.handlers.load_post.append(initialise)
bpy.types.STATUSBAR_HT_header.append(statusbar_progress_bar)
def unregister():
try:
bpy.types.STATUSBAR_HT_header.remove(statusbar_progress_bar)
bpy.app.handlers.load_post.remove(initialise)
except:
pass
del bpy.types.Object.blendquery
del bpy.types.WindowManager.blendquery
bpy.utils.unregister_class(BlendQueryPanel)
bpy.utils.unregister_class(BlendQueryWindowPropertyGroup)
bpy.utils.unregister_class(BlendQueryRegenerateOperator)
bpy.utils.unregister_class(BlendQueryInstallOperator)
bpy.utils.unregister_class(BlendQueryImportDependenciesOperator)
bpy.utils.unregister_class(BlendQueryPropertyGroup)
bpy.utils.unregister_class(ObjectPropertyGroup)
@persistent
def initialise(_=None):
bpy.ops.blendquery.import_dependencies()
if not are_dependencies_installed:
return
# TODO: find a cleaner solution than this
# as `update_object` may delete objects from `bpy.data.objects` to perform cleanup, iterate on a copy of it instead to avoid crashes due to `EXCEPTION_ACCESS_VIOLATION`
objects = []
for object in bpy.data.objects:
objects.append(object)
for object in objects:
update(object)
disposers = {}
def update(object):
blendquery = object.blendquery
script, reload = (
blendquery.script,
blendquery.reload,
)
if script is not None and reload is True:
if not object in disposers:
from debounce import debounce
def invoke_update_operator():
context_override = bpy.context.copy()
context_override["active_object"] = object
with bpy.context.temp_override(**context_override):
bpy.ops.blendquery.regenerate()
invoke_update_operator()
disposers[object] = watch_for_text_changes(
script,
debounce(RELOAD_DEBOUNCE_S)(invoke_update_operator),
)
elif object in disposers:
disposer = disposers[object]
if callable(disposer):
disposer()
class ObjectPropertyGroup(bpy.types.PropertyGroup):
object: bpy.props.PointerProperty(type=bpy.types.Object)
class BlendQueryPropertyGroup(bpy.types.PropertyGroup):
def _update(self, _):
update(self.id_data)
script: bpy.props.PointerProperty(
name="Script", type=bpy.types.Text, update=_update
)
reload: bpy.props.BoolProperty(name="Automatic Regeneration", default=True, update=_update)
object_pointers: bpy.props.CollectionProperty(type=ObjectPropertyGroup)
def ui_update(self, context):
if context.area:
for region in context.area.regions:
if region.type == "UI":
region.tag_redraw()
return None
class BlendQueryWindowPropertyGroup(bpy.types.PropertyGroup):
# TODO: Find a better way to store/reset
# TODO: can we also use the `ui_update` here and for other properties?
installing_dependencies: bpy.props.BoolProperty(
name="Installing",
default=False,
description="Whether BlendQuery is installing dependencies",
)
is_regenerating: bpy.props.BoolProperty(
update=ui_update
)
regeneration_progress: bpy.props.FloatProperty(
update=ui_update
)
class BlendQueryImportDependenciesOperator(bpy.types.Operator):
bl_idname = "blendquery.import_dependencies"
bl_label = "BlendQuery Import Dependecies"
import_error = None
def execute(self, context):
global are_dependencies_installed
try:
global cadquery, build123d
global regenerate_blendquery_object
cadquery = importlib.import_module("cadquery")
build123d = importlib.import_module("build123d")
from .blendquery import regenerate_blendquery_object
are_dependencies_installed = True
except Exception:
are_dependencies_installed = False
exception_trace = traceback.format_exc()
self.import_error = f"Failed to import BlendQuery dependencies: {exception_trace}"
# `self.report` does not seem to work within `execute` or `invoke`, so we call it within `modal`
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
def modal(self, context, event):
if self.import_error is not None:
self.report(
{"WARNING"},
self.import_error,
)
# Info area seems to lag behind so we must force it to redraw
# TODO: Find a way to avoid this
redraw_info_area()
return {"FINISHED"}
def create_parse_parametric_script_thread(script: str):
import threading, subprocess, queue, pickle
response = queue.Queue()
def process():
cwd = os.path.dirname(os.path.abspath(__file__))
parent_directory = os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))
env = os.environ.copy()
env['PATH'] = parent_directory
process = subprocess.Popen([sys.executable, 'parse.py'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=cwd, env=env)
process.stdin.write(pickle.dumps(script))
process.stdin.close()
assert process.stdout
stdout_data = process.stdout.read()
process.wait()
response.put(pickle.loads(stdout_data if stdout_data else b""))
# TODO: Investigate return code issue.
# if process.returncode == 0:
# response.put(process.stdout.read().decode('utf-8'))
# else:
# exception = process.stdout.read().decode('utf-8')
# error_output = process.stderr.read().decode('utf-8')
# response.put(RuntimeError("Subprocess failed with return code {}; {}; {}".format(process.returncode, error_output, exception)))
thread = threading.Thread(target=process)
thread.start()
return thread, response
regenerate_operators = []
def update_regeneration_progress():
bpy.context.window_manager.blendquery.is_regenerating = len(regenerate_operators) > 0
bpy.context.window_manager.blendquery.regeneration_progress = 1 / (len(regenerate_operators) + 1)
class BlendQueryRegenerateOperator(bpy.types.Operator):
bl_idname = "blendquery.regenerate"
bl_label = "BlendQuery Regenerate"
# `execute` is required in order to call this operator via `bpy.ops.blendquery.regenerate()`
# (self, context) must be present in order to register modal operator in Blender
def execute(self, context):
self.object = context.active_object
# TODO: kill any existing threads for this `context.active_object`
self.thread, self.response = create_parse_parametric_script_thread(self.object.blendquery.script.as_string())
# `self.report` does not seem to work within `execute` or `invoke`, so we call it within `modal`
regenerate_operators.append(self)
update_regeneration_progress()
self.timer = context.window_manager.event_timer_add(0.1, window=context.window)
context.window_manager.modal_handler_add(self)
return {"RUNNING_MODAL"}
# (self, context, event) must be present in order to register modal operator in Blender
def modal(self, context, event):
if self.thread.is_alive():
return {"PASS_THROUGH"}
response = self.response.get()
if isinstance(response, Exception):
self.report_exception(response)
else:
regenerate_blendquery_object(response, self.object, self.object.blendquery.object_pointers)
regenerate_operators.remove(self)
update_regeneration_progress()
context.window_manager.event_timer_remove(self.timer)
return {"FINISHED"}
def report_exception(self, exception):
stack_trace = "".join(
traceback.format_exception(
type(exception),
exception,
exception.__traceback__,
)
)
script_error = re.search(
r'File "<string>",\s*(.*(?:\n.*)*)',
stack_trace,
re.MULTILINE | re.DOTALL,
)
# "ERROR" type opens an input blocking pop-up, so we report using "WARNING"
self.report(
{"WARNING"},
f"Failed to regenerate BlendQuery object: {script_error and script_error.group(1) or stack_trace}",
)
# Info area seems to lag behind so we must force it to redraw
# TODO: Find a way to avoid this
redraw_info_area()
class BlendQueryInstallOperator(bpy.types.Operator):
bl_idname = "blendquery.install"
bl_label = "Install"
bl_description = "Installs BlendQuery required dependencies."
exception = None
# (self, context, event) must be present in order to register a modal operator in Blender
def invoke(self, context, event):
from install import install_dependencies, BlendQueryInstallException
def callback(result):
if isinstance(result, BlendQueryInstallException):
self.exception = result
# TODO: this can probably be moved into `install_dependencies` with the new `venv.py` file
pip_executable = os.path.join(
venv_dir, "Scripts" if os.name == "nt" else "bin", "pip"
)
self.thread = install_dependencies(pip_executable, callback)
# `self.report` does not seem to work within `execute` or `invoke`, so we call it within `modal`
self.timer = context.window_manager.event_timer_add(0.1, window=context.window)
context.window_manager.modal_handler_add(self)
context.window_manager.blendquery.installing_dependencies = True
return {"RUNNING_MODAL"}
# (self, context, event) must be present in order to register a modal operator in Blender
def modal(self, context, event):
if not self.thread.is_alive():
if self.exception:
self.report(
{"WARNING"},
f"Failed to install BlendQuery dependencies: {self.exception}",
)
# Info area seems to lag behind so we must force it to redraw
# TODO: Find a way to avoid this
redraw_info_area()
else:
initialise()
context.window_manager.blendquery.installing_dependencies = False
context.window_manager.event_timer_remove(self.timer)
# Setting `installing_dependencies` here doesn't seem to redraw the UI despite it being a property group so we must force it to redraw
# TODO: Find a way to avoid this
redraw_ui()
return {"FINISHED"}
return {"PASS_THROUGH"}
# TODO: Pull UI components into separate functions
class BlendQueryPanel(bpy.types.Panel):
bl_idname = "OBJECT_PT_BLENDQUERY_PANEL"
bl_label = bl_info["name"]
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "object"
def draw(self, context):
layout = self.layout
if are_dependencies_installed:
self.installed(layout, context)
else:
self.not_installed(layout, context)
def installed(self, layout, context):
if context.active_object:
object = context.active_object
column = layout.column()
column.prop(object.blendquery, "script")
column.separator(factor=0.5)
row = column.row()
row.prop(object.blendquery, "reload")
row.operator("blendquery.regenerate", text="Regenerate")
def not_installed(self, layout, context):
box = layout.box()
box.label(
icon="INFO",
text="BlendQuery requires the following dependencies to be installed:",
)
box.label(text=" • CadQuery")
box.label(text=" • Build123d")
column = box.column()
if context.window_manager.blendquery.installing_dependencies:
column.enabled = False
column.operator(
"blendquery.install",
icon="PACKAGE",
text="Installing dependencies...",
)
else:
column.operator(
"blendquery.install",
icon="PACKAGE",
text="Install dependencies",
)
def redraw_ui():
bpy.ops.wm.redraw_timer(type="DRAW_WIN_SWAP", iterations=1)
def redraw_info_area():
for area in bpy.context.screen.areas:
if area.type == "INFO":
area.tag_redraw()