Here is condensed heavily edited sample add-on an artist requested t use blender as the middleman to mass convert vertex colors data from custom data source, the latter being removed as its not related to the question.
The color value was selected using FloatVectorProperty, specifically with subtype COLOR. This also has the advantage that if exposed through the UI is visualised by the nice UI Color wheel/hex-widget.
The code is split into properties.py and ui.py as this is done for ease of maintainance and separation of concerns, ie storage and display. You can store the custom property anywhere you want I choose object as per the question, the same applies for where the ui appears.
You can also combine it into one script, just need to change registration from module to class and supply each class name.
I generally use a properties group when working with multiple properties, but you can just define a single custom property to attach.
properties.py
import bpy
from bpy.props import FloatVectorProperty, StringProperty, PointerProperty
class ObjectProperties(bpy.types.PropertyGroup):
@classmethod
def register(cls):
bpy.types.Object.custom = PointerProperty(
name="Custom properties",
description="Custom Properties for Objects",
type=cls,
)
#Add in the properties you want
cls.hexwidget = FloatVectorProperty(name="Hex Value",
subtype='COLOR',
default=[0.0,0.0,0.0])
cls.test_string = StringProperty(name="Test String",
default="I am a string")
@classmethod
def unregister(cls):
del bpy.types.Object.custom
def register():
bpy.utils.register_class(ObjectProperties)
def unregister():
bpy.utils.unregister_class(ObjectProperties)
This module has specific registration handling as we prefer to get rid of custom info if the addon is not install, design decision.
Here is the UI which will display the value stored in the defined property
ui.py
import bpy
class UtilityPanel(bpy.types.Panel):
bl_label = "Utilites Panel"
bl_idname = "OBJECT_PT_utilities"
bl_space_type = "PROPERTIES"
bl_region_type = "WINDOW"
bl_context = "object"
def draw(self, context):
layout = self.layout
box = layout.box()
row = box.row()
row.prop(context.active_object.custom, "hexwidget")
row = box.row()
row.prop(context.active_object.custom, "test_string")
__init__.py
for completeness
#--- ### Header
bl_info = {
"name": "Custom Property: Color",
"category": "Object",
}
import bpy
import bpy.props
from <folder_name> import ui, properties
def register():
properties.register()
bpy.utils.register_module(__name__)
def unregister():
properties.unregister()
bpy.utils.unregister_module(__name__)
if __name__ == "__main__":
register()
When enabled the add-on adds a custom propertygroup to each Object, which can be altered or expanded as you need.
