Trying to make a simple script to change the viewport display colors of a selection of objects. (I like to keep track of things visually with color codes on objects.)

The script below will change the 'material color' of multiple objects, but will only change the Viewport Display color of the last selected object, which I've sussed out to be 'object color'

(Thanks for any tips, not much of a programmer at all)

import bpy

# Iterate over all the selected objects
for o in bpy.context.selected_objects:
    # Set the active materials diffuse color to the specified RGB
    o.active_material.diffuse_color = (0.8, 0.1, 0.0, 1)
    bpy.context.object.color = (0.8, 0.1, 0.0, 1)

1 Answer 1


It's even simpler than that ! Replace the last line with :

o.color = (0.8, 0.1, 0.0, 1)

FYI, the for loop iterates over all selected objects in the viewport, pretty straightforward.

Then the second line changes the material viewport display color. This is what you see when you set the Solid view mode to "Material". Be careful though, if your object doesn't have any active material, this will throw an error and terminate the script not so gracefully.

enter image description here

The last line is supposed to change the object viewport color, what you see when you set the mode to "Object".

Your script only takes the active object in the viewport, the one with the yellow outline. You rather want to change the color of each selected object, noted o inside the for loop.

  • 1
    $\begingroup$ Thank you! That did the trick. I had a feeling I was making it harder than it needed to be. Thanks for breaking it down for me too. Really learned something here. $\endgroup$ Commented Feb 15, 2021 at 16:48
  • $\begingroup$ Now I see...so 'o' is current index of the array of currently selected objects...I could change just about any property en masse this way? This is good stuff. I want to try and expand this into a little organizational tool that allows assigning and selecting coded mesh colors. I'm a 3DS Max convert, and I used to do a lot of organizing with mesh colors...made it easy to visually organize yourself. $\endgroup$ Commented Feb 15, 2021 at 20:28
  • $\begingroup$ Well it is not the index per se, it is actually the object as it is represented in the software memory. for loops in python are a bit different than in other languages you might be familiar with, you iterate over the actual list rather than iterating the index and looking it up in the list. With that you can get access to many of its properties. See the docs for which ones for instance. I recommend following a crash course in python to get familiar with the basic semantics of the language, you don't need much to automate things :) $\endgroup$
    – Gorgious
    Commented Feb 15, 2021 at 22:01
  • $\begingroup$ Also for instance you can tweak the script to automatically color the objects containing a certain tag in their name, or in a certain collection with a simple if "my_tag" in o.name: just after the for loop $\endgroup$
    – Gorgious
    Commented Feb 15, 2021 at 22:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .