> For the complete documentation index, see [llms.txt](https://bkortbus.gitbook.io/unreal-python-recipe-book/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://bkortbus.gitbook.io/unreal-python-recipe-book/calling-python-from-blueprint/blueprint-function-libraries/making-bp-function-libraries-safer.md).

# Making BP Function Libraries Safer

Two freely available plugins to make Python-generated content safer to use in Unreal

The heart of the problem with Python-generated BP Function Libraries is that Python initialization happens too late in the Editor Startup Process.

&#x20;The general order on startup looks something like this:

1. Plugins are mounted
2. The Default Map is opened in the Editor
3. Any cached Editor Utility Widgets are reopened (EUWs that were open when the Editor was previously closed)
4. <mark style="color:yellow;">Python Init startup is run</mark>

This page will show two available options to fix this issue, both are free plugins available online that allow us to run Python startup scripts before the Default Map has been loaded.

***

## The Demo Code

For this demo I have a basic function library:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2F0s9nXU3tzvTOINg6keh7%2Fimage.png?alt=media&amp;token=0ea8002e-9d8f-4b33-bd16-f4f3413d3101" alt=""><figcaption></figcaption></figure>

My Editor Utility Widget calls a BP Function from this module, in order to make it safe I need to run the following Python line as soon as possible:

```python
from dev import function_library
```

If this code is not run soon enough, my Editor Utility Widget will be broken on Editor Startup when Unreal relaunches this tool:&#x20;

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FLxnuwSkhY1ltqfE7SOyI%2Fimage.png?alt=media&amp;token=80e3cad2-409d-4b45-a47e-073c1a26b3c6" alt=""><figcaption></figcaption></figure>

Let's fix this!

***

## the <mark style="color:green;">Python Blueprint Fixer</mark> plugin

This plugin is available on [FAB](https://www.fab.com/listings/d650d089-120d-4c3d-b494-77b57eac6093), this plugin will run the Project's Python Startup Scripts before the Project's Default Map is loaded.&#x20;

With the Plugin enabled:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FJV9n7z4ZOzIPBUO7d1rV%2Fimage.png?alt=media&amp;token=99f0c03a-9e3b-4a7e-aae4-196a9210f42d" alt=""><figcaption></figcaption></figure>

We will need to open the Project Settings window and add our startup script:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FmvdNDRDZvN5R5k8snU8B%2Fimage.png?alt=media&amp;token=33d65638-3be7-4a10-8793-2118dc48ceae" alt="" width="501"><figcaption></figcaption></figure>

The Blueprint Fixer Plugin will load the Python plugin and run its Startup Scripts immediately, anything declared here will run before the Default Map loads:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FG8kjZBnOIizH9mLaH9VO%2Fimage.png?alt=media&amp;token=eeefbc83-5477-406f-be77-de79de12b6b0" alt=""><figcaption></figcaption></figure>

{% hint style="info" %} <mark style="color:yellow;">note</mark>: `init_unreal.py` scripts still run *after* the Default Map has been loaded
{% endhint %}

This is a Per-Project solution, each Project will need the appropriate entry in its Python Startup Scripts setting.

***

## the <mark style="color:green;">Python Add Pre Init</mark> plugin

*Disclaimer: This is a plugin I wrote*

This plugin is available on [FAB ](https://www.fab.com/listings/c212cae1-1814-4b42-98e5-bfeea23ebdf6)or my [GitHub](https://github.com/bralkor/unreal_python_pre_init), this plugin adds support for a new Python startup script: `pre_init_unreal.py`&#x20;

With the Plugin enabled:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FBjgbyDomJPn4oxTm1esn%2Fimage.png?alt=media&amp;token=4f11490f-5f8d-4118-8c3e-26714945031e" alt=""><figcaption></figcaption></figure>

We can add a `pre_init_unreal.py` file anywhere a `init_unreal.py` can be added:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FPKHaTssLgeuWnJFkKCnV%2Fimage.png?alt=media&amp;token=d61b7c49-688a-40b8-b5bf-3c1091d5f921" alt=""><figcaption></figcaption></figure>

and it will run before the Default Map loads:

<figure><img src="https://368271246-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FStUBkBT2qJGKNkIZ6yc7%2Fuploads%2FmLS1AlqomPpIfs1kjV7M%2Fimage.png?alt=media&amp;token=c9dbd63d-431c-409c-b1f8-1f3ad62f24a2" alt=""><figcaption></figcaption></figure>

{% hint style="info" %} <mark style="color:yellow;">note</mark>: `init_unreal.py` scripts still run *after* the Default Map has been loaded
{% endhint %}

Supported file locations for `pre_init_unreal.py` :

* The **Content/Python** sub-folder in your Project's folder
* The **Content/Python** sub-folder in the main Unreal Engine installation
* The **Content/Python** sub-folder in each enabled Plugin's folder
* The **Documents/UnrealEngine/Python** folder inside your user directory. For example, on Windows 10, this is equivalent to `C:/Users/Username/Documents/UnrealEngine/Python`

This is a flexible solution, it can be per-project, per-user, or per-plugin as desired.

***

## Summary

Both of these options achieve the same goal: **run desired Python code earlier in the startup process**

The Python BP Fixer plugin is great for project-oriented usage, making use of what's already available

The Python Pre Init plugin is good for project-oriented and plugin-oriented usage, adding new functionality
