Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
A Python function to create confetti in Blender, more specifically it'll create 100 small planes, assign those a new texture, and randomize their location. The confetti function takes three parameters--the first is the name of the material to be assigned, the next three are the RGB values for that material. The code below will generate yellow, g…
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)

This comment has been minimized.

Copy link

@RedBeansAndRice RedBeansAndRice commented Apr 6, 2020

I think you might want to change[MatCol].select = True to[MatCol].select_set(True)
for 2.8 and up.

I ran into an error and found this on the subject:

very helpful script for getting familiar with blenders data structures!! Thanks for sharing!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment