Fixed issue with flipped ifcblobtexture loaded to blender

This commit is contained in:
Andrej730
2023-12-25 15:35:35 +05:00
parent 3221219f4a
commit 8ddd349f8b
2 changed files with 10 additions and 7 deletions
+4 -3
View File
@@ -273,11 +273,12 @@ class Loader(blenderbim.core.tool.Loader):
value = texture["RasterCode"]
image_bytes = int(value, 2).to_bytes(len(value) // 8, "big")
pil_image = Image.open(io.BytesIO(image_bytes))
pil_image.save("test_image.png")
byte_to_normalized = 1.0 / 255.0
bpy_image = bpy.data.images.new("blob_texture", width=pil_image.width, height=pil_image.height)
bpy_image.pixels[:] = (
np.asarray(pil_image.convert("RGBA"), dtype=np.float32) * byte_to_normalized
).ravel()
# PIL returns rows ordered from top to bottom, blender from bottom to top
pil_pixel_data = np.asarray(pil_image.convert("RGBA"), dtype=np.float32)
bpy_image.pixels[:] = (pil_pixel_data * byte_to_normalized)[::-1].ravel()
bpy_image.pack()
return bpy_image
+6 -4
View File
@@ -184,7 +184,9 @@ class TestCreatingStyles(NewFile):
# https://blender.stackexchange.com/questions/62072/does-blender-have-a-method-to-a-get-png-formatted-bytearray-for-an-image-via-pyt
width = 2
height = 2
# just 4 pixels - red, green, blue, white
# 2x2 image:
# R G
# B W
# fmt: off
pixels = (
1,0,0,1,
@@ -195,11 +197,10 @@ class TestCreatingStyles(NewFile):
# fmt: on
buf = bytearray([int(p * 255) for p in pixels])
# reverse the vertical line order and add null bytes at the start
# add null bytes at the start
width_byte_4 = width * 4
raw_data = b"".join(
b"\x00" + buf[span : span + width_byte_4]
for span in range((height - 1) * width_byte_4, -1, -width_byte_4)
b"\x00" + buf[span : span + width_byte_4] for span in range(0, height * width_byte_4, width_byte_4)
)
def png_pack(png_tag, data):
@@ -251,6 +252,7 @@ class TestCreatingStyles(NewFile):
assert image_node.inputs["Vector"].links[0].from_socket.name == "Generated"
assert image_node.image.filepath == ""
# fmt: off
# blender has reversed pixels rows order
assert image_node.image.pixels[:] == (0.0, 0.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0)
# fmt: on