Blueprint Function Libraries
A more verbose method for calling Python inside of an Editor Asset is to create Python BP Function Libraries. This is only recommended for internal projects that can manager their UE environments
A Warning
It is recommended not to use this method for externally released Plugins due to the order of operations on Unreal Startup. The general order on startup looks something like this:
Plugins are mounted
The Default Map is opened in the Editor
Any cached Editor Utility Widgets are reopened (EUWs that were open when the Editor was previously closed)
Python startup is run
Editor Actors in the default map and cached EUWs will load before Python initializes, resulting in Blueprint Compiler errors as the Python-generated BP Function Library doesn't exist yet:
Making BP Function Libraries safer is covered here:
Declaring a BP Function Library class
We first need to create a class which will store all of our Blueprint Functions:
This PythonFunctionLibrary
class will be where all following function node examples are declared.
The @unreal.uclass() decorator tells Unreal to expose it to the Editor
The
PyDemoBPLibrary
class name is arbitrary, name it uniquely as you please!The unreal.BlueprintFunctionLibrary base class handles everything else
For the rest of this page we'll be adding functions to this class, any function added to this class will be made available to the Blueprint Graph.
Decorator Overview
Functions are exposed to Unreal through decorators, there are two areas this page will focus on. This section is on the expected data: what's our input and what's our return. In the following section we'll cover how to organize and modify the behavior of the function via metadata.
Basic function example
For all blueprint function this is our starting block:
@unreal.ufunction() converts our Python function into a Blueprint Graph node, all of our settings will go in here.
static=True
tells Unreal that aself
arg is not expected, all of our functions will have thisAny Python docstring will also show in Unreal as the tool tip
Single Input / Output
To pass blueprint data to Python and back we must tell it what type of data we're expecting to provide/receive:
params
Is where we map the input type of each kwarg in sequential orderAny default kwarg values will display in the BP Graph node
ret
Is where we tell it what return data type to expect
Multiple Input / Output
For inputs we can add to the list in sequential order of our Python function's params:
To return
str, bool, int
you tell unreal to expectint, bool, str
That's right, the return is reverse order!
Another option is to use
ret=(str, bool, int)[::-1]
if we want to keep it visually consistent
Unreal will also add a bogus
returnValue
bool that does nothing
Handling Lists
When handling lists you must use the unreal.Array(type) class and declare its content:
This example uses a list of
str
however it can be any data type, evenunreal.actor
!It's okay to return a python List as well, but if any of its contents are not
str
it will throw an error
Pure functions (no exec in/out connections)
For getter functions we can use the pure
flag:
Metadata Specifiers
This section covers the meta
arg, which represents Metadata Specifiers. Given a dict of specifiers this flag grants further control over how the Blueprint node is organized, displayed, and behaves. To learn more about Metadata Specifiers this Unreal page is a great resource.
Category
The Category
meta dict member controls how the function is organized in the right click menu:
The
|
separator is how you set multiple depth levels in the menu
Key Words
The KeyWords
meta dict member registers additional words that may be used to find our function:
You can find a function buy its name, its category, or any of its keywords
Key Words
The CompactNodeTitle
meta dict member tells our function to use a compact display in the Blueprint Graph:
Default To Self
The DefaultToSelf
meta dict member will populate the given kwarg with a reference to the containing BP Class that's calling it:
Hide Pin (Fixes Multiple Returns)
The HidePin
meta dict member tells Unreal to hide the desired pin, we can use this to fix the multiple returns example:
Determines Output Type
The DeterminesOutputType
meta dict member controls our function's return data type:
This can help inform the Blueprint Graph when returning BP Assets created in the Editor
ret
andparams
still need to be provided, use an appropriate parent class
Advantages
The main advantage of this method is its verbosity, you can do so much more with this method than what's possible with Execute Python Script nodes.
Disadvantages
A major disadvantage of this method is the instability covered in the warning section above. It is possible to mitigate the Editor Startup issues, but for public release projects it can't be guaranteed that other users will take the appropriate steps.
A minor inconvenience is that we have to provide our own Success?
return, if we want it. If a Python error occurs during one of these nodes, it won't stop the Blueprint Graph from continuing on to the next node.
Last updated