2
$\begingroup$

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?

enter image description here

enter image description here

$\endgroup$
2
  • $\begingroup$ That code is only creating the new materials. The code that associates the material to the object/mesh must be somewhere else in the add-on. Search the add-on with your favorite editor/ide and look for 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 like bpy.contex.active_object in the context where you see material_slots. $\endgroup$ Commented Feb 16, 2022 at 14:55
  • $\begingroup$ 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$
    – Vincent
    Commented Feb 17, 2022 at 9:01

1 Answer 1

3
$\begingroup$

When you select objects, either in the 3D viewport, or the outliner, the last one you select becomes the active object. By using bpy.context.active_object, the add-on is using that object. It is also assuming that there isn't already a material assigned to slot 1 of the object, so it is putting the material in that slot. For it to be completely useful, the add-on probably has a way of assigning the material in slot 1 to selected faces on your mesh.

On the other hand, slot 0, the first slot, is important because all faces of the object that you don't explicitly assign a material to will be assigned to that slot by default.

$\endgroup$
2
  • $\begingroup$ Thank you so much, Marty. It's a very clear answer. When I was creating the model, I firstly added a meterial slot “side.001”, and assigned it to the all face. Then I added the 2st meterial slot “front.001”, and assigned it to a special face area selected. This special face area is where addon operate an image as texture meterial on. If material_slots[1] is specified as this “front.001”. Then the “side.001” should be material_slots[0], right? And If I add more meterial slots behind it, their code parameter should be: material_slots[2], material_slots[3] …? $\endgroup$
    – Vincent
    Commented Feb 18, 2022 at 7:44
  • $\begingroup$ Yes. I also screwed up my answer slightly, since it's slot 0 not 1 that gets automatically assigned. Let me edit the answer and fix it. $\endgroup$ Commented Feb 18, 2022 at 16:05

You must log in to answer this question.

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