Unreal - Python / PySide2

Setup Python

Enable the plugins you wish to use. There might be more scripting modules under the scripting tab depending on what you have installed and what you wish to be able to modify with python.

  • Edit > Plugins > Scripting >
    • Python Editor Script Plugin : Enable

We need to enable developer mode on a per project basis

  • Edit > Project Settings > Plugins > Python
    • Developer Mode : Enable

We can find the output log here:

  • Windows > Developer Tools > Output Log
    • With the bottom-left dropdown you can switch between:
      • CMD
      • Python
      • Python (REPL)

Python Version

I am using Unreal 4.26 and it uses Python 3.7 by default. The path to the Python location is:

  • C:\Program Files\Epic Games\UE_4.26\Engine\Binaries\ThirdParty\Python3\Win64

Python and Qt

If you look inside the python install dir you can se pip there. I read somewhere that you can just use pip to install PySide in the python dir that comes with unreal. I have not tried that yet but instead I just created a virtual env elsewhere and pointed unreal to the site-packages of that install. See this post on Setup a virtual environment

Then we need to point to the site-pacakges dir.

  • Project Settings > Plugins > Python > Additional Paths: ../path/to/venv/Lib/site-packages

Pyside2 example

Here is a small snippet of how to creater a widget that is parented to the main window.

import sys

from PySide2 import QtGui
from PySide2 import QtWidgets

import unreal

class TestWidget(QtWidgets.QWidget):
	def __init__(self, parent=None):
		super(TestWidget, self).__init__(parent)

		vbox = QtWidgets.QVBoxLayout(self)
		btn = QtWidgets.QPushButton('Test')
		btn.clicked.connect(self.btn_clicked)
		vbox.addWidget(btn)

	def btn_clicked(self):
		print('Clicked')
		unreal.log('Clicked')


app = None
if not QtWidgets.QApplication.instance():
	app = QtWidgets.QApplication(sys.argv)	

widget = TestWidget()
widget.show()
unreal.parent_external_window_to_slate(widget.winId())

Then we can execute the script:

  • File > Execute Python Script : Browse to the script location

Docs