2
$\begingroup$


I'm working on an addon which will add image nodes with custom images.
Here is the plan:
- click on "file browser" button and choose a folder with 5 textures (every texture has a suffix (tex_nor, tex_col etc.))
- script will automatically recognize suffixes and add textures as nodes to active material

I wrote a script with interface which adds new material, but I don't have any ideas how to write next part.

import bpy, os

# Add material
def MakeMaterial_1():
mat = bpy.data.materials.new("PBR_group")
mat.use_nodes = True
nodes = mat.node_tree.nodes
for node in nodes:
    nodes.remove(node)

##########   Add custom nodes ########
# create emission node        
    node_emission = nodes.new(type='ShaderNodeEmission')
    node_emission.inputs[0].default_value = (0,1,0,1)  # green RGBA
    node_emission.inputs[1].default_value = 5.0 # strength
    node_emission.location = 0,0

# create output node
node_output = nodes.new(type='ShaderNodeOutputMaterial')   
node_output.location = 400,0    
mat.diffuse_shader = 'MINNAERT'
mat.diffuse_color = (1.0, 0.719, 0.137081)
mat.darkness = 1
return mat

# Panel
class PBRGROUP(bpy.types.Panel):
bl_label = "PBR Group Addon"
bl_idname = "PBR_Group"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "material"

def draw(self, context):

    layout = self.layout
    obj = context.object
    layout.operator("add.button")   

# Button 
class AddButton(bpy.types.Operator):
bl_idname = "add.button"
bl_label = "Add PBR Group"

def execute(self, context):
    obj = bpy.context.object
    # Change render on Cycles
    bpy.context.scene.render.engine = 'CYCLES'
    # Add a material slot
    bpy.ops.object.material_slot_add()
    # Assign a material to the last slot
    obj.material_slots[obj.material_slots.__len__() - 1].material = MakeMaterial_1()

    return{'FINISHED'}    

def register():
bpy.utils.register_class(PBRGROUP)


def unregister():
bpy.utils.unregister_class(PBRGROUP)


if __name__ == "__main__":
register()
bpy.utils.register_module(__name__)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.