2
$\begingroup$

I'm trying to print the name/second value of the selected enum, but I only managed to print the identifier/first value of the item. By selected I mean the enum is displayed in a panel and what is selected there. This is how I did that:

print(bpy.context.scene.printenum)

This is my enum prop:

bpy.types.Scene.printenum = bpy.props.EnumProperty( items=[('A', "Apple", ""), ('B', "Lemon", ""), ('C', "Orange", "")], name="")

With this method, it either prints "A", "B", or "C", but that's not what I want. I'd like to print either "Apple", "Lemon", or "Orange". What would be the way to do that?

$\endgroup$

1 Answer 1

4
$\begingroup$

One approach to achieve your goal is by accessing the data via the bl_rna of your enum variable.

import bpy

bpy.types.Scene.printenum = bpy.props.EnumProperty(
    items=[('A', "Apple", ""), ('B', "Lemon", ""), ('C', "Orange", "")],
    name="",
    )

scn= bpy.context.scene

items = scn.bl_rna.properties['printenum'].enum_items


print(f"item [ identifier ] : {items[scn.printenum].identifier}")
print(f"item [ name ] : {items[scn.printenum].name}")
print(f"item [ description ] : {items[scn.printenum].description}")
$\endgroup$
5
  • $\begingroup$ Perfect, thank you! $\endgroup$
    – Tsybe
    Feb 3, 2023 at 3:03
  • 1
    $\begingroup$ Note that this won't work if your enum property items are fetched via a callback. In which case you can use this solution blender.stackexchange.com/a/269715/86891 $\endgroup$
    – Gorgious
    Feb 3, 2023 at 7:10
  • $\begingroup$ Thank you @Gorgious for the additional information. Is that method works for displaying the items as "label(text=...)" and the text based on each item selected enum values (still Apple, Lemon, Orange)? $\endgroup$
    – Tsybe
    Feb 3, 2023 at 19:32
  • $\begingroup$ Well if I understood correctly @Tsybe you can still get the information by testing the value of the enum and checking if it's the same as the first item of the list of tuples returned by that method. next((t[1] for t in items_callback(self, context) if t[0] == context.scene.printenum)) or something like that I haven't actually tested $\endgroup$
    – Gorgious
    Feb 6, 2023 at 17:26
  • $\begingroup$ I didn't exactly mean it that way. I want to display per item. I created a topic about it if you could take a look at it: blender.stackexchange.com/questions/285402/… $\endgroup$
    – Tsybe
    Feb 12, 2023 at 17:55

You must log in to answer this question.

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