Select by rgb color script help

I’ve been using the script below in Blender 2.49. It was posted some time ago by some kind person, but I’m afraid I’ve forgotten where. What it does is allow you to select objects with the same material rgb colour. The problem I’ve got is that firstly even though I’ve put the script file in the scripts folder (C:\Users\XXX\AppData\Roaming\Blender Foundation\Blender\2.57\scripts\addons) I can’t see it in Blender when I go to User Preferences and look under Add-ons.

If I try to run the script under Text Editor then it highlights the line 'from Blender import Redraw ’ in red.

What do I need to change to get this script working?

I’m using Blender 2.57 and Windows 7.

Thanks in advance.

#!BPY
“”"
Name: ‘Select Colour’
Blender: 249
Group: ‘Add’
Tooltip: ‘Selects objects with the same RGB colour’
“”"

import bpy
from Blender import Redraw
from Blender.Draw import PupMenu as P

def main():
sce = bpy.data.scenes.active
ref_ob = sce.objects.active
if not ref_ob: P(“Error%t|No active object”);return
if ref_ob.type != “Mesh”: P(“Error%t|Active object is no mesh”);return
if len(sce.objects.selected)>1: P(“Error%t|More than 1 object selected”);return
ref_mat = ref_ob.getMaterials()
if not ref_mat:
me = ref_ob.getData(False, True)
ref_mat = me.materials
if not ref_mat[0]: P(“Error%t|Mesh has no material”);return
ref_mat_col = [mat.getRGBCol() for mat in ref_mat]

obs = [ob for ob in sce.objects if ob.type == “Mesh”]
for ob in obs:
mat = ob.getMaterials()
if not mat: mat = ob.getData(False,True).materials
if mat[0]:
mat_col = [mat.getRGBCol() for mat in mat]
if mat_col == ref_mat_col: ob.sel = True
sce.objects.active = ref_ob
Redraw()

main()

that is an old script, it won’t run in 2.5 or newer…
here is a short one to select by diffusse color


import bpy
col = bpy.context.object.data.materials[0].diffuse_color
mess = [o for o in bpy.context.scene.objects if o.type == 'MESH']

for obj in mess:
    list = []
    for mat in obj.data.materials:
        try: list.append(mat.diffuse_color)
        except: pass
    obj.select = col in list

this is a select by material that I guess can be more useful…?


import bpy
mat = bpy.context.object.material_slots.data.active_material

for obj in bpy.context.scene.objects:
    try: sel = mat in obj.data.materials[:]
    except: sel = False
    obj.select = sel

very simple scripts, run from a text editor with a selected object

edit: improved scripts a bit…

Liero, these scripts are great and just what I was looking for. The reason I find it useful is I’m importing models from elsewhere that get imported as hundreds of separate objects (spheres, cylinders etc), but frustratingly each object is assigned its own material even when the rgb and everything else about the material is actually identical. Being able to select by colour means that I can link them all and assign a single material, without the tedious and impractical job of selecting them all individually.

Thank you so much.

Liero, they are just what I wanted. Thank you so much for taking the time to do these.