3

I'm trying to set the properties of a Cycles material, which is linked to a Plane object, using the API.

So far this is what I've got:

  1. Create a floor plane object:

    bpy.ops.mesh.primitive_plane_add(radius=3, view_align=False, enter_editmode=False, location=(0, 0, 0))
    
    my_floor = bpy.data.objects["Plane"]
    
  2. Create the material for my floor, (I'm assuming it does not already exist):

    my_floors_material_name = "Material"
    
    #create the materials object
    my_floors_material_object =    bpy.data.materials.new(my_floors_material_name)
    
    #Enable 'Use nodes':
    my_floors_material_object.use_nodes = True
    
  3. Add a new node type from the list [types.ShaderNode][1]. I'd like to choose the equivalent of node_groups["Shader Nodetree"].nodes["Anisotropic BSDF"]:

    #create the input node
    shinny_floor_input_node = my_floors_material_nodes.new(type='ShaderNodeBsdfAnisotropic')
    
    #create the output node
    shinny_floor_output_node = my_floors_material_nodes.new(type='ShaderNodeOutputMaterial') 
    
    #link the input and output nodes, on the node tree of my object
        my_floors_material_object.node_tree.links.new(shinny_floor_input_node.outputs[0], shinny_floor_output_node.inputs[0])
    
    
    #this runs and returns 
    bpy.data.node_groups['Shader Nodetree']...NodeLink
    
  4. Assign the floor material to the floor object:

    #assign the material object, to the plane object
    my_floor.data.materials.append( my_floors_material_object )  
    

Unfortunately it does not work? Here's the floor I want to create, (the shinny one below the statue)

Floor created using GUI

Here's what I get with my current script:

Floor created using API

Seems like it should an easy thing to do, but I'm confused? Here's the a link that I used to get started, Control Cycles material nodes and material properties in Python

Thanks a lot for any help!

Ajay T
  • 115
  • 1
  • 7
  • As last step you only have to assign the material to the object, see: http://blender.stackexchange.com/questions/23433/assign-a-new-material-to-an-object-in-the-scene?rq=1 – p2or Jun 21 '15 at 12:13
  • Hey poor, thanks alot for the link :) I've added the last step as you advise, and I also found out how to create the materials object using the API from here, http://blender.stackexchange.com/questions/5668/add-nodes-to-material-with-python?rq=1 Unfortunately it still doesn't work ? – Ajay T Jun 21 '15 at 12:44
  • What exactly is not working? Error messages? – p2or Jun 21 '15 at 12:49
  • The code runs without any error messages. It just does not do what I expect/want it to do? Basically the material of the floor/plane object is unchanged from Diffuse BSDF, (the default), and not Ansiotropic BSDF ; which is what I want the script to do. When I run the script, in the the Surface panel of the material, the box for the Surface, is unchanged from Diffuse BSDF to Anisotropic BSDF ??? So I think that's the problem? – Ajay T Jun 21 '15 at 13:01
  • Please check your output Node, it should looks like: node_output = nodes.new(type='ShaderNodeOutputMaterial'), creating shader code: https://gist.github.com/p2or/fbadce83877a36622720#file-create-anisotropic-py – p2or Jun 21 '15 at 13:28
  • Yes !! Thank you very much indeed, I think it works now. I just need to experiment with the default settings, but the basic material seems to be right now ;) How do I accept your answer as the best? – Ajay T Jun 21 '15 at 13:53
  • Sure, can you do it, or do you want me to do it? – Ajay T Jun 21 '15 at 14:01
  • Glad I could help :) Don't delete it, it's helpful for others too, I'll mark it as duplicate. – p2or Jun 21 '15 at 14:03

0 Answers0