There are times when I feel the need to take a step back and re-evaluate all my life decisions. Those moments tend to be preceded by questions like how the crap did I end up in this field of kudzu, and where the hell did that snake go? Or how hard could it be to script random materials in Blender with Python—an hour tops?
That last one led me to three days of HELL. This was, by far, the most difficult Blender script I’ve tackled because the API doesn’t have much on how to create materials.
Eventually I gave up on that and just started experimenting with the script in this thread on BlenderArtists.org.
It doesn’t have much by way of comments, but it can be broken up into 3 steps.
Step 1 will require some additional tweaking to set up the right node tree—there is a list here with the different types that can be plugged in.
The only thing to remember in Step 2 is that the Material Output has the input. The material node has the output going to to that input. I know, it’s like they’re going out of their way to make it confusing, right?
And presto—assign the material in Step 3. Done.
Now for the fun part.
An Application: The Confetti Function
One preliminary warning. You have to be in Cycles for this to work. And if you run it in Blender render and then switch to Cycles render, it probably won’t work—I don’t know why. You have to start in Cycles.
import bpy import random def confetti(MatCol,r,g,b): bpy.ops.mesh.primitive_plane_add() bpy.ops.transform.resize(value=(.1,.1,.1)) #Resize to fit the scene bpy.data.objects['Plane'].name = MatCol #Rename the planes as the 1st paramater above mat_name = MatCol mat = bpy.data.materials.new(mat_name) bpy.data.materials[mat_name].use_nodes = True bpy.data.materials[mat_name].node_tree.nodes.new(type='ShaderNodeEmission') inp = bpy.data.materials[mat_name].node_tree.nodes['Material Output'].inputs['Surface'] outp = bpy.data.materials[mat_name].node_tree.nodes['Emission'].outputs['Emission'] bpy.data.materials[mat_name].node_tree.links.new(inp,outp) bpy.data.materials[mat_name].node_tree.nodes['Emission'].inputs[0].default_value = (r,g,b,1) bpy.data.objects[MatCol].active_material = bpy.data.materials[mat_name] #Run a loop 100 times for index in range(100): bpy.ops.object.select_all(action='DESELECT') #If you don't deslect the other objects, the results are real weird bpy.data.objects[MatCol].select = True x=random.uniform(-10,10) y=random.uniform(-10,10) z=random.uniform(-10,10) #Randomize some variables to plug into the location parameter bpy.ops.object.duplicate_move(OBJECT_OT_duplicate={"linked":False,"mode":'TRANSLATION'}, TRANSFORM_OT_translate={"value":(x,y,z)}) #Duplicate the object and keep it unlinked confetti("YellowMat", 1, 1, 0) confetti("GreenMat", 0, 1, 0) confetti("RedMat", 1, 0, 0) confetti("BlueMat", 0, 0, 1)
(WordPress really butchers code. If you want a copy/paste version, here’s a gist.)
That first block creates the confetti function with parameters for the material name and RGB color values. The last four lines call up that function for yellow, green, red, and blue confetti. The result it this:
Nice, right? I’ll be updating soon with animation.
just so you know, something weird is going on. i found your post (this one) and it said i wasnt following you. what?!
so i clicked follow and tried to guess what the story was. then i click this post to reply here, and it said i wasnt following you even though i just clicked follow. so i clicked it again. whatevers going on, it may tell you ive started following you a few more times. the last wordpress update was more like a gutting of reliability– so much of their stuff is working less than usual– not great. cheers.
LikeLiked by 1 person
That’s happened to me too! It has been really glitchy since that last update–when I get some spare time, I gotta try out Blogger.
LikeLiked by 1 person
you couldnt pay me to use blogger, i like wordpress– but its getting silly.
LikeLiked by 1 person
Blogger’s that bad, huh? You know any other (good) alternatives?
LikeLike
how assign image?
LikeLike