0
$\begingroup$

So I've been making a system that needs a lot of masks, and I need a script/addon to automatically make blank mask images at a set resolution, named appropriately, and put into the proper Image Node.

This is Blender 2.93 BTW if that makes any different to syntax.

I found some blocks of code to do different bits of this, and I have it mostly working, but for some reason it doesn't put it in the Image Node.

size = 1024, 1024

import bpy
# blank image
image = bpy.data.images.new("NipRough", width=size[0], height=size[1])

## For white image
# pixels = [1.0] * (4 * size[0] * size[1])

pixels = [None] * size[0] * size[1]
for x in range(size[0]):
    for y in range(size[1]):
        # assign RGBA to something useful
        r = 0
        g = 0
        b = 0
        a = 1.0

        pixels[(y * size[0]) + x] = [r, g, b, a]

# flatten list
pixels = [chan for px in pixels for chan in px]

# assign pixels
image.pixels = pixels

# write image
image.filepath_raw = "//NipRough.png"
image.file_format = 'PNG'
image.save()

# **works fine up to here**

# find node and assign the texture to it
bpy.data.images.load("//NipRough.png", check_existing=True)
mat = bpy.data.materials["Skin"]
node = bpy.data.materials["Skin"].node_tree.nodes["NipRough"].name
tex = ('//NipRough.png')
node.image = tex

It keeps telling me:

line 39, in "< module >" AttributeError: 'str' object has no attribute 'image'

$\endgroup$

1 Answer 1

0
$\begingroup$

The image object is returned by bpy.data.images.load(). Assuming that "NipRough" is an existing image texture node, meaning its type is ShaderNodeTexImage, then the texture can be assigned as follows:

img = bpy.data.images.load("//NipRough.png", check_existing=True)
bpy.data.materials["Skin"].node_tree.nodes["NipRough"].image = img
$\endgroup$
1
  • $\begingroup$ Halleluja! It works! Thank you Robert. Man I was really overthinking that. Shows how little I know about code. I feel like Lisa Simpson in the Australia episode. "I know all those words, but that sentence makes no sense." Anyway, thank you so much. Now I need to scale this out to all the masks, and set up an interface for the addon. I should be able to piece that together at least. $\endgroup$ Jun 16, 2022 at 22:26

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .