0
$\begingroup$

This is a follow up question to:

I'm trying to duplicate properties of an existing operator (the Alembic exporter). It works well I think, with the exception that I can't set a default value on EnumProps. I get a type error as soon as I enable the default value:

TypeError: EnumProperty(..., default=''): not found in enum members

Here's the code I have in the init.py. As soon as the default=prop.default is enabled the error is thrown. This works for String/Bool/Int/Float properties, but not Enums.

import bpy
    
def get_alembic_enums():
    props = []
    for prop in bpy.ops.wm.alembic_export.get_rna_type().properties:
        if prop.type == "ENUM":
            props.append(prop)
            
    return props

def dup_enum_prop(prop):
    '''
    Duplicate an EnumProp.
    '''    
    items = [(x.identifier, x.name, "") for x in prop.enum_items.values()]
    
    dup_prop = bpy.props.EnumProperty(
        name=prop.name,
        description=prop.description,
        items=items,
        # If this line is enabled an error occurs.
        default=prop.default
    )
    
    return dup_prop
    
    
class AlembicProps(bpy.types.PropertyGroup):
    __annotations__ = {x.identifier: dup_enum_prop(x) for x in get_alembic_enums()}
    
    
bpy.utils.register_class(AlembicProps)    
    
bpy.types.Scene.alembic_props = bpy.props.PointerProperty(type=AlembicProps)
$\endgroup$
3
  • 1
    $\begingroup$ Hello ! Just as a shot in the dark, if the source enum property is dynamic (the items are gathered through a callback) the default attribute will not be available since dynamic enum props can't have a default value. You can try prop.enum_items_static docs.blender.org/api/current/… $\endgroup$
    – Gorgious
    Commented Oct 9, 2022 at 16:12
  • $\begingroup$ Thanks for the reply. It seems to give the same error unfortunately. Also, if i print prop.default it IS a valid value (meaning I have it in my items list), but I just can't set it for some reason. $\endgroup$ Commented Oct 9, 2022 at 16:31
  • $\begingroup$ Yup I didn't think it was the problem just wanted to give a heads up :) cheers $\endgroup$
    – Gorgious
    Commented Oct 9, 2022 at 16:33

1 Answer 1

1
$\begingroup$

There is an enum with zero items and an empty '' default value in the enums. It appears Blender doesn't like it when you set an empty default. So you can test for an empty default value and skip setting it:

import bpy
    
def get_alembic_enums():
    props = []
    for prop in bpy.ops.wm.alembic_export.get_rna_type().properties:
        if prop.type == "ENUM":
            props.append(prop)
            
    return props

def dup_enum_prop(prop):
    '''
    Duplicate an EnumProp.
    '''    
    items = [(x.identifier, x.name, "") for x in prop.enum_items.values()]
    
    # Test for empty default value
    if prop.default != '':
        dup_prop = bpy.props.EnumProperty(
            name=prop.name,
            description=prop.description,
            items=items,
            default=prop.default
        )
    else:
        dup_prop = bpy.props.EnumProperty(
            name=prop.name,
            description=prop.description,
            items=items,
        )
                
    return dup_prop
    
    
class AlembicProps(bpy.types.PropertyGroup):
    __annotations__ = {x.identifier: dup_enum_prop(x) for x in get_alembic_enums()}
    

bpy.utils.register_class(AlembicProps)    
    
bpy.types.Scene.alembic_props = bpy.props.PointerProperty(type=AlembicProps)

It's not the most beautiful Python code, but it works ;-)

$\endgroup$
1
  • $\begingroup$ That does seem to work! Thanks $\endgroup$ Commented Oct 10, 2022 at 15:16

You must log in to answer this question.

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