# Could I see a simple example of using a color picker in a script?

I've seen a lot of variations of using color pickers, but I want to see just a simple, clear example of being able to bring up a color picker (with a default color set), pick a color, and being able to get the value from that color picker either as a return value after it's picked, or being able to access that value at another time.

One resource that has helped me some is this answer on another SE:Blender question. In that case, the script provided 5 color pickers. I edited it some, both to help me understand it and to make it work as just a single color picker:

import bpy

class ColorItem(bpy.types.PropertyGroup):
color = bpy.props.FloatVectorProperty(
name = "Color Picker",
subtype = "COLOR",
size = 4,
min = 0.0,
max = 1.0,
default = (1.0,1.0,1.0,1.0)
)

class ColorPanel(bpy.types.Panel):
"""Creates a Panel in the Scene properties window"""
bl_label = "Colors"
bl_idname = "SCENE_PT_colors"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"

def draw(self, context):
row = self.layout.row()
row.prop(context.scene.colors[0], "color", text='Pick Color')

def register():
bpy.utils.register_class(ColorPanel)
bpy.utils.register_class(ColorItem)

def unregister():
bpy.utils.unregister_class(ColorPanel)
bpy.utils.unregister_class(ColorItem)

if __name__ == "__main__":
register()
bpy.types.Scene.colors = bpy.props.CollectionProperty(type=ColorItem)


While just a simple example would provide me with what I need (again, with me being able to set the default color and being able to get the color value either as a return value or to get it when I need it), there are parts in this script I don't understand and would like to. Also, on that 2nd to last line, I'm not sure just how the object creation is done. By passing "type=ColorItem" to bpy.props.CollectionProperty(), is it returning an object of that class? (I'm assuming it does, but realize I don't understand all that's going on there.)

Also, on the last line, just what is being added to bpy.context.scene.colors?

I don't see the difference (in the last part) between "Scene" (2nd to last line) and using "scene," but capitalizing it instead of keeping it lower case. There's also the object in the draw subroutine context.scene.colors. Are they the same object?

When I've tried to figure out just what kind of object I'm working with, even with the ColorItem class in this code, I'm not clear how there are 16 of this item, and not just one, and why that class is not just a single floating point vector with 4 parts that makes up the color.

Any explanation of this that would clarify things for me would be a big help.

Just declare a FloatVectorProperty, set its subtype to 'COLOR' and add it to a panel or whatever.

my_color = bpy.props.FloatVectorProperty(
name = "Color Picker",
subtype = "COLOR",
default = (1.0,1.0,1.0,1.0)
size = 4
)


In order to access the values of the property, you would have to create an Operator. Minimal working example, adds a new panel to the render settings, allows to pick a color as well as printing the values to the console. Further reading: How to create a custom UI?

import bpy

class SimpleOperator(bpy.types.Operator):
"""Tooltip"""
bl_idname = "object.simple_operator"
bl_label = "Simple Object Operator"

def execute(self, context):
clr = context.scene.mytool_color
print (clr[0], clr[1], clr[2], clr[3])
return {'FINISHED'}

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 = "render"

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

row = layout.row()
row.prop(context.scene, "mytool_color")
row = layout.row()
row.operator(SimpleOperator.bl_idname)

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

# Register the property per Scene, Object or whatever
bpy.types.Scene.mytool_color = bpy.props.FloatVectorProperty(
name = "Color Picker",
subtype = "COLOR",
size = 4,
min = 0.0,
max = 1.0,
default = (1.0,1.0,1.0,1.0))

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

del bpy.types.Scene.mytool_color

if __name__ == "__main__":
register()

• Thank you! Just tested it and can't wait to look it over more closely and parse the code in it so I know just what I'm doing! (Gotta get out and do a few things and when I get back, I'll have time to go over it closely.) – Tango Mar 8 at 12:09