So I’m having a heck of a time figuring out how to read or set Blueprint enum variables from Python. I’ve found the unreal docs on unreal.enum, and looked in that and all its superclasses, but having no luck solving this mystery. Obviously I’ve tried get_editor_property() and set_editor_property(), but those don’t work on enum variables. This is as far as I’ve gotten.
# get selected scene object
obj = unreal.EditorLevelLibrary.get_selected_level_actors()
# get enum variable called 'enum'
my_enum = unreal.Enum(obj.get_outer(), 'enum')
# now what?
Any links or pointers appreciated.
Update - So the above example is trying to access an unreal.Enum type, and this is an enum type that is generated if you create a prototype blueprint enum, then reference that blueprint to establish an enum variable in another blueprint. Looking at the C++ code for this Enum type, there are a number of important member functions that are not exposed to Python as of UE v4.24 (correct me if I’m wrong), so I feel like this is a dead-end for now.
However, I believe that that vast majority, if not all, enums already existing in Unreal’s native blueprints - which are based on C++ classes - are derived from unreal.EnumBase, and that’s much easier to read and set through Python. Since I can’t find much on Google regarding how to access this enums, I’ll post a couple of snippets to help anyone get started who needs to read or set these EnumBase derived variables through Python.
First we’ll get an actor object by selecting a pawn (or pawn derived object) in the content browser and running this.
selected_actor = get_selected_content_browser_items()
bgc_asset = unreal.load_object(None, selected_actor.get_path_name() + '_C')
actor = unreal.get_default_object(bgc_asset)
Get the enum’s set string value
enum = actor.get_editor_property('AutoPossessAI')
enum_sel_str = enum.name
print('Enum string value: %s' % enum_sel_str)
# Enum string value: PLACED_IN_WORLD
To get its selected index you would just use this instead
enum_sel_index = enum.value
And to set the enum value
auto_possess_player = actor.get_editor_property("auto_possess_player")
My enum.value is a number and if I try to set it by ```
I know this thread is super old, but I am having this issue. actor.set_editor_property(‘auto_possess_player’, auto_possess_player.PLAYER1) does not seem to work. Anybody else know a fix for this?
Well I managed to figure it out, although it is a slightly inconvenient approach.
def change_blueprint_default_value(blueprint_generated_class, variable_name, new_value):
blueprint = unreal.load_object(None, blueprint_generated_class)
some_actor_cdo = unreal.get_default_object(blueprint)