11
$\begingroup$

I want to be able to change the background color of a property ui.

At the moment it does:

  • Green = has an Fcurve
  • Yellow = has a keyframe
  • Purple = has a Driver

I want to to be able to change the color of the background depending my own conditions. IE I don't want to change the color theme, I want to add a new color and define its condition.

For Example: If a property is above 500 it changes to red!

Is this possible?

$\endgroup$

1 Answer 1

16
$\begingroup$

The colors are controlled by C code, you can't set it to arbitrary colors with python.

However, you can change the background to alert color (=red) by setting the alert property on the layout block:

Panel - layout alert state

Blender 2.8+

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout

        obj = context.object
        if obj is None: return

        col = layout.column()
        col.alert = True
        col.label(text="Alert state NOT supported:")

        box = col.box()
        box.label(text="Labels", icon="X")
        box.menu("VIEW3D_MT_object", text="Menus")

        col.separator()
        col.label(text="Supported:")
        box = col.box()
        box.operator("mesh.primitive_cube_add", text="Operators (Buttons)")
        box.prop(obj, "name", text="Many properties")
        box.prop(obj, "location", index=1, text="Vector props: only single")
        box.label(text="also with emboss=False, but...")
        box.prop(obj, "name", text="read-only?!", emboss=False)
        box.label(text="template_list() sort of (needs materials):")
        box.template_list("UI_UL_list", "foobar", obj.data, "materials", obj, "active_material_index")

def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()

Blender 2.7x

import bpy


class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        layout = self.layout

        obj = context.object
        if obj is None: return

        col = layout.column()
        col.alert = True
        col.label("Alert state NOT supported:")
        
        box = col.box()
        box.label("Labels", icon="X")
        box.menu("VIEW3D_MT_object", text="Menus")
        box.prop(obj, "layers", text="Not all properties")
        
        col.separator()
        col.label("Supported:")
        box = col.box()
        box.operator("mesh.primitive_cube_add", text="Operators (Buttons)")
        box.prop(obj, "name", text="Many properties")
        box.prop(obj, "location", index=1, text="Vector props: only single")
        box.label("also with emboss=False, but...")
        box.prop(obj, "name", text="read-only?!", emboss=False)
        box.label("template_list() sort of (needs materials):")
        box.template_list("UI_UL_list", "foobar", obj.data, "materials", obj, "active_material_index")

def register():
    bpy.utils.register_class(HelloWorldPanel)


def unregister():
    bpy.utils.unregister_class(HelloWorldPanel)


if __name__ == "__main__":
    register()

Run the above code in Text Editor and check out the Object tab.

$\endgroup$
3
  • $\begingroup$ Thank you!. I could use the alert to color my property but how can I change the color from red? $\endgroup$
    – MCHammond
    Apr 24, 2014 at 20:38
  • $\begingroup$ Unlike the other colors for animated and driven properties, the alert color is hardcoded to red. $\endgroup$
    – CodeManX
    Apr 24, 2014 at 21:25
  • $\begingroup$ thank you man .... which c file control colors ? $\endgroup$ May 28, 2016 at 13:25

You must log in to answer this question.

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