-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoperators.py
More file actions
1562 lines (1283 loc) · 63.8 KB
/
operators.py
File metadata and controls
1562 lines (1283 loc) · 63.8 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
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# operators.py
import bpy
import time
import traceback
from contextlib import contextmanager
from mathutils import Vector, Quaternion
from . import utils
from .utils import (
get_current_view_state,
states_are_similar,
add_to_history,
history_go_back,
history_go_forward,
get_view_location,
)
from .state_controller import get_controller, UpdateSource, LockPriority
from .preferences import get_preferences
from .thumbnail_generator import generate_thumbnail, delete_thumbnail
from .modal_gallery import VIEW3D_OT_thumbnail_gallery
# ========================================================================
# VIEW HISTORY OPERATORS
# ========================================================================
class VIEW3D_OT_view_history_monitor(bpy.types.Operator):
"""Background monitor to save view state after movement settles."""
bl_idname = "view3d.view_history_monitor"
bl_label = "View History Monitor"
_timer = None
last_known_state = None
is_moving = False
settle_start_time = 0.0
was_in_camera_view = False # Track camera view transitions
last_selection_hash = None # Track selection changes for orbit mode
last_orbit_mode = False # Track orbit mode transitions
last_scene_count = 0 # Track scene count for UUID duplicate detection
last_view_layer_counts = {} # Track view layer count per scene {scene_name: count}
last_camera_count = 0 # Track camera count for dropdown sync
last_maintenance_time = 0.0 # Last periodic maintenance timestamp
# Settings
CHECK_INTERVAL = 0.1
MAINTENANCE_INTERVAL_ACTIVE = 0.5
MAINTENANCE_INTERVAL_IDLE = 2.0
def _pass_through_tick(self, tick_start=None):
return {'PASS_THROUGH'}
def _run_periodic_maintenance(self, context):
"""Run lower-frequency checks that do not need to execute every timer tick."""
from . import data_storage
# --- SCENE COUNT CHANGE DETECTION ---
current_scene_count = len(bpy.data.scenes)
if current_scene_count != self.last_scene_count:
if self.last_scene_count > 0: # Skip initial detection
fixed = data_storage.fix_duplicate_scene_uuids()
# Also check for new scenes needing UUIDs
for scene in bpy.data.scenes:
data_storage.ensure_scene_uuid(scene)
self.last_scene_count = current_scene_count
# --- VIEW LAYER COUNT CHANGE DETECTION ---
current_scene_names = set()
for scene in bpy.data.scenes:
current_scene_names.add(scene.name)
current_vl_count = len(scene.view_layers)
last_vl_count = self.last_view_layer_counts.get(scene.name)
# Initialize baseline without triggering duplicate-fix logic.
if last_vl_count is None:
self.last_view_layer_counts[scene.name] = current_vl_count
continue
if current_vl_count != last_vl_count:
fixed = data_storage.fix_duplicate_view_layer_uuids(scene)
# Also ensure new view layers have UUIDs
for vl in scene.view_layers:
data_storage.ensure_view_layer_uuid(vl)
self.last_view_layer_counts[scene.name] = current_vl_count
# Remove stale tracking entries for scenes that no longer exist.
for scene_name in tuple(self.last_view_layer_counts.keys()):
if scene_name not in current_scene_names:
del self.last_view_layer_counts[scene_name]
# --- CAMERA COUNT CHANGE DETECTION ---
camera_count = sum(1 for obj in context.scene.objects if obj.type == 'CAMERA')
if camera_count != self.last_camera_count:
self.last_camera_count = camera_count
# Resync camera dropdown to current scene camera.
props = context.scene.viewpilot
active_cam = context.scene.camera
if active_cam:
try:
props.camera_enum = active_cam.name
except TypeError:
pass # Enum items not yet populated
def _current_maintenance_interval(self, context):
"""Return maintenance cadence based on current activity level."""
try:
props = context.scene.viewpilot
if (
self.is_moving or
self.was_in_camera_view or
props.orbit_around_selection or
props.keep_camera_active
):
return self.MAINTENANCE_INTERVAL_ACTIVE
except (AttributeError, ReferenceError, RuntimeError):
pass
return self.MAINTENANCE_INTERVAL_IDLE
def _maybe_run_periodic_maintenance(self, context, now):
interval = self._current_maintenance_interval(context)
if (now - self.last_maintenance_time) < interval:
return
self.last_maintenance_time = now
self._run_periodic_maintenance(context)
def modal(self, context, event):
if event.type == 'TIMER':
tick_start = time.perf_counter()
controller = get_controller()
now = time.time()
self._maybe_run_periodic_maintenance(context, now)
# Auto-initialize if needed
if not context.scene.viewpilot.init_complete:
context.scene.viewpilot.reinitialize_from_context(context)
current_state = get_current_view_state(context)
if not current_state:
return self._pass_through_tick(tick_start)
# Check if we're in a grace period
# During grace period: DON'T reinitialize (to avoid fighting slider input)
# But DO continue tracking movement so history can be recorded after settle
in_grace = controller.is_in_grace_period()
props = context.scene.viewpilot
is_in_camera = current_state.get('view_perspective') == 'CAMERA'
# Orbit mode can be enabled while idle; seed selection baseline on transition.
if props.orbit_around_selection and not self.last_orbit_mode:
current_sel = frozenset(obj.name for obj in context.selected_objects)
self.last_selection_hash = hash(current_sel)
self.last_orbit_mode = bool(props.orbit_around_selection)
# Fast path: unchanged idle ticks with no special sync modes enabled.
if (
self.last_known_state is not None and
not self.is_moving and
not in_grace and
not self.was_in_camera_view and
not is_in_camera and
not props.orbit_around_selection and
not props.keep_camera_active and
states_are_similar(current_state, self.last_known_state)
):
return self._pass_through_tick(tick_start)
# --- SELECTION CHANGE DETECTION ---
# If orbit mode is active and selection changes, disable orbit
if props.orbit_around_selection:
# Calculate hash of current selection (names of selected objects)
current_sel = frozenset(obj.name for obj in context.selected_objects)
current_hash = hash(current_sel)
if self.last_selection_hash is not None and current_hash != self.last_selection_hash:
# Selection changed! Disable orbit mode
props['orbit_around_selection'] = False
props['orbit_initialized'] = False
self.last_selection_hash = current_hash
else:
# Keep tracking selection even when orbit is off
current_sel = frozenset(obj.name for obj in context.selected_objects)
self.last_selection_hash = hash(current_sel)
# --- KEEP CAMERA ACTIVE MODE DETECTION ---
# If mode is on but camera is no longer active, turn off the mode
if props.keep_camera_active:
cam = context.scene.camera
if cam:
is_cam_active = (context.view_layer.objects.active == cam)
if not is_cam_active:
# External selection change - disable the mode
props['keep_camera_active'] = False
# Handle camera view - sync UI when camera properties change externally
if is_in_camera:
cam = context.scene.camera
if cam:
# Check if camera properties have changed externally
cam_loc = cam.location
cam_rot = cam.rotation_euler
# Compare with ViewPilot's tracked values (with small threshold)
loc_changed = (abs(props.loc_x - cam_loc.x) > 0.0001 or
abs(props.loc_y - cam_loc.y) > 0.0001 or
abs(props.loc_z - cam_loc.z) > 0.0001)
rot_changed = (abs(props.rot_x - cam_rot.x) > 0.0001 or
abs(props.rot_y - cam_rot.y) > 0.0001 or
abs(props.rot_z - cam_rot.z) > 0.0001)
# Reinitialize when first entering OR when camera changed externally
# Skip if an update is in progress (grace period active)
if not self.was_in_camera_view or (loc_changed or rot_changed):
if not in_grace:
context.scene.viewpilot.reinitialize_from_context(context)
self.was_in_camera_view = True
self.last_known_state = current_state
self.is_moving = False
return self._pass_through_tick(tick_start)
else:
# Just exited camera view - reinitialize to viewport mode
if self.was_in_camera_view and not in_grace:
context.scene.viewpilot.reinitialize_from_context(context)
self.was_in_camera_view = False
# Initialize if empty (and not in camera view)
if self.last_known_state is None:
self.last_known_state = current_state
add_to_history(current_state)
return self._pass_through_tick(tick_start)
# Check for difference
if not states_are_similar(current_state, self.last_known_state):
# Update UI properties to match the new viewport state (Live Sync)
# BUT skip if we're in a grace period (property update in progress)
if not in_grace:
try:
context.scene.viewpilot.reinitialize_from_context(context)
except (RuntimeError, ReferenceError, AttributeError, ValueError):
pass
# Detected movement away from the current state.
# If we are currently "on" a saved view, mark it as ghost (last active) and reset current index
if context.scene.saved_views_index != -1:
context.scene.viewpilot.last_active_view_index = context.scene.saved_views_index
context.scene.saved_views_index = -1
try:
with _suppress_saved_view_enum_load():
context.scene.viewpilot.saved_views_enum = 'NONE'
_set_panel_gallery_enum_safe(context, 'NONE')
except (TypeError, ValueError, RuntimeError, AttributeError):
pass
else:
# We are in a grace period.
# Special Case: USER_DRAG (Panel Sliders)
# If the user is dragging the UI sliders, we ARE modifying the state.
# We should NOT reinitialize (fight the user), but we SHOULD trigger Ghost Mode
# because the view is no longer the pristine saved view.
if controller.grace_period_source == UpdateSource.USER_DRAG:
if context.scene.saved_views_index != -1:
context.scene.viewpilot.last_active_view_index = context.scene.saved_views_index
context.scene.saved_views_index = -1
try:
with _suppress_saved_view_enum_load():
context.scene.viewpilot.saved_views_enum = 'NONE'
_set_panel_gallery_enum_safe(context, 'NONE')
except (TypeError, ValueError, RuntimeError, AttributeError):
pass
# If this is due to VIEW_RESTORE (loading a view), we should accept this new state
# as the baseline immediately to prevent "Ghost View" triggering once grace ends.
if controller.grace_period_source == UpdateSource.VIEW_RESTORE:
self.last_known_state = current_state
# Check if this change is just us restoring a history state within grace period
# OR if the state is actually identical (floating point drift)
if states_are_similar(current_state, self.last_known_state):
# False alarm or drift
self.is_moving = False
return self._pass_through_tick(tick_start)
# Check if this change is just us restoring a history state
if utils.view_history_index != -1 and utils.view_history:
# Safely get the state at the current index
if 0 <= utils.view_history_index < len(utils.view_history):
target_state = utils.view_history[utils.view_history_index]
if states_are_similar(current_state, target_state):
# We just restored this state. Update tracker but DON'T save as new.
self.last_known_state = current_state
self.is_moving = False
return self._pass_through_tick(tick_start)
# --- AUTO-DISABLE ORBIT MODE ON EXTERNAL MOVEMENT ---
# Only disable orbit if the camera POSITION or ROTATION actually changed.
# Ignore perspective mode changes (ortho/persp toggle).
props = context.scene.viewpilot
if props.orbit_around_selection and not in_grace:
# Check if position/rotation actually changed (not just perspective mode)
pos_changed = (current_state['view_location'] - self.last_known_state['view_location']).length_squared > 0.0001
rot_diff = abs(current_state['view_rotation'].dot(self.last_known_state['view_rotation']))
rot_changed = rot_diff < 0.9999
if pos_changed or rot_changed:
props['orbit_around_selection'] = False
props['orbit_initialized'] = False
# Movement detected!
self.is_moving = True
self.settle_start_time = now
self.last_known_state = current_state
elif self.is_moving:
# No movement, but we were moving recently. Check settle timer.
try:
settle_delay = get_preferences().settle_delay
except (AttributeError, RuntimeError, ValueError):
settle_delay = 0.3
if (now - self.settle_start_time) > settle_delay:
# Check if we should record this to history
# (suppressed during VIEW_RESTORE, HISTORY_NAV, or grace periods)
if controller.should_record_history():
add_to_history(current_state)
self.is_moving = False
return self._pass_through_tick(tick_start)
return {'PASS_THROUGH'}
def invoke(self, context, event):
if utils.monitor_running:
return {'CANCELLED'}
utils.monitor_running = True
self.last_known_state = None
self.is_moving = False
self.settle_start_time = 0.0
self.was_in_camera_view = False
self.last_selection_hash = None
self.last_orbit_mode = bool(context.scene.viewpilot.orbit_around_selection)
self.last_scene_count = len(bpy.data.scenes)
self.last_view_layer_counts = {scene.name: len(scene.view_layers) for scene in bpy.data.scenes}
self.last_camera_count = sum(1 for obj in context.scene.objects if obj.type == 'CAMERA')
self.last_maintenance_time = 0.0
self._timer = context.window_manager.event_timer_add(self.CHECK_INTERVAL, window=context.window)
context.window_manager.modal_handler_add(self)
return {'RUNNING_MODAL'}
def cancel(self, context):
utils.monitor_running = False
if self._timer:
context.window_manager.event_timer_remove(self._timer)
class VIEW3D_OT_view_history_back(bpy.types.Operator):
"""Go back in view history"""
bl_idname = "view3d.view_history_back"
bl_label = "View History Back"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.space_data and context.space_data.type == 'VIEW_3D'
def execute(self, context):
# Exit camera view if we're in it - history is for viewport navigation
if context.region_data.view_perspective == 'CAMERA':
bpy.ops.view3d.view_camera() # Toggle out of camera view
state = history_go_back(context)
if state:
self.report({'INFO'}, f"< History {utils.view_history_index + 1}/{len(utils.view_history)}")
# Sync UI properties to new view state
try:
context.scene.viewpilot.reinitialize_from_context(context)
except (RuntimeError, ReferenceError, AttributeError, ValueError) as e:
pass
return {'FINISHED'}
else:
if not utils.view_history:
self.report({'WARNING'}, "No history")
else:
self.report({'INFO'}, "< Reached start of history")
return {'FINISHED'}
class VIEW3D_OT_view_history_forward(bpy.types.Operator):
"""Go forward in view history"""
bl_idname = "view3d.view_history_forward"
bl_label = "View History Forward"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.space_data and context.space_data.type == 'VIEW_3D'
def execute(self, context):
# Exit camera view if we're in it - history is for viewport navigation
if context.region_data.view_perspective == 'CAMERA':
bpy.ops.view3d.view_camera() # Toggle out of camera view
state = history_go_forward(context)
if state:
self.report({'INFO'}, f"> History {utils.view_history_index + 1}/{len(utils.view_history)}")
# Sync UI properties to new view state
try:
context.scene.viewpilot.reinitialize_from_context(context)
except (RuntimeError, ReferenceError, AttributeError, ValueError) as e:
pass
return {'FINISHED'}
else:
# Show current position even when can't go forward
display_index = utils.view_history_index + 1 if utils.view_history_index != -1 else len(utils.view_history)
self.report({'INFO'}, f"> History {display_index}/{len(utils.view_history)}")
return {'FINISHED'}
# ========================================================================
# SYNC OPERATOR (for N-Panel/Popover initialization)
# ========================================================================
class VIEW3D_OT_sync_viewpilot(bpy.types.Operator):
"""Sync ViewPilot controls with current viewport state"""
bl_idname = "view3d.sync_viewpilot"
bl_label = "Sync to View"
bl_description = "Initialize or refresh ViewPilot controls from current view"
bl_options = {'REGISTER', 'INTERNAL'}
def execute(self, context):
try:
context.scene.viewpilot.reinitialize_from_context(context)
self.report({'INFO'}, "ViewPilot Synced")
return {'FINISHED'}
except (RuntimeError, ReferenceError, AttributeError, ValueError) as e:
self.report({'ERROR'}, f"Sync failed: {str(e)}")
return {'CANCELLED'}
class VIEW3D_OT_open_viewpilot_prefs(bpy.types.Operator):
"""Toggle ViewPilot addon preferences"""
bl_idname = "view3d.open_viewpilot_prefs"
bl_label = "ViewPilot Preferences"
bl_description = "Open ViewPilot addon preferences"
bl_options = {'REGISTER', 'INTERNAL'}
def execute(self, context):
# Open preferences window, switch to addons tab, filter by addon name, expand panel
bpy.ops.screen.userpref_show('INVOKE_DEFAULT')
context.preferences.active_section = 'ADDONS'
context.window_manager.addon_search = "ViewPilot"
bpy.ops.preferences.addon_expand(module=__package__)
return {'FINISHED'}
# ========================================================================
# CAMERA UTILITY OPERATORS
# ========================================================================
class VIEW3D_OT_toggle_camera_selection(bpy.types.Operator):
"""Toggle 'Keep Camera Active' mode - camera stays selected as active object"""
bl_idname = "view3d.toggle_camera_selection"
bl_label = "Keep Camera Active"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.scene.camera is not None
def execute(self, context):
props = context.scene.viewpilot
cam = context.scene.camera
# Toggle the mode
props.keep_camera_active = not props.keep_camera_active
if props.keep_camera_active:
# Mode ON: Select and make camera active
cam.select_set(True)
context.view_layer.objects.active = cam
else:
# Mode OFF: Deselect camera
cam.select_set(False)
if context.view_layer.objects.active == cam:
context.view_layer.objects.active = None
# Sync camera dropdown to current scene camera (fixes blank dropdown after camera deletion)
try:
props.camera_enum = cam.name
except TypeError:
pass # Enum items not yet populated
return {'FINISHED'}
class VIEW3D_OT_toggle_camera_name(bpy.types.Operator):
"""Toggle camera name visibility in viewport"""
bl_idname = "view3d.toggle_camera_name"
bl_label = "Toggle Camera Name"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.scene.camera is not None
def execute(self, context):
cam_obj = context.scene.camera
cam_data = cam_obj.data
# Toggle both properties together
new_state = not cam_data.show_name
cam_data.show_name = new_state
cam_obj.show_name = new_state
return {'FINISHED'}
class VIEW3D_OT_exit_camera_view(bpy.types.Operator):
"""Exit camera view (Numpad 0)"""
bl_idname = "view3d.exit_camera_view"
bl_label = "Exit Camera View"
bl_options = {'REGISTER', 'UNDO'}
clear_camera: bpy.props.BoolProperty(
name="Clear Camera",
description="Also clear the scene camera after exiting",
default=False
)
@classmethod
def poll(cls, context):
return (context.space_data and
context.space_data.type == 'VIEW_3D' and
context.region_data and
context.region_data.view_perspective == 'CAMERA')
def execute(self, context):
# Use Blender's view_camera operator - this properly restores the pre-camera viewport state
bpy.ops.view3d.view_camera()
# Optionally clear the scene camera
if self.clear_camera:
context.scene.camera = None
# Sync UI properties immediately
try:
context.scene.viewpilot.reinitialize_from_context(context)
except (RuntimeError, ReferenceError, AttributeError, ValueError):
pass
return {'FINISHED'}
class VIEW3D_OT_create_camera_from_view(bpy.types.Operator):
"""Create a new camera matching the current viewport look"""
bl_idname = "view3d.create_camera_from_view"
bl_label = "Create Camera from Current"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
return context.space_data and context.space_data.type == 'VIEW_3D'
def execute(self, context):
from .utils import get_view_location, create_camera_from_view_data
space = context.space_data
region = context.region_data
# Get preferences with fallbacks
try:
prefs = get_preferences()
passepartout = prefs.camera_passepartout
show_passepartout = prefs.show_passepartout
show_name = prefs.show_camera_name
show_sensor = prefs.show_camera_sensor
use_collection = prefs.use_camera_collection
make_active = prefs.make_camera_active
camera_name_prefix = prefs.camera_name_prefix
collection_name = prefs.camera_collection_name
collection_color = prefs.camera_collection_color
except (AttributeError, RuntimeError, ValueError):
passepartout = 0.95
show_passepartout = True
show_name = True
show_sensor = True
use_collection = True
make_active = True
camera_name_prefix = "ViewCam"
collection_name = "ViewPilot"
collection_color = 'COLOR_04'
# Use active view name if available, otherwise just the prefix
camera_name = camera_name_prefix
active_view_index = context.scene.saved_views_index
if active_view_index >= 0 and active_view_index < len(context.scene.saved_views):
view_name = context.scene.saved_views[active_view_index].name
camera_name = f"{camera_name_prefix} [{view_name}]"
# Get current view data
eye_pos = get_view_location(context)
# Create camera using centralized utility
cam_obj = create_camera_from_view_data(
context=context,
name=camera_name,
location=eye_pos,
rotation=region.view_rotation,
is_perspective=region.is_perspective,
lens=space.lens,
distance=region.view_distance,
clip_start=space.clip_start,
clip_end=space.clip_end,
passepartout=passepartout,
show_passepartout=show_passepartout,
show_name=show_name,
show_sensor=show_sensor,
use_collection=use_collection,
collection_name=collection_name,
collection_color=collection_color
)
cam_data = cam_obj.data
# Make it the active camera if enabled
if make_active:
context.view_layer.objects.active = cam_obj
cam_obj.select_set(True)
context.scene.camera = cam_obj
# Swap render resolution if orientation doesn't match sensor
# This prevents the view from jumping when centering the camera
render = context.scene.render
sensor_is_horizontal = cam_data.sensor_fit == 'HORIZONTAL' or (
cam_data.sensor_fit == 'AUTO' and cam_data.sensor_width >= cam_data.sensor_height
)
render_is_horizontal = render.resolution_x >= render.resolution_y
rotated_resolution = False
if sensor_is_horizontal != render_is_horizontal:
# Swap resolution to match sensor orientation
render.resolution_x, render.resolution_y = render.resolution_y, render.resolution_x
rotated_resolution = True
bpy.ops.view3d.view_camera() # Use operator to properly store previous view state
bpy.ops.view3d.view_center_camera() # Center/zoom to fit camera frame
if rotated_resolution:
self.report({'INFO'}, "New Camera - Output resolution rotated to fit sensor")
else:
self.report({'INFO'}, "Created new Active Camera")
else:
self.report({'INFO'}, "Created Camera from View")
return {'FINISHED'}
class VIEW3D_OT_dolly_to_obstacle(bpy.types.Operator):
"""Move camera backward until it hits an obstacle (useful for maximizing view in tight spaces)"""
bl_idname = "view3d.dolly_to_obstacle"
bl_label = "Dolly to Obstacle"
bl_options = {'REGISTER', 'UNDO'}
offset: bpy.props.FloatProperty(
name="Offset",
description="Distance to keep from the obstacle",
default=0.05,
min=0.001,
max=1.0,
unit='LENGTH'
)
@classmethod
def poll(cls, context):
# Available in 3D view (camera view or viewport)
if not context.space_data or context.space_data.type != 'VIEW_3D':
return False
return True
def execute(self, context):
from mathutils import Vector
space = context.space_data
region = space.region_3d
# Determine if we're in camera view or viewport mode
in_camera_view = region.view_perspective == 'CAMERA' and context.scene.camera
if in_camera_view:
# Camera mode: move the actual camera object
cam = context.scene.camera
cam_pos = cam.matrix_world.translation.copy()
# Camera looks down its negative local Z, so backward is positive local Z
cam_backward = cam.matrix_world.to_3x3() @ Vector((0, 0, 1))
cam_backward.normalize()
else:
# Viewport mode: move the viewport "eye" position
cam_pos = get_view_location(context)
# Backward is opposite of view direction
cam_backward = region.view_rotation @ Vector((0, 0, 1))
cam_backward.normalize()
# Calculate max ray distance from scene bounding box (capped at 1km)
max_distance = self._get_scene_diagonal(context)
max_distance = min(max_distance, 1000.0) # Cap at 1km
# Use depsgraph for evaluated objects (visible meshes only)
depsgraph = context.evaluated_depsgraph_get()
# Raycast backward from camera/viewport
result, location, normal, index, obj, matrix = context.scene.ray_cast(
depsgraph,
cam_pos,
cam_backward,
distance=max_distance
)
if result:
# Calculate new position with offset
hit_distance = (location - cam_pos).length
new_distance = hit_distance - self.offset
if new_distance > 0:
new_pos = cam_pos + cam_backward * new_distance
if in_camera_view:
cam.location = new_pos
else:
# For viewport: compute new view_location from new eye position
# view_location = eye_position - (rotation @ view_z) * view_distance
from mathutils import Vector
view_z = Vector((0.0, 0.0, 1.0))
offset = (region.view_rotation @ view_z) * region.view_distance
region.view_location = new_pos - offset
self.report({'INFO'}, f"Moved back {new_distance:.2f} units to obstacle")
else:
self.report({'WARNING'}, "Already at or past obstacle")
else:
self.report({'INFO'}, "No obstacle found behind")
return {'FINISHED'}
def _get_scene_diagonal(self, context):
"""Calculate the diagonal of the bounding box containing all visible mesh objects."""
from mathutils import Vector
min_corner = Vector((float('inf'), float('inf'), float('inf')))
max_corner = Vector((float('-inf'), float('-inf'), float('-inf')))
found_mesh = False
for obj in context.visible_objects:
if obj.type != 'MESH':
continue
found_mesh = True
# Get world-space bounding box corners
bbox_corners = [obj.matrix_world @ Vector(corner) for corner in obj.bound_box]
for corner in bbox_corners:
min_corner.x = min(min_corner.x, corner.x)
min_corner.y = min(min_corner.y, corner.y)
min_corner.z = min(min_corner.z, corner.z)
max_corner.x = max(max_corner.x, corner.x)
max_corner.y = max(max_corner.y, corner.y)
max_corner.z = max(max_corner.z, corner.z)
if not found_mesh:
return 100.0 # Default fallback
# Return diagonal length
return (max_corner - min_corner).length
# ========================================================================
# SAVED VIEWS OPERATORS
# ========================================================================
@contextmanager
def _suppress_saved_view_enum_load():
"""Temporarily suppress enum load callbacks and always restore prior state."""
controller = get_controller()
prev_skip = controller.skip_enum_load
controller.skip_enum_load = True
try:
yield
finally:
controller.skip_enum_load = prev_skip
def _set_panel_gallery_enum_safe(context, preferred_value=None):
"""Delegate to the canonical enum-safe setter in properties.py."""
try:
from .properties import _set_panel_gallery_enum_safe as _set_safe
return _set_safe(context.scene.viewpilot, preferred_value)
except (ImportError, AttributeError, TypeError, ValueError, RuntimeError):
return False
def _sync_saved_view_enums_safe(context, enum_value):
"""Synchronize dropdown + panel enums without assuming panel supports NONE."""
props = context.scene.viewpilot
try:
props.saved_views_enum = enum_value
except (AttributeError, TypeError, ValueError, RuntimeError):
pass
_set_panel_gallery_enum_safe(context, enum_value)
def _handle_storage_invalid(context, reporter, action_label="save view"):
"""Report invalid storage and prompt user with overwrite/cancel options."""
reporter.report({'ERROR'}, f"Can't {action_label}: ViewPilot storage is corrupted")
try:
bpy.ops.viewpilot.recover_storage_overwrite('INVOKE_DEFAULT', action_label=action_label)
except RuntimeError as error:
reporter.report({'WARNING'}, "Recovery dialog unavailable (see console)")
except (TypeError, AttributeError, ValueError) as error:
reporter.report({'WARNING'}, "Recovery dialog unavailable (see console)")
def _refresh_saved_views_ui(include_modal_gallery=True):
"""Invalidate saved-view UI caches and optionally refresh modal gallery."""
try:
from .properties import invalidate_saved_views_ui_caches
invalidate_saved_views_ui_caches()
except (ImportError, AttributeError, TypeError, ValueError, RuntimeError) as error:
try:
from .preview_manager import invalidate_panel_gallery_cache
invalidate_panel_gallery_cache()
except (ImportError, AttributeError, TypeError, ValueError, RuntimeError) as fallback_error:
pass
if include_modal_gallery and VIEW3D_OT_thumbnail_gallery._is_active:
VIEW3D_OT_thumbnail_gallery.request_refresh()
class VIEWPILOT_OT_recover_storage_overwrite(bpy.types.Operator):
"""Overwrite corrupted ViewPilot storage with a fresh empty payload."""
bl_idname = "viewpilot.recover_storage_overwrite"
bl_label = "ViewPilot Storage Recovery"
bl_options = {'INTERNAL'}
action_label: bpy.props.StringProperty(default="save view", options={'SKIP_SAVE'})
def invoke(self, context, event):
wm = context.window_manager
try:
return wm.invoke_props_dialog(
self,
width=520,
title="ViewPilot Storage Error",
confirm_text="Overwrite",
cancel_default=True,
)
except TypeError:
return wm.invoke_props_dialog(self, width=520)
def draw(self, context):
from . import data_storage
layout = self.layout
layout.label(text=f"Can't {self.action_label} because JSON storage is corrupted.")
layout.label(text="Overwrite ViewPilot storage and start from scratch?")
backup_name = data_storage.get_storage_error_backup_name()
if backup_name:
layout.label(text=f"Backup: {backup_name}", icon='FILE_TEXT')
error_msg = data_storage.get_storage_error_message()
if error_msg:
layout.label(text=f"Details: {error_msg}")
def execute(self, context):
from . import data_storage
if not data_storage.force_reset_storage():
self.report({'ERROR'}, "Failed to overwrite ViewPilot storage (see console)")
return {'CANCELLED'}
try:
data_storage.sync_to_all_scenes()
except (RuntimeError, ReferenceError, AttributeError, ValueError):
pass
_refresh_saved_views_ui()
self.report({'INFO'}, "ViewPilot storage overwritten. You can save views again.")
return {'FINISHED'}
class VIEW3D_OT_save_current_view(bpy.types.Operator):
"""Save the current viewport as a new view"""
bl_idname = "view3d.save_current_view"
bl_label = "Save Current View"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
_, space, region = utils.find_view3d_context(context)
return bool(space and region)
def execute(self, context):
from . import data_storage
preferred_area = VIEW3D_OT_thumbnail_gallery._context_area
_, space, region = utils.find_view3d_context(context, preferred_area=preferred_area)
if not space or not region:
self.report({'ERROR'}, "No 3D View found")
return {'CANCELLED'}
# Auto-generate unique name using the counter
view_number = data_storage.get_next_view_number()
if view_number < 0:
_handle_storage_invalid(context, self, "save view")
return {'CANCELLED'}
view_name = f"View {view_number}"
# Capture viewport state as a dictionary
view_dict = data_storage.capture_viewport_as_dict(space, region, context, view_name)
# Apply default remember toggles from preferences
try:
prefs = get_preferences()
view_dict["remember_perspective"] = prefs.default_remember_perspective
view_dict["remember_shading"] = prefs.default_remember_shading
view_dict["remember_overlays"] = prefs.default_remember_overlays
view_dict["remember_composition"] = prefs.default_remember_composition
except (AttributeError, RuntimeError, ValueError):
pass # Keep defaults from capture_viewport_as_dict
# Add to JSON storage (auto-syncs to PropertyGroup)
new_index = data_storage.add_saved_view(view_dict)
if new_index < 0:
_handle_storage_invalid(context, self, "save view")
return {'CANCELLED'}
# Generate thumbnail for this view
# Create a temporary PropertyGroup-like object for thumbnail generator
try:
from types import SimpleNamespace
temp_view = SimpleNamespace(**view_dict)
# Convert lists to tuples for compatibility
temp_view.location = tuple(view_dict["location"])
temp_view.rotation = tuple(view_dict["rotation"])
thumb_name = generate_thumbnail(context, temp_view, view_name)
if thumb_name:
view_dict["thumbnail_image"] = thumb_name
if not data_storage.update_saved_view(new_index, view_dict):
self.report({'WARNING'}, "Thumbnail saved in-memory but ViewPilot storage update was blocked")
# Notify gallery to refresh if open
if VIEW3D_OT_thumbnail_gallery._is_active:
VIEW3D_OT_thumbnail_gallery.request_refresh()
except (RuntimeError, ReferenceError, AttributeError, TypeError, ValueError, OSError):
self.report({'WARNING'}, "Thumbnail generation failed (see console)")
traceback.print_exc()
# Set as active
context.scene.saved_views_index = new_index
# Sync the shared property enum to show the new view
# We temporarily skip loading because we are already AT the view
with _suppress_saved_view_enum_load():
_sync_saved_view_enums_safe(context, str(new_index))
self.report({'INFO'}, f"Saved view: {view_name}")
return {'FINISHED'}
class VIEW3D_OT_load_saved_view(bpy.types.Operator):
"""Load the selected saved view"""
bl_idname = "view3d.load_saved_view"
bl_label = "Load Saved View"
bl_options = {'REGISTER', 'UNDO'}
@classmethod
def poll(cls, context):
from . import data_storage
views = data_storage.get_saved_views()
_, space, region = utils.find_view3d_context(context)
return bool(space and region and len(views) > 0 and context.scene.saved_views_index >= 0)
def execute(self, context):
from . import data_storage
preferred_area = VIEW3D_OT_thumbnail_gallery._context_area
_, space, region = utils.find_view3d_context(context, preferred_area=preferred_area)
if not space or not region:
self.report({'ERROR'}, "No 3D View found")
return {'CANCELLED'}
index = context.scene.saved_views_index
view_dict = data_storage.get_saved_view(index)
if not view_dict:
self.report({'WARNING'}, "No saved view selected")
return {'CANCELLED'}
# Apply view using data_storage helper
data_storage.apply_view_to_viewport(view_dict, space, region, context)
# Lock history recording briefly using StateController
get_controller().start_grace_period(0.5, UpdateSource.VIEW_RESTORE)
# Sync properties to new view
context.scene.viewpilot.reinitialize_from_context(context)
# Reset ghost tracking
context.scene.viewpilot.last_active_view_index = -1
self.report({'INFO'}, f"Loaded view: {view_dict.get('name', 'View')}")
return {'FINISHED'}
class VIEW3D_OT_delete_saved_view(bpy.types.Operator):
"""Delete the selected saved view"""
bl_idname = "view3d.delete_saved_view"
bl_label = "Delete Saved View"
bl_options = {'REGISTER', 'UNDO'}
# Optional index - if >= 0, use this instead of saved_views_index
index: bpy.props.IntProperty(default=-1)