Skip to content

Commit

Permalink
Rename to save_directory for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhph111 committed Aug 12, 2021
1 parent 9e95b65 commit 429bf19
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
24 changes: 12 additions & 12 deletions imagepaste/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,19 +198,19 @@ def poll(_cls, context):
)


class IMAGEPASTE_OT_move_to_saved_directory(bpy.types.Operator):
class IMAGEPASTE_OT_move_to_save_directory(bpy.types.Operator):
"""Move images to a target directory"""

bl_idname = "imagepaste.move_to_saved_directory"
bl_idname = "imagepaste.move_to_save_directory"
bl_label = "Move to Target Directory"
bl_options = {"UNDO_GROUPED"}

def __init__(self) -> None:
from .helper import get_save_directory

super().__init__()
self.saved_directory = get_save_directory()
self.orphaned_images = self.get_orphaned_images(self.saved_directory)
self.save_directory = get_save_directory()
self.orphaned_images = self.get_orphaned_images(self.save_directory)

def execute(self, _context):
from .image import Image
Expand All @@ -221,7 +221,7 @@ def execute(self, _context):
# Change the paths the images refers to with the new one
for orphaned_image in self.orphaned_images:
old_filepath = self.get_abspath(orphaned_image.filepath)
self.change_image_directory(orphaned_image, self.saved_directory)
self.change_image_directory(orphaned_image, self.save_directory)
new_filepath = self.get_abspath(orphaned_image.filepath)
# Also change in the dictionary
if old_filepath in pasted_images:
Expand Down Expand Up @@ -269,11 +269,11 @@ def get_abspaths(self, images: list[bpy.types.Image]) -> list[str]:
"""
return [self.get_abspath(image.filepath) for image in images]

def get_orphaned_images(self, saved_directory: str) -> list[bpy.types.Image]:
def get_orphaned_images(self, save_directory: str) -> list[bpy.types.Image]:
"""Get images that are not in the target directory.
Args:
saved_directory (str): The target directory.
save_directory (str): The target directory.
Returns:
list[bpy.types.Image]: A list of orphaned images.
Expand All @@ -296,7 +296,7 @@ def get_orphaned_images(self, saved_directory: str) -> list[bpy.types.Image]:
if not image.filepath:
continue
filepath = self.get_abspath(image.filepath)
if dirname(filepath) == saved_directory:
if dirname(filepath) == save_directory:
continue
if preferences.image_type_to_move == "all_images":
orphaned_images.append(image)
Expand All @@ -307,18 +307,18 @@ def get_orphaned_images(self, saved_directory: str) -> list[bpy.types.Image]:
return orphaned_images

def change_image_directory(
self, orphaned_image: bpy.types.Image, saved_directory: str
self, orphaned_image: bpy.types.Image, save_directory: str
) -> None:
"""Change the directory of an orphaned image.
Args:
orphaned_image (bpy.types.Image): An orphaned image.
saved_directory (str): The target directory.
save_directory (str): The target directory.
"""
from os.path import join
from shutil import copyfile

new_filepath = join(saved_directory, bpy.path.basename(orphaned_image.filepath))
new_filepath = join(save_directory, bpy.path.basename(orphaned_image.filepath))
copyfile(bpy.path.abspath(orphaned_image.filepath), new_filepath)
orphaned_image.filepath = new_filepath
orphaned_image.reload()
Expand All @@ -331,7 +331,7 @@ def change_image_directory(
IMAGEPASTE_OT_shadereditor_paste,
IMAGEPASTE_OT_view3d_paste_plane,
IMAGEPASTE_OT_view3d_paste_reference,
IMAGEPASTE_OT_move_to_saved_directory,
IMAGEPASTE_OT_move_to_save_directory,
)


Expand Down
8 changes: 4 additions & 4 deletions imagepaste/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,8 @@ def view3d_paste_reference_imageaddmenu_draw(self, _context):


@bpy.app.handlers.persistent
def move_to_saved_directory_handler(self, _context):
bpy.ops.imagepaste.move_to_saved_directory("INVOKE_DEFAULT")
def move_to_save_directory_handler(self, _context):
bpy.ops.imagepaste.move_to_save_directory("INVOKE_DEFAULT")


addon_keymaps = []
Expand All @@ -250,7 +250,7 @@ def register():
bpy.types.NODE_MT_context_menu.append(shadereditor_paste_contextmenu_draw)
bpy.types.VIEW3D_MT_image_add.append(view3d_paste_plane_imageaddmenu_draw)
bpy.types.VIEW3D_MT_image_add.append(view3d_paste_reference_imageaddmenu_draw)
bpy.app.handlers.save_post.append(move_to_saved_directory_handler)
bpy.app.handlers.save_post.append(move_to_save_directory_handler)

kc = bpy.context.window_manager.keyconfigs.addon

Expand Down Expand Up @@ -322,7 +322,7 @@ def unregister():
km.keymap_items.remove(kmi)
addon_keymaps.clear()

bpy.app.handlers.save_post.remove(move_to_saved_directory_handler)
bpy.app.handlers.save_post.remove(move_to_save_directory_handler)
bpy.types.VIEW3D_MT_image_add.remove(view3d_paste_reference_imageaddmenu_draw)
bpy.types.VIEW3D_MT_image_add.remove(view3d_paste_plane_imageaddmenu_draw)
bpy.types.NODE_MT_context_menu.remove(shadereditor_paste_contextmenu_draw)
Expand Down

0 comments on commit 429bf19

Please sign in to comment.