Need help setting materials on cubes.

Okay, so iv been working on a visualizer fora week and finally got the baking the sound to an object. but what i want to do is select each existing obj and apply a material to it. this is how i got the obj’s set up.


(prereference: I’d like to use cycles)
what i want to do is take one and apply a color/shader and move onto the next obj.

i already have a for loop that should cover all of the obj’s. and help or comments are much appreciated.

:slight_smile:

So i just figured out how to select an obj.

bpy.ops.object.select_pattern(pattern="Cube")

and then i added a wildcard to it:

bpy.ops.object.select_pattern(pattern="Cube*")

My problem now is selecting each one individually so i can set a certain color/shader to each to form a rainbow.

You don’t really have to ‘select’ an object to operate upon it. You can reference it directly and thus leave the selection in the interface alone.


import bpy

myMaterialName = "myMat"
mat = bpy.data.materials.new(myMaterialName)
mat.diffuse_color = (0.9,0.9,0.9)

ob_list_names = ["Cube", "Cone"] #Generate a list of names.
for ob_name in ob_list_names:
    ob = bpy.data.objects.get(ob_name)
    me = ob.data
    me.materials.append(mat)

NOTE: If you run this twice, you will continue to append more materials.

Further to what Atom said, plus I started to reply about an hour ago and got sidetracked.

Cycles materials is not my strong suit. Anyway here’s a start, it uses list comprehension to get all the objects in the scene whos name starts with “Cube” adds a material with the cubes name and adds a shadernode. Prob needs to be linked in properly.


import bpy
context = bpy.context
scene = context.scene


cubes = [c for c in scene.objects if c.name.startswith("Cube")]


for cube in cubes:
    scene.objects.active = cube
    mat = bpy.data.materials.new(cube.name)


    bpy.ops.object.material_slot_add()
    cube.active_material = mat
    
    #cube.data.materials.append(mat) # or as Atom suggested.   
    
    mat.use_nodes = True
    nodes = mat.node_tree.nodes
    node = nodes.new('ShaderNodeBsdfDiffuse') 
    node.inputs["Color"].default_value = (1, 0, 0, 1)  #  (r, g, b, a)


hmm so I’m having trouble, I’m trying to make a mix shader work because I want something like a mix shader with emission (because i want a black or dark scene) and glass with a bit of roughness, do you have any tips that could help achieve that? :smiley: y’all have been a big help so far thanks a lot.

The python console can be a real help here.

Set up the nodes how you want the script to, then use the console to find out what to set.

First I set up a simple mix shader node, with a glass and emission node. (manually)


>> o = C.object
>>> m = C.object.active_material
>>> m.node_tree.nodes['Mix Shader'].rna_type.identifier
'ShaderNodeMixShader'


Then using the console I get the material node tree and can use auto complete to give me options (ctrl - space)
The code above shows where to get the type to add a new node (rna_type.identifier) and I added a new node “Mix.002” to test.


>>> m.node_tree.nodes.new('ShaderNodeMixShader')
bpy.data...nodes["Mix Shader.002"]


Have a look at the links The output to the “material output” node


>>> m.node_tree.nodes['Mix Shader'].outputs['Shader'].links[0].to_node
bpy.data...nodes["Material Output"]


>>> m.node_tree.nodes['Mix Shader'].outputs['Shader'].links[0].to_socket
bpy.data...nodes["Material Output"].inputs[0]

The inputs (notice the autocomplete)


>>> m.node_tree.nodes['Mix Shader'].inputs['
                                            Fac']
                                            Shader']
                                            Shader']
>>> m.node_tree.nodes['Mix Shader'].inputs['Shader']
                                                    
                                                    
>>> m.node_tree.nodes['Mix Shader'].inputs['Shader'].link
                                                         _limit
                                                         s
>>> m.node_tree.nodes['Mix Shader'].inputs['Shader'].links[0].from_
                                                                   node
                                                                   socket
>>> m.node_tree.nodes['Mix Shader'].inputs['Shader'].links[0].from_node
bpy.data...nodes["Emission"]


>>> m.node_tree.nodes['Mix Shader'].inputs['Shader'].links[0].from_socket
bpy.data...nodes["Emission"].outputs[0]

do the same for the emission and glass nodes then link their inputs and outputs to the appropriate nodes / sockets to emulate the set up.