Download

How To: Expose C++ function to Python and Editor Utility Widgets

Mar 9, 2021.Knowledge

Many useful editor functions are exposed to scripting through Editor Utility Widgets or Python, but there are still some features that aren’t covered by our existing editor scripting libraries. If you find that a necessary function hasn’t been exposed, you can set up your own Blueprint Function Library and wrap the missing functionality for use in your projects.

First, you’ll want to ensure the function you need hasn’t already been added. You can do this by searching the Unreal Python API , or looking through the libraries included with the EditorScriptingUtilities plugin, which is disabled by default. These libraries (UEditorAssetLibrary, UEditorLevelLibrary, etc.) will also serve as a good example if you determine that you need to write your own library.

Note: In a future version, the built-in editor scripting libraries will be migrated from the EditorScriptingUtilities plugin into editor subsystems, but you’ll still be able to provide your own additional scripting libraries via a plugin.

Once you’ve determined that you need to add new scripting functions, you can either create a new plugin to house them or add them to one of the existing scripting libraries if you’re open to engine changes. In the latter case, you can submit a pull request via GitHub and we’ll review them for inclusion in the engine. You can follow the Plugins documentation to create your plugin, and you’ll set the type to Editor in the uplugin file so that it’s not included with packaged builds. You can use the Blueprint Library template in the Plugin wizard for some boilerplate code to get you started.

In your plugin, you’ll add a class that implements UBlueprintFunctionLibrary and declare static functions using the UFUNCTION specifier to mark them as BlueprintCallable and set up their name and category:

UCLASS() class UMyScriptingLibraryBPLibrary : public UBlueprintFunctionLibrary { GENERATED_BODY() UFUNCTION(BlueprintCallable, meta = (DisplayName = “Execute Sample function”, Keywords = “MyScriptingLibrary sample test testing”), Category = “MyScriptingLibraryTesting”) static float MyScriptingLibrarySampleFunction(float Param); };

In the cpp file, you’ll implement the function with whatever C++ code you need to run

float UMyScriptingLibraryBPLibrary::MyScriptingLibrarySampleFunction(float Param) { return -1; }

After compiling, this function will automatically be available in both editor utility widgets and Python. By setting up your library in an Editor plugin, we can ensure the functions won’t appear in normal blueprints, where it isn’t safe to run editor-only code. If needed, you can navigate to Engine/Plugins/Experimental/PythonScriptPlugin/SphinxDocs/PythonAPI_docs_readme for instructions on how to generate your own version of the Python API docs locally that includes your new functions

1 Like