0
$\begingroup$

I have some meshes in the scene (7 mesh objects for example). I want to assign each object a color. The color should vary in a specific scale. For example: green scale. So, first object has green value 0.1, second 0.2 and so on. So in the end, I can able to see a pattern of color.

All this I want as a BoolProperty checkbox button in my UI Panel. So, when I check it, it should show me colors and vice versa.

def switch(self, context):
   bool = context.scene.bool
   objects = bpy.data.objects
   mat = bpy.data.materials['Material']
   if bool == True:
       print ("True")
       for ind, object in enumerate(bpy.context.scene.objects):
           mat = bpy.data.materials.new("BSDF")
           val = 0.0
           mat.diffuse_color = (float(val), 1, float(val))
           object[ind].active_material = mat 
           val += .1

    else:
        for ind, object in enumerate(bpy.context.scene.objects):
            object[ind].active_material.delete()

This is what I want to achieve but it's not working.

$\endgroup$
2
  • $\begingroup$ Object.active_material.delete() ? $\endgroup$ – batFINGER Feb 3 '17 at 15:05
  • $\begingroup$ I don't know, whether it is correct or wrong. I just want to give an idea of what I want to do. Basically, I want a switch check box that will turn on off the color of objects. $\endgroup$ – mGm Feb 3 '17 at 15:24
1
$\begingroup$

You only want to iterate thru mesh (or meta i suppose) objects that can have a material. Here is a sample, changing scene.bool removes material if False, or adds a new material to each object with a differing shade of green.

import bpy
from bpy.props import BoolProperty

def toggle_bool(self, context):

    val = 0.0
    for o in self.objects:
        if o.type not in {'MESH', 'META'}:
            continue

        if self.bool:
            print(o, val)
            mat = bpy.data.materials.new("BSDF")
            mat.diffuse_color = (val, 1, val)

            o.active_material = mat
            val += 0.1
        else:
            o.active_material = None

bpy.types.Scene.bool = BoolProperty(update=toggle_bool)
$\endgroup$
1
  • $\begingroup$ Is it possible to use another toggle_bool function to inverse the coloring scheme. I mean something like this: {o_1 : 0, o_2 : 0.1, o_3 : 0.3} can be converted to {o_3: 0, o_2 : 0.2, o_1 : 0.3}. Can you briefly comment as well to make it easier to understand. $\endgroup$ – mGm Feb 3 '17 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.