2020-03-20 14:56:50 +01:00
|
|
|
# ##### BEGIN GPL LICENSE BLOCK #####
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
# ##### END GPL LICENSE BLOCK #####
|
|
|
|
|
|
|
|
|
2020-09-21 00:11:37 +02:00
|
|
|
import logging
|
|
|
|
import os
|
|
|
|
from pathlib import Path
|
|
|
|
|
2019-08-08 23:42:02 +02:00
|
|
|
import bpy
|
|
|
|
import mathutils
|
2020-09-21 00:11:37 +02:00
|
|
|
|
2020-03-02 11:09:45 +01:00
|
|
|
from .. import utils
|
2019-08-26 17:27:12 +02:00
|
|
|
from .bl_datablock import BlDatablock
|
2020-09-21 00:11:37 +02:00
|
|
|
from .dump_anything import Dumper, Loader
|
2020-09-21 12:12:19 +02:00
|
|
|
from .bl_file import get_filepath, ensure_unpacked
|
2019-08-08 23:42:02 +02:00
|
|
|
|
2020-09-18 15:25:52 +02:00
|
|
|
format_to_ext = {
|
|
|
|
'BMP': 'bmp',
|
|
|
|
'IRIS': 'sgi',
|
|
|
|
'PNG': 'png',
|
|
|
|
'JPEG': 'jpg',
|
|
|
|
'JPEG2000': 'jp2',
|
|
|
|
'TARGA': 'tga',
|
|
|
|
'TARGA_RAW': 'tga',
|
|
|
|
'CINEON': 'cin',
|
|
|
|
'DPX': 'dpx',
|
|
|
|
'OPEN_EXR_MULTILAYER': 'exr',
|
|
|
|
'OPEN_EXR': 'exr',
|
|
|
|
'HDR': 'hdr',
|
|
|
|
'TIFF': 'tiff',
|
|
|
|
'AVI_JPEG': 'avi',
|
|
|
|
'AVI_RAW': 'avi',
|
|
|
|
'FFMPEG': 'mpeg',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-08-26 17:27:12 +02:00
|
|
|
class BlImage(BlDatablock):
|
2020-01-13 18:51:31 +01:00
|
|
|
bl_id = "images"
|
|
|
|
bl_class = bpy.types.Image
|
2020-09-17 22:47:11 +02:00
|
|
|
bl_check_common = False
|
2020-01-13 18:51:31 +01:00
|
|
|
bl_icon = 'IMAGE_DATA'
|
2020-12-09 14:49:26 +01:00
|
|
|
bl_reload_parent = False
|
2020-01-13 18:51:31 +01:00
|
|
|
|
2021-03-26 12:30:15 +01:00
|
|
|
def construct(data: dict) -> object:
|
2019-08-13 11:24:22 +02:00
|
|
|
return bpy.data.images.new(
|
2020-09-18 15:25:52 +02:00
|
|
|
name=data['name'],
|
|
|
|
width=data['size'][0],
|
|
|
|
height=data['size'][1]
|
|
|
|
)
|
2019-08-13 11:24:22 +02:00
|
|
|
|
2021-03-24 16:08:12 +01:00
|
|
|
def load(self, data, target):
|
2020-09-21 12:12:19 +02:00
|
|
|
loader = Loader()
|
|
|
|
loader.load(data, target)
|
|
|
|
|
2020-09-21 00:11:37 +02:00
|
|
|
target.source = 'FILE'
|
|
|
|
target.filepath_raw = get_filepath(data['filename'])
|
|
|
|
target.colorspace_settings.name = data["colorspace_settings"]["name"]
|
2019-08-08 23:42:02 +02:00
|
|
|
|
2021-03-24 16:08:12 +01:00
|
|
|
def dump(self, instance=None):
|
2020-04-07 14:49:43 +02:00
|
|
|
assert(instance)
|
2020-09-21 00:11:37 +02:00
|
|
|
|
|
|
|
filename = Path(instance.filepath).name
|
|
|
|
|
|
|
|
data = {
|
|
|
|
"filename": filename
|
|
|
|
}
|
|
|
|
|
2020-04-01 11:34:24 +02:00
|
|
|
dumper = Dumper()
|
2020-02-25 17:06:40 +01:00
|
|
|
dumper.depth = 2
|
2020-09-18 15:25:52 +02:00
|
|
|
dumper.include_filter = [
|
|
|
|
"name",
|
|
|
|
'size',
|
|
|
|
'height',
|
|
|
|
'alpha',
|
|
|
|
'float_buffer',
|
|
|
|
'alpha_mode',
|
|
|
|
'colorspace_settings']
|
2020-04-07 14:49:43 +02:00
|
|
|
data.update(dumper.dump(instance))
|
2019-08-08 23:42:02 +02:00
|
|
|
return data
|
|
|
|
|
2019-08-25 16:19:01 +02:00
|
|
|
def diff(self):
|
2020-12-09 18:35:29 +01:00
|
|
|
if self.instance.is_dirty:
|
|
|
|
self.instance.save()
|
|
|
|
|
2020-09-18 15:25:52 +02:00
|
|
|
if self.instance and (self.instance.name != self.data['name']):
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
2020-09-21 00:11:37 +02:00
|
|
|
|
2021-03-25 14:55:53 +01:00
|
|
|
@staticmethod
|
2021-03-26 12:30:15 +01:00
|
|
|
def resolve_deps(datablock: object) -> [object]:
|
2020-09-21 00:11:37 +02:00
|
|
|
deps = []
|
2020-09-21 12:12:19 +02:00
|
|
|
|
2021-03-25 14:55:53 +01:00
|
|
|
if datablock.packed_file:
|
|
|
|
filename = Path(bpy.path.abspath(datablock.filepath)).name
|
|
|
|
datablock.filepath_raw = get_filepath(filename)
|
|
|
|
datablock.save()
|
2020-12-09 14:49:26 +01:00
|
|
|
# An image can't be unpacked to the modified path
|
|
|
|
# TODO: make a bug report
|
2021-03-25 14:55:53 +01:00
|
|
|
datablock.unpack(method="REMOVE")
|
2020-09-21 12:12:19 +02:00
|
|
|
|
2021-03-25 14:55:53 +01:00
|
|
|
elif datablock.source == "GENERATED":
|
|
|
|
filename = f"{datablock.name}.png"
|
|
|
|
datablock.filepath = get_filepath(filename)
|
|
|
|
datablock.save()
|
2020-09-21 12:12:19 +02:00
|
|
|
|
2021-03-25 14:55:53 +01:00
|
|
|
if datablock.filepath:
|
|
|
|
deps.append(Path(bpy.path.abspath(datablock.filepath)))
|
2020-09-21 00:11:37 +02:00
|
|
|
|
|
|
|
return deps
|