Skip to content

Commit fa58ace

Browse files
author
Roberto De Ioris
committed
implemented set_property and fixed components
1 parent 54a5970 commit fa58ace

1 file changed

Lines changed: 105 additions & 2 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyModule.cpp‎

Lines changed: 105 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,15 @@ static PyObject *py_unreal_engine_print_string(PyObject * self, PyObject * args)
8686
return Py_None;
8787
}
8888

89+
8990
static PyObject *py_unreal_engine_find_class(PyObject * self, PyObject * args) {
9091
char *name;
9192
if (!PyArg_ParseTuple(args, "s:find_class", &name)) {
9293
return NULL;
9394
}
9495

9596
UClass *u_class = FindObject<UClass>(ANY_PACKAGE, UTF8_TO_TCHAR(name));
96-
97+
9798
if (u_class) {
9899
ue_PyUObject *ret = ue_get_python_wrapper(u_class);
99100
if (!ret)
@@ -106,6 +107,26 @@ static PyObject *py_unreal_engine_find_class(PyObject * self, PyObject * args) {
106107
return Py_None;
107108
}
108109

110+
static PyObject *py_unreal_engine_find_object(PyObject * self, PyObject * args) {
111+
char *name;
112+
if (!PyArg_ParseTuple(args, "s:find_object", &name)) {
113+
return NULL;
114+
}
115+
116+
UObject *u_object = FindObject<UObject>(ANY_PACKAGE, UTF8_TO_TCHAR(name));
117+
118+
if (u_object) {
119+
ue_PyUObject *ret = ue_get_python_wrapper(u_object);
120+
if (!ret)
121+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
122+
Py_INCREF(ret);
123+
return (PyObject *)ret;
124+
}
125+
126+
Py_INCREF(Py_None);
127+
return Py_None;
128+
}
129+
109130
#if WITH_EDITOR
110131
static PyObject *py_unreal_engine_get_editor_world(PyObject * self, PyObject * args) {
111132

@@ -266,6 +287,7 @@ static PyMethodDef unreal_engine_methods[] = {
266287
{ "get_editor_world()", py_unreal_engine_get_editor_world, METH_VARARGS, "" },
267288
#endif
268289
{ "find_class", py_unreal_engine_find_class, METH_VARARGS, "" },
290+
{ "find_object", py_unreal_engine_find_object, METH_VARARGS, "" },
269291
{ "vector_add_vector", py_unreal_engine_vector_add_vector, METH_VARARGS, "" },
270292
{ "vector_add_float", py_unreal_engine_vector_add_float, METH_VARARGS, "" },
271293
{ "vector_mul_vector", py_unreal_engine_vector_mul_vector, METH_VARARGS, "" },
@@ -290,7 +312,9 @@ static PyObject *py_ue_call(ue_PyUObject *self, PyObject * args) {
290312
}
291313

292314
FOutputDeviceNull od_null;
293-
self->ue_object->CallFunctionByNameWithArguments(UTF8_TO_TCHAR(call_args), od_null, NULL, true);
315+
if (!self->ue_object->CallFunctionByNameWithArguments(UTF8_TO_TCHAR(call_args), od_null, NULL, true)) {
316+
return PyErr_Format(PyExc_Exception, "error while calling \"%s\"", call_args);
317+
}
294318

295319
Py_INCREF(Py_None);
296320
return Py_None;
@@ -332,6 +356,38 @@ static PyObject *py_ue_get_property(ue_PyUObject *self, PyObject * args) {
332356

333357
}
334358

359+
static PyObject *py_ue_set_property(ue_PyUObject *self, PyObject * args) {
360+
361+
ue_py_check(self);
362+
363+
char *property_name;
364+
PyObject *property_value;
365+
if (!PyArg_ParseTuple(args, "sO:set_property", &property_name, &property_value)) {
366+
return NULL;
367+
}
368+
369+
UProperty *u_property = self->ue_object->GetClass()->FindPropertyByName(FName(UTF8_TO_TCHAR(property_name)));
370+
if (!u_property)
371+
return PyErr_Format(PyExc_Exception, "unable to find property %s", property_name);
372+
373+
if (PyFloat_Check(property_value)) {
374+
UFloatProperty *u_float_property = Cast<UFloatProperty>(u_property);
375+
if (u_float_property)
376+
u_float_property->SetPropertyValue(u_property->ContainerPtrToValuePtr<float>(self->ue_object), PyFloat_AsDouble(property_value));
377+
}
378+
379+
if (PyUnicode_Check(property_value)) {
380+
UStrProperty *u_str_property = Cast<UStrProperty>(u_property);
381+
if (u_str_property)
382+
u_str_property->SetPropertyValue(u_property->ContainerPtrToValuePtr<FString>(self->ue_object), FString(UTF8_TO_TCHAR(PyUnicode_AsUTF8(property_value))));
383+
}
384+
385+
386+
Py_INCREF(Py_None);
387+
return Py_None;
388+
389+
}
390+
335391
static PyObject *py_ue_properties(ue_PyUObject *self, PyObject * args) {
336392

337393
ue_py_check(self);
@@ -1153,6 +1209,7 @@ static PyObject *py_ue_destructible_apply_damage(ue_PyUObject *, PyObject *);
11531209
static PyObject *py_ue_set_view_target(ue_PyUObject *, PyObject *);
11541210
static PyObject *py_ue_add_actor_component(ue_PyUObject *, PyObject *);
11551211
static PyObject *py_ue_get_actor_component_by_type(ue_PyUObject *, PyObject *);
1212+
static PyObject *py_ue_add_actor_root_component(ue_PyUObject *, PyObject *);
11561213

11571214
static PyMethodDef ue_PyUObject_methods[] = {
11581215
{ "get_actor_location", (PyCFunction)py_ue_get_actor_location, METH_VARARGS, "" },
@@ -1164,6 +1221,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
11641221
{ "set_actor_location", (PyCFunction)py_ue_set_actor_location, METH_VARARGS, "" },
11651222
{ "set_actor_rotation", (PyCFunction)py_ue_set_actor_rotation, METH_VARARGS, "" },
11661223
{ "get_property", (PyCFunction)py_ue_get_property, METH_VARARGS, "" },
1224+
{ "set_property", (PyCFunction)py_ue_set_property, METH_VARARGS, "" },
11671225
{ "properties", (PyCFunction)py_ue_properties, METH_VARARGS, "" },
11681226
{ "call", (PyCFunction)py_ue_call, METH_VARARGS, "" },
11691227
{ "get_owner", (PyCFunction)py_ue_get_owner, METH_VARARGS, "" },
@@ -1196,6 +1254,7 @@ static PyMethodDef ue_PyUObject_methods[] = {
11961254
{ "destructible_apply_damage", (PyCFunction)py_ue_destructible_apply_damage, METH_VARARGS, "" },
11971255
{ "set_view_target", (PyCFunction)py_ue_set_view_target, METH_VARARGS, "" },
11981256
{ "add_actor_component", (PyCFunction)py_ue_add_actor_component, METH_VARARGS, "" },
1257+
{ "add_actor_root_component", (PyCFunction)py_ue_add_actor_root_component, METH_VARARGS, "" },
11991258
{ "get_actor_component_by_type", (PyCFunction)py_ue_get_actor_component_by_type, METH_VARARGS, "" },
12001259
{ "set_simulate_physics", (PyCFunction)py_ue_set_simulate_physics, METH_VARARGS, "" },
12011260
{ "get_world", (PyCFunction)py_ue_get_world, METH_VARARGS, "" },
@@ -1263,6 +1322,50 @@ static PyObject *py_ue_add_actor_component(ue_PyUObject * self, PyObject * args)
12631322
if (!component)
12641323
return PyErr_Format(PyExc_Exception, "unable to create component");
12651324

1325+
component->RegisterComponent();
1326+
1327+
PyObject *ret = (PyObject *)ue_get_python_wrapper(component);
1328+
if (!ret)
1329+
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
1330+
Py_INCREF(ret);
1331+
return ret;
1332+
1333+
}
1334+
1335+
static PyObject *py_ue_add_actor_root_component(ue_PyUObject * self, PyObject * args) {
1336+
1337+
ue_py_check(self);
1338+
1339+
PyObject *obj;
1340+
char *name;
1341+
if (!PyArg_ParseTuple(args, "Os:add_actor_root_component", &obj, &name)) {
1342+
return NULL;
1343+
}
1344+
1345+
if (!self->ue_object->IsA<AActor>()) {
1346+
return PyErr_Format(PyExc_Exception, "uobject is not an AActor");
1347+
}
1348+
1349+
AActor *actor = (AActor *)self->ue_object;
1350+
1351+
if (!PyObject_IsInstance(obj, (PyObject *)&ue_PyUObjectType)) {
1352+
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
1353+
}
1354+
1355+
ue_PyUObject *py_obj = (ue_PyUObject *)obj;
1356+
1357+
if (!py_obj->ue_object->IsA<UClass>()) {
1358+
return PyErr_Format(PyExc_Exception, "argument is not a class");
1359+
}
1360+
1361+
USceneComponent *component = NewObject<USceneComponent>(actor, (UClass *)py_obj->ue_object, FName(UTF8_TO_TCHAR(name)));
1362+
if (!component)
1363+
return PyErr_Format(PyExc_Exception, "unable to create component");
1364+
1365+
actor->SetRootComponent(component);
1366+
1367+
component->RegisterComponent();
1368+
12661369
PyObject *ret = (PyObject *)ue_get_python_wrapper(component);
12671370
if (!ret)
12681371
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");

0 commit comments

Comments
 (0)