Merge branch 'blender-v3.6-release'
[blender-addons.git] / greasepencil_tools / prefs.py
blob0918a7a8ca7f564b2c77261f62bb0a159de76a50
1 # SPDX-License-Identifier: GPL-2.0-or-later
3 import bpy
4 import os
5 from bpy.props import (
6 BoolProperty,
7 EnumProperty,
8 StringProperty,
9 PointerProperty,
10 FloatProperty,
11 # IntProperty,
14 from .ui_panels import GP_PT_sidebarPanel
16 def get_addon_prefs():
17 import os
18 addon_name = os.path.splitext(__name__)[0]
19 addon_prefs = bpy.context.preferences.addons[addon_name].preferences
20 return (addon_prefs)
22 from .timeline_scrub import GPTS_timeline_settings, draw_ts_pref
23 from .layer_navigator import GPNAV_layer_navigator_settings, draw_nav_pref
25 ## Addons Preferences Update Panel
26 def update_panel(self, context):
27 try:
28 bpy.utils.unregister_class(GP_PT_sidebarPanel)
29 except:
30 pass
31 GP_PT_sidebarPanel.bl_category = get_addon_prefs().category
32 bpy.utils.register_class(GP_PT_sidebarPanel)
34 ## keymap binder for rotate canvas
35 def auto_rebind(self, context):
36 unregister_keymaps()
37 register_keymaps()
39 class GreasePencilAddonPrefs(bpy.types.AddonPreferences):
40 bl_idname = os.path.splitext(__name__)[0] #__package__
42 ts: PointerProperty(type=GPTS_timeline_settings)
43 nav: PointerProperty(type=GPNAV_layer_navigator_settings)
46 category : StringProperty(
47 name="Category",
48 description="Choose a name for the category of the panel",
49 default="Grease Pencil",
50 update=update_panel)
52 ## Tool tab
53 pref_tab : EnumProperty(
54 name="Preference Tool Tab", description="Choose tool preferences to display",
55 default='canvas_rotate',
56 items=(
57 ('canvas_rotate', 'Rotate Canvas', 'Canvas Rotate tool shortcut and prefs', 0),
58 ('box_deform', 'Box Deform', 'Box Deform tool prefs', 1),
59 ('timeline_scrub', 'Timeline Scrub', 'Timeline Scrub tool shortcut and prefs', 2),
60 ('layer_navigator', 'Layer Navigator', 'Layer Navigation tool shortcut and prefs', 3),
64 # --- props
66 use_clic_drag : BoolProperty(
67 name='Use click drag directly on points',
68 description="Change the active tool to 'tweak' during modal, Allow to direct clic-drag points of the box",
69 default=True)
71 default_deform_type : EnumProperty(
72 items=(('KEY_LINEAR', "Linear (perspective mode)", "Linear interpolation, like corner deform / perspective tools of classic 2D", 'IPO_LINEAR',0),
73 ('KEY_BSPLINE', "Spline (smooth deform)", "Spline interpolation transformation\nBest when lattice is subdivided", 'IPO_CIRC',1),
75 name='Starting Interpolation', default='KEY_LINEAR', description='Choose default interpolation when entering mode')
77 # About interpolation : https://docs.blender.org/manual/en/2.83/animation/shape_keys/shape_keys_panel.html#fig-interpolation-type
79 auto_swap_deform_type : BoolProperty(
80 name='Auto swap interpolation mode',
81 description="Automatically set interpolation to 'spline' when subdividing lattice\n Back to 'linear' when",
82 default=True)
84 ## rotate canvas variables
86 ## Use HUD
87 canvas_use_hud: BoolProperty(
88 name = "Use Hud",
89 description = "Display angle lines and angle value as text on viewport",
90 default = False)
92 canvas_use_view_center: BoolProperty(
93 name = "Rotate From View Center In Camera",
94 description = "Rotate from view center in camera view, Else rotate from camera center",
95 default = True)
97 ## Canvas rotate
98 canvas_use_shortcut: BoolProperty(
99 name = "Use Default Shortcut",
100 description = "Use default shortcut: mouse double-click + modifier",
101 default = True,
102 update=auto_rebind)
104 mouse_click : EnumProperty(
105 name="Mouse button", description="click on right/left/middle mouse button in combination with a modifier to trigger alignment",
106 default='MIDDLEMOUSE',
107 items=(
108 ('RIGHTMOUSE', 'Right click', 'Use click on Right mouse button', 'MOUSE_RMB', 0),
109 ('LEFTMOUSE', 'Left click', 'Use click on Left mouse button', 'MOUSE_LMB', 1),
110 ('MIDDLEMOUSE', 'Mid click', 'Use click on Mid mouse button', 'MOUSE_MMB', 2),
112 update=auto_rebind)
114 use_shift: BoolProperty(
115 name = "combine with shift",
116 description = "add shift",
117 default = False,
118 update=auto_rebind)
120 use_alt: BoolProperty(
121 name = "combine with alt",
122 description = "add alt",
123 default = True,
124 update=auto_rebind)
126 use_ctrl: BoolProperty(
127 name = "combine with ctrl",
128 description = "add ctrl",
129 default = True,
130 update=auto_rebind)
132 rc_angle_step: FloatProperty(
133 name="Angle Steps",
134 description="Step the rotation using this angle when using rotate canvas step modifier",
135 default=0.2617993877991494, # 15
136 min=0.01745329238474369, # 1
137 max=3.1415927410125732, # 180
138 soft_min=0.01745329238474369, # 1
139 soft_max=1.5707963705062866, # 90
140 step=10, precision=1, subtype='ANGLE', unit='ROTATION')
142 def draw(self, context):
143 prefs = get_addon_prefs()
144 layout = self.layout
145 # layout.use_property_split = True
146 row= layout.row(align=True)
148 ## TAB CATEGORY
149 box = layout.box()
150 row = box.row(align=True)
151 row.label(text="Panel Category:")
152 row.prop(self, "category", text="")
154 row = layout.row(align=True)
155 row.prop(self, "pref_tab", expand=True)
157 if self.pref_tab == 'box_deform':
159 ## BOX DEFORM
160 box = layout.box()
161 row = box.row(align=True)
162 row.label(text='Box Deform:')
163 row.operator("wm.call_menu", text="", icon='QUESTION').name = "GPT_MT_box_deform_doc"
164 box.prop(self, "use_clic_drag")
166 box.prop(self, "default_deform_type")
167 box.label(text="Deformer type can be changed during modal with 'M' key, this is for default behavior", icon='INFO')
169 box.prop(self, "auto_swap_deform_type")
170 box.label(text="Once 'M' is hit, auto swap is deactivated to stay in your chosen mode", icon='INFO')
172 if self.pref_tab == 'canvas_rotate':
173 ## ROTATE CANVAS
174 box = layout.box()
175 box.label(text='Rotate Canvas:')
177 box.prop(self, "canvas_use_shortcut", text='Bind Shortcuts')
179 if self.canvas_use_shortcut:
181 row = box.row()
182 row.label(text="(Auto rebind when changing shortcut)")#icon=""
183 # row.operator("prefs.rebind_shortcut", text='Bind/Rebind shortcuts', icon='FILE_REFRESH')#EVENT_SPACEKEY
184 row = box.row(align = True)
185 row.prop(self, "use_ctrl", text='Ctrl')#, expand=True
186 row.prop(self, "use_alt", text='Alt')#, expand=True
187 row.prop(self, "use_shift", text='Shift')#, expand=True
188 row.prop(self, "mouse_click",text='')#expand=True
190 if not self.use_ctrl and not self.use_alt and not self.use_shift:
191 box.label(text="Choose at least one modifier to combine with click (default: Ctrl+Alt)", icon="ERROR")# INFO
193 if not all((self.use_ctrl, self.use_alt, self.use_shift)):
194 row = box.row(align = True)
195 snap_key_list = []
196 if not self.use_ctrl:
197 snap_key_list.append('Ctrl')
198 if not self.use_shift:
199 snap_key_list.append('Shift')
200 if not self.use_alt:
201 snap_key_list.append('Alt')
203 row.label(text=f"Step rotation with: {' or '.join(snap_key_list)}", icon='DRIVER_ROTATIONAL_DIFFERENCE')
204 row.prop(self, "rc_angle_step", text='Angle Steps')
206 else:
207 box.label(text="No hotkey has been set automatically. Following operators needs to be set manually:", icon="ERROR")
208 box.label(text="view3d.rotate_canvas")
209 box.prop(self, 'canvas_use_view_center')
210 box.prop(self, 'canvas_use_hud')
212 if self.pref_tab == 'timeline_scrub':
213 ## SCRUB TIMELINE
214 box = layout.box()
215 draw_ts_pref(prefs.ts, box)
217 if self.pref_tab == 'layer_navigator':
218 ## LAYER NAVIGATOR
219 box = layout.box()
220 draw_nav_pref(prefs.nav, box)
224 class GPT_MT_box_deform_doc(bpy.types.Menu):
225 # bl_idname = "OBJECT_MT_custom_menu"
226 bl_label = "Box Deform Infos Sheet"
228 def draw(self, context):
229 layout = self.layout
230 # call another menu
231 #layout.operator("wm.call_menu", text="Unwrap").name = "VIEW3D_MT_uv_map"
232 #**Behavior from context mode**
233 col = layout.column()
234 col.label(text='Box Deform Tool')
235 col.label(text="Usage:", icon='MOD_LATTICE')
236 col.label(text="Use the shortcut 'Ctrl+T' in available modes (listed below)")
237 col.label(text="The lattice box is generated facing your view (be sure to face canvas if you want to stay on it)")
238 col.label(text="Use shortcuts below to deform (a help will be displayed in the topbar)")
240 col.separator()
241 col.label(text="Shortcuts:", icon='HAND')
242 col.label(text="Spacebar / Enter : Confirm")
243 col.label(text="Shift + Spacebar / Enter : Confirm and let the lattice in place")
244 col.label(text="Delete / Backspace / Tab(twice) / Ctrl+T : Cancel")
245 col.label(text="M : Toggle between Linear and Spline mode at any moment")
246 col.label(text="1-9 top row number : Subdivide the box")
247 col.label(text="Ctrl + arrows-keys : Subdivide the box incrementally in individual X/Y axis")
249 col.separator()
250 col.label(text="Modes and deformation target:", icon='PIVOT_BOUNDBOX')
251 col.label(text="- Object mode : The whole GP object is deformed (including all frames)")
252 col.label(text="- GPencil Edit mode : Deform Selected points")
253 col.label(text="- Gpencil Paint : Deform last Strokes")
254 # col.label(text="- Lattice edit : Revive the modal after a ctrl+Z")
256 col.separator()
257 col.label(text="Notes:", icon='TEXT')
258 col.label(text="- If you return in box deform after applying (with a ctrl+Z), you need to hit 'Ctrl+T' again to revive the modal.")
259 col.label(text="- A cancel warning will be displayed the first time you hit Tab")
261 ### rotate canvas keymap
263 addon_keymaps = []
264 def register_keymaps():
265 pref = get_addon_prefs()
266 if not pref.canvas_use_shortcut:
267 return
268 addon = bpy.context.window_manager.keyconfigs.addon
270 km = addon.keymaps.new(name = "3D View", space_type = "VIEW_3D")
272 if 'view3d.rotate_canvas' not in km.keymap_items:
273 km = addon.keymaps.new(name='3D View', space_type='VIEW_3D')
274 kmi = km.keymap_items.new('view3d.rotate_canvas',
275 type=pref.mouse_click, value="PRESS", alt=pref.use_alt, ctrl=pref.use_ctrl, shift=pref.use_shift, any=False)
277 addon_keymaps.append((km, kmi))
279 def unregister_keymaps():
280 for km, kmi in addon_keymaps:
281 km.keymap_items.remove(kmi)
282 addon_keymaps.clear()
285 ### REGISTER ---
287 classes = (
288 GPTS_timeline_settings,
289 GPNAV_layer_navigator_settings,
290 GPT_MT_box_deform_doc,
291 GreasePencilAddonPrefs,
294 def register():
295 for cls in classes:
296 bpy.utils.register_class(cls)
297 # Force box deform running to false
298 bpy.context.preferences.addons[os.path.splitext(__name__)[0]].preferences.boxdeform_running = False
299 register_keymaps()
301 def unregister():
302 unregister_keymaps()
303 for cls in reversed(classes):
304 bpy.utils.unregister_class(cls)