feat: various images format support

feat: world environment image support
This commit is contained in:
Swann 2020-09-18 15:25:52 +02:00
parent fe6ffd19b4
commit ca5aebfeff
No known key found for this signature in database
GPG Key ID: E1D3641A7C43AACB
6 changed files with 78 additions and 41 deletions

View File

@ -24,20 +24,39 @@ from .. import utils
from .dump_anything import Loader, Dumper
from .bl_datablock import BlDatablock
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',
}
def dump_image(image):
pixels = None
if image.source == "GENERATED" or image.packed_file is not None:
prefs = utils.get_preferences()
img_name = f"{image.name}.png"
img_name = f"{image.uuid}.{format_to_ext[image.file_format]}"
# Cache the image on the disk
image.filepath_raw = os.path.join(prefs.cache_directory, img_name)
os.makedirs(prefs.cache_directory, exist_ok=True)
image.file_format = "PNG"
image.save()
if image.source == "FILE":
image_path = bpy.path.abspath(image.filepath_raw)
image_path = bpy.path.abspath(image.filepath_raw)
image_directory = os.path.dirname(image_path)
os.makedirs(image_directory, exist_ok=True)
image.save()
@ -48,29 +67,30 @@ def dump_image(image):
raise ValueError()
return pixels
class BlImage(BlDatablock):
bl_id = "images"
bl_class = bpy.types.Image
bl_delay_refresh = 0
bl_delay_refresh = 1
bl_delay_apply = 1
bl_automatic_push = False
bl_automatic_push = True
bl_check_common = False
bl_icon = 'IMAGE_DATA'
def _construct(self, data):
return bpy.data.images.new(
name=data['name'],
width=data['size'][0],
height=data['size'][1]
)
name=data['name'],
width=data['size'][0],
height=data['size'][1]
)
def _load(self, data, target):
image = target
prefs = utils.get_preferences()
img_format = data['file_format']
img_name = f"{self.uuid}.{format_to_ext[img_format]}"
img_name = f"{image.name}.png"
img_path = os.path.join(prefs.cache_directory,img_name)
img_path = os.path.join(prefs.cache_directory, img_name)
os.makedirs(prefs.cache_directory, exist_ok=True)
file = open(img_path, 'wb')
file.write(data["pixels"])
@ -79,7 +99,11 @@ class BlImage(BlDatablock):
image.source = 'FILE'
image.filepath = img_path
image.colorspace_settings.name = data["colorspace_settings"]["name"]
# Unload image from memory
del data['pixels']
loader = Loader()
loader.load(data, target)
def _dump(self, instance=None):
assert(instance)
@ -87,20 +111,23 @@ class BlImage(BlDatablock):
data['pixels'] = dump_image(instance)
dumper = Dumper()
dumper.depth = 2
dumper.include_filter = [
"name",
'size',
'height',
'alpha',
'float_buffer',
'filepath',
'source',
'colorspace_settings']
dumper.include_filter = [
"name",
'size',
'height',
'alpha',
'float_buffer',
'file_format',
'alpha_mode',
'filepath',
'source',
'colorspace_settings']
data.update(dumper.dump(instance))
return data
def diff(self):
return False
if self.instance and (self.instance.name != self.data['name']):
return True
else:
return False

View File

@ -21,7 +21,7 @@ import mathutils
import logging
import re
from .. import utils
from ..utils import get_datablock_from_uuid
from .dump_anything import Loader, Dumper
from .bl_datablock import BlDatablock
@ -39,6 +39,10 @@ def load_node(node_data, node_tree):
target_node = node_tree.nodes.new(type=node_data["bl_idname"])
loader.load(target_node, node_data)
image_uuid = node_data.get('image_uuid', None)
if image_uuid and not target_node.image:
target_node.image = get_datablock_from_uuid(image_uuid,None)
for input in node_data["inputs"]:
if hasattr(target_node.inputs[input], "default_value"):
@ -118,7 +122,8 @@ def dump_node(node):
"show_preview",
"show_texture",
"outputs",
"width_hidden"
"width_hidden",
"image"
]
dumped_node = node_dumper.dump(node)
@ -153,7 +158,8 @@ def dump_node(node):
'location'
]
dumped_node['mapping'] = curve_dumper.dump(node.mapping)
if hasattr(node, 'image') and getattr(node, 'image'):
dumped_node['image_uuid'] = node.image.uuid
return dumped_node
@ -261,7 +267,7 @@ class BlMaterial(BlDatablock):
if self.instance.use_nodes:
for node in self.instance.node_tree.nodes:
if node.type == 'TEX_IMAGE':
if node.type in ['TEX_IMAGE','TEX_ENVIRONMENT']:
deps.append(node.image)
if self.is_library:
deps.append(self.instance.library)

View File

@ -108,7 +108,8 @@ class BlObject(BlDatablock):
object_data = get_datablock_from_uuid(
data_uuid,
find_data_from_name(data_id)) #TODO: use resolve_from_id
find_data_from_name(data_id),
ignore=['images']) #TODO: use resolve_from_id
instance = bpy.data.objects.new(object_name, object_data)
instance.uuid = self.uuid
@ -122,8 +123,8 @@ class BlObject(BlDatablock):
if target.type != data['type']:
raise ReparentException()
elif target.data.name != data_id:
target.data = get_datablock_from_uuid(data_uuid, find_data_from_name(data_id))
elif target.data and (target.data.name != data_id):
target.data = get_datablock_from_uuid(data_uuid, find_data_from_name(data_id), ignore=['images'])
# vertex groups
if 'vertex_groups' in data:
@ -190,10 +191,9 @@ class BlObject(BlDatablock):
# TODO: find another way...
if target.type == 'EMPTY':
img_key = data.get('data')
if target.data is None and img_key:
target.data = bpy.data.images.get(img_key, None)
img_uuid = data.get('data_uuid')
if target.data is None and img_uuid:
target.data = get_datablock_from_uuid(img_uuid, None)#bpy.data.images.get(img_key, None)
def _dump_implementation(self, data, instance=None):
assert(instance)
@ -235,7 +235,7 @@ class BlObject(BlDatablock):
]
data = dumper.dump(instance)
data['data_uuid'] = instance.data.uuid
data['data_uuid'] = getattr(instance.data, 'uuid', None)
if self.is_library:
return data

View File

@ -85,7 +85,7 @@ class BlWorld(BlDatablock):
if self.instance.use_nodes:
for node in self.instance.node_tree.nodes:
if node.type == 'TEX_IMAGE':
if node.type in ['TEX_IMAGE','TEX_ENVIRONMENT']:
deps.append(node.image)
if self.is_library:
deps.append(self.instance.library)

View File

@ -61,7 +61,8 @@ def refresh_sidebar_view():
"""
area, region, rv3d = view3d_find()
area.regions[3].tag_redraw()
if area:
area.regions[3].tag_redraw()
def get_target(region, rv3d, coord):
target = [0, 0, 0]

View File

@ -78,10 +78,13 @@ def resolve_from_id(id, optionnal_type=None):
return root[id]
return None
def get_datablock_from_uuid(uuid, default):
def get_datablock_from_uuid(uuid, default, ignore=[]):
if not uuid:
return default
for category in dir(bpy.data):
root = getattr(bpy.data, category)
if isinstance(root, Iterable):
if isinstance(root, Iterable) and category not in ignore:
for item in root:
if getattr(item, 'uuid', None) == uuid:
return item