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)

You must be logged in to upload images. Register

Discussion

33 Responses to “Scripting a Random Material Assigner”
  1. Posts: 71

    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.

    #
    1
    Jul 19, 2011 at 7:12 am
  2. Posts: 256

    This looks interesting. Downloading it now. Always glad to see more python tutorials!

    #
    2
    Jul 19, 2011 at 7:16 am
    • Posts: 256

      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!

      #
      2.1
      Jul 20, 2011 at 10:11 am
      • Posts: 225

        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. ;)

        #
        2.1.1
        Jul 20, 2011 at 11:09 am
  3. CMITZ
    Posts: 14

    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 :P

    Whazaa, I’m vague again xD

    Anyway, Thanks!

    #
    3
    Jul 19, 2011 at 8:13 am
  4. Posts: 43

    Good tutorial. Makes python scripting in Blender very accessible.

    #
    4
    Jul 19, 2011 at 8:15 am
  5. Kris K
    Posts: 8

    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

    #
    5
    Jul 19, 2011 at 10:14 am
    • Posts: 225

      Hey Kris,

      You’ll need to use accolades (curly brackets) for the bl_options. So bl_options = {‘REGISTER’, ‘UnDO’}

      Good luck!
      Patrick

      #
      5.1
      Jul 19, 2011 at 10:24 am
    • Posts: 225

      Oops, I missed your second question there. You’ll need to capitalize Panel so it says bpy.types.Panel

      #
      5.2
      Jul 19, 2011 at 10:25 am
      • Kris K
        Posts: 8

        Thanks

        #
        5.2.1
        Jul 19, 2011 at 11:19 am
  6. FreeMind
    Posts: 45

    More python tutorials! :)

    #
    6
    Jul 19, 2011 at 10:58 am
  7. Posts: 1

    BlenderCookie,
    Thanks for finding this python wizard (Patrick Boelens) who shares his Python/ Blender knowledge with the rest of us.

    Thanks Patrick!

    #
    7
    Jul 19, 2011 at 11:28 am
  8. Posts: 19

    this tutorial worked when i was using 2.56 but when i used 2.58a, it wouldnt work :(

    #
    8
    Jul 19, 2011 at 2:16 pm
  9. Sandra Gilbert
    Posts: 8

    Excellent tutorial. I already have a great use for this tutorial.

    #
    9
    Jul 20, 2011 at 1:10 am
  10. Posts: 3

    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 :D

    #
    10
    Jul 20, 2011 at 6:15 am
    • Posts: 225

      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. =)

      #
      10.1
      Jul 20, 2011 at 11:13 am
  11. Tom
    Posts: 6

    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.

    #
    11
    Jul 20, 2011 at 10:29 am
  12. Posts: 8

    Thanks for adding more Blender Python tutorials. ^^

    #
    12
    Jul 21, 2011 at 4:56 pm
  13. lokoi
    Posts: 4

    How to run Blender in mode where you can see prints in console?

    #
    13
    Jul 22, 2011 at 11:39 am
    • lokoi
      Posts: 4

      on linux

      #
      13.1
      Jul 22, 2011 at 11:44 am
      • Posts: 256

        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?

        #
        13.1.1
        Jul 23, 2011 at 7:08 am
      • Posts: 225

        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! =)

        #
        13.1.2
        Jul 23, 2011 at 9:18 am
  14. Posts: 1

    Very useful! Thank you!

    #
    14
    Jul 29, 2011 at 7:59 am
  15. Brock
    Posts: 8

    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 :)

    #
    15
    Jul 31, 2011 at 1:37 am
    • Posts: 225

      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! =)

      #
      15.1
      Jul 31, 2011 at 2:00 pm
      • Brock
        Posts: 8

        Cheers man,

        p.s- i love your tutorials!!
        Hope to see you make more legendary ones!! :D

        #
        15.1.1
        Aug 1, 2011 at 2:12 am
  16. Posts: 2

    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)

    #
    17
    Mar 3, 2012 at 3:20 pm
  17. abdoo47
    Posts: 1

    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 :)

    #
    18
    Feb 14, 2013 at 11:20 pm

Leave a Comment

You must be logged in to post a comment.