bl-image-editor-plus/addon/api.py

104 lines
3.1 KiB
Python

'''
Copyright (C) 2021 Akaneyu
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 <http://www.gnu.org/licenses/>.
'''
import bpy
from . import app
from . import utils
class API:
VERSION = (0, 0, 0) # set by register()
@staticmethod
def read_pixels_from_image(img):
return utils.read_pixels_from_image(img)
@staticmethod
def write_pixels_to_image(img, pixels):
utils.write_pixels_to_image(img, pixels)
@staticmethod
def refresh_image(context):
app.refresh_image(context)
@staticmethod
def select_layer(img, layer):
img_props = img.imageeditorplus_properties
layers = img_props.layers
if layer and layer in layers:
img_props.selected_layer_index = layers.index(layer)
else:
img_props.selected_layer_index = -1
@staticmethod
def get_selected_layer(img):
img_props = img.imageeditorplus_properties
layers = img_props.layers
selected_layer_index = img_props.selected_layer_index
if selected_layer_index == -1 or selected_layer_index >= len(layers):
return None
return layers[selected_layer_index]
@staticmethod
def create_layer(base_img, pixels, img_settings={}, layer_settings={}):
img_settings_mod = {
'is_float': img_settings.get('is_float', True),
'colorspace_name': img_settings.get('colorspace_name', 'Linear')
}
layer_settings_mod = {
'rotation': layer_settings.get('rotation', 0),
'scale': layer_settings.get('scale', [1.0, 1.0]),
'custom_data': layer_settings.get('custom_data', '{}')
}
app.create_layer(base_img, pixels, img_settings_mod, layer_settings_mod)
@staticmethod
def read_pixels_from_layer(layer):
layer_img = bpy.data.images.get(layer.name, None)
if not layer_img:
return 0, 0, None
layer_width, layer_height = layer_img.size[0], layer_img.size[1]
layer_pixels = utils.read_pixels_from_image(layer_img)
return layer_width, layer_height, layer_pixels
@staticmethod
def write_pixels_to_layer(layer, pixels):
layer_img = bpy.data.images.get(layer.name, None)
if not layer_img:
return
utils.write_pixels_to_image(layer_img, pixels)
@staticmethod
def scale_layer(layer, width, height):
layer_img = bpy.data.images.get(layer.name, None)
if not layer_img:
return
layer_img.scale(width, height)
@staticmethod
def update_layers(img):
app.rebuild_image_layers_nodes(img)