massive performance improvement

This commit is contained in:
x-o-i-o-x 2025-10-18 01:25:52 +02:00
parent 51d148df80
commit 872b5934ec

View File

@ -3,8 +3,11 @@ from .constants import image_name, image_width, image_height
import numpy as np import numpy as np
from .libs.PIL import Image from .libs.PIL import Image
import mathutils import mathutils
from timeit import default_timer as timer
def apply_color(): def apply_color():
start = timer()
context = bpy.context context = bpy.context
paint_buffer = bpy.data.images.get(image_name) paint_buffer = bpy.data.images.get(image_name)
@ -21,46 +24,29 @@ def apply_color():
if normal_buffer and normal_color != mathutils.Vector((1.0, 1.0, 1.0)): if normal_buffer and normal_color != mathutils.Vector((1.0, 1.0, 1.0)):
if paint_buffer.size[0] == normal_buffer.size[0] and paint_buffer.size[1] == normal_buffer.size[1]: if paint_buffer.size[0] == normal_buffer.size[0] and paint_buffer.size[1] == normal_buffer.size[1]:
# create arrays # create arrays
# paint_mask_pixels = np.array(paint_buffer.pixels[:], dtype=np.float32).reshape(-1, 4)[:, 0] paint_mask_pixels = np.empty(len(paint_buffer.pixels), dtype=np.float32)
# target_pixels = np.array(normal_buffer.pixels[:], dtype=np.float32).reshape(-1, 4) paint_buffer.pixels.foreach_get(paint_mask_pixels)
# new_pixels = np.tile(np.array([*normal_color, 1.0], dtype=np.float32), (paint_buffer.size[0] * paint_buffer.size[1], 1)) paint_mask_pixels = (paint_mask_pixels * 255).astype(np.uint8)
paint_mask_pixels = (np.array(paint_buffer.pixels[:], dtype=np.float32) * 255).astype(np.uint8) target_pixels = np.empty(len(normal_buffer.pixels), dtype=np.float32)
target_pixels = (np.array(normal_buffer.pixels[:], dtype=np.float32) * 255).astype(np.uint8) normal_buffer.pixels.foreach_get(target_pixels)
target_pixels = (target_pixels * 255).astype(np.uint8)
new_pixels = np.tile((np.array([*normal_color, 1.0], dtype=np.float32) * 255), (image_width * image_height, 1)).astype(np.uint8) new_pixels = np.tile((np.array([*normal_color, 1.0], dtype=np.float32) * 255), (image_width * image_height, 1)).astype(np.uint8)
print("image lengths: ", len(paint_mask_pixels), len(target_pixels), len(new_pixels))
print("image shapes: ", paint_mask_pixels.shape, target_pixels.shape, new_pixels.shape)
print("image arrays: ", paint_mask_pixels, "next", target_pixels, "next", new_pixels)
# Convert arrays to PIL Images # Convert arrays to PIL Images
# paint_mask_image = Image.fromarray(paint_mask_pixels[:, 3], mode='F') # Use alpha channel for mask
# paint_mask_image = Image.fromarray(paint_mask_pixels, mode='F') # Use alpha channel for mask
# target_image = Image.fromarray(target_pixels, mode='RGBA')
# new_image = Image.fromarray(new_pixels, mode='RGBA')
paint_mask_image = Image.frombuffer('RGBA', (image_width, image_height), paint_mask_pixels, 'raw', 'RGBA', 0, 1) paint_mask_image = Image.frombuffer('RGBA', (image_width, image_height), paint_mask_pixels, 'raw', 'RGBA', 0, 1)
target_image = Image.frombuffer('RGBA', (image_width, image_height), target_pixels, 'raw', 'RGBA', 0, 1) target_image = Image.frombuffer('RGBA', (image_width, image_height), target_pixels, 'raw', 'RGBA', 0, 1)
new_image = Image.frombuffer('RGBA', (image_width, image_height), new_pixels, 'raw', 'RGBA', 0, 1) new_image = Image.frombuffer('RGBA', (image_width, image_height), new_pixels, 'raw', 'RGBA', 0, 1)
r_mask, g_mask, b_mask, a_mask = paint_mask_image.split() # separates the channels r_mask, g_mask, b_mask, a_mask = paint_mask_image.split() # separates the channels
paint_mask_image = Image.merge("RGBA", (r_mask, g_mask, b_mask, r_mask)) # replace alpha with red paint_mask_image = Image.merge("RGBA", (r_mask, g_mask, b_mask, r_mask)) # replace alpha with red
print("PIL image sizes: ", paint_mask_image.size, target_image.size, new_image.size)
print("PIL image modes: ", paint_mask_image.mode, target_image.mode, new_image.mode)
# paint_mask_image.show()
# target_image.show()
# new_image.show()
# Blend buffers # Blend buffers
blended_image = Image.composite(new_image, target_image, paint_mask_image) blended_image = Image.composite(new_image, target_image, paint_mask_image)
# blended_image.show()
# Convert blended image back to numpy array and normalize # Convert blended image back to numpy array and normalize
# blended_pixels = np.asarray(blended_image).astype(np.float32)
blended_pixels = np.array(blended_image, dtype=np.uint8).astype(np.float32) / 255.0 blended_pixels = np.array(blended_image, dtype=np.uint8).astype(np.float32) / 255.0
# Save texture # Save texture
# normal_buffer.pixels[:] = blended_pixels.flatten() normal_buffer.pixels.foreach_set(blended_pixels.ravel())
normal_buffer.pixels[:] = tuple(blended_pixels.reshape(-1))
normal_buffer.update() normal_buffer.update()
print("Color applied") print("Color applied")
@ -74,15 +60,21 @@ def apply_color():
returnValue = -1 returnValue = -1
# reset paint buffer # reset paint buffer
reset_pixels = np.array(paint_buffer.pixels[:], dtype=np.float32).reshape(-1, 4) reset_pixels = np.empty(len(paint_buffer.pixels), dtype=np.float32)
reset_pixels[:] = np.array((0, 0, 0, 1), dtype=np.float32) reset_pixels[0::4] = 0 # R
paint_buffer.pixels[:] = reset_pixels.flatten() reset_pixels[1::4] = 0 # G
reset_pixels[2::4] = 0 # B
reset_pixels[3::4] = 1 # A
paint_buffer.pixels.foreach_set(reset_pixels)
paint_buffer.update() paint_buffer.update()
else: else:
print("Texture 'vPainter_buffer' not found") print("Texture 'vPainter_buffer' not found")
returnValue = -1 returnValue = -1
end = timer()
print(f"Time taken to apply color: {end - start:.6f} seconds")
return returnValue return returnValue
def register(): def register():