Skip to content

Commit 4c8a9fe

Browse files
ikrimaRoberto De Ioris
authored andcommitted
Enabling UMG Widgets in python
-Adding take_widget to enable using UMG Widgets inside of PythonBlueprintFunctionLibrary -Adding BPFL to execute ptyhon string -Binding py.exec to console command to run python scripts from in-game console
1 parent 3ff4d72 commit 4c8a9fe

File tree

6 files changed

+61
-1
lines changed

6 files changed

+61
-1
lines changed

Source/UnrealEnginePython/Private/PythonBlueprintFunctionLibrary.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,9 @@ void UPythonBlueprintFunctionLibrary::ExecutePythonScript(FString script) {
66
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
77
PythonModule.RunFile(TCHAR_TO_UTF8(*script));
88
}
9+
10+
void UPythonBlueprintFunctionLibrary::ExecutePythonString(const FString& PythonCmd)
11+
{
12+
FUnrealEnginePythonModule &PythonModule = FModuleManager::GetModuleChecked<FUnrealEnginePythonModule>("UnrealEnginePython");
13+
PythonModule.RunString(TCHAR_TO_UTF8(*PythonCmd));
14+
}

Source/UnrealEnginePython/Private/UEPyModule.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "UEPyAudio.h"
2323
#include "UEPySequencer.h"
2424
#include "UEPyViewport.h"
25+
#include "UEPyWidget.h"
2526
#include "UEPyWidgetComponent.h"
2627

2728
#include "UEPyPackage.h"
@@ -566,6 +567,9 @@ static PyMethodDef ue_PyUObject_methods[] = {
566567
{ "get_world_location_at_distance_along_spline", (PyCFunction)py_ue_get_world_location_at_distance_along_spline, METH_VARARGS, "" },
567568
{ "get_spline_length", (PyCFunction)py_ue_get_spline_length, METH_VARARGS, "" },
568569

570+
// Widget
571+
{ "take_widget", (PyCFunction)py_ue_take_widget, METH_VARARGS, "" },
572+
569573
// WidgetComponent
570574
{ "set_slate_widget", (PyCFunction)py_ue_set_slate_widget, METH_VARARGS, "" },
571575

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "UnrealEnginePythonPrivatePCH.h"
2+
3+
4+
#include "Runtime/UMG/Public/Components/Widget.h"
5+
6+
7+
PyObject *py_ue_take_widget(ue_PyUObject * self, PyObject * args) {
8+
9+
ue_py_check(self);
10+
11+
UWidget *widget = ue_py_check_type<UWidget>(self);
12+
if (!widget)
13+
return PyErr_Format(PyExc_Exception, "uobject is not a UWidget");
14+
15+
ue_PySWidget *s_widget = ue_py_get_swidget(widget->TakeWidget());
16+
17+
return (PyObject *)s_widget;
18+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#pragma once
2+
3+
4+
#include "UnrealEnginePython.h"
5+
6+
7+
PyObject *py_ue_take_widget(ue_PyUObject *, PyObject *);

Source/UnrealEnginePython/Private/UnrealEnginePython.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright 1998-2016 Epic Games, Inc. All Rights Reserved.
22

33
#include "UnrealEnginePythonPrivatePCH.h"
4+
#include "PythonBlueprintFunctionLibrary.h"
5+
#include "HAL/IConsoleManager.h"
46

57
void unreal_engine_init_py_module();
68

@@ -107,6 +109,26 @@ static void setup_stdout_stderr() {
107109
PyRun_SimpleString(code);
108110
}
109111

112+
namespace {
113+
static void consoleExecScript(const TArray<FString>& Args)
114+
{
115+
if (Args.Num() != 1)
116+
{
117+
UE_LOG(LogPython, Warning, TEXT("Usage: 'py.exec <scriptname>'."));
118+
UE_LOG(LogPython, Warning, TEXT(" scriptname: Name of script, must reside in Scripts folder. Ex: myscript.py"));
119+
}
120+
else
121+
{
122+
UPythonBlueprintFunctionLibrary::ExecutePythonScript(Args[0]);
123+
}
124+
}
125+
126+
}
127+
FAutoConsoleCommand ExecPythonScriptCommand(
128+
TEXT("py.exec"),
129+
*NSLOCTEXT("UnrealEnginePython", "CommandText_Exec", "Execute python script").ToString(),
130+
FConsoleCommandWithArgsDelegate::CreateStatic(consoleExecScript));
131+
110132
void FUnrealEnginePythonModule::StartupModule()
111133
{
112134
// This code will execute after your module is loaded into memory; the exact timing is specified in the .uplugin file per-module

Source/UnrealEnginePython/Public/PythonBlueprintFunctionLibrary.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ class UNREALENGINEPYTHON_API UPythonBlueprintFunctionLibrary : public UBlueprint
1111
GENERATED_BODY()
1212

1313
public:
14-
UFUNCTION(BlueprintCallable, Category = "Python")
14+
UFUNCTION(BlueprintCallable, Exec, Category = "Python")
1515
static void ExecutePythonScript(FString script);
16+
17+
UFUNCTION(BlueprintCallable, Exec, Category = "Python")
18+
static void ExecutePythonString(const FString& PythonCmd);
1619
};
1720

0 commit comments

Comments
 (0)