SAFINA3D

RandomColor Script

Hi,
I did a quick script to assign a random color to the objects present in the scene. I also added some functions to make it more 'convenient'. now, you can change the color of one/multiple objects, or inversely, disable single/all colors.

Check out the RandomColor plugin version

Download old RandomColor script



 here you can find source of the main Script RandomColor_All :



""" Set a random color to all objects """
import c4d
from c4d import Vector
from random import randint

def get_random_color():
    """ Return a random color as c4d.Vector """
    def get_random_value():
        """ Return a random value between 0.0 and 1.0 """
        return randint(0, 255) / 256.0
    return Vector(get_random_value(), get_random_value(), get_random_value())

def process(fct):
    """ Loop over the current document objects and apply a function to each one """
    def get_next_object(current_object):
        """ Return the next object in the hierarchy """
        if current_object.GetDown(): return current_object.GetDown()
        while not current_object.GetNext() and current_object.GetUp():
            current_object = current_object.GetUp()
        return current_object.GetNext()

    used_colors_list = []
    obj = doc.GetFirstObject()
    while obj is not None:
        doc.AddUndo(c4d.UNDOTYPE_CHANGE, obj)
        fct(obj, used_colors_list)
        obj = get_next_object(obj)

def set_color(obj, used_colors):
    """ Applay a random color to an object """
    color = get_random_color()
    # Check that the current color is not already used
    while color in used_colors:
        color = get_random_color()
    used_colors.append(color)
    # Apply the chosen color to the object
    obj.SetParameter(c4d.ID_BASEOBJECT_USECOLOR, c4d.ID_BASEOBJECT_USECOLOR_ALWAYS, 0);
    obj.SetParameter(c4d.ID_BASEOBJECT_COLOR, color, 0);

def main():
    doc.StartUndo()
    process(set_color)
    doc.EndUndo()
    c4d.EventAdd()

if __name__=='__main__':
    main()

4 comments

  1. This is very interesting, Thank for your contribution.
    Excuse my poor english, I'm from Spain :)
    Adiós amigo.

    ReplyDelete
  2. not sure if I'm reading this right, but shouldn't it be ls.append(cColor) instead of ls.append(randomColor()), assumming you are trying to get unique values..

    ReplyDelete