In Blender 2.5 video tutorial we’ll be creating a script that can assign a random material to every object in the selection. We’ll also build in a search field so we can filter which materials should be allowed, as well as taking a look at random seeds.
What is covered in this section:
- Editing the active material through python
- Operator properties
- Python’s Random Module (random numbers and seeds)











Cool, I can see where this would be handy. Say an Architectural wall or building with tiled floors you want all different colors within a range.
Thanks.
This looks interesting. Downloading it now. Always glad to see more python tutorials!
I watched this at work and really enjoyed it. I didn’t mind that you were a “loose canon” as you put it – especially considering you realized your mistakes fairly quickly. I think anyone who has done any amount of programming knows that rarely does the program work correctly from the get go and constant debugging is necessary.
I really enjoy that you take the time to show in the console the different properties. Along with showing the python api doc. I think it helps people see where they can go for information they need while they are developing their own scripts so the tutorials become even more useful than just the topic of the script at hand.
Thanks again for the tutorial and looking forward to the next one!
Thank you for your kind words, I’m glad you liked the tutorial!
You’re right in that 99% of the time you’ll need to debug the script you’re writing, but as I’m doing the tutorial (with a cheat sheet next to me even) I always get that “d’ow”-feeling when I’m missing something that, in hindsight, seems so obvious.
You’re a genius!
This would be excellent for testing a couple of materials in a environment when not sure which one to use. for example when multiple floor materials can be chosen but only one fits the scene
Whazaa, I’m vague again xD
Anyway, Thanks!
Good tutorial. Makes python scripting in Blender very accessible.
Help Please
http://www.pasteall.org/23355/python
The problems I know is that in line 19 where it says bl_options = (), I couldn’t put both ‘REGISTER’ and ‘UNDO’ ; it keeps erroring.
Also, on line 4, it says in the console (or terminal) that “AttributeError: ‘RNA_Types’ object has no attribute ‘panel’ ”
Thanks in advance
Hey Kris,
You’ll need to use accolades (curly brackets) for the bl_options. So bl_options = {‘REGISTER’, ‘UnDO’}
Good luck!
Patrick
Oops, I missed your second question there. You’ll need to capitalize Panel so it says bpy.types.Panel
Thanks
More python tutorials!
BlenderCookie,
Thanks for finding this python wizard (Patrick Boelens) who shares his Python/ Blender knowledge with the rest of us.
Thanks Patrick!
this tutorial worked when i was using 2.56 but when i used 2.58a, it wouldnt work
nevermind, just had a mistake in my code
Excellent tutorial. I already have a great use for this tutorial.
Im having a problem with line 21, When I press run script and go to the spacebar menu and type in the random material assigner and click it it comes up with this error…
Traceback (most recent call list)
File “C:UsersPatrickDocumentsMy StuffBlenderBlender ProjectsTUT_SCRIPT.blendText”, line 21 in execute
File “C:Program FilesBlender FoundationBlender2.58Scriptsmodulesbpy_types.py”, line 472, in_getattribute_return super()._getattribute_(attr)
AttributeError:’mat_assigner’ object has no attribute ‘availableMaterials’
location::-1
Help would be greatly appreciated
Hi Yarkata,
Did you make sure to define availableMaterials at the top of your class? If you’re still stuck, please feel free to upload your script to pastebin.com or pasteall.org and post a link to it here so I can take a look at it. =)
http://www.pasteall.org/23521/python
Cool, I like these Python tutorials. But I think many people don’t have much experience in Python, so could you consider making a ‘Python for Beginners’-tutorial? Without Blender.
Hi Tom,
Thanks for your input! I’ll be sure to talk this over Wes or Jonathan. =)
Ooh! I’d really like that.
Thanks for adding more Blender Python tutorials. ^^
How to run Blender in mode where you can see prints in console?
on linux
Do you know how to use a terminal in linux? If you navigate to the folder with the blender binary from a terminal you can start blender by typing ‘./blender’ (without the single quotes) and it should start up. Then the print statements will appear in that terminal window. If you need me to be more specific just let me know and I can try to help better.
On a related note I was curious if anyone knew how to do the same thing for Windows?
Hey Jeff,
Thanks for answering that question on Linux; I have almost 0 experience with that. If it doesn’t start up alongside Blender by default you can open it by going to Help > Toggle Console.
Thanks again! =)
Very useful! Thank you!
http://www.pasteall.org/23576/python
Line 17 isn’t working,
I have completed the whole script but when I try running it it says that line 17 has an error and that I should look in the python console, I don’t know what I did wrong, I checked it over heaps of times and I still have not figured it out.
cheers
Hey Brock,
Seems there are a few small errors with your script. The most important one is that you’ll want to have lines 59 and 60 all the way to the left, with no whitespace in front of it. Right now python will think these lines are still part of your class.
Then in line 21 you’ll need to use curly braces {} for your bl_options, rather than parenthesis ().
After doing this, the script will not return any errors but the button won’t show. This is because on line 13 the operator should be between quotation marks so that it reads layout.operator(“object.random_material”)
On line 48 you made a small typo (availabeMaterials, instead of availableMaterials).
Lastly, on line 50 you’ll want to use square brackets [] instead of parenthesis () to access the randnum key.
These are all minor mistakes though, so don’t get discouraged! =)
Cheers man,
p.s- i love your tutorials!!
Hope to see you make more legendary ones!!
here is my video:
http://www.facebook.com/photo.php?v=10150349835192113&set=vb.132831550129151&type=2&theater
the materials doesn`t change
here is my script:
import bpy
import random
class random_mat_panel(bpy.types.Panel):
bl_idname = “matPanel”
bl_label = “Random Material Assigner”
bl_space_type = ‘VIEW_3D’
bl_region_type = ‘Tools’
def draw(self, context):
layout = self.layout
layout.operator(“object.random_material”)
class mat_assigner(bpy.types.Operator):
bl_idname = “object.random_material”
bl_label = “Assign Random Material”
bl_description = “Use this to assign a random material”
bl_options = {‘REGISTER’, ‘UNDO’}
searchString = bpy.props.StringProperty(name=”Filter”)
seedProp = bpy.props.IntProperty(name=”Seed”)
availableMaterials = []
def invoke(self, context, event):
self.randomize()
return {‘FINISHED’}
def check(self, context):
self.randomize()
def randomize(self):
random.seed(self.seedProp)
self.availableMaterials = []
for mat in bpy.data.materials:
if(self.searchString.lower() in mat.name.lower()):
self.availableMaterials.append(mat)
for ob in bpy.context.selected_objects:
randNum = random.randint(0, len(self.availableMaterials)-1)
active_material = self.availableMaterials[randNum]
def execute(self, context):
return {‘FINISHED’}
bpy.utils.register_class(mat_assigner)
bpy.utils.register_class(random_mat_panel)
Really helpful Tutorial , Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you Thank you