Add random vertex color to individual subobjects

Hi all.

Lately I’ve faced a slight problem everyone who works with archviz probably know about - one does not simply get a parameter variation via random node in case of using Array modifier or after converting the particle system into single mesh. So the things like roof tiling, especially ones with complex slope geometry and all that stuff becoming just a pain a the… well, you know. If you using the flat elements, you have a solution - the addon called Random Vertex Colors will alternate the vertex color of an individual faces, so you can just use ngons on that and be cool. But if you’re using more complex elements like wavy roof tiles, or have a tree or a grass model with all the leaves / grass stems joined in single mesh this won’t help. So I decided to give a try and make something myself.

Important: I’m not a real coder, so it’s the best I can do after a couple of hours. Solution is pretty straightforward and dumb, but it works. If you see a possibility to make it better - please do not hesistate to do so!

# ADD RANDOM VERTEX COLOR TO INDIVIDUAL SUBOBJECTS 

import bpy
from random import random

for i in range (0, 100):
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.select_all(action='DESELECT')
    bpy.ops.mesh.select_random(percent=0.5, action='SELECT')
    bpy.ops.mesh.select_linked()
    bpy.ops.object.mode_set(mode='VERTEX_PAINT')
    bpy.context.object.data.use_paint_mask = True
    color=(random(), random(), random())
    bpy.data.brushes["Draw"].color = color
    bpy.ops.paint.vertex_color_set()
    bpy.ops.object.mode_set(mode='EDIT')
    bpy.ops.mesh.hide()
bpy.ops.mesh.reveal ()
bpy.ops.object.mode_set(mode='VERTEX_PAINT')

Here’s the result of running this script on a mesh after applying the Array modifier:

http://s6.postimg.org/is9h4sfod/random_colors.jpg

How Fantastic! Thank you very much, this is exactly what I was needing.
Glenn

Nice work, well done : another solution is maybe this addon https://blenderartists.org/forum/showthread.php?391477-Add-random-vertex-color-to-individual-subobjects:p

That’s a link to this page ???

Sorry , wrong link , https://blenderartists.org/forum/showthread.php?342546-Blender-addon-Assign-random-vertex-colors-to-connected-vertices