0
$\begingroup$

I have a problem with a dynamic enum. Strangely enough special characters are not handled there as you would imagine.

Here is a script to test. So that you don't have to create extra files, as it is the case with me, I just made a text file, where each line is an item for the enum property (So to test the script you need a text file with name text in the text editor, where the lines should contain special characters).

In the 3D window 2 enum properties are displayed under My Panel. The first enum shows special characters, the second enum, with the dynamic icons does not. I am completely at a loss. Can anyone help me?

import bpy
from bpy.props import EnumProperty
                       
                       

def Enum_Items(self,context):
    
    EnumItems = [('1','Fluegel',''),('2','Nägel',''),('3','Mönch','')]
    
    return EnumItems

def Dynamic_Enum(self,context):
    
    EnumItems = []
    Index = 0
    
    for i in bpy.data.texts['Text'].lines:
        Index += 1
        EnumItems.append((str(Index),i.body,''))
    
    return EnumItems

class MyProperties(bpy.types.PropertyGroup):
    
    my_enum : EnumProperty(
        name = "Enumarator / Dropdown",
        description = "sample text",
        items = Enum_Items)
        
    Dynamic_Enum : EnumProperty(
        name = "Enumarator_2 / Dropdown",
        description = "dynamic items",
        items = Dynamic_Enum)

class ADDONNAME_PT_main_panel(bpy.types.Panel):
    bl_label = "My Panel"
    bl_idname = "ADDONNAME_PT_main_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Test Tab"

    def draw(self, context):
        layout = self.layout
        mytool = context.scene.my_tool
        
        layout.prop(mytool, "my_enum")
        layout.prop(mytool, "Dynamic_Enum")

            
        return {'FINISHED'}
    

classes = [MyProperties, ADDONNAME_PT_main_panel]
 
 
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
#Props zugänglich machen
    bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=MyProperties)
        
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
#Props zugänglich machen
    del bpy.types.Scene.my_tool
        
 
if __name__ == "__main__":
    register()

Enumator with a fixed list:

enter image description here

Enumarator with a dynamic list:

enter image description here

This is what the funny characters are supposed to look like:

enter image description here

$\endgroup$
0

1 Answer 1

0
$\begingroup$
My problem was apparently related to this post here:

Is there a workaround for the known bug in dynamic EnumProperty?

I could not see the connection between update and translation. But it works :) I have adjusted my script like this:

import bpy
from bpy.props import EnumProperty
                       
                       
def Enum_Items(self,context):
    
    EnumItems = [('1','Fluegel',''),('2','Nägel',''),('3','Mönch','')]
    
    return EnumItems

def Dynamic_Enum(context):
    
    Index = 0
    
#    Dynamic_Enum.enum_items = [((line.body, line.body, "")) for line in bpy.data.texts['Text'].lines]
    
    Dynamic_Enum.enum_items = []
    for line in bpy.data.texts['Text'].lines:
        Dynamic_Enum.enum_items.append((line.body, line.body,""))
    
#Dynamic_Enum.enum_items = [] 
    

class MyProperties(bpy.types.PropertyGroup):
    
    my_enum : EnumProperty(
        name = "Enumarator / Dropdown",
        description = "sample text",
        items = Enum_Items)
        
    Dynamic_Enum : EnumProperty(
        name = "Enumarator_2 / Dropdown",
        description = "dynamic items",
        items = lambda self, context: Dynamic_Enum.enum_items,
        )

class ADDONNAME_PT_main_panel(bpy.types.Panel):
    bl_label = "My Panel"
    bl_idname = "ADDONNAME_PT_main_panel"
    bl_space_type = 'VIEW_3D'
    bl_region_type = 'UI'
    bl_category = "Test Tab"

    def draw(self, context):
        layout = self.layout
        mytool = context.scene.my_tool
        Dynamic_Enum(context) # Update enum items before drawing them
        layout.prop(mytool, "my_enum",text="static")
        layout.prop(mytool, "Dynamic_Enum", text= "dynamic")

    

classes = [MyProperties, ADDONNAME_PT_main_panel]
 
 
def register():
    for cls in classes:
        bpy.utils.register_class(cls)
#Props zugänglich machen
    bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=MyProperties)
        
 
def unregister():
    for cls in classes:
        bpy.utils.unregister_class(cls)
#Props zugänglich machen
    del bpy.types.Scene.my_tool
        
 
if __name__ == "__main__":
    register()
$\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.