Skip to content

Commit 26b0784

Browse files
author
Roberto De Ioris
committed
added get_py_proxy(), 20tab#38
1 parent b0a0b6e commit 26b0784

8 files changed

Lines changed: 60 additions & 75 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyEngine.cpp‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#include "Kismet/KismetSystemLibrary.h"
44
#include "Kismet/KismetMathLibrary.h"
55

6-
#include "PythonGeneratedClass.h"
7-
86
PyObject *py_unreal_engine_log(PyObject * self, PyObject * args) {
97
PyObject *py_message;
108
if (!PyArg_ParseTuple(args, "O:log", &py_message)) {

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ static PyObject *py_unreal_engine_exec(PyObject * self, PyObject * args) {
8585
return Py_None;
8686
}
8787

88+
static PyObject *py_ue_get_py_proxy(ue_PyUObject *self, PyObject * args) {
89+
90+
ue_py_check(self);
91+
92+
if (self->py_proxy) {
93+
Py_INCREF(self->py_proxy);
94+
return (PyObject *)self->py_proxy;
95+
}
96+
97+
Py_INCREF(Py_None);
98+
return Py_None;
99+
}
100+
88101
static PyMethodDef unreal_engine_methods[] = {
89102
{ "log", py_unreal_engine_log, METH_VARARGS, "" },
90103
{ "log_warning", py_unreal_engine_log_warning, METH_VARARGS, "" },
@@ -220,6 +233,8 @@ static PyMethodDef ue_PyUObject_methods[] = {
220233

221234
{ "bind_event", (PyCFunction)py_ue_bind_event, METH_VARARGS, "" },
222235

236+
{ "get_py_proxy", (PyCFunction)py_ue_get_py_proxy, METH_VARARGS, "" },
237+
223238
#if WITH_EDITOR
224239
{ "get_actor_label", (PyCFunction)py_ue_get_actor_label, METH_VARARGS, "" },
225240
{ "set_actor_label", (PyCFunction)py_ue_set_actor_label, METH_VARARGS, "" },

‎Source/UnrealEnginePython/Private/UEPyModule.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ typedef struct {
1111
PyObject_HEAD
1212
/* Type-specific fields go here. */
1313
UObject *ue_object;
14+
// reference to proxy class (can be null)
15+
PyObject *py_proxy;
16+
// list of exposed delegates
1417
std::list<UPythonDelegate*> *python_delegates_gc;
1518
} ue_PyUObject;
1619

‎Source/UnrealEnginePython/Public/PyActor.cpp‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,7 @@ APyActor::APyActor()
99

1010
PythonTickForceDisabled = false;
1111
PythonDisableAutoBinding = false;
12-
13-
FScopePythonGIL gil;
14-
// pre-generate PyUObject (for performance)
15-
py_uobject = ue_get_python_wrapper(this);
16-
if (!py_uobject) {
17-
unreal_engine_py_log_error();
18-
}
12+
1913
}
2014

2115

@@ -26,11 +20,17 @@ void APyActor::BeginPlay()
2620

2721
// ...
2822

29-
if (!py_uobject || PythonModule.IsEmpty())
23+
if (PythonModule.IsEmpty())
3024
return;
3125

3226
FScopePythonGIL gil;
3327

28+
py_uobject = ue_get_python_wrapper(this);
29+
if (!py_uobject) {
30+
unreal_engine_py_log_error();
31+
return;
32+
}
33+
3434
PyObject *py_actor_module = PyImport_ImportModule(TCHAR_TO_UTF8(*PythonModule));
3535
if (!py_actor_module) {
3636
unreal_engine_py_log_error();
@@ -53,7 +53,7 @@ void APyActor::BeginPlay()
5353
PyObject *py_actor_class = PyDict_GetItemString(py_actor_module_dict, TCHAR_TO_UTF8(*PythonClass));
5454

5555
if (!py_actor_class) {
56-
unreal_engine_py_log_error();
56+
UE_LOG(LogPython, Error, TEXT("Unable to find class %s in module %s"), *PythonClass, *PythonModule);
5757
return;
5858
}
5959

@@ -63,6 +63,8 @@ void APyActor::BeginPlay()
6363
return;
6464
}
6565

66+
py_uobject->py_proxy = py_actor_instance;
67+
6668
PyObject_SetAttrString(py_actor_instance, (char*)"uobject", (PyObject *)py_uobject);
6769

6870

‎Source/UnrealEnginePython/Public/PyCharacter.cpp‎

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,6 @@ APyCharacter::APyCharacter()
1010
PythonTickForceDisabled = false;
1111
PythonDisableAutoBinding = false;
1212

13-
FScopePythonGIL gil;
14-
// pre-generate PyUObject (for performance)
15-
py_uobject = ue_get_python_wrapper(this);
16-
if (!py_uobject) {
17-
unreal_engine_py_log_error();
18-
}
1913
}
2014

2115

@@ -28,11 +22,17 @@ void APyCharacter::BeginPlay()
2822

2923
// ...
3024

31-
if (!py_uobject || PythonModule.IsEmpty())
25+
if (PythonModule.IsEmpty())
3226
return;
3327

3428
FScopePythonGIL gil;
3529

30+
py_uobject = ue_get_python_wrapper(this);
31+
if (!py_uobject) {
32+
unreal_engine_py_log_error();
33+
return;
34+
}
35+
3636
PyObject *py_character_module = PyImport_ImportModule(TCHAR_TO_UTF8(*PythonModule));
3737
if (!py_character_module) {
3838
unreal_engine_py_log_error();
@@ -55,7 +55,7 @@ void APyCharacter::BeginPlay()
5555
PyObject *py_character_class = PyDict_GetItemString(py_character_module_dict, TCHAR_TO_UTF8(*PythonClass));
5656

5757
if (!py_character_class) {
58-
unreal_engine_py_log_error();
58+
UE_LOG(LogPython, Error, TEXT("Unable to find class %s in module %s"), *PythonClass, *PythonModule);
5959
return;
6060
}
6161

@@ -65,6 +65,8 @@ void APyCharacter::BeginPlay()
6565
return;
6666
}
6767

68+
py_uobject->py_proxy = py_character_instance;
69+
6870
PyObject_SetAttrString(py_character_instance, (char *)"uobject", (PyObject *)py_uobject);
6971

7072
// disable ticking if no tick method is exposed

‎Source/UnrealEnginePython/Public/PyPawn.cpp‎

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,7 @@ APyPawn::APyPawn()
88

99
PythonTickForceDisabled = false;
1010
PythonDisableAutoBinding = false;
11-
12-
FScopePythonGIL gil;
13-
// pre-generate PyUObject (for performance)
14-
py_uobject = ue_get_python_wrapper(this);
15-
if (!py_uobject) {
16-
unreal_engine_py_log_error();
17-
}
11+
1812
}
1913

2014

@@ -25,11 +19,17 @@ void APyPawn::BeginPlay()
2519

2620
// ...
2721

28-
if (!py_uobject || PythonModule.IsEmpty())
22+
if (PythonModule.IsEmpty())
2923
return;
3024

3125
FScopePythonGIL gil;
3226

27+
py_uobject = ue_get_python_wrapper(this);
28+
if (!py_uobject) {
29+
unreal_engine_py_log_error();
30+
return;
31+
}
32+
3333
PyObject *py_pawn_module = PyImport_ImportModule(TCHAR_TO_UTF8(*PythonModule));
3434
if (!py_pawn_module) {
3535
unreal_engine_py_log_error();
@@ -52,7 +52,7 @@ void APyPawn::BeginPlay()
5252
PyObject *py_pawn_class = PyDict_GetItemString(py_pawn_module_dict, TCHAR_TO_UTF8(*PythonClass));
5353

5454
if (!py_pawn_class) {
55-
unreal_engine_py_log_error();
55+
UE_LOG(LogPython, Error, TEXT("Unable to find class %s in module %s"), *PythonClass, *PythonModule);
5656
return;
5757
}
5858

@@ -62,6 +62,7 @@ void APyPawn::BeginPlay()
6262
return;
6363
}
6464

65+
py_uobject->py_proxy = py_pawn_instance;
6566

6667
PyObject_SetAttrString(py_pawn_instance, (char *)"uobject", (PyObject *)py_uobject);
6768

‎Source/UnrealEnginePython/Public/PythonComponent.cpp‎

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@ UPythonComponent::UPythonComponent()
1313
PythonTickForceDisabled = false;
1414
PythonDisableAutoBinding = false;
1515

16-
FScopePythonGIL gil;
17-
// pre-generate PyUObject (for performance)
18-
py_uobject = ue_get_python_wrapper(this);
19-
if (!py_uobject) {
20-
unreal_engine_py_log_error();
21-
}
16+
2217
}
2318

2419

@@ -29,11 +24,17 @@ void UPythonComponent::BeginPlay()
2924

3025
// ...
3126

32-
if (!py_uobject || PythonModule.IsEmpty())
27+
if (PythonModule.IsEmpty())
3328
return;
3429

3530
FScopePythonGIL gil;
3631

32+
py_uobject = ue_get_python_wrapper(this);
33+
if (!py_uobject) {
34+
unreal_engine_py_log_error();
35+
return;
36+
}
37+
3738
PyObject *py_component_module = PyImport_ImportModule(TCHAR_TO_UTF8(*PythonModule));
3839
if (!py_component_module) {
3940
unreal_engine_py_log_error();
@@ -56,7 +57,7 @@ void UPythonComponent::BeginPlay()
5657
PyObject *py_component_class = PyDict_GetItemString(py_component_module_dict, TCHAR_TO_UTF8(*PythonClass));
5758

5859
if (!py_component_class) {
59-
unreal_engine_py_log_error();
60+
UE_LOG(LogPython, Error, TEXT("Unable to find class %s in module %s"), *PythonClass, *PythonModule);
6061
return;
6162
}
6263

@@ -66,6 +67,8 @@ void UPythonComponent::BeginPlay()
6667
return;
6768
}
6869

70+
py_uobject->py_proxy = py_component_instance;
71+
6972
PyObject_SetAttrString(py_component_instance, (char *)"uobject", (PyObject *)py_uobject);
7073

7174

‎Source/UnrealEnginePython/Public/PythonGeneratedClass.h‎

Lines changed: 0 additions & 39 deletions
This file was deleted.

0 commit comments

Comments
 (0)