2
$\begingroup$

Let's say I have this custom property fu3dm_model_gender: bpy.props.EnumProperty(name="Gender", description="Choose model's gender", items= [('OP1', "Male", ""), ('OP2', "Female", "")], default='OP1') and I want to print the string of name parameter. How can I do this?

UPDATE

Actually, my property is part of an operator. In the following example I'd like to get the string of name parameter and display it where the three X's (XXX) are:

class fu3dm_WM_OT_Model_Prefs_Operator(bpy.types.Operator):
    bl_idname = "wm.fu3dm_model_prefs"
    bl_label = "Model Preferences"
    bl_description = "..."

    fu3dm_model_gender : bpy.props.EnumProperty(name="Gender", description="Choose model's gender", items= [('OP1', "Male", ""), ('OP2', "Female", "")], default='OP1')

    def invoke(self, context, event):
        return context.window_manager.invoke_props_dialog(self)
        bpy.ops.wm.fu3dm_model_prefs('INVOKE_DEFAULT')

    def draw(self, context):

        layout = self.layout

    # BODY PREFS BOX - START
        box = layout.box()
        col = box.column()

        row = col.split()
        row.label(text=XXX)
        row.prop(self, "fu3dm_model_gender", text="")
    # BODY PREFS BOX - END

    def execute(self, context):
        pass
        return {"FINISHED"}
$\endgroup$
4
$\begingroup$

Yes. Once registered you can use bl_rna.properties to access the name, its type, the items, the description etc... I'd suggest use the python console to test:

>>> test_items = [
...     ("RED", "Red", "", 1),
...     ("GREEN", "Green", "", 2),
...     ("BLUE", "Blue", "", 3),
...     ("YELLOW", "Yellow", "", 4),
... ] 

>>> bpy.types.Scene.my_enum = bpy.props.EnumProperty(name="Foo", items=test_items)
>>> bpy.types.Scene.bl_rna.properties['my_enum'].name
'Foo'
>>> bpy.types.Scene.bl_rna.properties['my_enum'].enum_items[0]
<bpy_struct, EnumPropertyItem("RED")>
>>> bpy.types.Scene.bl_rna.properties['my_enum'].enum_items[0].name
'Red'

In case of an operator property, I'd suggest read the values from the operators annotation dict:

class SimpleOperator(bpy.types.Operator):
    """Tooltip"""
    bl_idname = "object.simple_operator"
    bl_label = "Simple Object Operator"
    
    my_prop: bpy.props.StringProperty(name="foo")

    def execute(self, context):
        
        # via bl_rna
        print (self.properties.bl_rna.properties['my_prop'].name)
        
        # using annotations
        print (self.__annotations__.items())
        print (self.__annotations__['my_prop'][1]['name'])
        
        return {'FINISHED'}
$\endgroup$
6
  • $\begingroup$ Hm... I didn't get it!!! Maybe I described it wrong. Can you please take a look on my question again? I updated it... $\endgroup$ Oct 2 '20 at 10:13
  • 1
    $\begingroup$ Yep, your description was not precise enough @SimonetosTheGreek $\endgroup$
    – brockmann
    Oct 2 '20 at 10:48
  • $\begingroup$ Yes I am sorry!!! So, if you have some time more, some clarifications so to understand 100% what I am learning and not just copying code. When do we use the bl_rna way? In self.__annotations__['my_prop'][1]['name'] why do we use 1 and not 0since name is the first parameter? $\endgroup$ Oct 2 '20 at 10:55
  • 1
    $\begingroup$ Basically a python question - the second item is dict we are looking for. Recommend make yourself familiar with dir() -> print(dir(self.__annotations__['my_prop'])) @SimonetosTheGreek $\endgroup$
    – brockmann
    Oct 2 '20 at 10:59
  • $\begingroup$ Thank you for your time!!! :) $\endgroup$ Oct 2 '20 at 11:02

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.