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
3 changed files with 115 additions and 6 deletions
Showing only changes of commit 5905a174f5 - Show all commits

View File

@ -1,8 +1,8 @@
from . import utils, icons, settings, ui, draw_tool, ops
from . import utils, icons, settings, preferences, ui, draw_tool, ops
import tomllib as toml
import bpy
modules = [utils, icons, settings, ui, draw_tool, ops]
modules = [utils, icons, settings, preferences, ui, draw_tool, ops]
def register():
# register modules

View File

@ -0,0 +1,83 @@
import bpy
from . import utils, settings
import os
class BSBST_OT_copy_resources_to_path(bpy.types.Operator):
"""
Remove the active Style Preset Library.
"""
bl_idname = "brushstroke_tools.copy_resources"
bl_label = "Copy Resources to Directory"
bl_description = "Remove the modifier to the preset"
bl_options = {"REGISTER", "UNDO"}
update: bpy.props.BoolProperty(default=False)
@classmethod
def poll(cls, context):
addon_prefs = context.preferences.addons[__package__].preferences
return os.path.isdir(addon_prefs.resource_path)
def execute(self, context):
utils.copy_resources_to_dir()
return {"FINISHED"}
def invoke(self, context, event):
wm = context.window_manager
return wm.invoke_props_dialog(self)
def draw(self, context):
layout = self.layout
layout.label(text='This will overwrite files in the target directory!', icon='ERROR')
layout.label(text=utils.get_resource_directory())
class BSBST_preferences(bpy.types.AddonPreferences):
bl_idname = __package__
resource_path: bpy.props.StringProperty(name='Resource Directory', subtype='DIR_PATH', )
import_method: bpy.props.EnumProperty(name='Import Method', default='APPEND',
items= [('APPEND', 'Append', 'Append data-blocks and pack image data as local to this file.', 'APPEND_BLEND', 0),\
('LINK', 'Link', 'Link data-blocks from resource directory.', 'LINK_BLEND', 1),
])
def draw(self, context):
layout = self.layout
row = layout.row()
row.label(text='Brushstrokes Library', icon='BRUSHES_ALL')
layout.prop(self, 'import_method')
row = layout.row()
col = row.column()
dir_exists = os.path.isdir(utils.get_resource_directory())
if not dir_exists:
col.alert = True
col.prop(self, 'resource_path', placeholder=utils.get_resource_directory())
resources_available = os.path.isfile(f"{utils.get_resource_directory()}brushstroke_tools-resources.blend")
if dir_exists:
if not resources_available:
col = row.column()
col.alert = True
op = col.operator('brushstroke_tools.copy_resources', icon='IMPORT')
op.update = False
elif self.resource_path:
op = row.operator('brushstroke_tools.copy_resources', icon='IMPORT', text='')
op.update = True
col = layout.column()
col.alert = True
if not resources_available:
col.label(text='The required resources were not found at the specified directory.', icon='ERROR')
if self.import_method == 'LINK' and not self.resource_path:
col.label(text='Linking the resources from the default addon directory is not recommended.', icon='ERROR')
classes = [
BSBST_preferences,
BSBST_OT_copy_resources_to_path,
]
def register():
for c in classes:
bpy.utils.register_class(c)
def unregister():
for c in classes:
bpy.utils.unregister_class(c)

View File

@ -1,7 +1,7 @@
import os
import bpy
from bpy.app.handlers import persistent
import math
import math, shutil, errno
ng_list = [
".brushstroke_tools.processing",
@ -134,20 +134,46 @@ def deep_copy_mod_info(source_object, target_object):
def get_addon_directory() -> str:
"""
Returns the path of the addon directory to be used to append resource data-blocks.
Returns the path of the addon directory.
"""
return bpy.path.abspath(os.path.dirname(os.path.realpath(__file__)))
def get_resource_directory() -> str:
"""
Returns the path to be used to append resource data-blocks.
"""
addon_prefs = bpy.context.preferences.addons[__package__].preferences
resource_path = addon_prefs.resource_path
if not resource_path:
resource_path = f'{get_addon_directory()}/assets/'
return resource_path
def import_resources(ng_names = ng_list):
"""
Imports the necessary blend data resources required by the addon.
"""
path = f"{get_addon_directory()}/assets/"
path = get_resource_directory()
with bpy.data.libraries.load(f'{path}brushstroke_tools-resources.blend', link=False) as (data_src, data_dst):
data_dst.node_groups = ng_names
def copy_resources_to_dir(tgt_dir = ''):
resource_dir = f'{get_addon_directory()}/assets/'
if not tgt_dir:
tgt_dir = get_resource_directory()
try:
shutil.copytree(resource_dir, tgt_dir, dirs_exist_ok=True)
except OSError as err:
# error caused if the source was not a directory
if err.errno == errno.ENOTDIR:
shutil.copy2(resource_dir, tgt_dir)
else:
print("Error: % s" % err)
def ensure_resources():
ng_missing = set()
@ -164,7 +190,7 @@ def register_asset_lib():
return
lib = asset_libs.new()
lib.name = asset_lib_name
lib.path = f'{get_addon_directory()}/assets'
lib.path = get_resource_directory()
lib.use_relative_path = False
def unregister_asset_lib():