Files
multi-user/multi_user/io_bpy/bl_image.py

125 lines
3.3 KiB
Python
Raw Normal View History

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
import bpy
import mathutils
2020-09-21 00:11:37 +02:00
from .. import utils
from .bl_datablock import BlDatablock
2020-09-21 00:11:37 +02:00
from .dump_anything import Dumper, Loader
from .bl_file import get_filepath, ensure_unpacked
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',
}
class BlImage(BlDatablock):
bl_id = "images"
bl_class = bpy.types.Image
2020-09-17 22:47:11 +02:00
bl_check_common = False
bl_icon = 'IMAGE_DATA'
bl_reload_parent = False
def construct(data: dict) -> object:
return bpy.data.images.new(
name=data['name'],
width=data['size'][0],
height=data['size'][1]
)
def load(self, data, target):
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"]
def dump(self, instance=None):
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
dumper.include_filter = [
"name",
'size',
'height',
'alpha',
'float_buffer',
'alpha_mode',
'colorspace_settings']
data.update(dumper.dump(instance))
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()
if self.instance and (self.instance.name != self.data['name']):
return True
else:
return False
2020-09-21 00:11:37 +02:00
@staticmethod
def resolve_deps(datablock: object) -> [object]:
2020-09-21 00:11:37 +02:00
deps = []
if datablock.packed_file:
filename = Path(bpy.path.abspath(datablock.filepath)).name
datablock.filepath_raw = get_filepath(filename)
datablock.save()
# An image can't be unpacked to the modified path
# TODO: make a bug report
datablock.unpack(method="REMOVE")
elif datablock.source == "GENERATED":
filename = f"{datablock.name}.png"
datablock.filepath = get_filepath(filename)
datablock.save()
if datablock.filepath:
deps.append(Path(bpy.path.abspath(datablock.filepath)))
2020-09-21 00:11:37 +02:00
return deps