0
$\begingroup$

The code I have creates an addon which takes an existing material on my model and then grabs baked image textures from my laptop before it adds them using the principle texture setup feature from the node wrangler. The image below is an example of the output after I have ran my code and hit the button on my addon.

enter image description here enter image description here

class SHADER_OT_bunnygirl_body(bpy.types.Operator):
     bl_label = "Bunny Girl Body"
     bl_idname = 'myaddonbody.my_operator'
    
     #Executes once the button is pressed
     def execute(self, context):
        win = bpy.context.window
        scr = win.screen
        areas  = [area for area in scr.areas if area.type == 'NODE_EDITOR']
        regions = [region for region in areas[0].regions if region.type == 'WINDOW']
    
        with bpy.context.temp_override(window=win,area=areas[0], region=regions[0],screen=scr):
             bpy.ops.node.nw_add_textures_for_principled(filepath="//Body Color.png", directory="C:\\stable internship\\bunny girl\\bunny girl baked textures ATTEMPT 3\\", files=[{"name":"Body Normal.png", "name":"Body Normal.png"}, {"name":"Body Specular.png", "name":"Body Specular.png"}, {"name":"Body Roughness.png", "name":"Body Roughness.png"}, {"name":"Body Color.png", "name":"Body Color.png"}], relative_path=True)
    
        #I have no idea whether the following line of code works 
        body = bpy.data.materials.get("Body")
        #My idea here is to connect the top Base Color image texture's alpha thing with the 
        #alpha slider on principled BSDF        
    
        return {'FINISHED'}

However, in all the tutorials I watched they manually created the new nodes on the node tree using code rather than condensing it to 1 line like I have. Because of this I don't exactly know how I can grab the image texture on the top that says "Base Color" and connecting it with the Alpha slider on my "Principled BSDF" node.

Please don't flame me if this question is really dumb I am new to this kind of stuff. Many thanks!

$\endgroup$

1 Answer 1

0
$\begingroup$

Assuming your material is named 'Body' instead of the default 'Material' on the selected object (bpy.context.object), you can use the following script to connect the Alpha socket output of your Base Color Image Texture node to the Principled BSDF Alpha socket input:

import bpy

material = bpy.context.object.data.materials['Body']
principled_bsdf_node = material.node_tree.nodes['Principled BSDF']

base_color_texture_node = None
for node in material.node_tree.nodes:
    if node.type == 'TEX_IMAGE' and node.label.startswith('Base Color'):
        base_color_texture_node = node
        break

material.node_tree.links.new(base_color_texture_node.outputs['Alpha'], principled_bsdf_node.inputs['Alpha'])
$\endgroup$
4
  • $\begingroup$ Works perfect thanks so much man! $\endgroup$ Commented Jul 19, 2023 at 2:26
  • $\begingroup$ Is it possible for instead of just grabbing the body material that it checks to see whether the body material exists on the object that is selected? Like for instance if I have the body object selected before hitting the button it would check whether the body material exists on the body object? $\endgroup$ Commented Jul 19, 2023 at 2:27
  • $\begingroup$ you're welcome. yes just check if material exists like if "body" in bpy.context.object.data.materials: print("do something") $\endgroup$
    – Harry McKenzie
    Commented Jul 19, 2023 at 3:00
  • 1
    $\begingroup$ thank you so much for your time man $\endgroup$ Commented Jul 19, 2023 at 3:05

You must log in to answer this question.

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