Skip to content

Commit 6b65673

Browse files
author
Roberto De Ioris
committed
code re-roganization
1 parent 4ed5ef7 commit 6b65673

8 files changed

Lines changed: 643 additions & 571 deletions

File tree

‎.gitignore‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,3 +87,5 @@ ENV/
8787

8888
# Rope project settings
8989
.ropeproject
90+
91+
Binaries/
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
#include "UnrealEnginePythonPrivatePCH.h"
2+
3+
4+
5+
#if WITH_EDITOR
6+
PyObject *py_unreal_engine_get_editor_world(PyObject * self, PyObject * args) {
7+
8+
if (!GEditor)
9+
return PyErr_Format(PyExc_Exception, "no GEditor found");
10+
11+
UWorld *world = GEditor->GetEditorWorldContext().World();
12+
ue_PyUObject *ret = ue_get_python_wrapper(world);
13+
if (!ret)
14+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
15+
Py_INCREF(ret);
16+
return (PyObject *)ret;
17+
}
18+
19+
PyObject *py_unreal_engine_editor_get_selected_actors(PyObject * self, PyObject * args) {
20+
21+
if (!GEditor)
22+
return PyErr_Format(PyExc_Exception, "no GEditor found");
23+
24+
PyObject *actors = PyList_New(0);
25+
26+
USelection *selection = GEditor->GetSelectedActors();
27+
int32 nums = selection->CountSelections<UObject>();
28+
29+
for (int32 i = 0; i < nums; i++) {
30+
UObject *obj = selection->GetSelectedObject(i);
31+
if (!obj->IsA<AActor>())
32+
continue;
33+
AActor *actor = (AActor *)obj;
34+
ue_PyUObject *item = ue_get_python_wrapper(actor);
35+
if (item)
36+
PyList_Append(actors, (PyObject *)item);
37+
}
38+
39+
return actors;
40+
}
41+
42+
PyObject *py_unreal_engine_editor_deselect_actors(PyObject * self, PyObject * args) {
43+
44+
if (!GEditor)
45+
return PyErr_Format(PyExc_Exception, "no GEditor found");
46+
47+
GEditor->SelectNone(true, true, false);
48+
49+
Py_INCREF(Py_None);
50+
return Py_None;
51+
}
52+
53+
PyObject *py_unreal_engine_editor_select_actor(PyObject * self, PyObject * args) {
54+
55+
if (!GEditor)
56+
return PyErr_Format(PyExc_Exception, "no GEditor found");
57+
58+
Py_INCREF(Py_None);
59+
return Py_None;
60+
}
61+
#endif
62+
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#pragma once
2+
3+
4+
5+
#include "UnrealEnginePython.h"
6+
PyObject *py_unreal_engine_get_editor_world(PyObject *, PyObject * args);
7+
PyObject *py_unreal_engine_editor_get_selected_actors(PyObject *, PyObject *);
8+
PyObject *py_unreal_engine_editor_deselect_actors(PyObject *, PyObject *);
9+
PyObject *py_unreal_engine_editor_select_actor(PyObject *, PyObject *);
Lines changed: 294 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,294 @@
1+
#include "UnrealEnginePythonPrivatePCH.h"
2+
3+
#include "Kismet/KismetSystemLibrary.h"
4+
#include "Kismet/KismetMathLibrary.h"
5+
6+
PyObject *py_unreal_engine_log(PyObject * self, PyObject * args) {
7+
char *message;
8+
if (!PyArg_ParseTuple(args, "s:log", &message)) {
9+
return NULL;
10+
}
11+
UE_LOG(LogPython, Log, TEXT("%s"), UTF8_TO_TCHAR(message));
12+
Py_INCREF(Py_None);
13+
return Py_None;
14+
}
15+
16+
PyObject *py_unreal_engine_log_warning(PyObject * self, PyObject * args) {
17+
char *message;
18+
if (!PyArg_ParseTuple(args, "s:log_warning", &message)) {
19+
return NULL;
20+
}
21+
UE_LOG(LogPython, Warning, TEXT("%s"), UTF8_TO_TCHAR(message));
22+
Py_INCREF(Py_None);
23+
return Py_None;
24+
}
25+
26+
PyObject *py_unreal_engine_log_error(PyObject * self, PyObject * args) {
27+
char *message;
28+
if (!PyArg_ParseTuple(args, "s:log_error", &message)) {
29+
return NULL;
30+
}
31+
UE_LOG(LogPython, Error, TEXT("%s"), UTF8_TO_TCHAR(message));
32+
Py_INCREF(Py_None);
33+
return Py_None;
34+
}
35+
36+
PyObject *py_unreal_engine_add_on_screen_debug_message(PyObject * self, PyObject * args) {
37+
int key;
38+
float time_to_display;
39+
char *message;
40+
if (!PyArg_ParseTuple(args, "ifs:add_on_screen_debug_message", &key, &time_to_display, &message)) {
41+
return NULL;
42+
}
43+
44+
if (!GEngine)
45+
goto end;
46+
47+
GEngine->AddOnScreenDebugMessage(key, time_to_display, FColor::Green, FString::Printf(TEXT("%s"), UTF8_TO_TCHAR(message)));
48+
49+
end:
50+
Py_INCREF(Py_None);
51+
return Py_None;
52+
}
53+
54+
PyObject *py_unreal_engine_print_string(PyObject * self, PyObject * args) {
55+
56+
char *message;
57+
if (!PyArg_ParseTuple(args, "s:print_string", &message)) {
58+
return NULL;
59+
}
60+
61+
if (!GEngine)
62+
goto end;
63+
64+
GEngine->AddOnScreenDebugMessage(-1, 2.0, FColor::Cyan, FString::Printf(TEXT("%s"), UTF8_TO_TCHAR(message)));
65+
66+
end:
67+
Py_INCREF(Py_None);
68+
return Py_None;
69+
}
70+
71+
PyObject *py_unreal_engine_get_forward_vector(PyObject * self, PyObject * args) {
72+
float roll, pitch, yaw;
73+
if (!PyArg_ParseTuple(args, "fff:get_forward_vector", &roll, &pitch, &yaw)) {
74+
return NULL;
75+
}
76+
FVector vec = UKismetMathLibrary::GetForwardVector(FRotator(pitch, yaw, roll));
77+
return Py_BuildValue("fff", vec.X, vec.Y, vec.Z);
78+
}
79+
80+
PyObject *py_unreal_engine_get_right_vector(PyObject * self, PyObject * args) {
81+
float roll, pitch, yaw;
82+
if (!PyArg_ParseTuple(args, "fff:get_right_vector", &roll, &pitch, &yaw)) {
83+
return NULL;
84+
}
85+
FVector vec = UKismetMathLibrary::GetRightVector(FRotator(pitch, yaw, roll));
86+
return Py_BuildValue("fff", vec.X, vec.Y, vec.Z);
87+
}
88+
89+
PyObject *py_unreal_engine_get_up_vector(PyObject * self, PyObject * args) {
90+
float roll, pitch, yaw;
91+
if (!PyArg_ParseTuple(args, "fff:get_up_vector", &roll, &pitch, &yaw)) {
92+
return NULL;
93+
}
94+
FVector vec = UKismetMathLibrary::GetUpVector(FRotator(pitch, yaw, roll));
95+
return Py_BuildValue("fff", vec.X, vec.Y, vec.Z);
96+
}
97+
98+
99+
PyObject *py_unreal_engine_find_class(PyObject * self, PyObject * args) {
100+
char *name;
101+
if (!PyArg_ParseTuple(args, "s:find_class", &name)) {
102+
return NULL;
103+
}
104+
105+
UClass *u_class = FindObject<UClass>(ANY_PACKAGE, UTF8_TO_TCHAR(name));
106+
107+
if (u_class) {
108+
ue_PyUObject *ret = ue_get_python_wrapper(u_class);
109+
if (!ret)
110+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
111+
Py_INCREF(ret);
112+
return (PyObject *)ret;
113+
}
114+
115+
Py_INCREF(Py_None);
116+
return Py_None;
117+
}
118+
119+
120+
PyObject *py_unreal_engine_color_to_linear(PyObject * self, PyObject * args) {
121+
uint8 r, g, b;
122+
uint8 a = 255;
123+
if (!PyArg_ParseTuple(args, "iii|i:color_to_linear", &r, &g, &b, &a)) {
124+
return NULL;
125+
}
126+
127+
FLinearColor lcolor = FColor(r, g, b, a).ReinterpretAsLinear();
128+
return Py_BuildValue("fff", lcolor.R, lcolor.G, lcolor.B);
129+
}
130+
131+
PyObject *py_unreal_engine_color_from_linear(PyObject * self, PyObject * args) {
132+
float r, g, b;
133+
float a = 1;
134+
if (!PyArg_ParseTuple(args, "fff|f:color_from_linear", &r, &g, &b, &a)) {
135+
return NULL;
136+
}
137+
138+
FColor color = FLinearColor(r, g, b, a).ToFColor(true);
139+
return Py_BuildValue("iii", color.R, color.G, color.B);
140+
}
141+
142+
PyObject *py_unreal_engine_find_object(PyObject * self, PyObject * args) {
143+
char *name;
144+
if (!PyArg_ParseTuple(args, "s:find_object", &name)) {
145+
return NULL;
146+
}
147+
148+
UObject *u_object = FindObject<UObject>(ANY_PACKAGE, UTF8_TO_TCHAR(name));
149+
150+
if (u_object) {
151+
ue_PyUObject *ret = ue_get_python_wrapper(u_object);
152+
if (!ret)
153+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
154+
Py_INCREF(ret);
155+
return (PyObject *)ret;
156+
}
157+
158+
Py_INCREF(Py_None);
159+
return Py_None;
160+
}
161+
162+
PyObject *py_unreal_engine_vector_add_vector(PyObject * self, PyObject *args) {
163+
164+
float x = 0, y = 0, z = 0;
165+
166+
Py_ssize_t items = PyTuple_Size(args);
167+
168+
if ((items % 3) != 0) {
169+
return PyErr_Format(PyExc_TypeError, "this function requires a 3-multiple number of args");
170+
}
171+
172+
for (int i = 0; i < (int)items; i += 3) {
173+
PyObject *x_py = PyTuple_GetItem(args, i);
174+
if (!PyFloat_Check(x_py))
175+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
176+
x += (float)PyFloat_AsDouble(x_py);
177+
PyObject *y_py = PyTuple_GetItem(args, i + 1);
178+
if (!PyFloat_Check(y_py))
179+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (y)");
180+
y += (float)PyFloat_AsDouble(y_py);
181+
PyObject *z_py = PyTuple_GetItem(args, i + 2);
182+
if (!PyFloat_Check(z_py))
183+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (z)");
184+
z += (float)PyFloat_AsDouble(z_py);
185+
}
186+
187+
return Py_BuildValue("fff", x, y, z);
188+
}
189+
190+
PyObject *py_unreal_engine_vector_add_float(PyObject * self, PyObject *args) {
191+
192+
float x = 0, y = 0, z = 0;
193+
194+
Py_ssize_t items = PyTuple_Size(args);
195+
196+
if (items < 3) {
197+
return PyErr_Format(PyExc_TypeError, "this function requires a xyz vector");
198+
}
199+
200+
PyObject *x_py = PyTuple_GetItem(args, 0);
201+
if (!PyFloat_Check(x_py))
202+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
203+
x = (float)PyFloat_AsDouble(x_py);
204+
205+
206+
PyObject *y_py = PyTuple_GetItem(args, 1);
207+
if (!PyFloat_Check(y_py))
208+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
209+
y = (float)PyFloat_AsDouble(y_py);
210+
211+
PyObject *z_py = PyTuple_GetItem(args, 2);
212+
if (!PyFloat_Check(z_py))
213+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
214+
z = (float)PyFloat_AsDouble(z_py);
215+
216+
for (int i = 3; i < (int)items; i++) {
217+
PyObject *delta_py = PyTuple_GetItem(args, i);
218+
if (!PyFloat_Check(delta_py))
219+
return PyErr_Format(PyExc_TypeError, "this function supports only float values");
220+
float delta = (float)PyFloat_AsDouble(delta_py);
221+
x += delta;
222+
y += delta;
223+
z += delta;
224+
}
225+
226+
return Py_BuildValue("fff", x, y, z);
227+
}
228+
229+
PyObject *py_unreal_engine_vector_mul_vector(PyObject * self, PyObject *args) {
230+
231+
float x = 1, y = 1, z = 1;
232+
233+
Py_ssize_t items = PyTuple_Size(args);
234+
235+
if ((items % 3) != 0) {
236+
return PyErr_Format(PyExc_TypeError, "this function requires a 3-multiple number of args");
237+
}
238+
239+
for (int i = 0; i < (int)items; i += 3) {
240+
PyObject *x_py = PyTuple_GetItem(args, i);
241+
if (!PyFloat_Check(x_py))
242+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
243+
x *= (float)PyFloat_AsDouble(x_py);
244+
PyObject *y_py = PyTuple_GetItem(args, i + 1);
245+
if (!PyFloat_Check(y_py))
246+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (y)");
247+
y *= (float)PyFloat_AsDouble(y_py);
248+
PyObject *z_py = PyTuple_GetItem(args, i + 2);
249+
if (!PyFloat_Check(z_py))
250+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (z)");
251+
z *= (float)PyFloat_AsDouble(z_py);
252+
}
253+
254+
return Py_BuildValue("fff", x, y, z);
255+
}
256+
257+
PyObject *py_unreal_engine_vector_mul_float(PyObject * self, PyObject *args) {
258+
259+
float x = 0, y = 0, z = 0;
260+
261+
Py_ssize_t items = PyTuple_Size(args);
262+
263+
if (items < 3) {
264+
return PyErr_Format(PyExc_TypeError, "this function requires a xyz vector");
265+
}
266+
267+
PyObject *x_py = PyTuple_GetItem(args, 0);
268+
if (!PyFloat_Check(x_py))
269+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
270+
x = (float)PyFloat_AsDouble(x_py);
271+
272+
273+
PyObject *y_py = PyTuple_GetItem(args, 1);
274+
if (!PyFloat_Check(y_py))
275+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
276+
y = (float)PyFloat_AsDouble(y_py);
277+
278+
PyObject *z_py = PyTuple_GetItem(args, 2);
279+
if (!PyFloat_Check(z_py))
280+
return PyErr_Format(PyExc_TypeError, "this function supports only float values (x)");
281+
z = (float)PyFloat_AsDouble(z_py);
282+
283+
for (int i = 3; i < (int)items; i++) {
284+
PyObject *delta_py = PyTuple_GetItem(args, i);
285+
if (!PyFloat_Check(delta_py))
286+
return PyErr_Format(PyExc_TypeError, "this function supports only float values");
287+
float delta = (float)PyFloat_AsDouble(delta_py);
288+
x *= delta;
289+
y *= delta;
290+
z *= delta;
291+
}
292+
293+
return Py_BuildValue("fff", x, y, z);
294+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
4+
5+
#include "UnrealEnginePython.h"
6+
7+
PyObject *py_unreal_engine_log(PyObject *, PyObject *);
8+
PyObject *py_unreal_engine_log_warning(PyObject *, PyObject *);
9+
PyObject *py_unreal_engine_log_error(PyObject *, PyObject *);
10+
PyObject *py_unreal_engine_add_on_screen_debug_message(PyObject *, PyObject *);
11+
PyObject *py_unreal_engine_print_string(PyObject *, PyObject *);
12+
PyObject *py_unreal_engine_get_forward_vector(PyObject *, PyObject *);
13+
PyObject *py_unreal_engine_get_right_vector(PyObject *, PyObject *);
14+
PyObject *py_unreal_engine_get_up_vector(PyObject *, PyObject *);
15+
PyObject *py_unreal_engine_find_class(PyObject *, PyObject *);
16+
PyObject *py_unreal_engine_color_to_linear(PyObject *, PyObject *);
17+
PyObject *py_unreal_engine_color_from_linear(PyObject *, PyObject *);
18+
PyObject *py_unreal_engine_find_object(PyObject *, PyObject *);
19+
PyObject *py_unreal_engine_vector_add_vector(PyObject *, PyObject *);
20+
PyObject *py_unreal_engine_vector_add_float(PyObject *, PyObject *);
21+
PyObject *py_unreal_engine_vector_mul_vector(PyObject *, PyObject *);
22+
PyObject *py_unreal_engine_vector_mul_float(PyObject *, PyObject *);

0 commit comments

Comments
 (0)