Vertex Colors and Python

Using this reference here, since it works. It applies vertex colors randomly.(latest blender version 2.69)

I’m trying to get random colors but all from the blue channel right now.
When I change the line:
vertexColor[i].color = rgb

to

vertexColor[i].color = (0, 0, 1)
obviously it can’t be random variations of blue, because it is set at a specific number.

I think it would be done something like this:
faces[0].col[0].b = .2
faces[0].col[1].b = .5
faces[0].col[2].b = .9
…but I can’t find the new way to set this up.

*Another story, but then I want to be able to blend/add green and red channels that are random also or set by some sort of masking system.

Appreciate any help thx…

Great, although I cannot seem to test your script in the last post on that thread to see the effect.
I’ve done some some basic coding in unreal 3. I find python to be more confusing even with the API reference.
I’ve only looked at python a day or so ago so maybe it will click when I can get some read time in…

Added bl_info, so it’s possible to install as addon

bl_info = {
    "name": "Random Vertex Colors",
    "author": "CoDEmanX",
    "version": (1, 0),
    "blender": (2, 65, 0),
    "location": "View3D > Vertex Paint mode > Paint > Random Vertex Colors",
    "description": "Assign random vertex colors per Loop, Edge or Face",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Mesh"}
    

import bpy
from random import random

def main(self, context):

    me = context.object.data
    vcol_data = me.vertex_colors.active.data

    if self.random_per == 'LOOP':
        
        for l in me.loops:
            vcol_data[l.index].color = random(), random(), random()

    elif self.random_per == 'VERT':
        
        vert_loop_map = {}
    
        for l in me.loops:
            try:
                vert_loop_map[l.vertex_index].append(l.index)
            except KeyError:
                vert_loop_map[l.vertex_index] = [l.index]
        
        for vertex_index in vert_loop_map:
            color = random(), random(), random()
            for loop_index in vert_loop_map[vertex_index]:
                vcol_data[loop_index].color = color
    
    # per EDGE isn't possible, there are no distinct edge loops
    
    else: # == 'FACE'
        
        for p in me.polygons:
            color = random(), random(), random()
            for loop_index in p.loop_indices:
                vcol_data[loop_index].color = color
                
    me.update()


class MESH_OT_vertex_color_random(bpy.types.Operator):
    """Assign random vertex colors"""
    bl_idname = "mesh.vertex_color_random"
    bl_label = "Random Vertex Colors"
    bl_options = {'REGISTER', 'UNDO'}
    
    random_per = bpy.props.EnumProperty(name="Random per",
                   items=(('LOOP', "Loop", "Give every loop a random color"),
                          ('VERT', "Vertex", "Give every vertex a random color"),
                          ('FACE', "Face", "Give every face a random color"))
               )

    @classmethod
    def poll(cls, context):
        return (context.object is not None and
                context.object.type == 'MESH' and
                context.object.data.vertex_colors.active is not None)

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


def draw_func(self, context):
    self.layout.operator(MESH_OT_vertex_color_random.bl_idname)
    

def register():
    bpy.utils.register_class(MESH_OT_vertex_color_random)
    bpy.types.VIEW3D_MT_paint_vertex.append(draw_func)


def unregister():
    bpy.utils.unregister_class(MESH_OT_vertex_color_random)
    bpy.types.VIEW3D_MT_paint_vertex.remove(draw_func)


if __name__ == "__main__":
    register()

Save the code to e.g. mesh_vertex_color_random.py and install via File > User preferences > Addons > Install from file…

Tick the checkbox on the right to enable it.

You’ll find a new menu entry in Vertex Paint mode in the 3D View, Paint menu,

Ok, so it worked before when I ran the script and it placed the option like you mentioned in the menu.
So to do a random only blue or only red or only green, I will have to use a number or some type of range value that covers the part of the blue spectrum that is needed. What would be the best way.
for ex. color = (0, 0, .1)
color = (0, 0, .3)
color = (0, 0, .6)
Something along that line or maybe the colors can be added like the color hex values in the script?? IDK yet!

You find 3x random(), random(), random() in my script, which is used to generate the random color.

It stands for red, green, blue.

I replaced the inline code for random rgb by a function call, so it’s easier to replace the code (random_color function):

bl_info = {
    "name": "Random Vertex Colors",
    "author": "CoDEmanX",
    "version": (1, 0),
    "blender": (2, 65, 0),
    "location": "View3D > Vertex Paint mode > Paint > Random Vertex Colors",
    "description": "Assign random vertex colors per Loop, Edge or Face",
    "warning": "",
    "wiki_url": "",
    "tracker_url": "",
    "category": "Mesh"}
    

import bpy
from random import random

def random_color():
    return 0, 0, random()


def main(self, context):

    me = context.object.data
    vcol_data = me.vertex_colors.active.data

    if self.random_per == 'LOOP':
        
        for l in me.loops:
            vcol_data[l.index].color = random_color()
    elif self.random_per == 'VERT':
        
        vert_loop_map = {}
    
        for l in me.loops:
            try:
                vert_loop_map[l.vertex_index].append(l.index)
            except KeyError:
                vert_loop_map[l.vertex_index] = [l.index]
        
        for vertex_index in vert_loop_map:
            color = random_color()
            for loop_index in vert_loop_map[vertex_index]:
                vcol_data[loop_index].color = color
    
    # per EDGE isn't possible, there are no distinct edge loops
    
    else: # == 'FACE'
        
        for p in me.polygons:
            color = random_color()
            for loop_index in p.loop_indices:
                vcol_data[loop_index].color = color
                
    me.update()


class MESH_OT_vertex_color_random(bpy.types.Operator):
    """Assign random vertex colors"""
    bl_idname = "mesh.vertex_color_random"
    bl_label = "Random Vertex Colors"
    bl_options = {'REGISTER', 'UNDO'}
    
    random_per = bpy.props.EnumProperty(name="Random per",
                   items=(('LOOP', "Loop", "Give every loop a random color"),
                          ('VERT', "Vertex", "Give every vertex a random color"),
                          ('FACE', "Face", "Give every face a random color"))
               )

    @classmethod
    def poll(cls, context):
        return (context.object is not None and
                context.object.type == 'MESH' and
                context.object.data.vertex_colors.active is not None)

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


def draw_func(self, context):
    self.layout.operator(MESH_OT_vertex_color_random.bl_idname)
    

def register():
    bpy.utils.register_class(MESH_OT_vertex_color_random)
    bpy.types.VIEW3D_MT_paint_vertex.append(draw_func)


def unregister():
    bpy.utils.unregister_class(MESH_OT_vertex_color_random)
    bpy.types.VIEW3D_MT_paint_vertex.remove(draw_func)


if __name__ == "__main__":
    register()

0, 0, random() will give blue variations, although they tend to be very dark. You probably want cyan, reddish blue, light blue etc., but no colors close to black.

Here’s a bit hacky but practical code snippet to generate variations of (perceived) blue:

def random_color():
    
    if random() > 0.6:
        r = uniform(0.0, 0.3)
        g = random()
        b = uniform(max(g, r+g+0.2), 1.0)
    else:
        r = uniform(0.0, 0.1)
        g = uniform(0.0, 0.2)
        b = uniform(0.4, 1.0)
            
    return r, g, b

Thx, for the help. You make it look easy and are the codemasterman! I was wanting to use it for this type of thing here.
http://www.bing.com/images/search?q=plant+vertex+colors&FORM=HDRSC2#view=detail&id=5D9A63CDAEBEBA8C600BCD31F6BE3E7E88F806AE&selectedIndex=0

It adds red to the outer surface of a leaf. Then adds blue. Then green and blends them rgb(a-black) together.
I know they use splines now but. I like to use both and it does take forever by hand. Especially a many leaved tree.
I fig. just to group certain leaves as objects to get some randomness. Easy to group when modeling by hand but generated trees with many leaves well would be a slight problem but could still group them somehow:/.

Actually something like this makes even more sense…Perfect!!..and could be done in Blender 3D. Not by me don’t have time right now!
http://www.mj-multimedia.net/2012/10/maxscript-random-vertice-painter-for-3ds-max/

Something like this would work. http://www.blendernation.com/2013/01/31/add-on-assign-random-materials-to-mesh-faces/

…but all connected faces to form one leaf would be needed along with a Photoshop painted type of mask for each layer. Then could bake the material to vertex color. Would work that way with a vertex way but rgb masks would be needed(probably a gradient also)…or some fancy coding!