-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathpreferences.py
More file actions
420 lines (354 loc) · 15.4 KB
/
preferences.py
File metadata and controls
420 lines (354 loc) · 15.4 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
import bpy
import textwrap
from .keymap import keymaps_items_dict
from .keymap import remove_key, add_key
from .ui import VIEW3D_PT_SimpleCameraManager
def label_multiline(context, text, parent):
"""
Draw a label with multiline text in the layout.
Args:
context (Context): The current context.
text (str): The text to display.
parent (UILayout): The parent layout to add the label to.
"""
chars = int(context.region.width / 7) # 7 pix on 1 character
wrapper = textwrap.TextWrapper(width=chars)
text_lines = wrapper.wrap(text=text)
for text_line in text_lines:
parent.label(text=text_line)
def update_popup_size(self, context):
"""Defer re-registration so the preferences window has finished its event."""
def _apply():
from .ui import OBJECT_PT_camera_manager_popup
prefs = bpy.context.preferences.addons[__package__].preferences
# Close the popup if it is open — it lives as a temporary window.
# Must happen before unregister_class while the popup holds a reference.
for window in list(bpy.context.window_manager.windows):
if window.screen.is_temporary:
with bpy.context.temp_override(window=window):
bpy.ops.wm.window_close()
try:
bpy.utils.unregister_class(OBJECT_PT_camera_manager_popup)
OBJECT_PT_camera_manager_popup.bl_ui_units_x = prefs.popup_width
bpy.utils.register_class(OBJECT_PT_camera_manager_popup)
except Exception as e:
print(f"[CAM_MANAGER] Could not apply popup size: {e}")
bpy.app.timers.register(_apply, first_interval=0.0)
def update_panel_category(self, context):
"""Update panel tab for simple export"""
from .ui import VIEW3D_PT_SimpleCameraManager as _panel_cls
panels = [
_panel_cls,
]
for panel in panels:
try:
bpy.utils.unregister_class(panel)
except:
pass
prefs = context.preferences.addons[__package__].preferences
panel.bl_category = prefs.panel_category
if prefs.enable_n_panel:
try:
bpy.utils.register_class(panel)
except ValueError:
pass # Avoid duplicate registrations
return
def update_key(self, context, property_prefix):
"""Update keymap for a given property prefix (e.g., 'next_cam', 'prev_cam')."""
wm = context.window_manager
km = wm.keyconfigs.addon.keymaps.get('Window') # Using Window instead of 3D View to fix issue of keymap not working on Linux
if not km:
return
prefs = context.preferences.addons[__package__].preferences
# Get the relevant dictionary entry
key_entry = None
for key, value in keymaps_items_dict.items():
if value["name"] == property_prefix:
key_entry = value
break
if not key_entry:
return
idname = key_entry["idname"]
operator_name = key_entry["operator"]
# Remove previous key assignment
remove_key(context, idname, operator_name)
# Add new key assignment with user preferences
add_key(
context,
idname,
getattr(prefs, f"{property_prefix}_type"),
getattr(prefs, f"{property_prefix}_ctrl"),
getattr(prefs, f"{property_prefix}_shift"),
getattr(prefs, f"{property_prefix}_alt"),
operator_name,
getattr(prefs, f"{property_prefix}_active")
)
# addon Preferences
class CAM_MANAGER_OT_renaming_preferences(bpy.types.AddonPreferences):
"""Contains the blender addon preferences"""
# this must match the addon name, use '__package__'
# when defining this in a submodule of a python package.
bl_idname = __package__ ### __package__ works on multifile and __name__ not
# addon updater preferences
prefs_tabs: bpy.props.EnumProperty(items=(('GENERAL', "General", "General Settings"),
('KEYMAPS', "Keymaps", "Keymap Settings"),
('SUPPORT', "Support", "Support me")),
default='GENERAL')
next_cam_type: bpy.props.StringProperty(
name="Next Camera",
default=keymaps_items_dict["Next Camera"]["type"],
update=lambda self, context: update_key(self, context, "next_cam")
)
next_cam_ctrl: bpy.props.BoolProperty(
name="Ctrl",
default=keymaps_items_dict["Next Camera"]["ctrl"],
update=lambda self, context: update_key(self, context, "next_cam")
)
next_cam_shift: bpy.props.BoolProperty(
name="Shift",
default=keymaps_items_dict["Next Camera"]["shift"],
update=lambda self, context: update_key(self, context, "next_cam")
)
next_cam_alt: bpy.props.BoolProperty(
name="Alt",
default=keymaps_items_dict["Next Camera"]["alt"],
update=lambda self, context: update_key(self, context, "next_cam")
)
next_cam_active: bpy.props.BoolProperty(
name="Active",
default=keymaps_items_dict["Next Camera"]["active"],
update=lambda self, context: update_key(self, context, "next_cam")
)
prev_cam_type: bpy.props.StringProperty(
name="Previous Camera",
default=keymaps_items_dict["Previous Camera"]["type"],
update=lambda self, context: update_key(self, context, "prev_cam")
)
prev_cam_ctrl: bpy.props.BoolProperty(
name="Ctrl",
default=keymaps_items_dict["Previous Camera"]["ctrl"],
update=lambda self, context: update_key(self, context, "prev_cam")
)
prev_cam_shift: bpy.props.BoolProperty(
name="Shift",
default=keymaps_items_dict["Previous Camera"]["shift"],
update=lambda self, context: update_key(self, context, "prev_cam")
)
prev_cam_alt: bpy.props.BoolProperty(
name="Alt",
default=keymaps_items_dict["Previous Camera"]["alt"],
update=lambda self, context: update_key(self, context, "prev_cam")
)
prev_cam_active: bpy.props.BoolProperty(
name="Active",
default=keymaps_items_dict["Previous Camera"]["active"],
update=lambda self, context: update_key(self, context, "prev_cam")
)
cam_pie_type: bpy.props.StringProperty(
name="Active Camera Pie",
default=keymaps_items_dict["Active Camera Pie"]["type"],
update=lambda self, context: update_key(self, context, "cam_pie")
)
cam_pie_ctrl: bpy.props.BoolProperty(
name="Ctrl",
default=keymaps_items_dict["Active Camera Pie"]["ctrl"],
update=lambda self, context: update_key(self, context, "cam_pie")
)
cam_pie_shift: bpy.props.BoolProperty(
name="Shift",
default=keymaps_items_dict["Active Camera Pie"]["shift"],
update=lambda self, context: update_key(self, context, "cam_pie")
)
cam_pie_alt: bpy.props.BoolProperty(
name="Alt",
default=keymaps_items_dict["Active Camera Pie"]["alt"],
update=lambda self, context: update_key(self, context, "cam_pie")
)
cam_pie_active: bpy.props.BoolProperty(
name="Active",
default=keymaps_items_dict["Active Camera Pie"]["active"],
update=lambda self, context: update_key(self, context, "cam_pie")
)
cam_menu_type: bpy.props.StringProperty(
name="Camera Manager Popup",
default=keymaps_items_dict["Simple Camera Manager"]["type"],
update=lambda self, context: update_key(self, context, "cam_menu")
)
cam_menu_ctrl: bpy.props.BoolProperty(
name="Ctrl",
default=keymaps_items_dict["Simple Camera Manager"]["ctrl"],
update=lambda self, context: update_key(self, context, "cam_menu")
)
cam_menu_shift: bpy.props.BoolProperty(
name="Shift",
default=keymaps_items_dict["Simple Camera Manager"]["shift"],
update=lambda self, context: update_key(self, context, "cam_menu")
)
cam_menu_alt: bpy.props.BoolProperty(
name="Alt",
default=keymaps_items_dict["Simple Camera Manager"]["alt"],
update=lambda self, context: update_key(self, context, "cam_menu")
)
cam_menu_active: bpy.props.BoolProperty(
name="Active",
default=keymaps_items_dict["Simple Camera Manager"]["active"],
update=lambda self, context: update_key(self, context, "cam_menu")
)
panel_category: bpy.props.StringProperty(name="Category Tab",
description="The category name used to organize the addon in the properties panel for all the addons",
default='Simple Camera Manager',
update=update_panel_category) # update = update_panel_position,
enable_n_panel: bpy.props.BoolProperty(
name="Enable Simple Camera Manager N-Panel",
description="Toggle the N-Panel on and off.",
default=True,
update=update_panel_category)
popup_width: bpy.props.IntProperty(
name="Popup Width",
description="Width of the Simple Camera Manager popup window in UI units",
default=65,
min=10,
max=100,
update=update_popup_size)
def keymap_ui(self, layout, title, property_prefix):
box = layout.box()
split = box.split(align=True, factor=0.5)
col = split.column()
# Is hotkey active checkbox
row = col.row(align=True)
row.prop(self, f"{property_prefix}_active", text="")
row.label(text=title)
# Button to assign the key assignments
col = split.column()
row = col.row(align=True)
key_type = getattr(self, f"{property_prefix}_type")
text = (
bpy.types.Event.bl_rna.properties["type"].enum_items[key_type].name
if key_type != "NONE"
else "Press a key"
)
op = row.operator("cam.key_selection_button", text=text)
op.property_prefix = property_prefix
op = row.operator("cam.remove_hotkey", text="", icon="X")
op.idname = keymaps_items_dict[title]["idname"]
op.properties_name = keymaps_items_dict[title]["operator"]
op.property_prefix = property_prefix
row = col.row(align=True)
row.prop(self, f"{property_prefix}_ctrl")
row.prop(self, f"{property_prefix}_shift")
row.prop(self, f"{property_prefix}_alt")
locked_camera_overlay_color: bpy.props.FloatVectorProperty(
name="Locked Camera",
description="Passepartout tint colour when the active camera is locked",
subtype='COLOR_GAMMA',
size=4,
min=0.0, max=1.0,
default=(0.1, 0.3, 1.0, 0.4),
)
linked_camera_overlay_color: bpy.props.FloatVectorProperty(
name="Viewport Linked Camera",
description="Passepartout tint colour when the viewport is locked to the camera",
subtype='COLOR_GAMMA',
size=4,
min=0.0, max=1.0,
default=(1.0, 0.2, 0.1, 0.4),
)
def draw(self, context):
""" simple preference UI to define custom inputs and user preferences"""
layout = self.layout
row = layout.row(align=True)
row.prop(self, "prefs_tabs", expand=True)
if self.prefs_tabs == 'GENERAL':
box = layout.box()
box.label(text="UI")
box.prop(self, 'enable_n_panel')
box.prop(self, 'panel_category')
box = layout.box()
box.label(text="Popup Window Size")
box.prop(self, 'popup_width')
# updater draw function
# could also pass in col as third arg
box = layout.box()
box.operator("cam_manager.reload_addon", icon='FILE_REFRESH')
box = layout.box()
box.label(text="Camera Overlay Colours")
row = box.row()
row.prop(self, "locked_camera_overlay_color")
row = box.row()
row.prop(self, "linked_camera_overlay_color")
# Settings regarding the keymap
elif self.prefs_tabs == 'KEYMAPS':
box = layout.box()
for title, value in keymaps_items_dict.items():
self.keymap_ui(box, title, value["name"])
elif self.prefs_tabs == 'SUPPORT':
# Cross Promotion
box = layout.box()
### SIMPLE Camera Manager
col = box.column(align=True)
row = col.row()
row.label(text="♥♥♥ Leave a Review or Rating! ♥♥♥")
row = col.row()
row.label(text='Support & Feedback')
row = col.row(align=True)
row.label(text="Simple Camera Manager")
row.operator("wm.url_open", text="Superhive",
icon="URL").url = "https://superhivemarket.com/products/simple-camera-manager"
row.operator("wm.url_open", text="Gumroad",
icon="URL").url = "https://weisl.gumroad.com/l/simple_camera_manager"
col = box.column(align=True)
row = col.row()
row.label(text='Join the Discussion!')
row = col.row()
row.operator("wm.url_open", text="Join Discord", icon="URL").url = "https://discord.gg/VRzdcFpczm"
### SIMPLE TOOLS PROMOTION
box = layout.box()
col = box.column(align=True)
text = "Explore my other Blender Addons designed for more efficient game asset workflows!"
label_multiline(
context=context,
text=text,
parent=col
)
box.label(text="Simple Tools ($)")
col = box.column(align=True)
row = col.row(align=True)
row.label(text="Simple Collider")
row.operator("wm.url_open", text="Superhive",
icon="URL").url = "https://superhivemarket.com/products/simple-collider"
row.operator("wm.url_open", text="Gumroad",
icon="URL").url = "https://weisl.gumroad.com/l/simple_collider"
row = col.row(align=True)
row.label(text="Simple Export")
# row.operator("wm.url_open", text="Superhive",
# icon="URL").url = "https://superhivemarket.com/products/simple-export"
row.operator("wm.url_open", text="Gumroad",
icon="URL").url = "https://weisl.gumroad.com/l/simple_export"
box.label(text="Simple Tools (Free)")
col = box.column(align=True)
row = col.row(align=True)
row.label(text="Simple Renaming")
row.operator("wm.url_open", text="Blender Extensions",
icon="URL").url = "https://extensions.blender.org/add-ons/simple-renaming-panel/"
row.operator("wm.url_open", text="Gumroad",
icon="URL").url = "https://weisl.gumroad.com/l/simple_renaming"
classes = (
CAM_MANAGER_OT_renaming_preferences,
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
from .keymap import add_keymap
add_keymap()
# Defer panel category and popup width updates so they run after the full
# registration chain completes — avoids unregister/re-register during register.
bpy.app.timers.register(
lambda: update_panel_category(None, bpy.context), first_interval=0.0
)
update_popup_size(None, bpy.context)
def unregister():
from bpy.utils import unregister_class
for cls in reversed(classes):
if hasattr(cls, 'bl_rna'):
unregister_class(cls)