kopia lustrzana https://github.com/vilemduha/blendercam
Merge pull request #228 from dwrobel/Fix-cannot-access-local-variable-error-in-numpy_to_image
Fix 'cannot access local variable' error in numpy_to_imagepull/292/head
commit
92e2844ce1
|
|
@ -13,6 +13,7 @@ from math import (
|
||||||
sin,
|
sin,
|
||||||
tan,
|
tan,
|
||||||
)
|
)
|
||||||
|
from typing import Optional
|
||||||
import os
|
import os
|
||||||
import random
|
import random
|
||||||
import time
|
import time
|
||||||
|
|
@ -134,7 +135,7 @@ def get_circle_binary(r):
|
||||||
# get cutters for the z-buffer image method
|
# get cutters for the z-buffer image method
|
||||||
|
|
||||||
|
|
||||||
def numpy_to_image(a, iname):
|
def numpy_to_image(a: numpy.ndarray, iname: str) -> bpy.types.Image:
|
||||||
"""Convert a NumPy array to a Blender image.
|
"""Convert a NumPy array to a Blender image.
|
||||||
|
|
||||||
This function takes a NumPy array and converts it into a Blender image.
|
This function takes a NumPy array and converts it into a Blender image.
|
||||||
|
|
@ -151,46 +152,55 @@ def numpy_to_image(a, iname):
|
||||||
bpy.types.Image: The Blender image object that was created or found.
|
bpy.types.Image: The Blender image object that was created or found.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
print("Numpy to Image", iname)
|
|
||||||
t = time.time()
|
t = time.time()
|
||||||
print(a.shape[0], a.shape[1])
|
|
||||||
foundimage = False
|
|
||||||
|
|
||||||
for image in bpy.data.images:
|
width = a.shape[0]
|
||||||
if (
|
height = a.shape[1]
|
||||||
image.name[: len(iname)] == iname
|
# Based on the Blender source code: source/blender/makesdna/DNA_ID.h. MAX_ID_NAME=64
|
||||||
and image.size[0] == a.shape[0]
|
# is defining the maximum length of the id and we need to subtract four letters for
|
||||||
and image.size[1] == a.shape[1]
|
# suffix as Blender seems to use the ".%03d" pattern to avoid creating duplicate ids.
|
||||||
):
|
iname_59 = iname[:59]
|
||||||
i = image
|
|
||||||
foundimage = True
|
print(f"numpy_to_image: iname:{iname}, width:{width}, height:{height}")
|
||||||
if not foundimage:
|
|
||||||
bpy.ops.image.new(
|
def find_image(name: str, width: int, heigh: int) -> Optional[bpy.types.Image]:
|
||||||
name=iname,
|
if name in bpy.data.images:
|
||||||
width=a.shape[0],
|
image = bpy.data.images[name]
|
||||||
height=a.shape[1],
|
|
||||||
|
if image.size[0] == width and image.size[1] == height:
|
||||||
|
return image
|
||||||
|
|
||||||
|
return None
|
||||||
|
|
||||||
|
image = find_image(iname, width, height) or find_image(iname_59, width, height)
|
||||||
|
|
||||||
|
if image is None:
|
||||||
|
print(f"numpy_to_image: Creating a new image:{iname_59}")
|
||||||
|
result = bpy.ops.image.new(
|
||||||
|
name=iname_59,
|
||||||
|
width=width,
|
||||||
|
height=height,
|
||||||
color=(0, 0, 0, 1),
|
color=(0, 0, 0, 1),
|
||||||
alpha=True,
|
alpha=True,
|
||||||
generated_type="BLANK",
|
generated_type="BLANK",
|
||||||
float=True,
|
float=True,
|
||||||
)
|
)
|
||||||
for image in bpy.data.images:
|
print(f"numpy_to_image: Image creation result:{result}")
|
||||||
# print(image.name[:len(iname)],iname, image.size[0],a.shape[0],image.size[1],a.shape[1])
|
|
||||||
if (
|
# If 'iname_59' id didn't exist previously, then
|
||||||
image.name[: len(iname)] == iname
|
# it should have been created without changing its id.
|
||||||
and image.size[0] == a.shape[0]
|
image = bpy.data.images[iname_59]
|
||||||
and image.size[1] == a.shape[1]
|
|
||||||
):
|
|
||||||
i = image
|
|
||||||
|
|
||||||
d = a.shape[0] * a.shape[1]
|
|
||||||
a = a.swapaxes(0, 1)
|
a = a.swapaxes(0, 1)
|
||||||
a = a.reshape(d)
|
a = a.reshape(width * height)
|
||||||
a = a.repeat(4)
|
a = a.repeat(4)
|
||||||
a[3::4] = 1
|
a[3::4] = 1
|
||||||
i.pixels[:] = a[:] # this gives big speedup!
|
|
||||||
print("\ntime " + str(time.time() - t))
|
image.pixels[:] = a[:] # this gives big speedup!
|
||||||
return i
|
|
||||||
|
print(f"numpy_to_image: Time:{str(time.time() - t)}")
|
||||||
|
|
||||||
|
return image
|
||||||
|
|
||||||
|
|
||||||
def image_to_numpy(i):
|
def image_to_numpy(i):
|
||||||
|
|
|
||||||
Ładowanie…
Reference in New Issue