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.
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? y’all have been a big help so far thanks a lot.
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.