Casting a blueprint in Python

As far as I’ve been able to tell, there’s no way to get Python to do the kind of dynamic node cast you’d use in regular blueprints to cast an instance of a blueprint to a derived type – no matter what blueprint asset you load in Python, it’s just a “Blueprint”, and thus Python can’t access it’s custom events or public attributes. That’s the underlying cause of the issue in this question: How to cast result of spawn_and_register_tab in Python - Editor Scripting - Unreal Engine Forums, but it seems to be true for any use of blueprint assets in Python.

Is there a way to do this at all? Or is it planned for a future release? Supporting it would make tools development much more productive, with the team able to pick and choose blueprint or python as appropriate without being complete boxed off into separate worlds.

It’s not really direct anwser but might help you find solution

I think you my misunderstanding what blueprint really is, blueprint assets as you already find out have 0 executive power, it’s just a source code asset for virtual machine. When you compile the blueprint it generated virtual machine code and create UClass object (UClass | Unreal Engine Documentation) for it in reflection system referencing to that code, and by that rest of the engine code can reference that class, it also generates other objects that represents UPropeties ( and UFunctions (UFunction | Unreal Engine Documentation), which allows to be refrenced and edit propeties as well as execute functions.This is where true executive power is, so you really searching it in wrong place. C++ also generates those object allowing to refrence C++ classes,functions and propeties and by that both C++ and blueprints can reference each other and communicate (and as explained below it also lets python do call to UE4)

Just to put you in perspective… it’s exactly the same in C++, you can’t make direct calls to blueprint (compiler don’t see blueprints to begin with, so how it can make call to them?), but you can use reflection system to make a calls to blueprint functions the reflection system refers to and it actually the way blueprints are executed in first place. But in C++ the situation is better as you can make base classes for bueprints with events allowing to make somewhat direct function calls to blueprint in C++ by calling those events (in reality you calling functions generated by UHT that do call in reflection system from there respective UFunction), while as far as i know python can’t make valid UObject classes to begin with, that might be reaosn why you see them as seperate worlds, but in reality you still using same system.

That said UE4 python it self use reflection system to bind functions an classes from UE4 to python and as explained i this video:

UE4 pythons bind all functions that are BlueprintCallable in C++ which all blueprint functions technically are, so maybe blueprint functions are automatically binded to python too under same convention. But again if not you can use Class, Property and Function objects to control blueprint object and actors.

For soem reason i dont see class for function in python api refrence but there this:

At the end keep in mind that python in UE4 is (at least currently) intended to be only used for editor scripting, not runtime.

1 Like

Thanks for the reply

My specific intention is to find an equivalent to the auto-generated k2_DynamicCast nodes which (in blueprint) do the reflection checks on a blueprint asset and then allow calls to its custom methods and functions.

That works fine in blueprint – but does not seem to have a Python equivalent right now. The main reason I’m interested is specifically to be able to work with an EditorUtilityWidget, so that you could write python logic for editor tools but display an EditorUtilityWidget based GUI. However since there’s not visible way to get to the reflected custom attributes or events, you can’t actually talk to the widget from Python. You can from blueprint, which is what’s so annoying…

I can see this post is pretty old and I’m not sure if this response will help your specific problem, but it may, or perhaps someone else who comes across this.

I’m in a similar boat in that I am wanting to mainly use an editor utility widget for the UI side of a tool, but most of the work is done via python so I need to pass variable arrays to python and have python trigger events and functions in the widget.

I initially tried casting like mentioned above which I couldn’t get to work for the same reason given above (it always said the class was the parent instead of my specific child class) but I think I finally figured out how to do it.

basically I get a soft reference to self in the utility widget turn the reference into a string and pass that as an argument to python.

in python I use utility_widget = unreal.load_object(None, sys.argv[1])

after that I can successfully get current variable values on the widget(not defaults) as well as call methods on the utility_widget from python.

I haven’t specifically tested it but I imagine this method would also work for any actors you’re dealing with in the editor.

Hope this helps someone else out there.