0
$\begingroup$

I want to assign arbitrary data in the form of custom properties to a category of objects within my scene.

For the sake of this question, let's say that the arbitrary data is comprised of several named custom properties ranging from 0.0 to 255.0 in value, effectively several bytes of data.

Next I want to build a custom panel that will facilitate visualising and editing specific bits of an objects arbitrary data.

I've not had much luck finding an example of how to achieve this.

To elaborate on the question with some code, I know I can do this kind of thing:

class BM_PT_block_inspector(bpy.types.Panel):
    bl_idname = "BM_PT_block_inspector"
    bl_label = "Block"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

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

        # toggle a pre-existing setting
        self.layout.row().prop(obj, "show_wire", toggle=True, text="Wireframe", icon="SHADING_WIRE")

        # show custom property values directly in panel
        self.layout.row().prop(obj, '["my_prop_0"]')
        self.layout.row().prop(obj, '["my_prop_1"]')

but I would like to be able to render UI elements like checkboxes in the panel that will essentially act as toggles for specific bits of the data stored in the active object's custom properties.

$\endgroup$

1 Answer 1

0
$\begingroup$

This can be achieved with custom property getters/setters.

Use custom getters/setters to read/write to the desired data instead of the default behaviour.

def get_array(self):
    mc = self["blk_flags"]
    return list (map (lambda i: ((mc >> i) & 1) > 0, range (8)))

def set_array(self, values):
    mc = 0
    for i in range (8):
        if values[i]: mc |= (1 << i)
    self["blk_flags"] = mc

class BMBlockSettings(PropertyGroup):
    blk_flags : IntProperty (name="Bit Flags")
    blk_arr : BoolVectorProperty(name="Toggles", size=8, get=get_array, set=set_array)

class BM_PT_block_inspector(bpy.types.Panel):
    bl_idname = "BM_PT_block_inspector"
    bl_label = "Block"
    bl_space_type = 'PROPERTIES'
    bl_region_type = 'WINDOW'
    bl_context = "object"

    def draw(self, context):
        self.layout.row().prop(context.object.bm_block, "blk_arr")

def register ():
    bpy.types.Object.bm_block = PointerProperty(type=BMBlockSettings)

def unregister():
    del bpy.types.Object.bm_block

$\endgroup$

You must log in to answer this question.

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