-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAutoScreenshot.py
More file actions
660 lines (603 loc) · 28.2 KB
/
AutoScreenshot.py
File metadata and controls
660 lines (603 loc) · 28.2 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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
##AutoScreenshots 4 Fusion360, made by netrun.exe
## v0.81 RC1
import adsk.core, adsk.fusion, traceback
import threading, time, os, re
import ctypes
import shutil # Check for ffmpeg availability
import subprocess
import sys
from ctypes import wintypes
from ctypes import windll, byref
from ctypes.wintypes import RECT
# Ottieni il percorso assoluto del file corrente
BASE_DIR = os.path.dirname(__file__)
# Percorso della cartella 'lib' all'interno dell'add-in
LIB_PATH = os.path.join(BASE_DIR, 'lib')
# Aggiungi la cartella alla sys.path se non è già presente
if LIB_PATH not in sys.path:
sys.path.insert(0, LIB_PATH)
import PIL
from PIL import Image
# ---------- Globals ----------
app: adsk.core.Application = None
ui: adsk.core.UserInterface = None
handlers = []
capture_thread: threading.Thread = None
isCapturing = False
save_path = ''
interval = 1 # default seconds
resWidth = 1920 # default width
resHeight = 1080 # default height
filenamePrefix = ''
active_doc_name = None # track active document name
fusion_hwnd = None # handle to Fusion 360 window
capture_method = 'printwindow' # 'fusion' or 'printwindow' or 'printwindowcropped'
title_bar_height = 0 # Calcolato dinamicamente all'avvio
controls_panel_height = 0 # Calcolato dinamicamente all'avvio
# --- Persistent config for capture_method ---
CONFIG_FILE = os.path.join(BASE_DIR, 'autoscreenshot_config.txt')
def load_capture_method():
global capture_method
try:
if os.path.isfile(CONFIG_FILE):
with open(CONFIG_FILE, 'r') as f:
val = f.read().strip()
if val in ('fusion', 'printwindow', 'printwindowcropped', 'printwindowcroppedsquare'):
capture_method = val
except:
pass
def save_capture_method():
global capture_method
try:
with open(CONFIG_FILE, 'w') as f:
f.write(capture_method)
except:
pass
cmdDef: adsk.core.CommandDefinition = None
control: adsk.core.CommandControl = None
qat_control: adsk.core.CommandControl = None # Separate control for QAT
# Default folder base = Pictures/Fusion360 AutoScreen
PICTURES = os.path.join(os.path.expanduser("~"), "Pictures")
DEFAULT_BASE_FOLDER = os.path.join(PICTURES, "Fusion360 AutoScreen")
# Path to single 32×32 PNG icon
THIS_DIR = os.path.dirname(__file__)
ICON_NORMAL32 = os.path.join(THIS_DIR, 'resources', 'normal')
ICON_PAUSED32 = os.path.join(THIS_DIR, 'resources', 'paused')
# Verify icon existence safely
if not os.path.isfile(ICON_NORMAL32):
try: ui.messageBox(f"Icon not found: {ICON_NORMAL32}")
except: pass
# ---------- Capture Loop ----------
def capture_loop():
global isCapturing, save_path, interval, resWidth, resHeight, filenamePrefix, active_doc_name, fusion_hwnd, capture_method
global ui
user32 = ctypes.windll.user32
x = 0
while isCapturing:
try:
# Verify active document
current_doc = app.activeDocument
current_name = current_doc.name if current_doc else None
# Check if the active document is still open
open_docs = [doc.name for doc in app.documents]
if active_doc_name and active_doc_name not in open_docs:
stop_capture()
remove_from_qat() # Remove from QAT when stopping
break
# Verify Fusion 360 in foreground
foreground_hwnd = user32.GetForegroundWindow()
fusion_in_focus = (foreground_hwnd == fusion_hwnd)
if not fusion_in_focus or current_name != active_doc_name:
# Aggiorna icona in base allo stato di cattura
cmdDef.resourceFolder = ICON_PAUSED32
if control:
control.isVisible = False
control.isVisible = True
# Pause until focus returns and project unchanged
while isCapturing:
current_doc = app.activeDocument
current_name = current_doc.name if current_doc else None
foreground_hwnd = user32.GetForegroundWindow()
fusion_in_focus = (foreground_hwnd == fusion_hwnd)
# Check again if the active document is still open
open_docs = [doc.name for doc in app.documents]
if active_doc_name and active_doc_name not in open_docs:
stop_capture()
remove_from_qat()
break
if fusion_in_focus and current_name == active_doc_name:
# Aggiorna icona in base allo stato di cattura
cmdDef.resourceFolder = ICON_NORMAL32
if control:
control.isVisible = False
control.isVisible = True
break
time.sleep(1)
if x == 1:
stop_capture()
remove_from_qat()
else:
x += 1
continue
# Capture screenshot
ts = time.strftime('%Y%m%d%H%M%S')
filename = f"{filenamePrefix}_{ts}.png"
filepath = os.path.join(save_path, filename)
capture_with_fusionapi(app, filepath)
except Exception:
ui.messageBox(f'Error during capture:\n{traceback.format_exc()}')
time.sleep(interval)
def capture_with_fusionapi(app, filepath):
app.activeViewport.refresh()
adsk.doEvents()
time.sleep(0.1)
temp_fp = filepath + "_tmp"
app.activeViewport.saveAsImageFile(temp_fp, 0, 0)
# ---------- Control Capture ----------
def start_capture():
global capture_thread, isCapturing, filenamePrefix, save_path, active_doc_name
if not isCapturing:
try:
doc = app.activeDocument
if doc:
if active_doc_name != doc.name:
base = os.path.splitext(doc.name)[0]
base = re.sub(r'v\d+(\.\d+)*$', '', base)
filenamePrefix = re.sub(r'\s+', '_', base).rstrip('_')
save_path = os.path.join(DEFAULT_BASE_FOLDER, filenamePrefix)
active_doc_name = doc.name
else:
filenamePrefix = 'Project'
save_path = os.path.join(DEFAULT_BASE_FOLDER, filenamePrefix)
active_doc_name = None
except:
filenamePrefix = 'Project'
save_path = os.path.join(DEFAULT_BASE_FOLDER, filenamePrefix)
active_doc_name = None
os.makedirs(save_path, exist_ok=True)
if not capture_thread or not capture_thread.is_alive():
isCapturing = True
capture_thread = threading.Thread(target=capture_loop)
capture_thread.start()
def stop_capture():
global isCapturing
isCapturing = False
# ---------- UI Update ----------
def update_button():
global cmdDef, control, isCapturing
if not cmdDef: return
tooltipstring = (
"Captures screenshots at fixed intervals while Fusion 360 is in the foreground and a project is open. "
"Capture automatically pauses if you switch projects or Fusion is not active, and resumes when it is.\n\n"
"To stop, relaunch the add-in and press Stop.\n\n"
"Screenshots are saved in the user's Pictures/Fusion360 AutoScreen folder and are never deleted — manage them manually. "
"Changing the folder or filename is possible but not recommended because you could have issues with ffmpeg automatic video encoding.\n\n"
"Files are timestamped and alphabetically ordered, allowing ffmpeg to generate a video and letting you pause/resume "
"recording at any time without using sequential numbers."
)
cmdDef.name = 'AutoScreenshot (STOP)' if isCapturing else 'Auto Screenshot (START)'
cmdDef.tooltip = 'Stop recording screenshots' if isCapturing else tooltipstring
try:
for panel in ui.allToolbarPanels:
for i in range(panel.controls.count):
try:
ctrl = panel.controls.item(i)
if ctrl.id == 'AutoScreenshotCmd':
ctrl.isVisible = False
ctrl.isVisible = True
except:
continue
except:
pass
def define_screenshot_area():
global title_bar_height, controls_panel_height
# Dynamically calculate title_bar_height
try:
if fusion_hwnd:
rect_win = RECT()
rect_cli = RECT()
windll.user32.GetWindowRect(fusion_hwnd, byref(rect_win))
windll.user32.GetClientRect(fusion_hwnd, byref(rect_cli))
pt = wintypes.POINT()
pt.x = rect_cli.left
pt.y = rect_cli.top
windll.user32.ClientToScreen(fusion_hwnd, byref(pt))
client_top = pt.y
title_bar_height = client_top - rect_win.top
# Calculate controls_panel_height
# Find the position of the viewport relative to the client
try:
# Make the process DPI-aware (important)
user32 = ctypes.windll.user32
if hasattr(user32, 'SetProcessDPIAware'):
user32.SetProcessDPIAware()
# 1) Get the height of the work area (screen minus taskbar)
SPI_GETWORKAREA = 0x0030
work_rect = wintypes.RECT()
user32.SystemParametersInfoW(SPI_GETWORKAREA, 0, byref(work_rect), 0)
work_height = work_rect.bottom - work_rect.top
# 2) Read the "logical" height of the viewport
vp = app.activeViewport
vp_logical = vp.height # height according to Fusion API
# 3) Calculate DPI scaling to convert to physical pixels
hdc = user32.GetDC(None)
gdi = ctypes.windll.gdi32
LOGPIXELSY = 90
dpi = gdi.GetDeviceCaps(hdc, LOGPIXELSY)
user32.ReleaseDC(None, hdc)
scale = dpi / 96.0
vp_physical = int(vp_logical * scale)
# 4) Difference = space occupied by toolbars/panels
controls_panel_height = work_height - vp_physical
# DEBUG — you can remove in production
# ui.messageBox(
# f"DEBUG:\n"
# f" work_height = {work_height}\n"
# f" vp.logical = {vp_logical}\n"
# f" dpi scaling = {dpi} ⇒ scale={scale:.2f}\n"
# f" vp.physical = {vp_physical}\n"
# f" controls height = {controls_panel_height}"
# )
except Exception as e:
controls_panel_height = 0
#ui.messageBox(f"DEBUG: error calculating controls_panel_height: {e}")
else:
title_bar_height = 0
controls_panel_height = 0
except:
title_bar_height = 0
controls_panel_height = 0
# Helper functions for QAT management
def add_to_qat():
global qat_control, cmdDef
if cmdDef:
qat = ui.toolbars.itemById('QAT')
if qat:
# Remove any existing control
existing = qat.controls.itemById('AutoScreenshotCmd')
if existing: existing.deleteMe()
# Add to QAT
qat_control = qat.controls.addCommand(cmdDef)
qat_control.isVisible = True
def remove_from_qat():
global qat_control
if qat_control:
qat_control.deleteMe()
qat_control = None
def toggle_capture():
global save_path, isCapturing
if not (save_path and os.path.isdir(save_path)):
ui.messageBox('Please set a valid folder first!')
return
if isCapturing:
stop_capture()
remove_from_qat() # Remove from QAT when stopping
else:
start_capture()
add_to_qat() # Add to QAT when starting
update_button()
# ---------- input.txt Support ----------
def ensure_input_txt_exists():
global save_path
input_txt = os.path.join(save_path, 'input.txt')
if not os.path.isfile(input_txt):
try:
with open(input_txt, 'w'): pass
except Exception as e:
ui.messageBox(f"Failed to create input.txt:\n{e}")
def update_input_txt(file_list):
global save_path
input_txt = os.path.join(save_path, 'input.txt')
try:
with open(input_txt, 'w') as f:
for fn in file_list:
f.write(f"file '{os.path.join(save_path, fn)}'\n")
except Exception as e:
ui.messageBox(f"Failed to update input.txt:\n{e}")
# ---------- Command Handlers ----------
class CommandCreatedHandler(adsk.core.CommandCreatedEventHandler):
def notify(self, args):
global filenamePrefix, save_path, resWidth, resHeight, active_doc_name, fusion_hwnd, capture_method
# --- Load persisted capture_method ---
load_capture_method()
# --- Define screenshot area ---
if not isCapturing:
define_screenshot_area()
# Detect desktop resolution
try:
user32 = ctypes.windll.user32
if hasattr(user32, 'SetProcessDPIAware'):
user32.SetProcessDPIAware()
resWidth = user32.GetSystemMetrics(78)
resHeight = user32.GetSystemMetrics(79)
if resWidth <= 0 or resHeight <= 0:
raise ValueError("Invalid resolution detected")
except:
resWidth, resHeight = 1920, 1080
# Initialize doc name and window handle
if not isCapturing:
try:
active_doc_name = app.activeDocument.name
except:
active_doc_name = None
try:
fusion_hwnd = ctypes.windll.user32.GetForegroundWindow()
except:
fusion_hwnd = None
try:
doc = app.activeDocument
if doc:
base = os.path.splitext(doc.name)[0]
base = re.sub(r'v\d+(\.\d+)*$', '', base)
filenamePrefix = re.sub(r'\s+', '_', base).rstrip('_')
else:
filenamePrefix = 'Project'
except:
filenamePrefix = 'Project'
save_path = os.path.join(DEFAULT_BASE_FOLDER, filenamePrefix)
cmd = args.command
inputs = cmd.commandInputs
global prefixInput, savePathInput, intervalInput, videoNameInput, fpsInput, ffmpegButton
prefix_label = 'Active project' if isCapturing else 'File Name (Prefix)'
prefixInput = inputs.addStringValueInput('filePrefix', prefix_label, filenamePrefix)
savePathInput = inputs.addStringValueInput('savePath', 'Save Folder', save_path)
intervalInput = inputs.addValueInput('interval', 'Interval (s)', 's', adsk.core.ValueInput.createByReal(interval))
# --- Capture method section ---
inputs.addTextBoxCommandInput('captureMethodLabel', '', 'Capture method - DEMO 1 Enabled', 1, True)
global fusionCaptureInput, printWindowInput, printWindowCroppedInput, printWindowCroppedSquareInput
fusionCaptureInput = inputs.addBoolValueInput('fusionCapture', 'Fusion API (no UI)', True, '', capture_method == 'fusion')
printWindowInput = inputs.addBoolValueInput('gdiCapture', 'GDI (with UI)', True, '', capture_method == 'printwindow')
printWindowCroppedInput = inputs.addBoolValueInput('gdiCroppedCapture', 'GDI (no UI)', True, '', capture_method == 'printwindowcropped')
printWindowCroppedSquareInput = inputs.addBoolValueInput('gdiCroppedSquareCapture', 'GDI (no UI, squared)', True, '', capture_method == 'printwindowcroppedsquare')
# Enable only fusion, disable all others
fusionCaptureInput.isEnabled = True
printWindowInput.isEnabled = False
printWindowCroppedInput.isEnabled = False
printWindowCroppedSquareInput.isEnabled = False
# --- End capture method section ---
resolution_info = f"Resolution: {resWidth}x{resHeight}"
inputs.addTextBoxCommandInput('resolutionInfo', '', resolution_info, 1, True)
inputs.addTextBoxCommandInput('description', '', 'Create FFmpeg video', 1, True)
videoNameInput = inputs.addStringValueInput('videoName', 'Name:', 'animation.mp4')
fpsInput = inputs.addValueInput('fps', 'FPS:', '', adsk.core.ValueInput.createByReal(30))
ffmpegButton = inputs.addBoolValueInput('createVideo', 'Create Video', False, '', False)
ffmpegButton.isEnabled = not isCapturing
for inp in [prefixInput, savePathInput, intervalInput, videoNameInput, fpsInput]:
inp.isEnabled = not isCapturing
onInput = CommandInputChangedHandler()
cmd.inputChanged.add(onInput); handlers.append(onInput)
cmd.okButtonText = 'STOP' if isCapturing else 'CAPTURE'
cmd.cancelButtonText = 'Cancel'
onExec = CommandExecuteHandler()
cmd.execute.add(onExec); handlers.append(onExec)
onDst = CommandDestroyHandler()
cmd.destroy.add(onDst); handlers.append(onDst)
class CommandExecuteHandler(adsk.core.CommandEventHandler):
def notify(self, args):
global save_path, interval, resWidth, resHeight, filenamePrefix, active_doc_name, capture_method
pref = prefixInput.value.strip()
path = savePathInput.value.strip()
# --- Read capture method from UI ---
if fusionCaptureInput.value:
capture_method = 'fusion'
elif printWindowInput.value:
capture_method = 'printwindow'
elif printWindowCroppedInput.value:
capture_method = 'printwindowcropped'
elif printWindowCroppedSquareInput.value:
capture_method = 'printwindowcroppedsquare'
if not isCapturing:
if pref:
filenamePrefix = pref
else:
try:
doc = app.activeDocument
if doc:
base = os.path.splitext(doc.name)[0]
base = re.sub(r'v\d+(\.\d+)*$', '', base)
filenamePrefix = re.sub(r'\s+', '_', base).rstrip('_')
else:
filenamePrefix = 'Project'
except:
filenamePrefix = 'Project'
if path and os.path.isdir(path):
save_path = path
else:
save_path = os.path.join(DEFAULT_BASE_FOLDER, filenamePrefix)
active_doc_name = app.activeDocument.name if app.activeDocument else None
os.makedirs(save_path, exist_ok=True)
ensure_input_txt_exists()
interval = max(1, intervalInput.value)
toggle_capture()
try:
for inp in [prefixInput, savePathInput, intervalInput, videoNameInput, fpsInput, ffmpegButton, fusionCaptureInput, printWindowInput, printWindowCroppedInput, printWindowCroppedSquareInput]:
inp.isEnabled = not isCapturing
except:
pass
class CommandInputChangedHandler(adsk.core.InputChangedEventHandler):
def notify(self, args):
global save_path, filenamePrefix, videoNameInput, fpsInput, ffmpegButton, fusionCaptureInput, printWindowInput, printWindowCroppedInput, printWindowCroppedSquareInput, capture_method
# --- Interlocked capture method flags ---
changed = False
if args.input.id == 'fusionCapture' and fusionCaptureInput.value:
capture_method = 'fusion'
printWindowInput.value = False
printWindowCroppedInput.value = False
printWindowCroppedSquareInput.value = False
changed = True
elif args.input.id == 'gdiCapture' and printWindowInput.value:
capture_method = 'printwindow'
fusionCaptureInput.value = False
printWindowCroppedInput.value = False
printWindowCroppedSquareInput.value = False
changed = True
elif args.input.id == 'gdiCroppedCapture' and printWindowCroppedInput.value:
capture_method = 'printwindowcropped'
fusionCaptureInput.value = False
printWindowInput.value = False
printWindowCroppedSquareInput.value = False
changed = True
elif args.input.id == 'gdiCroppedSquareCapture' and printWindowCroppedSquareInput.value:
capture_method = 'printwindowcroppedsquare'
fusionCaptureInput.value = False
printWindowInput.value = False
printWindowCroppedInput.value = False
changed = True
if changed:
save_capture_method()
if args.input.id == 'createVideo' and ffmpegButton.value:
ffmpegButton.value = False
if not shutil.which("ffmpeg"):
ui.messageBox('FFmpeg is not installed or not in PATH.')
return
video_name = videoNameInput.value.strip()
if not video_name:
ui.messageBox('Please provide a valid video name.')
return
try:
fps = int(fpsInput.value)
if fps <= 0:
raise ValueError
except:
ui.messageBox('Please provide a valid FPS value.')
return
ensure_input_txt_exists()
pngs = sorted([f for f in os.listdir(save_path)
if f.startswith(f"{filenamePrefix}_") and f.endswith(".png")])
update_input_txt(pngs)
input_txt = os.path.join(save_path, "input.txt")
output_v = os.path.join(save_path, video_name)
cmd_line = (
f'ffmpeg -r {fps} -f concat -safe 0 -i "{input_txt}" '
f'-c:v libx264 -preset slow -crf 22 -pix_fmt yuv420p -an "{output_v}"'
)
try:
os.system(f'start cmd /k "{cmd_line}"')
except Exception as e:
ui.messageBox(f"Failed to execute ffmpeg:\n{e}")
class CommandDestroyHandler(adsk.core.CommandEventHandler):
def notify(self, args):
update_button()
try:
for inp in [prefixInput, savePathInput, intervalInput, videoNameInput, fpsInput, ffmpegButton, fusionCaptureInput, printWindowInput, printWindowCroppedInput, printWindowCroppedSquareInput]:
inp.isEnabled = True
except:
pass
# ---------- run / stop ----------
def run(context):
global app, ui, cmdDef, control, qat_control, isCapturing, active_doc_name, fusion_hwnd, title_bar_height, controls_panel_height
try:
app = adsk.core.Application.get()
ui = app.userInterface
try:
active_doc_name = app.activeDocument.name
except:
active_doc_name = None
try:
fusion_hwnd = ctypes.windll.user32.GetForegroundWindow()
except:
fusion_hwnd = None
# --- Load persisted capture_method at startup ---
load_capture_method()
stop_capture()
isCapturing = False
# Clean up existing command
cmdDefs = ui.commandDefinitions
cmdDef = cmdDefs.itemById('AutoScreenshotCmd')
if cmdDef:
cmdDef.deleteMe()
# Create command definition
cmdDef = cmdDefs.addButtonDefinition(
'AutoScreenshotCmd',
'Auto Screenshot',
'Automatically capture screenshots of the active project',
ICON_NORMAL32
)
onCreate = CommandCreatedHandler()
cmdDef.commandCreated.add(onCreate)
handlers.append(onCreate)
# Try to add the command to the "Utilità" panel (ID: TSplineUtilitiesPanel)
panel_found = False
# Safer approach to find the panel - don't rely on activeProduct
try:
# Try to find the specific panel by ID in any workspace
for workspace in ui.workspaces:
try:
# Check if this workspace has toolbar tabs before iterating
if workspace.isValid and workspace.toolbarTabs:
for tab in workspace.toolbarTabs:
try:
panel = tab.toolbarPanels.itemById('SolidScriptsAddinsPanel')
if panel:
existing = panel.controls.itemById('AutoScreenshotCmd')
if existing: existing.deleteMe()
control = panel.controls.addCommand(cmdDef)
control.isPromoted = True
control.isVisible = False
control.isVisible = True
panel_found = True
break
except:
continue
if panel_found:
break
except:
continue
except:
pass
# If panel not found, try using product types
if not panel_found:
try:
# If active document exists, try its product
if app.activeDocument:
product = app.activeDocument.products.itemByProductType('DesignProductType')
if product:
designWS = ui.workspacesByProductType('DesignProductType').itemById('FusionSolidEnvironment')
if designWS:
for tab in designWS.toolbarTabs:
if "utilit" in tab.id.lower() or "utilit" in tab.name.lower():
for panel in tab.toolbarPanels:
existing = panel.controls.itemById('AutoScreenshotCmd')
if existing: existing.deleteMe()
control = panel.controls.addCommand(cmdDef)
control.isPromoted = True
control.isVisible = False
control.isVisible = True
panel_found = True
break
if panel_found:
break
except:
pass
# Fallback to traditional location if TSplineUtilitiesPanel not found
if not panel_found:
designWS = ui.workspaces.itemById('FusionSolidEnvironment')
toolsTab = designWS.toolbarTabs.itemById('ToolsTab')
utilitiesPanel = toolsTab.toolbarPanels.itemById('SolidScriptsAddinsPanel')
if utilitiesPanel:
existing = utilitiesPanel.controls.itemById('AutoScreenshotCmd')
if existing: existing.deleteMe()
control = utilitiesPanel.controls.addCommand(cmdDef)
control.isPromoted = True
control.isVisible = False
control.isVisible = True
else:
ui.messageBox("SolidScriptsAddinsPanel not found in ToolsTab. Using Quick Access Toolbar only.")
# Only add to QAT if already capturing (which should be false on startup)
qat_control = None
if isCapturing:
add_to_qat()
update_button()
adsk.autoTerminate(False)
except:
ui.messageBox(f'Error in run():\n{traceback.format_exc()}')
def stop(context):
global control, cmdDef
try:
stop_capture()
if control:
control.deleteMe(); control = None
if cmdDef:
cmdDef.deleteMe(); cmdDef = None
except:
ui.messageBox(f'stop() error():\n{traceback.format_exc()}')