Random Material Viewport Color Script

Hi All,
I wanted to know if somebody knows any script to assign random colours to the “Material Viewport Color” in Cycles.
Thanks in advance.

Hi All,
Here you have the addon I was requesting above (Phyton is fun!)
Thanks again to all the people that helped me in this thread (specially to Atom) and please let me know in the comments if you have any problem with it.

Link to the addon:
https://drive.google.com/file/d/0B-gDxEywD9nfQ0dHQVRpMVExTlE/edit?usp=sharing

Cheers!

there was one script at blender org wiki
or in script thread !

but not certain if it did only viewport color or mat color!

happy bl

Thanks RickyBlender, but I haven’t been able to find the script you were saying neither something similar to the one I was asking about…
The closest one I found was this one, but doesn’t does what I want: wiki.blender.org/index.php/Extensions:2.6/Py/Scripts/System/Gyes

Thanks again!

Oh we can make one right? It can’t be that hard.
If we look at the tool tip of the Cycles viewport color we see that it is the diffuse_color from a Blender Internal material.


import bpy, random


def randFloat(a, b):
    return random.random()*(b-a)+a


for ob in bpy.data.objects:
    for mat_slot in ob.material_slots:
        if mat_slot != None:
            mat = mat_slot.material
            mat.diffuse_color = (randFloat(0.2,1.0),randFloat(0.2,1.0),randFloat(0.2,1.0))


Hi Atom,

That’s exactly what I needed, thanks a lot!
It would be cool to have it as a button :wink:

Thanks a lot again Atom!

Hi All,

I have been trying to create a button in the Material Panel that simply applies the code Atom wrote.
My python abilities are 0, but I think I must not be that far away.

If somebody wants to have a look, here is the code I have so far (not working)

Cheers!

# 
# special thanks to user blenderartists.org Atom  
# 
#
# Show Information About the Blend.
# ***** BEGIN GPL LICENSE BLOCK *****
#
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# ***** END GPL LICENCE BLOCK *****


bl_info = {
    "name": "Cycles Random Material Viewport Color",
    "author": "Atom, carlos-mazon",
    "version": (0,1),
    "blender": (2, 7, 0),
    "api": 44136,
    "location": "",
    "description": "Automatic random material viewport colors button",
    "warning": "beta",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Materials"}



import bpy

#
#    Menu in window region, material context
#

class MaterialPanel(bpy.types.Panel):
    bl_label = "Random Material Viewport colors"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "material"
 
    def draw(self, context):
        self.layout.operator("rand.mat.col", text="Random Material Viewport Color")
    
    #end draw
 

class RandMatViewCol(bpy.types.Operator):
    bl_idname = "rand.mat.col"
    bl_label = "Random Material Viewport Color"
   
def randFloat(a, b):
    return random.random()*(b-a)+a

    for ob in bpy.data.objects:
        for mat_slot in ob.material_slots:
            if mat_slot != None:
                mat = mat_slot.material
                mat.diffuse_color =     (randFloat(0.2,1.0),randFloat(0.2,1.0),randFloat(0.2,1.0))

  
   #end invoke

 
bpy.utils.register_module(__name__)


If you are not aware of the Templates menu in the text editor check it out. There are a lot of simple example code to get new users up and running.

I took the Simple Panel template and the Simple Operator template and installed the random color code to make a button that appear under the Material context. I’ll leave it to you to alter the code to change the label on the button.


import bpy,random

def randFloat(a, b):
    return random.random()*(b-a)+a
                
def main(context):
    for ob in bpy.data.objects:
        for mat_slot in ob.material_slots:
            if mat_slot != None:
                mat = mat_slot.material
                mat.diffuse_color =     (randFloat(0.2,1.0),randFloat(0.2,1.0),randFloat(0.2,1.0))

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"

    @classmethod
    def poll(cls, context):
        return context.active_object is not None

    def execute(self, context):
        main(context)
        return {'FINISHED'}

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    #bl_label = "Hello World Panel"
    #bl_idname = "OBJECT_PT_hello"
    #bl_space_type = 'PROPERTIES'
    #bl_region_type = 'WINDOW'
    #bl_context = "object"
    
    bl_label = "Random Material Viewport colors"
    bl_space_type = "PROPERTIES"
    bl_region_type = "WINDOW"
    bl_context = "material"

    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.operator("object.simple_operator")

def register():
    bpy.utils.register_class(SimpleOperator)
    bpy.utils.register_class(HelloWorldPanel)

def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)
    bpy.utils.unregister_class(SimpleOperator)

if __name__ == "__main__":
    register()

Edited the first post with the link to download the addon. Hope it helps somebody!

PS how can is switch it back easily, im not really formiliar with this. Know i need to do this manual for each mesh right?

Hi rombout,

There is no way to switch it back apart from doing an “undo”.
This script is useful if you are trying to get an easy colour pass in cycles like shown in this tutorial: http://www.carlos-mazon.com/blog/sl-kindergard-tutorial/ (search for the word “Random Material Viewport” to go directly to that section)

Cheers!

Yes thats why i came here :slight_smile: I later read that this is undable. Though the script shows different text than in the tutorial i got what i wanted. Perhaps this could come in handy when i will use cycles more. For now i use Thea Render only :slight_smile:

PS thanks for the quick response

hi. it seems to take a long way coming here in 2021. can i ask you to make this addont working with 2.93 or even better v3 blender? i really ned random colors for materials and objects (selected) to make additional passes usable in photoshop.

1 Like

I could use that too, je je Im looking all over the place for an addon that does just that, no luck so far tho.