Python: How to Make Hundreds of Random Lights in Blender

Hey, look. Lights.

Modeling randomness in Blender can be frustrating. But once I figured out I could use Python to generate random numbers, it got a lot easier.

For this week’s project, I made 200 lights around my little bug using a simple loop, some variables, and a Python module.

Hold up. What’s a Module?

A module is a present from Python—it contains prewritten variables and functions to make things happen. The random module makes random numbers happen. And all you have to do is type “import WhateverModule” (in this case “import random”) at the beginning of the script.

You can read about this particular module’s functions here: https://docs.python.org/2/library/random.html.

Because I’m not so particular, I picked out the simplest one:

random.randint(MinimumValue, MaximumValue)
#This will return a random number between the two values

Alright, What About the Blender Commands?

The bug scene above doesn’t actually have any Blender lamps. I opted for UV spheres with a strong emission material to look like little lights.

Here’s the bare-bones formula for adding one:

bpy.ops.mesh.primitive_uv_sphere_add(size=#, location=(x,y,z))

The beauty of this is that the x,y,z parameters can be assigned to variables, ones that can be reassigned to a random number at the end of each run of the loop.

Before running the script, just remember to activate a layer with nothing else in it. And change up the parameters below to fit your model.

The Python Code

If all has gone according to plan, you should see this:

Ta da!

Now it’s just a matter of creating a texture to make the spheres light up. Deselect all other layers, so you’re only interacting with the spheres. Then assign one a texture.

Assigning a Texture in Blender.

Now select all the spheres (either by pressing A or box selecting), and press CTRL + L to link the material to all of them.

That’ll get you here.

A Bug with Lights
After that, you can mix in more looped lights for different effects.

All the loopy lights.

 

 

 

2 Replies to “Python: How to Make Hundreds of Random Lights in Blender”

  1. Je n'ai pas à me déplacer très loin pour voir de la neige, en fait c'est le contraire, si je veux me déplacer je dois enlever la neige!Néanmoins, je suis un des rares Québécois qui admet aimer la neige, le froid, les tempêtes, l'hiver quoi.Je comprends votre éAeeveillrment!mccent Grave

    Like

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

%d bloggers like this: