2
$\begingroup$

I am trying to create a type bpy.types.UILayout.template_palette in my bpy.type.Panel.

In my bpy.type.Panel I am unsure what object I need to pass to the first argument called data.

I think it needs an object with a property of type bpy.types.Palette. I am unsure what is a valid object that can be passed as AnyType and have a bpy.types.Palette property.

I have figured out how to make a bpy.types.Palette using bpy.ops.palette.new().

I cant find a bpy.props function for adding a bpy.types.Palette property

$\endgroup$

1 Answer 1

4
$\begingroup$

To use the UILayout.template_palette you need data that has a property of type bpy.types.Palette

Here is a small test to add a new custom palette, assign it a couple of colors, yellow and red, set red as active.

The image paint tool settings palette property is assigned the new palette.

The palette is displayed in the image editor toolbar under the "misc" tab.

enter image description here

import bpy

class PaletteDemoPanel(bpy.types.Panel):
    """Creates a Panel in the tool panel of image editor"""
    bl_label = "Palette Demo"
    bl_idname = "IMAGE_PT_palette"
    bl_space_type = 'IMAGE_EDITOR'
    bl_region_type = 'TOOLS'

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

        ts = context.tool_settings
        if ts.image_paint.palette:
            layout.template_palette(ts.image_paint, "palette", color=True)


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


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


if __name__ == "__main__":
    register()

    # put some test code here to create palette etc
    # add a new pallete
    pal = bpy.data.palettes.get("CustomPalette")
    if pal is None:
        pal = bpy.data.palettes.new("CustomPalette")
        # add a color to that palette
        yellow = pal.colors.new()
        yellow.color = (1, 1, 0)
        yellow.weight = 1.0

        red = pal.colors.new()
        red.color = (1, 0, 0)

        # make red active
        pal.colors.active = red

    ts = bpy.context.tool_settings   
    ts.image_paint.palette = pal
$\endgroup$

You must log in to answer this question.

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