forked from EmGi96/TrailPrint3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress.py
More file actions
483 lines (398 loc) · 16.3 KB
/
Copy pathprogress.py
File metadata and controls
483 lines (398 loc) · 16.3 KB
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
# Copyright (C) 2026 EmGi
# You are free to modify it under the terms of the GNU General Public License as published by the Free Software Foundation.
# You are free to use any models Generated by this Addon Commercially
"""
GPU-drawn progress overlay for TrailPrint3D.
Usage:
overlay = ProgressOverlay.get()
overlay.start()
overlay.update(percent=0.5, phase="Fetching Elevation", message="Tile 5/10")
overlay.finish()
"""
import math
import time
import bpy
import gpu
import blf
from gpu_extras.batch import batch_for_shader
class ProgressOverlay:
_instance = None
# --- Layout ---
W = 400
PAD = 14
BAR_H = 16
SUB_BAR_H = 10
HEADER_H = 30
RADIUS = 6
MARGIN_X = 20
MARGIN_Y = 20
STEP_ROW_H = 18
MAX_STEPS = 5 # max completed steps shown at once
# --- Colors: Blender default dark theme ---
COL_BORDER = (0.08, 0.08, 0.08, 1.00)
COL_BG = (0.22, 0.22, 0.22, 0.96)
COL_HEADER_BG = (0.16, 0.16, 0.16, 1.00)
COL_SEPARATOR = (0.10, 0.10, 0.10, 1.00)
COL_ACCENT = (0.95, 0.47, 0.02, 1.00) # Blender orange
COL_BAR_BG = (0.13, 0.13, 0.13, 1.00)
COL_BAR_FILL = (0.95, 0.47, 0.02, 1.00) # Blender orange
COL_SUB_BAR_FILL = (0.42, 0.72, 0.95, 1.00) # blue for sub-task bar
COL_TEXT = (0.90, 0.90, 0.90, 1.00)
COL_MUTED = (0.55, 0.55, 0.55, 1.00)
COL_STEP_OK = (0.30, 0.85, 0.40, 1.00) # green checkmark
def __init__(self):
self.percent = 0.0
self.phase = ""
self.message = ""
self.sub_percent = None # float 0-1 when sub-bar is active, else None
self.sub_label = ""
self.completed_steps = [] # list of strings shown with green checkmarks
self.active = False
self._handler = None
self._start_time = None
@property
def H(self):
"""Dynamic panel height based on current content."""
body_h = self.PAD # bottom margin
body_h += self.BAR_H + 10 # main bar + gap above it
body_h += 14 + 5 # phase text + gap
if self.message:
body_h += 14 + 5 # message text + gap
if self.sub_percent is not None:
body_h += 13 + 4 # sub-label + gap
body_h += self.SUB_BAR_H + 12 # sub-bar + gap above
visible = self.completed_steps[-self.MAX_STEPS:]
if visible:
body_h += 6 # separator gap
body_h += len(visible) * self.STEP_ROW_H
body_h += 4 # top gap
body_h += 10 # top body padding
return body_h + self.HEADER_H
@classmethod
def get(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
def start(self):
self.percent = 0.0
self.phase = "Starting…"
self.message = ""
self.sub_percent = None
self.sub_label = ""
self.completed_steps = []
self.active = True
self._start_time = time.time()
if self._handler is None:
self._handler = bpy.types.SpaceView3D.draw_handler_add(
self._draw_cb, (), 'WINDOW', 'POST_PIXEL'
)
_force_redraw()
def update(self, percent=None, phase=None, message=None, sub_percent=None, sub_label=None):
if percent is not None:
self.percent = max(0.0, min(1.0, percent))
if phase is not None:
self.phase = phase
if message is not None:
self.message = message
if sub_percent is not None:
self.sub_percent = max(0.0, min(1.0, sub_percent))
if sub_label is not None:
self.sub_label = sub_label
_force_redraw()
def add_completed_step(self, text):
"""Append a green-checkmark step. Only the last MAX_STEPS are shown."""
self.completed_steps.append(text)
_force_redraw()
def finish(self):
self.active = False
self.percent = 1.0
if self._handler is not None:
bpy.types.SpaceView3D.draw_handler_remove(self._handler, 'WINDOW')
self._handler = None
_force_redraw()
# ------------------------------------------------------------------
def _draw_cb(self):
if not self.active:
return
region = bpy.context.region
if region is None:
return
x = self.MARGIN_X
y = self.MARGIN_Y
r = self.RADIUS
p = self.PAD
total_h = self.H # dynamic height
bar_w = self.W - p * 2
gpu.state.blend_set('ALPHA')
# 1px dark border
_rounded_rect(x - 1, y - 1, self.W + 2, total_h + 2, self.COL_BORDER, r + 1)
# Main panel background
_rounded_rect(x, y, self.W, total_h, self.COL_BG, r)
# Header background (top HEADER_H px, rounded at top only)
header_y = y + total_h - self.HEADER_H
_rounded_rect_top(x, header_y, self.W, self.HEADER_H, self.COL_HEADER_BG, r)
# Separator line between header and body
_rect(x, header_y, self.W, 1, self.COL_SEPARATOR)
# Header: title left, elapsed right
label_y = header_y + (self.HEADER_H - 13) // 2
_text("TrailPrint3D", x + p, label_y, 13, self.COL_TEXT)
elapsed = int(time.time() - self._start_time) if self._start_time else 0
m, s = divmod(elapsed, 60)
_text_right(f"{m:02d}:{s:02d}", x + self.W - p, label_y, 11, self.COL_MUTED)
# --- Body: build bottom-up ---
cur_y = y + p
# Main progress bar
_rounded_rect(x + p, cur_y, bar_w, self.BAR_H, self.COL_BAR_BG, 3)
fill_w = int(bar_w * self.percent)
if fill_w >= 6:
_rounded_rect(x + p, cur_y, fill_w, self.BAR_H, self.COL_BAR_FILL, 3)
elif fill_w > 0:
_rect(x + p, cur_y, fill_w, self.BAR_H, self.COL_BAR_FILL)
cur_y += self.BAR_H + 10
# Phase label + overall percentage
_text(self.phase, x + p, cur_y, 12, self.COL_ACCENT)
_text_right(f"{int(self.percent * 100)} %", x + self.W - p, cur_y, 11, self.COL_MUTED)
cur_y += 14 + 5
# Detail message
if self.message:
_text(self.message, x + p, cur_y, 11, self.COL_MUTED)
cur_y += 14 + 5
# Sub-progress bar (shown during elevation fetch)
if self.sub_percent is not None:
sub_label_text = self.sub_label or "Sub-task"
_text(sub_label_text, x + p, cur_y, 10, self.COL_MUTED)
_text_right(f"{int(self.sub_percent * 100)} %", x + self.W - p, cur_y, 10, self.COL_MUTED)
cur_y += 13 + 4
_rounded_rect(x + p, cur_y, bar_w, self.SUB_BAR_H, self.COL_BAR_BG, 2)
sub_fill_w = int(bar_w * self.sub_percent)
if sub_fill_w >= 4:
_rounded_rect(x + p, cur_y, sub_fill_w, self.SUB_BAR_H, self.COL_SUB_BAR_FILL, 2)
elif sub_fill_w > 0:
_rect(x + p, cur_y, sub_fill_w, self.SUB_BAR_H, self.COL_SUB_BAR_FILL)
cur_y += self.SUB_BAR_H + 12
# Completed steps (green checkmarks, newest at top = drawn last)
visible = self.completed_steps[-self.MAX_STEPS:]
if visible:
cur_y += 6 # gap before step list
for step in visible:
_text("✓", x + p, cur_y, 11, self.COL_STEP_OK)
_text(step, x + p + 16, cur_y, 11, self.COL_TEXT)
cur_y += self.STEP_ROW_H
gpu.state.blend_set('NONE')
class WarningsOverlay:
"""Displays a list of warning messages at the bottom-left of the viewport.
Usage:
# During generation, accumulate warnings:
WarningsOverlay.add_warning("Forest generation timed out and was skipped")
# After generation finishes, show them all:
WarningsOverlay.get().show()
# At the start of a new generation, clear old messages:
WarningsOverlay.clear()
"""
_instance = None
_messages = [] # class-level list shared across all calls
# --- Layout ---
W = 400
PAD = 12
ROW_H = 20
HEADER_H = 30
RADIUS = 6
MARGIN_X = 20
MARGIN_Y = 20
# --- Colors: match ProgressOverlay theme ---
COL_BORDER = (0.08, 0.08, 0.08, 1.00)
COL_BG = (0.22, 0.22, 0.22, 0.96)
COL_HEADER_BG = (0.16, 0.16, 0.16, 1.00)
COL_SEPARATOR = (0.10, 0.10, 0.10, 1.00)
COL_ACCENT = (0.95, 0.47, 0.02, 1.00)
COL_TEXT = (0.90, 0.90, 0.90, 1.00)
COL_MUTED = (0.55, 0.55, 0.55, 1.00)
# icon → (character, color)
ICONS = {
"warn": ("!", (0.95, 0.75, 0.10, 1.00)), # yellow !
"error": ("✗", (0.90, 0.25, 0.25, 1.00)), # red ✗
"ok": ("✓", (0.30, 0.85, 0.40, 1.00)), # green ✓
}
def __init__(self):
self.active = False
self._handler = None
@classmethod
def get(cls):
if cls._instance is None:
cls._instance = cls()
return cls._instance
@classmethod
def add_warning(cls, message, icon="warn"):
"""Append a message to be shown at the end of generation.
icon: "warn" (yellow !), "error" (red ✗), "ok" (green ✓)
"""
cls._messages.append((message, icon))
@classmethod
def clear(cls):
"""Clear all accumulated warnings (call at the start of each generation)."""
cls._messages.clear()
def show(self):
"""Display the warnings panel. Does nothing if there are no warnings."""
if not self.__class__._messages:
return
self.active = True
if self._handler is None:
self._handler = bpy.types.SpaceView3D.draw_handler_add(
self._draw_cb, (), 'WINDOW', 'POST_PIXEL'
)
bpy.app.timers.register(_invoke_warnings_modal, first_interval=0.05)
_force_redraw()
def finish(self):
self.active = False
if self._handler is not None:
bpy.types.SpaceView3D.draw_handler_remove(self._handler, 'WINDOW')
self._handler = None
_force_redraw()
# ------------------------------------------------------------------
def _draw_cb(self):
if not self.active:
return
region = bpy.context.region
if region is None:
return
msgs = self.__class__._messages
n = len(msgs)
h = self.HEADER_H + self.PAD + n * self.ROW_H + self.PAD
p = self.PAD
r = self.RADIUS
x = self.MARGIN_X
y = self.MARGIN_Y
gpu.state.blend_set('ALPHA')
# 1px dark border
_rounded_rect(x - 1, y - 1, self.W + 2, h + 2, self.COL_BORDER, r + 1)
# Main panel background
_rounded_rect(x, y, self.W, h, self.COL_BG, r)
# Header background (rounded at top only)
header_y = y + h - self.HEADER_H
_rounded_rect_top(x, header_y, self.W, self.HEADER_H, self.COL_HEADER_BG, r)
_rect(x, header_y, self.W, 1, self.COL_SEPARATOR)
# Header: title left, dismiss hint right
label_y = header_y + (self.HEADER_H - 13) // 2
_text("Info", x + p, label_y, 13, self.COL_ACCENT)
_text_right("click to dismiss", x + self.W - p, label_y, 11, self.COL_MUTED)
# Warning rows (newest at top)
for i, (msg, icon) in enumerate(msgs):
row_y = y + p + (n - 1 - i) * self.ROW_H
char, col = self.ICONS.get(icon, self.ICONS["warn"])
_text(char, x + p, row_y + 3, 11, col)
_text(msg, x + p + 14, row_y + 3, 11, self.COL_TEXT)
gpu.state.blend_set('NONE')
class TRAILPRINT_OT_warnings_mouse(bpy.types.Operator):
"""Modal that dismisses the warnings overlay on any mouse click."""
bl_idname = "trailprint.warnings_mouse"
bl_label = "Warnings Mouse Watcher"
bl_options = {'INTERNAL'}
def modal(self, context, event):
overlay = WarningsOverlay.get()
if not overlay.active:
return {'CANCELLED'}
if event.type in {'LEFTMOUSE', 'RIGHTMOUSE', 'MIDDLEMOUSE'} and event.value == 'PRESS':
overlay.finish()
return {'CANCELLED'}
return {'PASS_THROUGH'}
def invoke(self, context, event):
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def _invoke_warnings_modal():
"""Called via timer so a valid window context exists."""
try:
bpy.ops.trailprint.warnings_mouse('INVOKE_DEFAULT')
except Exception:
pass
return None # one-shot
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
def _force_redraw():
"""Tag all 3D viewports for redraw and flush immediately."""
target_window = None
target_area = None
target_region = None
for window in bpy.context.window_manager.windows:
for area in window.screen.areas:
if area.type == 'VIEW_3D':
area.tag_redraw()
if target_area is None:
for region in area.regions:
if region.type == 'WINDOW':
target_window = window
target_area = area
target_region = region
break
if target_area is not None:
try:
with bpy.context.temp_override(
window=target_window,
area=target_area,
region=target_region,
):
bpy.ops.wm.redraw_timer(type='DRAW_WIN_SWAP', iterations=1)
except Exception:
pass
def _rounded_rect_verts(x, y, w, h, r, n=8):
"""Return polygon vertices for a fully-rounded rectangle."""
r = min(r, w / 2, h / 2)
corners = [
(x + r, y + r, math.pi, 1.5 * math.pi),
(x + w - r, y + r, 1.5 * math.pi, 2.0 * math.pi),
(x + w - r, y + h - r, 0.0, 0.5 * math.pi),
(x + r, y + h - r, 0.5 * math.pi, math.pi),
]
verts = []
for cx, cy, a0, a1 in corners:
for i in range(n + 1):
a = a0 + (a1 - a0) * i / n
verts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
return verts
def _rounded_rect_top_verts(x, y, w, h, r, n=8):
"""Return polygon vertices for a rectangle with rounded top corners only."""
r = min(r, w / 2, h / 2)
verts = [(x, y), (x + w, y)]
# Top-right arc: 0 → π/2
cx, cy = x + w - r, y + h - r
for i in range(n + 1):
a = math.pi / 2 * i / n
verts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
# Top-left arc: π/2 → π
cx, cy = x + r, y + h - r
for i in range(n + 1):
a = math.pi / 2 + math.pi / 2 * i / n
verts.append((cx + r * math.cos(a), cy + r * math.sin(a)))
return verts
def _draw_convex(verts, color):
"""Fill a convex polygon with a triangle fan from the first vertex."""
n = len(verts)
indices = [(0, i, i + 1) for i in range(1, n - 1)]
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": verts}, indices=indices)
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
def _rounded_rect(x, y, w, h, color, r=6, n=8):
_draw_convex(_rounded_rect_verts(x, y, w, h, r, n), color)
def _rounded_rect_top(x, y, w, h, color, r=6, n=8):
_draw_convex(_rounded_rect_top_verts(x, y, w, h, r, n), color)
def _rect(x, y, w, h, color):
verts = [(x, y), (x + w, y), (x + w, y + h), (x, y + h)]
indices = [(0, 1, 2), (0, 2, 3)]
shader = gpu.shader.from_builtin('UNIFORM_COLOR')
batch = batch_for_shader(shader, 'TRIS', {"pos": verts}, indices=indices)
shader.bind()
shader.uniform_float("color", color)
batch.draw(shader)
def _text(text, x, y, size, color):
blf.size(0, size)
blf.color(0, *color)
blf.position(0, x, y, 0)
blf.draw(0, text)
def _text_right(text, x, y, size, color):
blf.size(0, size)
w, _ = blf.dimensions(0, text)
blf.color(0, *color)
blf.position(0, x - w, y, 0)
blf.draw(0, text)