1
$\begingroup$

I'm working on a Panel where a user shall be able to choose between different options using a dropdown box. What I'd like to do for convenience reasons is to define an EnumProperty directly in the Panel class. Here is an example, I modified the UI Panel Simple template that ships with Blender slightly and added Addon definition:

import bpy

bl_info = {
    "name": "Hello World Panel with Enum Prop",
    "author": "Rainer Trummer",
    "version": (0, 1, 0),
    "blender": (2, 76, 0),
    "description": "Enum Property is registered, but cannot be accessed",
    "category": "Interface"
}

class HelloWorldPanel(bpy.types.Panel):
    """Creates a Panel in the Object properties window"""
    bl_label = "Hello World Panel"
    bl_idname = "VIEW3D_PT_hello"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_context = "object"

    # EnumProperty definition
    test = bpy.props.EnumProperty(items =
        [
            ('ONE', 'one', 'first one'),
            ('TWO', 'two', 'second one')
        ]
    )

    def draw(self, context):
        layout = self.layout
        row = layout.row()
        row.label(text = "Hello world!", icon = 'WORLD_DATA')

        # the following line fails when hovering over the panel
        # with the mouse cursor
        row.prop(self, 'test')


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


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


if __name__ == "__main__":
    register()

You can run this code from the text editor. There are no errors thrown at first, but the Property never appears in the UI. When you hover over the Panel in the 3D View, the console prints the following error:

RNA Error in Console

However, I can happily access the property from the console:

accessing it

but only if the panel code is within an addon. Running JUST the panel code without the bl_info block does not register the property.

I know I can put the Enum somewhere else, like into bpy.types.scene, but I don't understand why this approach here would not work. It DOES work with operators, doesn't it? Why not with Panels?

$\endgroup$
  • $\begingroup$ See the comment of GaiaClary here blender.stackexchange.com/questions/56741/…. I have very few experience of that, but if properties cannot be changed in a draw function, maybe it cannot be created too (from the rna point of view) $\endgroup$ – lemon Aug 28 '16 at 16:13
  • $\begingroup$ @lemon thanks for the link. But I'm not creating the property in the draw function, I do it within the class definition. I'm just accessing it during draw, and that is valid for any custom property, just not for those that are created on the panel class itself. $\endgroup$ – aliasguru Aug 28 '16 at 16:51
1
$\begingroup$

items = (('OANS', 'oans', 'description', 0))

This is a tuple of four. (x) just evaluates to x. If you want a tuple that contains x, you have to write (x, ). Or just use a list. Either of those two should work:

items = (('OANS', 'oans', 'description', 0), )

items = [('OANS', 'oans', 'description', 0)]

$\endgroup$
  • $\begingroup$ hehe, i removed the comma before posting the example thinking it might confuse people. in my current addon the enum looks like your first example, so that's not the issue. but thanks, I got the explanation and wasn't aware about that evaluation habit. $\endgroup$ – aliasguru Aug 28 '16 at 16:45
  • $\begingroup$ Please provide complete examples. Just use copy & paste. Right now your example doesn't even compile (class ceFF(Panel, ceFF base) is invalid). $\endgroup$ – dr. Sybren Aug 28 '16 at 21:06
  • $\begingroup$ I didn't copy/paste the code because it is unfortunately closed source, so I had to strip and modify it. I forgot to test it, that's why it turned out not to compile. I've edited my question now and based it on a Template that ships with Blender. The 'issue' is the same. What I found out today is that the property only registers when I make the whole thing an addon. Registering just the panel does not register the property at all, I guess that's the key to it. A link to some documentation that enlightens this would already be a good answer for me. $\endgroup$ – aliasguru Aug 29 '16 at 7:49
  • $\begingroup$ The current 2.77 API docs for the Panel class do not elaborate on it, but the info might be on a different page than this one: blender.org/api/blender_python_api_2_77_3/… $\endgroup$ – aliasguru Aug 29 '16 at 7:51
1
$\begingroup$

Finally I found the hint in the Blender Reference manual on this page: https://www.blender.org/api/blender_python_api_2_77_3/bpy.types.bpy_struct.html#bpy.types.bpy_struct :

Note Only bpy.types.ID, bpy.types.Bone and bpy.types.PoseBone classes support custom properties

and on top of the https://www.blender.org/api/blender_python_api_2_77_3/bpy.types.ID.html#bpy.types.ID page, the different ID classes are mentioned:

subclasses — Action, Armature, Brush, CacheFile, Camera, Curve, FreestyleLineStyle, GreasePencil, Group, Image, Key, Lamp, Lattice, Library, Mask, Material, Mesh, MetaBall, MovieClip, NodeTree, Object, PaintCurve, Palette, ParticleSettings, Scene, Screen, Sound, Speaker, Text, Texture, VectorFont, WindowManager, World

So, since the Panel class is not an ID class, but another derivate of bpy_struct, it cannot support custom properties by definition.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.