PYTHON: How to Code Materials in Blender Cycles

What's Going On Here?

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

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):
#Resize to fit the scene['Plane'].name = MatCol
#Rename the planes as the 1st paramater above
mat_name = MatCol
mat =[mat_name].use_nodes = True[mat_name]'ShaderNodeEmission')
inp =[mat_name].node_tree.nodes['Material Output'].inputs['Surface']
outp =[mat_name].node_tree.nodes['Emission'].outputs['Emission'][mat_name],outp)[mat_name].node_tree.nodes['Emission'].inputs[0].default_value = (r,g,b,1)[MatCol].active_material =[mat_name]
#Run a loop 100 times
for index in range(100):
#If you don't deslect the other objects, the results are real weird[MatCol].select = True
#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:

The Confetti Function

Nice, right? I’ll be updating soon with animation.


5 Replies to “PYTHON: How to Code Materials in Blender Cycles”

  1. 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.

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: