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):
# return context.object and context.object.type == 'GPENCIL'
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 " }
### -- MENU ENTRY --
def load_brush_ui ( self , context ) :
2021-06-23 17:25:45 +02:00
prefs = get_addon_prefs ( )
2021-06-14 17:41:41 +02:00
if context . mode == ' PAINT_GPENCIL ' :
2021-06-23 17:25:45 +02:00
self . layout . operator ( ' gp.load_brushes ' , icon = ' SMALL_TRI_RIGHT_VEC ' ) . filepath = prefs . brush_path # KEYTYPE_JITTER_VEC
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 ( )
2021-06-14 17:41:41 +02:00
if context . mode == ' PAINT_GPENCIL ' :
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 ,
)
def register ( ) :
for cl in classes :
bpy . utils . register_class ( cl )
bpy . types . VIEW3D_MT_brush_gpencil_context_menu . append ( load_brush_ui )
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 )
bpy . types . VIEW3D_MT_brush_gpencil_context_menu . remove ( load_brush_ui )
for cl in reversed ( classes ) :
2021-06-14 18:53:15 +02:00
bpy . utils . unregister_class ( cl )