-
Notifications
You must be signed in to change notification settings - Fork 3
/
keymaps.py
379 lines (313 loc) · 14.9 KB
/
keymaps.py
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
import bpy
from bpy.props import (StringProperty,
IntProperty,
BoolProperty,
EnumProperty,)
from bpy.types import Context, OperatorProperties
from pathlib import Path
from . import fn
def get_blender_icons_as_enum():
# return ((i.identifier, i.name, '', i.value) for i in bpy.types.UILayout.bl_rna.functions['prop'].parameters['icon'].enum_items)
return tuple((i.identifier, i.name, '') for i in bpy.types.UILayout.bl_rna.functions['prop'].parameters['icon'].enum_items)
class STORYTOOLS_OT_set_draw_tool(bpy.types.Operator):
bl_idname = "storytools.set_draw_tool"
bl_label = "Set Draw Tool"
bl_description = "Tool preset\
\nChange tool / brush / layer / material to use"
bl_options = {"REGISTER", "UNDO", "INTERNAL"}
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GREASEPENCIL'
name : StringProperty(
name="Name", description="Name that define this preset (Optional).\
\nJust for personal organisation",
default="", options={'SKIP_SAVE'})
mode : EnumProperty(
name="Mode", description="Using shortcut will change to this mode",
default='PAINT_GREASE_PENCIL', options={'HIDDEN', 'SKIP_SAVE'},
items=(
('PAINT_GREASE_PENCIL', 'Draw', 'Switch to draw mode', 0),
('EDIT_GREASE_PENCIL', 'Edit', 'Switch to edit mode', 1),
('SCULPT_GREASE_PENCIL', 'Sculpt', 'Switch to Sculpt mode', 2),
('OBJECT', 'Object', 'Switch to Object mode', 3),
('NONE', 'Current', 'No change', 4),
))
tool : StringProperty(
name="Tool", description="Tool to set\
\nEmpty field = no change",
default="", # builtin.brush
options={'SKIP_SAVE'})
brush : StringProperty(
name="Brush", description="Brush to set\
\nEmpty field = no change",
default="",
options={'SKIP_SAVE'})
layer : StringProperty(
name="Layer", description="Layer to set (exact name, case sensitive)\
\nEmpty field = no change",
default="", # Sketch
options={'SKIP_SAVE'})
material : StringProperty(
name="Material", description="Material to set (exact name, case sensitive)\
\nEmpty field = No change or use layer-material synchronisation if enabled",
default="", # line
options={'SKIP_SAVE'})
## Interface options
show : BoolProperty(
name='Show in UI', description='Show in brushbar',
default=True,
)
icon : EnumProperty(
name="Icon", description="Icon to display in interface",
default='GREASEPENCIL',
items=get_blender_icons_as_enum()
)
description : StringProperty(default='', options={'SKIP_SAVE'})
## Shortcut text : Internal use only for description
shortcut : StringProperty(default='',
description='For internal use only',
options={'SKIP_SAVE', 'HIDDEN'})
order : IntProperty(default=0)
@classmethod
def description(cls, context, properties) -> str:
## User mande description is passed
if properties.description:
desc = properties.description
if properties.name:
desc = properties.name + '\n' + desc
if properties.shortcut:
desc = desc + '\n' + properties.shortcut
return desc
## Auto build description from properties
desc = []
if properties.name:
desc.append(properties.name)
else:
desc.append("Tool Preset")
desc.append("")
## List mode ?
# if properties.mode != 'NONE':
# desc.append(f"Mode: {properties.mode.title().split('_')[0]}")
tools = []
if properties.tool:
if 'builtin_brush.' in properties.tool:
tools.append(f"Tool: {properties.tool.replace('builtin_brush.', '')}")
else:
tools.append(f'Tool: {properties.tool}')
if properties.brush:
tools.append(f'Brush: {properties.brush}')
if tools:
desc.append(' > '.join(tools))
gp_properties = []
if properties.layer:
gp_properties.append(f'Layer: {properties.layer}')
if properties.material:
gp_properties.append(f'Material: {properties.material}')
if gp_properties:
desc.append(', '.join(gp_properties))
if properties.shortcut:
desc.append('')
desc.append(f'Shortcut: {properties.shortcut}')
return '\n'.join(desc)
@staticmethod
def get_brush_from_blend(brush_name, blend_file):
'''Load brush from blend file by name, return brush object if found'''
with bpy.data.libraries.load(str(blend_file), assets_only=True, link=False) as (data_from, data_to):
if brush_name in data_from.brushes:
print(f'Load brush {brush_name} from {blend_file}')
data_to.brushes = [brush_name]
if data_to.brushes:
return data_to.brushes[0]
def execute(self, context):
## Mode needs to add shortcut to generic Gpencil (would conflict with Selection mask)
# if self.mode != 'NONE' and context.mode != self.mode:
# bpy.ops.object.mode_set(mode=self.mode)
prop_names = ('tool', 'brush', 'layer', 'material')
# if not self.tool and not self.brush and not self.layer and not self.material:
if not any(getattr(self, prop_name) for prop_name in prop_names) and self.mode == 'NONE':
message = [
'This Storytools keymap has no settings yet',
'Customize or disable the keymap in addon preferences',
'Click on the following button to open:',
['storytools.open_addon_prefs', 'Open Storytools Preferences', 'PREFERENCES'],
]
fn.show_message_box(message, 'Keymap not set', 'ERROR')
return {"CANCELLED"}
ob = context.object
# Mode change (Need context aware shortcut protection to work well with other modes)...
if self.mode != 'NONE':
bpy.ops.object.mode_set(mode=self.mode)
if self.tool:
try:
bpy.ops.wm.tool_set_by_id(name=self.tool)
# self.report({'INFO'}, f'Tool {self.tool}')
except:
self.report({'ERROR'}, f'Cannot set tool {self.tool}, need identifier (ex: "builtin.brush")')
return {"CANCELLED"}
if self.brush:
## self.brush is brush name as str here
br = bpy.data.brushes.get(self.brush)
if not br:
## Try to load brush
## First search in brush from essential lib (Problem ! when used this way it does not)
essential_brush_lib = Path(bpy.utils.resource_path('LOCAL')) / 'datafiles' / 'assets' / 'brushes' / 'essentials_brushes-gp_draw.blend'
## /!\ Loading directly duplicate the brush if then used from asset shelf ! (Using operator)
# br = self.get_brush_from_blend(self.brush, essential_brush_lib)
# if br:
# print('Essential brush found')
## Load using "asset_activate" operator
with bpy.data.libraries.load(str(essential_brush_lib), assets_only=True, link=False) as (data_from, data_to):
if self.brush in data_from.brushes:
## Just True to know tha brush exists in essential pack, activate and skip next search
br = True
if br:
bpy.ops.brush.asset_activate(asset_library_type='ESSENTIALS',
asset_library_identifier="",
relative_asset_identifier=f"brushes/essentials_brushes-gp_draw.blend/Brush/{self.brush}")
if not br:
## Search in user libs
asset_libs = bpy.context.preferences.filepaths.asset_libraries
for asset_library in asset_libs:
library_path = Path(asset_library.path)
blend_files = [fp for fp in library_path.glob("**/*.blend") if fp.is_file()]
print('blend_files: ', blend_files)
for blend_file in blend_files:
br = self.get_brush_from_blend(self.brush, blend_file)
if br:
print('Found brush in ', blend_file)
break
if br:
break
if not isinstance(br, bool):
if br:
context.scene.tool_settings.gpencil_paint.brush = br
else:
self.report({'WARNING'}, f'Could not find brush named {self.brush}')
# return {"CANCELLED"}
## Using name, can directly try to load brush from essential library ?
# bpy.data.libraries['es`sentials_brushes-gp_draw.blend'].users_id[0]
if self.layer:
fn.set_layer_by_name(ob, self.layer)
if self.material:
# FIXME: Sync happen after material set... how to prevent
fn.set_material_by_name(ob, self.material)
return {"FINISHED"}
'''
## Hardcoded shortcuts, Keymap only set preset number (behavior defined in ops)
class STORYTOOLS_OT_set_draw_tool(bpy.types.Operator):
bl_idname = "storytools.set_draw_tool"
bl_label = "Set Draw Tool"
bl_description = "Set a draw tool, configuring brush / layer / material tool"
bl_options = {"REGISTER", "INTERNAL"}
# path_to_pal : bpy.props.StringProperty(name="paht to palette", description="path to the palette", default="")
@classmethod
def poll(cls, context):
return context.object and context.object.type == 'GREASEPENCIL'
preset : bpy.props.IntProperty(name='Preset number', default=1, options={'SKIP_SAVE'})
def execute(self, context):
# if context.mode != 'OBJECT':
# bpy.ops.object.mode_set(mode='PAINT_GREASE_PENCIL')
presets = {
1: {'tool': 'builtin.brush', 'layer':'Sketch', 'mat': 'line'},
2: {'tool': 'builtin_brush.Fill', 'layer':'Color', 'mat': 'fill_white'},
3: {'tool': 'builtin.brush', 'layer':'Color', 'mat': 'fill_white'},
4: {'tool': 'builtin_brush.Erase'},
}
preset_d = presets[self.preset]
ob = context.object
if 'tool' in preset_d.keys() and preset_d['tool']:
bpy.ops.wm.tool_set_by_id(name=preset_d['tool'])
if 'layer' in preset_d.keys() and preset_d['layer']:
fn.set_layer_by_name(ob, preset_d['layer'])
## Material not forced for now, Already changed by layer synchro
# if 'mat' in preset_d.keys() and preset_d['mat']:
# fn.set_material_by_name(ob, preset_d['mat'])
return {"FINISHED"}
'''
addon_keymaps = []
def register_keymap():
addon = bpy.context.window_manager.keyconfigs.addon
km = addon.keymaps.new(name = "Grease Pencil Paint Mode", space_type = "EMPTY")
'''
## Hardcoded
numbers = ['ZERO', 'ONE', 'TWO', 'THREE', 'FOUR']
for i, number in enumerate(numbers):
if i == 0:
continue
kmi = km.keymap_items.new('storytools.set_draw_tool', type=number, value='PRESS')
kmi.properties.preset=i
addon_keymaps.append((km, kmi))
'''
kmi = km.keymap_items.new('storytools.set_draw_tool', type='ONE', value='PRESS')
kmi.properties.name = 'Sketch Draw'
kmi.properties.mode = 'PAINT_GREASE_PENCIL'
kmi.properties.tool = 'builtin.brush'
kmi.properties.brush = 'Pencil'
kmi.properties.layer = 'Sketch'
# kmi.properties.material = '' # line
# kmi.properties.icon = 'GPBRUSH_PEN'
kmi.properties.icon = 'GREASEPENCIL'
# kmi.properties.description = 'Set Pencil brush on "Sketch" layer'
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('storytools.set_draw_tool', type='TWO', value='PRESS')
kmi.properties.name = 'Line Draw'
kmi.properties.mode = 'PAINT_GREASE_PENCIL'
kmi.properties.tool = 'builtin.brush'
kmi.properties.brush = 'Ink Pen'
kmi.properties.layer = 'Line'
kmi.properties.icon = 'LINE_DATA'
# kmi.properties.description = 'Set Ink Brush on "Line" layer'
# kmi.properties.material = '' # line
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('storytools.set_draw_tool', type='THREE', value='PRESS')
kmi.properties.name = 'Bucket Fill'
kmi.properties.mode = 'PAINT_GREASE_PENCIL'
kmi.properties.tool = 'builtin_brush.Fill'
kmi.properties.layer = 'Color'
# kmi.properties.material = '' # fill_white
kmi.properties.icon = 'SHADING_SOLID'
# kmi.properties.description = 'Set Fill tool on "Color" layer'
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('storytools.set_draw_tool', type='FOUR', value='PRESS')
kmi.properties.name = 'Fill Draw'
kmi.properties.mode = 'PAINT_GREASE_PENCIL'
kmi.properties.tool = 'builtin.brush'
kmi.properties.layer = 'Color'
kmi.properties.icon = 'NODE_MATERIAL'
# kmi.properties.description = 'Set draw tool "Color" layer'
# kmi.properties.material = '' # fill_white
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('storytools.set_draw_tool', type='FIVE', value='PRESS')
kmi.properties.name = 'Eraser by points'
kmi.properties.mode = 'PAINT_GREASE_PENCIL'
kmi.properties.tool = 'builtin_brush.Erase'
kmi.properties.brush = 'Eraser Point'
kmi.properties.icon = 'CLIPUV_DEHLT'
# kmi.properties.description = 'Set Point Eraser'
addon_keymaps.append((km, kmi))
kmi = km.keymap_items.new('storytools.set_draw_tool', type='SIX', value='PRESS')
kmi.properties.name = 'Eraser by strokes'
kmi.properties.mode = 'PAINT_GREASE_PENCIL'
kmi.properties.tool = 'builtin_brush.Erase'
kmi.properties.brush = 'Eraser Stroke'
kmi.properties.icon = 'CON_TRACKTO'
# kmi.properties.description = 'Set Stroke Eraser'
addon_keymaps.append((km, kmi))
# kmi = km.keymap_items.new('storytools.set_draw_tool', type='SEVEN', value='PRESS')
# addon_keymaps.append((km, kmi))
# kmi = km.keymap_items.new('storytools.set_draw_tool', type='SEVEN', value='PRESS')
# kmi.properties.name = 'Notes'
# kmi.properties.tool = 'builtin.brush'
# kmi.properties.layer = 'Line'
# kmi.properties.material = 'line_red' # Sync override material
# addon_keymaps.append((km, kmi))
def unregister_keymap():
for km, kmi in addon_keymaps:
km.keymap_items.remove(kmi)
addon_keymaps.clear()
def register():
bpy.utils.register_class(STORYTOOLS_OT_set_draw_tool)
register_keymap()
def unregister():
unregister_keymap()
bpy.utils.unregister_class(STORYTOOLS_OT_set_draw_tool)