2
$\begingroup$

I am trying to create a plane object in python that uses a mix shader with the first shader being a Diffuse BSDF Shader, color black, roughness 0.200 and the second shader being a Glossy BSDF shader, color 0.133, 0.133, 0.133, roughness 0.000

I know I need to use nodes, but not sure how to make the rest of it happen. Here is a second try, based on answer link in comments. Here is what I have so far, this does not give me any errors, but does not appear to work! Can anyone point out what I am doing wrong? :

import bpy


# Add new plane
bpy.ops.mesh.primitive_plane_add(view_align=False, enter_editmode=False, location=(0, -0, 0.03), layers=(True, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False, False))

bpy.context.object.scale[0] = 30
bpy.context.object.scale[1] = 30
bpy.context.object.scale[2] = 30


plane = bpy.context.active_object
plane.name = 'Light Plane'

mat_name = "MyMaterial"
# Test if material exists
# If it does not exist, create it:
mat = (bpy.data.materials.get(mat_name) or 
   bpy.data.materials.new(mat_name))

# Enable 'Use nodes':
mat.use_nodes = True
nodes = mat.node_tree.nodes


# Add a Mix shader and set its location:    
node = nodes.new('ShaderNodeMixShader')
node.location = (0,0)

# Add a Diffuse shader and set its location:    
node = nodes.new('ShaderNodeBsdfDiffuse')
node.location = (0,0)

# Add a Glossy shader and set its location:    
node = nodes.new('ShaderNodeBsdfGlossy')
node.location = (0,0)


# get some specific node:
# returns None if the node does not exist
mixshader = nodes.get("ShaderNodeMixShader")
diffuse = nodes.get("ShaderNodeBsdfDiffuse")
glossy = nodes.get("ShaderNodeBsdfGlossy")



# create mixshader node
node_mixshader = nodes.new(type='ShaderNodeMixShader')
node_mixshader.inputs[0].default_value = 0.5


# create output node
node_output = nodes.new(type='ShaderNodeOutputMaterial')   
node_output.location = 400,0


# create diffuse node
node_diffuse = nodes.new(type='ShaderNodeBsdfDiffuse')
node_diffuse.inputs[0].default_value = (0, 0, 0, 1) # black RGBA


# create output node
node_output = nodes.new(type='ShaderNodeOutputMaterial')   
node_output.location = 400,0


# create glossy node
node_glossy = nodes.new(type='ShaderNodeBsdfGlossy')
node_glossy.inputs[0].default_value = (0.133, 0.133, 0.133, 1) # gray RGBA


# create output node
node_output = nodes.new(type='ShaderNodeOutputMaterial')   
node_output.location = 400,0


# link nodes
links = mat.node_tree.links
link = links.new(node_mixshader.outputs[0], node_output.inputs[0])
link = links.new(node_diffuse.outputs[0], node_output.inputs[0])
link = links.new(node_glossy.outputs[0], node_output.inputs[0])

Thanks in advance for any help!

$\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.