[Bf-python] How to use "set" callbacks on EnumProperty? GUI no longer updates

Henrique Nunes Jung henriquenj at gmail.com
Fri Apr 8 05:34:37 CEST 2016


Hello bf-python! This is my first time posting on this list.

I working on a plugin for Blender for some time now and I'm having some
issues when using the EnumProperty object. My objective is to provide a
drop-down menu for the user where he/she can select a given device from a
list. I then want to retrieve the currently selected device and perform
some initialization.

Well I managed to create the menu and populated it with my device list -
which are strings, basically. But when the user select the device he wants,
I can only access the string that is currently selected, and not the unique
identifier of the string (such as an index). This can cause bugs on my
plugin since I can have two devices with the same name.

One of my attempted solutions is using the "set" callback from the
EnumProperties, that I can capture the "value" argument and use as an
index. But there's one problem, as soon as I set a "set" callback function,
the menu itself stop updating with the selected device, even though I
receive the right value on my callback.

Here's a sample

        cls.device = EnumProperty(
                name="Device",
                description="Device to use for rendering",
                items=my_devices,
                set=set_my_device,
                )

def set_my_device(self, value):
    print("Device index is {0}".format(value))
    return None



I do receive the "Device index is X" with the right index, but the user
interface does not change. I'm following the tutorial available on Property
documentation [1], my guess is that I'm suppose to do something within the
callback to get the right option to appear selected on Blender, but I don't
know what it is.

Attached there's a minimal sample that you can copy and paste into blender
to reproduce the problem. Button appears on the Render panel, below the
 render button. I'm testing in 2.76b.

I stuck into this problem for some time and would really appreciate any
help, or maybe some other solution to get the index from EnumProperty.

Henrique Jung

[1]
https://www.blender.org/api/blender_python_api_current/bpy.props.html#get-set-example
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.blender.org/pipermail/bf-python/attachments/20160408/1c8fdd03/attachment.html>
-------------- next part --------------
from bl_ui import properties_render
import bpy

from bpy.props import (BoolProperty,
                       EnumProperty,
                       FloatProperty,
                       IntProperty,
                       PointerProperty)


def my_devices(scene, context):
    devices = [("Device 1", "Device 1","", 1),
               ("Device 2", "Device 2","", 2),
    ]
    return devices

def set_my_device(self, value):
    print("Device index is {0}".format(value))
    return None

class MySettings(bpy.types.PropertyGroup):

    @classmethod
    def register(cls):

        bpy.types.Scene.my_settings = PointerProperty(
        name = "My settings",
        description = "My settings properties",
        type=MySettings)

        cls.device = EnumProperty(
                name="Device",
                description="Device to use for rendering",
                items=my_devices,
                set=set_my_device,
                )

    def set_my_device(self,value):
        print("Index is now {0}".format(value))
        return None

    @classmethod
    def unregister(cls):
        del bpy.types.Scene.my_settings

def my_options(self, context):
    self.layout.prop(context.scene.my_settings,"device")


def register():
    bpy.utils.register_class(MySettings)
    properties_render.RENDER_PT_render.append(my_options)



if __name__ == "__main__":
    register()


More information about the Bf-python mailing list