Skip to content

Instantly share code, notes, and snippets.

@p2or
Created June 24, 2019 10:37
Show Gist options
  • Save p2or/1ffcd6ed57bc8d857afbd3659c9a0089 to your computer and use it in GitHub Desktop.
Save p2or/1ffcd6ed57bc8d857afbd3659c9a0089 to your computer and use it in GitHub Desktop.
Example on how to generate gloss diffuse shader via pyhon #Blender #BSE
# for https://blender.stackexchange.com/q/143599/3710
import bpy
mat_name = "Material_Name"
# check whether the material already exists
if bpy.data.materials.get(mat_name):
mat = bpy.data.materials[mat_name]
else:
# create the material
mat = bpy.data.materials.new(mat_name)
mat.diffuse_color = (0.1,0.0,0.7) # viewport color
mat.use_nodes = True
# get the material nodes
nodes = mat.node_tree.nodes
# clear all nodes to start clean
for node in nodes:
nodes.remove(node)
# create glossy node
node_glossy = nodes.new(type='ShaderNodeBsdfGlossy')
node_glossy.location = -200,100
# create diffuse node
node_diffuse = nodes.new(type='ShaderNodeBsdfDiffuse')
node_diffuse.inputs[0].default_value = (0,1,0,1) # green RGBA
node_diffuse.inputs[1].default_value = 5.0 # strength
node_diffuse.location = -200,-100
# create mix shader node
node_mix = nodes.new(type='ShaderNodeMixShader')
node_mix.location = 0,0
# create output node
node_output = nodes.new(type='ShaderNodeOutputMaterial')
node_output.location = 200,0
# link nodes
links = mat.node_tree.links
link_diff_mix = links.new(node_diffuse.outputs[0], node_mix.inputs[2])
link_gloss_mix = links.new(node_glossy.outputs[0], node_mix.inputs[1])
# link mix to output node
link_mix_out = links.new(node_mix.outputs[0], node_output.inputs[0])
# Assign it to the context object
obj = bpy.context.object
if obj.data.materials:
# assign to 1st material slot
obj.data.materials[0] = mat
else:
# no slots
obj.data.materials.append(mat)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment