feat: handle packed datablock

feat: filecache settings
This commit is contained in:
Swann
2020-09-21 12:12:19 +02:00
parent af3afc1124
commit f992d06b03
6 changed files with 76 additions and 19 deletions

View File

@ -34,10 +34,20 @@ def get_filepath(filename):
""" """
Construct the local filepath Construct the local filepath
""" """
return os.path.join( return str(Path(
utils.get_preferences().cache_directory, utils.get_preferences().cache_directory,
filename filename
) ))
def ensure_unpacked(datablock):
if datablock.packed_file:
logging.info(f"Unpacking {datablock.name}")
filename = Path(bpy.path.abspath(datablock.filepath)).name
datablock.filepath = get_filepath(filename)
datablock.unpack(method="WRITE_ORIGINAL")
class BlFile(ReplicatedDatablock): class BlFile(ReplicatedDatablock):
@ -53,7 +63,6 @@ class BlFile(ReplicatedDatablock):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.instance = kwargs.get('instance', None) self.instance = kwargs.get('instance', None)
# TODO: handle packed_file
# TODO: ensure absolute path # TODO: ensure absolute path
# TODO: ensure file exist # TODO: ensure file exist
@ -62,12 +71,17 @@ class BlFile(ReplicatedDatablock):
def resolve(self): def resolve(self):
# TODO: generic check # TODO: generic check
# Ensure local cache directory
os.makedirs(self.preferences.cache_directory, exist_ok=True) os.makedirs(self.preferences.cache_directory, exist_ok=True)
if self.data: if self.data:
self.instance = Path(get_filepath(self.data['name'])) self.instance = Path(get_filepath(self.data['name']))
def push(self, socket, identity=None):
super().push(socket, identity=None)
if self.preferences.clear_memory_filecache:
del self.data['file']
def _dump(self, instance=None): def _dump(self, instance=None):
""" """
Read the file and return a dict as: Read the file and return a dict as:
@ -102,16 +116,17 @@ class BlFile(ReplicatedDatablock):
""" """
Writing the file Writing the file
""" """
logging.info(f"Writing {data['name']} to {target}")
# TODO: check for empty data # TODO: check for empty data
if target.exists() and not self.diff(): if target.exists() and not self.diff():
logging.info("File already loaded, skipping.") logging.info(f"{data['name']} already on the disk, skipping.")
return return
try: try:
file = open(target, "wb") file = open(target, "wb")
file.write() file.write(data['file'])
if self.preferences.clear_memory_filecache:
del self.data['file']
except IOError: except IOError:
logging.warning(f"{target} doesn't exist, skipping") logging.warning(f"{target} doesn't exist, skipping")
else: else:

View File

@ -23,7 +23,7 @@ from pathlib import Path
import bpy import bpy
from .bl_datablock import BlDatablock from .bl_datablock import BlDatablock
from .bl_file import get_filepath from .bl_file import get_filepath, ensure_unpacked
from .dump_anything import Dumper, Loader from .dump_anything import Dumper, Loader
@ -59,6 +59,8 @@ class BlFont(BlDatablock):
def _resolve_deps_implementation(self): def _resolve_deps_implementation(self):
deps = [] deps = []
if self.instance.filepath and self.instance.filepath != '<builtin>': if self.instance.filepath and self.instance.filepath != '<builtin>':
ensure_unpacked(self.instance)
deps.append(Path(self.instance.filepath)) deps.append(Path(self.instance.filepath))
return deps return deps

View File

@ -26,7 +26,7 @@ import mathutils
from .. import utils from .. import utils
from .bl_datablock import BlDatablock from .bl_datablock import BlDatablock
from .dump_anything import Dumper, Loader from .dump_anything import Dumper, Loader
from .bl_file import get_filepath from .bl_file import get_filepath, ensure_unpacked
format_to_ext = { format_to_ext = {
'BMP': 'bmp', 'BMP': 'bmp',
@ -65,23 +65,19 @@ class BlImage(BlDatablock):
) )
def _load(self, data, target): def _load(self, data, target):
loader = Loader()
loader.load(data, target)
target.source = 'FILE' target.source = 'FILE'
target.filepath_raw = get_filepath(data['filename']) target.filepath_raw = get_filepath(data['filename'])
target.colorspace_settings.name = data["colorspace_settings"]["name"] target.colorspace_settings.name = data["colorspace_settings"]["name"]
loader = Loader()
loader.load(data, target)
def _dump(self, instance=None): def _dump(self, instance=None):
assert(instance) assert(instance)
filename = Path(instance.filepath).name filename = Path(instance.filepath).name
# Cache the image on the disk
if instance.source == "GENERATED" or instance.packed_file is not None:
instance.filepath = get_filepath(filename)
instance.save()
data = { data = {
"filename": filename "filename": filename
} }
@ -108,6 +104,20 @@ class BlImage(BlDatablock):
def _resolve_deps_implementation(self): def _resolve_deps_implementation(self):
deps = [] deps = []
if self.instance.filepath: if self.instance.filepath:
if self.instance.packed_file:
filename = Path(bpy.path.abspath(self.instance.filepath)).name
self.instance.filepath = get_filepath(filename)
self.instance.save()
# An image can't be unpacked to the modified path
# TODO: make a bug report
self.instance.unpack(method="REMOVE")
elif self.instance.source == "GENERATED":
filename = f"{self.instance.name}.png"
self.instance.filepath = get_filepath(filename)
self.instance.save()
deps.append(Path(self.instance.filepath)) deps.append(Path(self.instance.filepath))
return deps return deps

View File

@ -22,7 +22,7 @@ from pathlib import Path
import bpy import bpy
from .bl_file import get_filepath from .bl_file import get_filepath, ensure_unpacked
from .bl_datablock import BlDatablock from .bl_datablock import BlDatablock
from .dump_anything import Dumper, Loader from .dump_anything import Dumper, Loader
@ -56,6 +56,8 @@ class BlSound(BlDatablock):
def _resolve_deps_implementation(self): def _resolve_deps_implementation(self):
deps = [] deps = []
if self.instance.filepath and self.instance.filepath != '<builtin>': if self.instance.filepath and self.instance.filepath != '<builtin>':
ensure_unpacked(self.instance)
deps.append(Path(self.instance.filepath)) deps.append(Path(self.instance.filepath))
return deps return deps

View File

@ -162,6 +162,11 @@ class SessionPrefs(bpy.types.AddonPreferences):
description="Enable objects update in edit mode (! Impact performances !)", description="Enable objects update in edit mode (! Impact performances !)",
default=False default=False
) )
clear_memory_filecache: bpy.props.BoolProperty(
name="Clear memory filecache",
description="Remove filecache from memory",
default=False
)
# for UI # for UI
category: bpy.props.EnumProperty( category: bpy.props.EnumProperty(
name="Category", name="Category",
@ -230,6 +235,12 @@ class SessionPrefs(bpy.types.AddonPreferences):
description="sidebar_advanced_net_expanded", description="sidebar_advanced_net_expanded",
default=False default=False
) )
sidebar_advanced_cache_expanded: bpy.props.BoolProperty(
name="sidebar_advanced_cache_expanded",
description="sidebar_advanced_cache_expanded",
default=False
)
auto_check_update: bpy.props.BoolProperty( auto_check_update: bpy.props.BoolProperty(
name="Auto-check for Update", name="Auto-check for Update",
description="If enabled, auto-check for updates using an interval", description="If enabled, auto-check for updates using an interval",
@ -343,6 +354,7 @@ class SessionPrefs(bpy.types.AddonPreferences):
emboss=False) emboss=False)
if self.conf_session_cache_expanded: if self.conf_session_cache_expanded:
box.row().prop(self, "cache_directory", text="Cache directory") box.row().prop(self, "cache_directory", text="Cache directory")
box.row().prop(self, "clear_memory_filecache", text="Clear memory filecache")
# INTERFACE SETTINGS # INTERFACE SETTINGS
box = grid.box() box = grid.box()

View File

@ -356,6 +356,22 @@ class SESSION_PT_advanced_settings(bpy.types.Panel):
replication_timers.label(text="Update rate (ms):") replication_timers.label(text="Update rate (ms):")
replication_timers.prop(settings, "depsgraph_update_rate", text="") replication_timers.prop(settings, "depsgraph_update_rate", text="")
cache_section = layout.row().box()
cache_section.prop(
settings,
"sidebar_advanced_cache_expanded",
text="Cache",
icon=get_expanded_icon(settings.sidebar_advanced_cache_expanded),
emboss=False)
if settings.sidebar_advanced_cache_expanded:
cache_section_row = cache_section.row()
cache_section_row.label(text="Cache directory:")
cache_section_row = cache_section.row()
cache_section_row.prop(settings, "cache_directory", text="")
cache_section_row = cache_section.row()
cache_section_row.label(text="Clear memory filecache:")
cache_section_row.prop(settings, "clear_memory_filecache", text="")
log_section = layout.row().box() log_section = layout.row().box()
log_section.prop( log_section.prop(
settings, settings,