save startup blend for animation tab & whatnot
This commit is contained in:
2026-04-08 12:10:18 -06:00
parent 57a652524a
commit 692e200ffe
180 changed files with 12336 additions and 3431 deletions
+115 -61
View File
@@ -16,11 +16,11 @@
#
# ##### END GPL LICENSE BLOCK #####
import os
import time
from functools import lru_cache
import logging
from dataclasses import dataclass
from functools import lru_cache
import os
import time
import bpy
@@ -180,13 +180,97 @@ def analyze_image_is_true_hdr(image):
imageHeight = size[1]
tempBuffer = numpy.empty(imageWidth * imageHeight * 4, dtype=numpy.float32)
image.pixels.foreach_get(tempBuffer)
image.blenderkit.true_hdr = numpy.amax(tempBuffer) > 1.05
image.blenderkit.true_hdr = bool(numpy.amax(tempBuffer) > 1.05)
def _save_hdr_thumbnail_image(
hdr_image,
output_path: str,
max_thumbnail_size: int,
use_custom_tone: bool,
exposure: float,
gamma: float,
):
import numpy
image_width, image_height = hdr_image.size
ratio = image_width / image_height
thumbnail_width = min(image_width, max_thumbnail_size)
thumbnail_height = min(image_height, int(max_thumbnail_size / ratio))
# Read once so we can both detect HDR and safely create a scaled temp image.
pixel_count = image_width * image_height
pixel_buffer = numpy.empty(pixel_count * 4, dtype=numpy.float32)
hdr_image.pixels.foreach_get(pixel_buffer)
hdr_image.blenderkit.true_hdr = bool(numpy.amax(pixel_buffer) > 1.05)
source_image = hdr_image
temp_image = None
if thumbnail_width < image_width:
temp_name = f"{hdr_image.name}_thumb_tmp"
temp_image = bpy.data.images.new(
temp_name,
width=image_width,
height=image_height,
alpha=False,
float_buffer=True,
)
temp_image.pixels.foreach_set(pixel_buffer)
temp_image.scale(thumbnail_width, thumbnail_height)
source_image = temp_image
try:
scene = bpy.context.scene
view_settings = scene.view_settings
orig_exposure = view_settings.exposure
orig_gamma = view_settings.gamma
try:
if use_custom_tone:
view_settings.exposure = exposure
view_settings.gamma = gamma
img_save_as(
source_image,
filepath=output_path,
view_transform="Standard",
)
finally:
view_settings.exposure = orig_exposure
view_settings.gamma = orig_gamma
finally:
if temp_image is not None:
bpy.data.images.remove(temp_image)
def generate_hdr_thumbnail_preview(
hdr_image,
use_custom_tone: bool,
exposure: float,
gamma: float,
max_preview_size: int = 256,
) -> str:
from . import paths
safe_name = "".join(
c if c.isalnum() or c in ("-", "_", ".") else "_" for c in hdr_image.name
)
preview_dir = paths.get_temp_dir(subdir="hdr_thumbnail_preview")
preview_path = os.path.join(preview_dir, f"{safe_name}_preview.jpg")
_save_hdr_thumbnail_image(
hdr_image=hdr_image,
output_path=preview_path,
max_thumbnail_size=max_preview_size,
use_custom_tone=use_custom_tone,
exposure=exposure,
gamma=gamma,
)
return preview_path
def generate_hdr_thumbnail():
import numpy
scene = bpy.context.scene
ui_props = bpy.context.window_manager.blenderkitUI
hdr_image = (
ui_props.hdr_upload_image
@@ -194,36 +278,15 @@ def generate_hdr_thumbnail():
base, ext = os.path.splitext(hdr_image.filepath)
thumb_path = base + ".jpg"
thumb_name = os.path.basename(thumb_path)
max_thumbnail_size = 2048
size = hdr_image.size
ratio = size[0] / size[1]
imageWidth = size[0]
imageHeight = size[1]
thumbnailWidth = min(size[0], max_thumbnail_size)
thumbnailHeight = min(size[1], int(max_thumbnail_size / ratio))
tempBuffer = numpy.empty(imageWidth * imageHeight * 4, dtype=numpy.float32)
inew = bpy.data.images.new(
thumb_name, imageWidth, imageHeight, alpha=False, float_buffer=False
_save_hdr_thumbnail_image(
hdr_image=hdr_image,
output_path=thumb_path,
max_thumbnail_size=2048,
use_custom_tone=ui_props.hdr_use_custom_thumbnail_tone,
exposure=ui_props.hdr_thumbnail_exposure,
gamma=ui_props.hdr_thumbnail_gamma,
)
hdr_image.pixels.foreach_get(tempBuffer)
hdr_image.blenderkit.true_hdr = numpy.amax(tempBuffer) > 1.05
inew.filepath = thumb_path
set_colorspace(inew, "Linear")
inew.pixels.foreach_set(tempBuffer)
bpy.context.view_layer.update()
if thumbnailWidth < imageWidth:
inew.scale(thumbnailWidth, thumbnailHeight)
img_save_as(inew, filepath=inew.filepath)
def find_color_mode(image):
if not isinstance(image, bpy.types.Image):
@@ -257,8 +320,7 @@ def can_erase_alpha(na):
alpha = na[3::4]
alpha_sum = alpha.sum()
if alpha_sum == alpha.size:
print("image can have alpha erased")
# print(alpha_sum, alpha.size)
bk_logger.info("image can have alpha erased")
return alpha_sum == alpha.size
@@ -269,9 +331,8 @@ def is_image_black(na):
rgbsum = r.sum() + g.sum() + b.sum()
# print('rgb sum', rgbsum, r.sum(), g.sum(), b.sum())
if rgbsum == 0:
print("image can have alpha channel dropped")
bk_logger.info("image can have alpha channel dropped")
return rgbsum == 0
@@ -284,7 +345,7 @@ def is_image_bw(na):
gb_equal = g == b
rgbequal = rg_equal.all() and gb_equal.all()
if rgbequal:
print("image is black and white, can have channels reduced")
bk_logger.info("image is black and white, can have channels reduced")
return rgbequal
@@ -326,7 +387,6 @@ def numpytoimage(a, iname, width=0, height=0, channels=3):
i = None
for image in bpy.data.images:
# print(image.name[:len(iname)],iname, image.size[0],a.shape[0],image.size[1],a.shape[1])
if (
image.name[: len(iname)] == iname
and image.size[0] == width
@@ -352,7 +412,7 @@ def numpytoimage(a, iname, width=0, height=0, channels=3):
# a = a.repeat(channels)
# a[3::4] = 1
i.pixels.foreach_set(a) # this gives big speedup!
print("\ntime " + str(time.time() - t))
bk_logger.info("\ntime " + str(time.time() - t))
return i
@@ -363,7 +423,6 @@ def imagetonumpy_flat(i):
width = i.size[0]
height = i.size[1]
# print(i.channels)
size = width * height * i.channels
na = numpy.empty(size, numpy.float32)
@@ -374,7 +433,6 @@ def imagetonumpy_flat(i):
# na = na.reshape(height, width, i.channels)
# na = na.swapaxnes(0, 1)
# print('\ntime of image to numpy ' + str(time.time() - t))
return na
@@ -385,7 +443,6 @@ def imagetonumpy(i):
width = i.size[0]
height = i.size[1]
# print(i.channels)
size = width * height * i.channels
na = np.empty(size, np.float32)
@@ -396,7 +453,6 @@ def imagetonumpy(i):
na = na.reshape(height, width, i.channels)
na = na.swapaxes(0, 1)
# print('\ntime of image to numpy ' + str(time.time() - t))
return na
@@ -424,9 +480,9 @@ def get_rgb_mean(i):
gmean = g.mean()
bmean = b.mean()
rmedian = numpy.median(r)
gmedian = numpy.median(g)
bmedian = numpy.median(b)
# rmedian = numpy.median(r)
# gmedian = numpy.median(g)
# bmedian = numpy.median(b)
# return(rmedian,gmedian, bmedian)
return (rmean, gmean, bmean)
@@ -514,15 +570,13 @@ def check_nmap_ogl_vs_dx(i, mask=None, generated_test_images=False):
ogl_std = ogl.std()
dx_std = dx.std()
# print(mean_ogl, mean_dx)
# print(max_ogl, max_dx)
print(ogl_std, dx_std)
print(i.name)
bk_logger.info("OpenGL std: %s, DirectX std: %s", ogl_std, dx_std)
bk_logger.info("Image name: %s", i.name)
# if abs(mean_ogl) > abs(mean_dx):
if abs(ogl_std) > abs(dx_std):
print("this is probably a DirectX texture")
bk_logger.info("this is probably a DirectX texture")
else:
print("this is probably an OpenGL texture")
bk_logger.info("this is probably an OpenGL texture")
if generated_test_images:
# red_x_comparison_img = red_x_comparison_img.swapaxes(0,1)
@@ -584,9 +638,9 @@ def make_possible_reductions_on_image(
# setup image depth, 8 or 16 bit.
# this should normally divide depth with number of channels, but blender always states that number of channels is 4, even if there are only 3
print(teximage.name)
print(teximage.depth)
print(teximage.channels)
bk_logger.info("Image name: %s", teximage.name)
bk_logger.info("Image depth: %s", teximage.depth)
bk_logger.info("Image channels: %s", teximage.channels)
bpy.context.scene.display_settings.display_device = "None"
@@ -594,16 +648,16 @@ def make_possible_reductions_on_image(
ims.color_mode = find_color_mode(teximage)
# image_depth = str(max(min(int(teximage.depth / 3), 16), 8))
print("resulting depth set to:", image_depth)
bk_logger.info("resulting depth set to: %s", image_depth)
fp = input_filepath
if do_reductions:
na = imagetonumpy_flat(teximage)
if can_erase_alpha(na):
print(teximage.file_format)
bk_logger.info("Image file format: %s", teximage.file_format)
if teximage.file_format == "PNG":
print("changing type of image to JPG")
bk_logger.info("Changing type of image to JPG")
base, ext = os.path.splitext(fp)
teximage["original_extension"] = ext