Skip to content

Commit

Permalink
Enhance add-on preferences with new options
Browse files Browse the repository at this point in the history
- Add new preferences UI with:
  - An option to use another directory for images instead of the
    temporary directory, including another option to force use it.
  - An option to use subdirectory in the `.blend` file path.
  - An option to disable debug message.
- Split multiple utilities code to to `helper.py` module.
- Rewrite the `get_save_directory()` function to adapt with the new
  preferences and fix the "not existent directory" bug.
  • Loading branch information
thanhph111 committed Jul 20, 2021
1 parent 5cbaed5 commit 2a99d8c
Show file tree
Hide file tree
Showing 4 changed files with 152 additions and 26 deletions.
41 changes: 41 additions & 0 deletions imagepaste/helper.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import bpy


ADDON_NAME = __package__.split(".")[0]


def get_addon_preferences() -> bpy.types.AddonPreferences:
"""Get the addon preferences.
Returns:
bpy.types.AddonPreferences: The addon preferences.
"""
import bpy

return bpy.context.preferences.addons[ADDON_NAME].preferences


def get_save_directory() -> str:
"""Get the path to the directory where the images are saved.
Returns:
str: The path to the directory where the images are saved.
"""
from os import makedirs
from os.path import exists
from os.path import dirname
from os.path import join

preferences = get_addon_preferences()

if bpy.data.filepath and not preferences.is_force_use_another_directory:
directory_path = dirname(bpy.data.filepath)
if preferences.is_use_subdirectory and preferences.subdirectory_name:
subdirectory_path = join(directory_path, preferences.subdirectory_name)
if not exists(subdirectory_path):
makedirs(subdirectory_path)
return subdirectory_path
return directory_path
if preferences.is_use_another_directory and preferences.another_directory:
return preferences.another_directory
return bpy.app.tempdir
18 changes: 8 additions & 10 deletions imagepaste/operators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import sys

# Platform-specific import
if sys.platform == "win32":
from .clipboard.windows.windows import WindowsClipboard as Clipboard
elif sys.platform == "linux":
Expand All @@ -14,15 +13,6 @@
import bpy


def get_save_directory() -> str:
"""Get the path to the directory where the images are saved.
Returns:
str: The path to the directory where the images are saved.
"""
return bpy.data.filepath or bpy.app.tempdir


class IMAGEPASTE_OT_imageeditor_copy(bpy.types.Operator):
"""Copy image to the clipboard"""

Expand All @@ -32,6 +22,7 @@ class IMAGEPASTE_OT_imageeditor_copy(bpy.types.Operator):

def execute(self, context):
from os.path import join
from .helper import get_save_directory

active_image = context.area.spaces.active.image
# If active image is render result, save it first
Expand Down Expand Up @@ -65,6 +56,8 @@ class IMAGEPASTE_OT_imageeditor_paste(bpy.types.Operator):
bl_options = {"UNDO_GROUPED"}

def execute(self, context):
from .helper import get_save_directory

clipboard = Clipboard.push(get_save_directory())
clipboard.report.log()
self.report({clipboard.report.type}, clipboard.report.message)
Expand All @@ -89,6 +82,8 @@ class IMAGEPASTE_OT_shadereditor_paste(bpy.types.Operator):
bl_options = {"UNDO_GROUPED"}

def execute(self, context):
from .helper import get_save_directory

clipboard = Clipboard.push(get_save_directory())
clipboard.report.log()
self.report({clipboard.report.type}, clipboard.report.message)
Expand Down Expand Up @@ -123,6 +118,7 @@ class IMAGEPASTE_OT_view3d_paste_plane(bpy.types.Operator):

def execute(self, _context):
from addon_utils import enable
from .helper import get_save_directory

# Enable the "Import Images as Planes" add-on to be used here
enable("io_import_images_as_planes")
Expand Down Expand Up @@ -153,6 +149,8 @@ class IMAGEPASTE_OT_view3d_paste_reference(bpy.types.Operator):
bl_options = {"UNDO_GROUPED"}

def execute(self, _context):
from .helper import get_save_directory

clipboard = Clipboard.push(get_save_directory())
clipboard.report.log()
self.report({clipboard.report.type}, clipboard.report.message)
Expand Down
113 changes: 98 additions & 15 deletions imagepaste/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from bpy.props import StringProperty
from bpy.types import AddonPreferences

from .helper import ADDON_NAME
from .operators import (
IMAGEPASTE_OT_imageeditor_copy,
IMAGEPASTE_OT_imageeditor_paste,
Expand All @@ -13,29 +14,111 @@


class IMAGEPASTE_AddonPreferences(AddonPreferences):
bl_idname = __package__.split(".")[0]
default_img_dir: StringProperty(
name="Default directory",
"""Add-on preferences for ImagePaste"""

bl_idname = ADDON_NAME
is_use_another_directory: BoolProperty(
name="Use a directory",
description=(
"Save images to another directory instead of temporary directory"
" when the file is not saved"
),
default=False,
)
another_directory: StringProperty(
name="Saving directory",
description="A path to directory where images saved to",
subtype="DIR_PATH",
default=bpy.context.preferences.filepaths.temporary_directory,
)
force_default_dir: BoolProperty(
name="Always use default directory",
is_force_use_another_directory: BoolProperty(
name="Force use another directory",
description="Save images to above directory even when the file is saved or not",
default=False,
)
is_use_subdirectory: BoolProperty(
name="Use subdirectory",
description="Save images to a subdirectory where the file is saved",
default=True,
)
subdirectory_name: StringProperty(
name="Sub directory name",
description="A name for subdirectory",
default="ImagePaste",
)
is_disable_debug: BoolProperty(
name="Disable debug message",
description="Debug message will not printed in console",
default=False,
)

def draw(self, _context):
split_ratio = 0.3
layout = self.layout
layout.label(text="Default directory for saving image files.")
layout.label(
text=(
"This directory will be used if"
"the blend file is not saved, "
"or always use default directory if it is checked."
)

# New box
box = layout.box().column()
box.label(
text="Specify a different directory for images when the file is not saved"
)

# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Use a directory")
# Second column
column_2 = split.column().row(align=True)
column_2.prop(self, "is_use_another_directory", text="")
column_2_sub = column_2.column()
column_2_sub.active = self.is_use_another_directory
column_2_sub.prop(self, "another_directory", text="")

# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
split.column()
# Second column
column_2 = split.column().row(align=True)
column_2.active = self.is_use_another_directory
column_2.prop(self, "is_force_use_another_directory", text="Force use")

# New box
box = layout.box().column()
box.label(text="Specify a subdirectory for images when the file is saved")

# New property
prop = box.row(align=True)
prop.active = not (
self.is_force_use_another_directory and self.is_use_another_directory
)
layout.prop(self, "default_img_dir")
layout.prop(self, "force_default_dir")
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Use subdirectory")
# Second column
column_2 = split.column().row(align=True)
column_2.prop(self, "is_use_subdirectory", text="")
column_2_sub = column_2.column()
column_2_sub.prop(self, "subdirectory_name", text="")

# New box
box = layout.box().column()
box.label(text="Miscellaneous")

# New property
prop = box.row(align=True)
split = prop.split(factor=split_ratio)
# First column
column_1 = split.column()
column_1.alignment = "RIGHT"
column_1.label(text="Disable debug message")
# Second column
column_2 = split.column()
column_2.prop(self, "is_disable_debug", text="")


def imageeditor_copy_imagemenu_draw(self, _context):
Expand Down
6 changes: 5 additions & 1 deletion imagepaste/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,11 @@ def __init__(self, code: int, verbose: str = None) -> None:

def log(self) -> None:
"""Log the report to the console."""
print(f"ImagePaste: {self.verbose}")
from .helper import get_addon_preferences

preferences = get_addon_preferences()
if not preferences.is_disable_debug:
print(f"ImagePaste: {self.verbose}")

def __repr__(self) -> str:
"""Return the string representation of the report
Expand Down

0 comments on commit 2a99d8c

Please sign in to comment.