I'm reading an addon's code. It's used to automatically add an image(texture) on an model and render out an image. User can feed any image and model. Here is the code snippet:
def addMaterialImg():
for img in bpy.data.images:
name = img.name_full
material = bpy.data.materials.new(name= name)
material.use_nodes = True
#create a reference to the material output
material_output = material.node_tree.nodes.get('Material Output')
Principled_BSDF = material.node_tree.nodes.get('Principled BSDF')
texImage_node = material.node_tree.nodes.new('ShaderNodeTexImage')
texImage_node.image = img
#set location of node
material_output.location = (400, 20)
Principled_BSDF.location = (0, 0)
texImage_node.location = (-400, -500)
material.node_tree.links.new(texImage_node.outputs[0], Principled_BSDF.inputs[0])
material.node_tree.nodes["Principled BSDF"].inputs['Specular'].default_value = 0
material.node_tree.nodes["Principled BSDF"].inputs['Roughness'].default_value = 0.5
mat = bpy.data.materials.get(name)
mat.blend_method = 'CLIP'
return mat
What I'm confused about is: how does the addon find the particular mesh which I want to add image(texture) on?
Here it seems the addon follows the node location. If so, what the value (400,20) (0,0) (-400,-500) mean?
I need to make it clear, because I'm creating models for it. There must be some standards should be followed.
The developer figured out that the models here should be merged into one mesh so the script could operate on them. I checked the sample model from the developer, there are 2 material slots. The image mesh is on one of them.
So the addon should know which material slot and which node, right? How?
material_slots
. There will be places where slots are set. My guess is that it's using the active object as the target so you'll see something likebpy.contex.active_object
in the context where you seematerial_slots.
$\endgroup$bpy.context.active_object.material_slots[1].material = bpy.data.materials[block.name_full]
There is only one row code including it. Could it locates which material slot? $\endgroup$