1
$\begingroup$

Im having so much trouble using the getter/setter for an EnumProperty. I have my property definition like:

blend_modes = (
    ("REGULAR", "Regular", "", 1),
    ("HARDLIGHT", "Hardlight", "", 2),
    ("ADD", "Add", "", 3),
    ("SUBTRACT", "Subtract", "", 4),
    ("MULTIPLY", "Multiply", "", 5),
    ("DIVIDE", "Divide", "", 6),
)    

blend_mode : bpy.props.EnumProperty(items=blend_modes, name="Blend Mode", 
                                    description="TODO add description", 
                                    default="REGULAR", set=set_blend_mode, get=get_blend_mode)

And the get/set:

def get_blend_mode(self):
    # return "REGULAR"
    # return 1
    # return self.get("blend_mode", "Regular")
    return self.get("blend_mode", "REGULAR")
    # return getattr(self, "blend_mode", "REGULAR")
    # return getattr(self, "blend_mode", ("REGULAR", "Regular", "", 1))
    # return getattr(self, "blend_mode", 1)
    # return getattr(self, "blend_mode", {'REGULAR'})
    # return getattr(self, "blend_mode", None)
    # return getattr(self, "blend_mode", "")
    # return int(getattr(self, "blend_mode", 1))
    # if "blend_mode" in self:
    #     return self["blend_mode"]
    # else:
    #     return 1
    # return self.get("blend_mode", int("REGULAR"))
    # return self.get("blend_mode", 1)
    # return getattr(self, "blend_mode", list(self.blend_modes)[0])
    
    
def set_blend_mode(self,value):
    print(f"{value = }")
    print(f"{type(value) = }") # says its an int
    value2 = "REGULAR"
    if value == 1:
        value2 = "REGULAR"
    elif value == 2:
        value2 = "HARDLIGHT"
    elif value == 3:
        value2 = "ADD"
    elif value == 4:
        value2 = "SUBTRACT"
    elif value == 5:
        value2 = "MULTIPLY"
    elif value == 6:
        value2 = "DIVIDE"
        
    
    print(f"{value = } AFTER")
    
    self["blend_mode"] = value
    # self["blend_mode"] = str(value)
    # self["blend_mode"] = int(value)
    # self["blend_mode"] = value2

But I got an error:

in get_blend_mode
TypeError: 'str' object cannot be interpreted as an integer

The commented code are all the things that I've already tried.

The value in the setter is always an int. Even though, via Blender's console, I can't set the blend_mode property of my object as an int (gives an error). But the setter gets an int. And despite I tried to convert that int to a str with the value on the enum, keeps complaining.

I wasn't able to see any example in the API or anywhere, so I'm pretty lost right now.

$\endgroup$
2
  • $\begingroup$ What are you trying to achieve? Explain in detail. $\endgroup$
    – Karan
    Jul 28, 2023 at 20:45
  • $\begingroup$ Sorry friend, my bad. Your answer helped me to figure out what was actually the problem (I'm opening a new question since for this, your answer is actually the right one). (Apart of that, I was doing nothing more than trying to set the blend_mode for a GreasePencil layer (but seems more complicated). Ill tag you and your code in the new question in case you are curious, and seems that it was the reason of the problem). $\endgroup$
    – Ommadawn
    Jul 29, 2023 at 11:40

1 Answer 1

1
$\begingroup$

Yes, value in the setter of EnumProperty is always an int.

You can use set_blend_mode() with value as str.

Like this:

import bpy
from bpy.types import Panel, PropertyGroup
from bpy.props import *


class CUSTOM_PG_property_group(PropertyGroup):

    def get_blend_mode(self):
        return self.get("blend_mode", "REGULAR")

    def set_blend_mode(self, value):
        if value == 'REGULAR':
            value = 0
        elif value == 'HARDLIGHT':
            value = 1
        elif value == 'ADD':
            value = 2
        elif value == 'SUBTRACT':
            value = 3
        elif value == 'MULTIPLY':
            value = 4
        elif value == 'DIVIDE':
            value = 5

        self["blend_mode"] = value

    blend_mode : EnumProperty(
        name = "Blend Mode",
        description = "TODO add description",
        items = (
            ("REGULAR", "Regular", ""),
            ("HARDLIGHT", "Hardlight", ""),
            ("ADD", "Add", ""),
            ("SUBTRACT", "Subtract", ""),
            ("MULTIPLY", "Multiply", ""),
            ("DIVIDE", "Divide", ""),
        ),
        get = get_blend_mode,
        set = set_blend_mode,
        default = "REGULAR",
    )


class HelloWorldPanel(Panel):
    bl_label = "Hello World Panel"
    bl_idname = "OBJECT_PT_hello"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Test"

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

        custom = context.scene.custom
        
        layout.prop(custom, 'blend_mode')


classes = (
    CUSTOM_PG_property_group,
    HelloWorldPanel,
)


def register():
    for cls in classes:
        bpy.utils.register_class(cls)

    bpy.types.Scene.custom = PointerProperty(type=CUSTOM_PG_property_group)


def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
    
    del bpy.types.Scene.custom


if __name__ == "__main__":
    register()

e.g. bpy.context.scene.custom.set_blend_mode('HARDLIGHT')

$\endgroup$
1
  • $\begingroup$ Thank you for the help friend, much appreciated! $\endgroup$
    – Ommadawn
    Jul 29, 2023 at 11:35

You must log in to answer this question.

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