update bg images operator
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
from pathlib import Path
|
||||
|
||||
import bpy
|
||||
import re
|
||||
from bpy.types import Operator
|
||||
from mathutils import Vector
|
||||
from os.path import abspath
|
||||
@@ -299,6 +300,91 @@ class BPM_OT_reload(Operator):
|
||||
return{'CANCELLED'}
|
||||
return {"FINISHED"}
|
||||
|
||||
class BPM_OT_update_bg_images(Operator):
|
||||
bl_idname = "bpm.update_bg_images"
|
||||
bl_label = "Update Bg Planes Images"
|
||||
bl_description = "Update the loaded images if there are newer version\
|
||||
\n(source files name needs to end with a version number)"
|
||||
bl_options = {"REGISTER"} # , "UNDO"
|
||||
|
||||
@classmethod
|
||||
def poll(cls, context):
|
||||
return True
|
||||
|
||||
def execute(self, context):
|
||||
print('Updating barckground images:')
|
||||
|
||||
ct = 0
|
||||
right_num = re.compile(r'(\d+)(?!.*\d)')
|
||||
for item in context.scene.bg_props.planes:
|
||||
if item.type != 'bg':
|
||||
continue
|
||||
holder = item.plane
|
||||
if not holder:
|
||||
continue
|
||||
for bg_obj in holder.children:
|
||||
image = core.get_image(bg_obj)
|
||||
# print(f'holder:{holder.name} > obj({bg_obj.type}):{bg_obj.name} > img:{image.name} > {image.filepath}')
|
||||
|
||||
img = Path(bpy.path.abspath(image.filepath))
|
||||
|
||||
if not re.search(r'\d+$', img.stem):
|
||||
print(f'SKIP: object "{bg_obj.name}" > image "{image.name}" does not end with version')
|
||||
img_folder = img.parent
|
||||
|
||||
# List in folder : only file versionned, with same suffix and same basename
|
||||
images_list = [i for i in img_folder.iterdir() if i.is_file()\
|
||||
and re.search(r'\d+$', i.stem)\
|
||||
and i.suffix == img.suffix\
|
||||
and right_num.sub('', img.stem) == right_num.sub('', i.stem)]
|
||||
|
||||
## images_list should neveer be empty (source img should be listed)
|
||||
# for i in images_list:
|
||||
# print(i.stem)
|
||||
|
||||
images_list.sort()
|
||||
last = images_list[-1]
|
||||
if last == img:
|
||||
print(f'Already up to date: {image.name}')
|
||||
continue
|
||||
|
||||
## Update with last number
|
||||
print(f'Update: {img.name} >> {last.name}')
|
||||
|
||||
## keep current fielpath head (to not affect relative/absolute)
|
||||
new_path = Path(image.filepath).parent / last.name
|
||||
print('New path: ', new_path)
|
||||
|
||||
# Update image filepath and name
|
||||
image.filepath = str(new_path)
|
||||
image.name = new_path.name
|
||||
|
||||
### Propagate new names
|
||||
|
||||
## Rename collection
|
||||
parent_col = core.get_parent_collection(holder)
|
||||
parent_col.name = new_path.stem
|
||||
|
||||
# Rename holder keeping prefix
|
||||
holder.name = holder.data.name = f'BG_{new_path.stem}'
|
||||
|
||||
## Rename object keeping type suffix
|
||||
if bg_obj.name.endswith(('_texgp', '_texplane', '_texempty')):
|
||||
type_suffix = f"_tex{bg_obj.name.split('_tex')[-1]}"
|
||||
else:
|
||||
type_suffix = ''
|
||||
|
||||
bg_obj.name = bg_obj.data.name = f'{new_path.stem}{type_suffix}'
|
||||
|
||||
ct += 1
|
||||
|
||||
if ct:
|
||||
self.report({'INFO'}, f'Updated {ct} background images')
|
||||
else:
|
||||
self.report({'INFO'}, 'All background images are up to date')
|
||||
|
||||
return {"FINISHED"}
|
||||
|
||||
classes=(
|
||||
# BPM_OT_change_background_type,
|
||||
BPM_OT_select_swap,
|
||||
@@ -307,7 +393,8 @@ BPM_OT_select_all,
|
||||
BPM_OT_reload,
|
||||
BPM_OT_open_bg_folder,
|
||||
BPM_OT_set_distance,
|
||||
BPM_OT_move_plane
|
||||
BPM_OT_move_plane,
|
||||
BPM_OT_update_bg_images,
|
||||
)
|
||||
|
||||
def register():
|
||||
|
||||
Reference in New Issue
Block a user