Brushstroke Tools: Initial Version #328

Merged
Simon Thommes merged 229 commits from SimonThommes/blender-studio-tools:brushstroke_tools-initial-version into main 2024-11-06 15:03:47 +01:00
6 changed files with 66 additions and 13 deletions
Showing only changes of commit d92c67de53 - Show all commits

View File

@ -18,7 +18,7 @@ def register():
utils.register_asset_lib() utils.register_asset_lib()
# Get available brush styles # Get available brush styles
#utils.refresh_brushstroke_styles(bpy.context) #utils.refresh_brushstroke_styles()
def unregister(): def unregister():
# un-register modules # un-register modules

View File

@ -1,7 +1,12 @@
import bpy import bpy
from . import utils, settings from bpy.app.handlers import persistent
from . import utils
import os import os
@persistent
def load_handler(dummy):
utils.refresh_brushstroke_styles()
def update_resource_path(self, context): def update_resource_path(self, context):
utils.update_asset_lib_path() utils.update_asset_lib_path()
@ -14,7 +19,7 @@ class BSBST_OT_refresh_brushstroke_styles(bpy.types.Operator):
bl_options = {"REGISTER", "UNDO"} bl_options = {"REGISTER", "UNDO"}
def execute(self, context): def execute(self, context):
utils.refresh_brushstroke_styles(context) utils.refresh_brushstroke_styles()
return {"FINISHED"} return {"FINISHED"}
class BSBST_OT_copy_resources_to_path(bpy.types.Operator): class BSBST_OT_copy_resources_to_path(bpy.types.Operator):
""" """
@ -47,7 +52,7 @@ class BSBST_OT_copy_resources_to_path(bpy.types.Operator):
class BSBST_brush_style(bpy.types.PropertyGroup): class BSBST_brush_style(bpy.types.PropertyGroup):
name: bpy.props.StringProperty(default='') name: bpy.props.StringProperty(default='')
file_path: bpy.props.StringProperty(default='') filepath: bpy.props.StringProperty(default='')
class BSBST_preferences(bpy.types.AddonPreferences): class BSBST_preferences(bpy.types.AddonPreferences):
bl_idname = __package__ bl_idname = __package__
@ -116,7 +121,9 @@ classes = [
def register(): def register():
for c in classes: for c in classes:
bpy.utils.register_class(c) bpy.utils.register_class(c)
bpy.app.handlers.load_post.append(load_handler)
def unregister(): def unregister():
for c in classes: for c in classes:
bpy.utils.unregister_class(c) bpy.utils.unregister_class(c)
bpy.app.handlers.load_post.remove(load_handler)

View File

@ -124,10 +124,13 @@ def set_active_context_brushstrokes_index(self, value):
def get_brush_style(self): def get_brush_style(self):
name = self.node_tree.nodes['Brush Style'].node_tree.name name = self.node_tree.nodes['Brush Style'].node_tree.name
return name return '.'.join(name.split('.')[1:])
def set_brush_style(self, value): def set_brush_style(self, value):
ng = bpy.data.node_groups.get(f'{value}') addon_prefs = bpy.context.preferences.addons[__package__].preferences
ng_name = f'BSBST-brushstroke.{value}'
ng = utils.ensure_node_group(ng_name, [bs for bs in addon_prefs.brush_styles if bs.name==value][0].filepath)
self.node_tree.nodes['Brush Style'].node_tree = ng self.node_tree.nodes['Brush Style'].node_tree = ng
self["brush_style"] = value self["brush_style"] = value

View File

@ -260,6 +260,26 @@ def compare_versions(v1: tuple, v2: tuple):
c += 1 c += 1
return 0 return 0
def ensure_node_group(name, path):
ng = bpy.data.node_groups.get(name)
if ng:
return ng
addon_prefs = bpy.context.preferences.addons[__package__].preferences
with bpy.data.libraries.load(path, link=addon_prefs.import_method=='LINK', relative=addon_prefs.import_relative_path) as (data_src, data_dst):
data_dst.node_groups = [name]
if addon_prefs.import_method=='APPEND':
# pack imported resources
for img in bpy.data.images:
if not img.library_weak_reference:
continue
if path in img.library_weak_reference.filepath:
img.pack()
ng = bpy.data.node_groups.get(name)
return ng
def ensure_resources(): def ensure_resources():
ng_missing = set() ng_missing = set()
@ -294,12 +314,35 @@ def update_asset_lib_path():
lib = asset_libs[asset_lib_name] lib = asset_libs[asset_lib_name]
lib.path = get_resource_directory() lib.path = get_resource_directory()
def refresh_brushstroke_styles(context): def refresh_brushstroke_styles():
addon_prefs = context.preferences.addons[__package__].preferences addon_prefs = bpy.context.preferences.addons[__package__].preferences
for ng in bpy.data.node_groups:
if 'BSBST-brushstroke' in ng.keys(): for a in range(len(addon_prefs.brush_styles)):
addon_prefs.brush_styles.remove(0)
lib_path = get_resource_directory()
add_brush_styles_from_directory(lib_path)
def add_brush_styles_from_directory(path):
addon_prefs = bpy.context.preferences.addons[__package__].preferences
subdirs = [f.path for f in os.scandir(path) if f.is_dir()]
files = [f.path for f in os.scandir(path) if not f.is_dir()]
for filepath in files:
if not filepath.endswith('.blend'):
continue
names = []
with bpy.data.libraries.load(filepath) as (data_from, data_to):
names = [name for name in data_from.node_groups if name.startswith('BSBST-brushstroke')]
for ng_name in names:
b_style = addon_prefs.brush_styles.add() b_style = addon_prefs.brush_styles.add()
b_style.name = ng.name b_style.name = '.'.join(ng_name.split('.')[1:])
b_style.filepath = filepath
for dir in subdirs:
add_brush_styles_from_directory(dir)
def transfer_modifier(modifier_name, target_obj, source_obj): def transfer_modifier(modifier_name, target_obj, source_obj):
""" """