2021-06-14 17:41:41 +02:00
import bpy
from bpy_extras . io_utils import ImportHelper
from pathlib import Path
from . utils import get_addon_prefs
def get_brushes ( blend_fp ) :
''' Get all brush from passed blend that aren ' t already in there (can take Path object) '''
cur_brushes = [ b . name for b in bpy . data . brushes ]
with bpy . data . libraries . load ( str ( blend_fp ) , link = False ) as ( data_from , data_to ) :
# load brushes if not already there
data_to . brushes = [ b for b in data_from . brushes if not b in cur_brushes ]
## force fake user for appended the brushes
for b in data_to . brushes :
print ( f ' Append Brush: { b . name } ' )
b . use_fake_user = True
return len ( data_to . brushes )
class GPTB_OT_load_brushes ( bpy . types . Operator , ImportHelper ) :
bl_idname = " gp.load_brushes "
bl_label = " Load Brushes "
2021-06-14 18:53:15 +02:00
bl_description = " Load all brushes from chosen blend file in current if brushes aren ' t already there \n If a replacement is needed, delete the previous brush before "
2021-06-14 17:41:41 +02:00
#bl_options = {"REGISTER", "INTERNAL"}
# @classmethod
# def poll(cls, context):
2024-11-11 15:35:39 +01:00
# return context.object and context.object.type == 'GREASEPENCIL'
2021-06-14 17:41:41 +02:00
filename_ext = ' .blend '
filter_glob : bpy . props . StringProperty ( default = ' *.blend ' , options = { ' HIDDEN ' } )
filepath : bpy . props . StringProperty (
name = " File Path " ,
description = " File path used for import " ,
maxlen = 1024 )
def execute ( self , context ) :
print ( f ' Appending brushes from file : { self . filepath } ' )
bct = get_brushes ( self . filepath )
if bct :
self . report ( { ' INFO ' } , f ' { bct } brushes appended ' )
else :
self . report ( { ' WARNING ' } , ' Brushes are already there (if need to re-import, delete first) ' )
return { " FINISHED " }
2022-11-23 15:25:28 +01:00
class GPTB_OT_brush_set ( bpy . types . Operator ) :
bl_idname = " gp.brush_set "
bl_label = " Set Brush "
bl_description = " Set Gpencil brush "
bl_options = { " REGISTER " , " UNDO " }
brush_name : bpy . props . StringProperty ( name = ' Brush ' , description = ' Name of the brush to use ' )
@classmethod
def poll ( cls , context ) :
2024-11-11 15:47:33 +01:00
return context . object and context . mode == ' PAINT_GREASE_PENCIL '
2022-11-23 15:25:28 +01:00
def execute ( self , context ) :
brush = bpy . data . brushes . get ( self . brush_name )
if not brush :
self . report ( { ' ERROR ' } , f ' Brush " { self . brush_name } " not found ' )
return { " CANCELLED " }
context . scene . tool_settings . gpencil_paint . brush = brush
return { " FINISHED " }
2021-06-14 17:41:41 +02:00
### -- MENU ENTRY --
def load_brush_ui ( self , context ) :
2021-06-23 17:25:45 +02:00
prefs = get_addon_prefs ( )
2024-11-11 15:47:33 +01:00
if context . mode == ' PAINT_GREASE_PENCIL ' :
2022-02-22 11:29:31 +01:00
self . layout . operator ( ' gp.load_brushes ' , icon = ' KEYTYPE_JITTER_VEC ' ) . filepath = prefs . brush_path
2021-06-14 17:41:41 +02:00
def load_brush_top_bar_ui ( self , context ) :
2021-06-23 17:25:45 +02:00
prefs = get_addon_prefs ( )
2024-11-11 15:47:33 +01:00
if context . mode == ' PAINT_GREASE_PENCIL ' :
2021-06-23 17:25:45 +02:00
self . layout . operator ( ' gp.load_brushes ' ) . filepath = prefs . brush_path
2021-06-14 17:41:41 +02:00
classes = (
GPTB_OT_load_brushes ,
2022-11-23 15:25:28 +01:00
GPTB_OT_brush_set ,
2021-06-14 17:41:41 +02:00
)
def register ( ) :
for cl in classes :
bpy . utils . register_class ( cl )
2024-11-11 17:32:58 +01:00
bpy . types . VIEW3D_MT_brush_context_menu . append ( load_brush_ui )
2021-06-14 17:41:41 +02:00
bpy . types . VIEW3D_HT_tool_header . append ( load_brush_top_bar_ui )
def unregister ( ) :
bpy . types . VIEW3D_HT_tool_header . remove ( load_brush_top_bar_ui )
2024-11-11 17:32:58 +01:00
bpy . types . VIEW3D_MT_brush_context_menu . remove ( load_brush_ui )
2021-06-14 17:41:41 +02:00
for cl in reversed ( classes ) :
2021-06-14 18:53:15 +02:00
bpy . utils . unregister_class ( cl )