Created
March 8, 2022 16:39
-
-
Save Gorgious56/4864c2c4eed1110b33916015faadf805 to your computer and use it in GitHub Desktop.
Dynamic enum shenanigans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import bpy | |
from bpy.props import EnumProperty | |
def Dynamic_Enum(context): | |
Dynamic_Enum.enum_items = [] | |
for line in bpy.data.texts['Text'].lines: | |
Dynamic_Enum.enum_items.append((line.body, line.body,"")) | |
def pick_enum(self, context): | |
# Only toggle if the user chose someting else than 1st option | |
self.picked_enum = self.Dynamic_Enum != "-1000" | |
class MyProperties(bpy.types.PropertyGroup): | |
picked_enum: bpy.props.BoolProperty(default=False) | |
Dynamic_Enum : EnumProperty( | |
name = "Enumarator_2 / Dropdown", | |
description = "dynamic items", | |
items=lambda self, context: Dynamic_Enum.enum_items, | |
update=pick_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 | |
Dynamic_Enum(context) # Update enum items before drawing them | |
if not mytool.picked_enum: # Prepend our menu option if the user never picked an item | |
Dynamic_Enum.enum_items = [("-1000", "Choose Something", "")] + list(Dynamic_Enum.enum_items) | |
layout.prop(mytool, "Dynamic_Enum", text= "dynamic") | |
classes = [MyProperties, ADDONNAME_PT_main_panel] | |
def register(): | |
for cls in classes: | |
bpy.utils.register_class(cls) | |
bpy.types.Scene.my_tool = bpy.props.PointerProperty(type=MyProperties) | |
if __name__ == "__main__": | |
register() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment