Skip to content

Commit c3fa588

Browse files
author
Roberto De Ioris
committed
more threading fixes
1 parent c6285f5 commit c3fa588

File tree

2 files changed

+81
-78
lines changed

2 files changed

+81
-78
lines changed

Source/UnrealEnginePython/Private/UEPyEngine.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -530,21 +530,13 @@ PyObject *py_unreal_engine_new_object(PyObject * self, PyObject * args)
530530
uint64 flags = (uint64)(RF_Public);
531531
if (!PyArg_ParseTuple(args, "O|OsK:new_object", &obj, &py_outer, &name, &flags))
532532
{
533-
return NULL;
534-
}
535-
536-
if (!ue_is_pyuobject(obj))
537-
{
538-
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
533+
return nullptr;
539534
}
540535

541-
ue_PyUObject *py_obj = (ue_PyUObject *)obj;
542-
543-
if (!py_obj->ue_object->IsA<UClass>())
536+
UClass *obj_class = ue_py_check_type<UClass>(obj);
537+
if (!obj_class)
544538
return PyErr_Format(PyExc_Exception, "uobject is not a UClass");
545539

546-
UClass *obj_class = (UClass *)py_obj->ue_object;
547-
548540
FName f_name = NAME_None;
549541

550542
if (name && strlen(name) > 0)
@@ -556,30 +548,29 @@ PyObject *py_unreal_engine_new_object(PyObject * self, PyObject * args)
556548

557549
if (py_outer && py_outer != Py_None)
558550
{
559-
if (!ue_is_pyuobject(py_outer))
560-
{
551+
outer = ue_py_check_type<UObject>(py_outer);
552+
if (!outer)
561553
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
562-
}
563-
564-
ue_PyUObject *py_outer_obj = (ue_PyUObject *)py_outer;
565-
566-
outer = py_outer_obj->ue_object;
567554
}
568555

569-
UObject *new_object = NewObject<UObject>(outer, obj_class, f_name, (EObjectFlags)flags);
556+
UObject *new_object = nullptr;
557+
Py_BEGIN_ALLOW_THREADS;
558+
new_object = NewObject<UObject>(outer, obj_class, f_name, (EObjectFlags)flags);
559+
if (new_object)
560+
new_object->PostLoad();
561+
Py_END_ALLOW_THREADS;
562+
570563
if (!new_object)
571564
return PyErr_Format(PyExc_Exception, "unable to create object");
572565

573-
new_object->PostLoad();
574-
575566
Py_RETURN_UOBJECT(new_object);
576567
}
577568

578569
PyObject *py_unreal_engine_get_mutable_default(PyObject * self, PyObject * args)
579570
{
580571

581572
PyObject *obj;
582-
if (!PyArg_ParseTuple(args, "O|Os:new_object", &obj))
573+
if (!PyArg_ParseTuple(args, "O|Os:get_mutable_default", &obj))
583574
{
584575
return NULL;
585576
}

Source/UnrealEnginePython/Private/UObject/UEPyActor.cpp

Lines changed: 68 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,15 @@ PyObject *py_ue_actor_destroy_component(ue_PyUObject * self, PyObject * args)
116116
if (!component)
117117
return PyErr_Format(PyExc_Exception, "argument is not a UActorComponent");
118118

119+
Py_BEGIN_ALLOW_THREADS
119120
#if ENGINE_MINOR_VERSION >= 17
120-
component->DestroyComponent();
121+
component->DestroyComponent();
121122
#else
122-
actor->K2_DestroyComponent(component);
123+
actor->K2_DestroyComponent(component);
123124
#endif
125+
Py_END_ALLOW_THREADS
124126

125-
Py_INCREF(Py_None);
126-
return Py_None;
127+
Py_RETURN_NONE;
127128
}
128129

129130
PyObject *py_ue_actor_destroy(ue_PyUObject * self, PyObject * args)
@@ -137,9 +138,11 @@ PyObject *py_ue_actor_destroy(ue_PyUObject * self, PyObject * args)
137138
return PyErr_Format(PyExc_Exception, "uobject is not an AActor");
138139
}
139140

140-
actor->Destroy();
141+
Py_BEGIN_ALLOW_THREADS
142+
actor->Destroy();
143+
Py_END_ALLOW_THREADS
141144

142-
Py_RETURN_NONE;
145+
Py_RETURN_NONE;
143146

144147
}
145148

@@ -401,29 +404,16 @@ PyObject *py_ue_add_actor_component(ue_PyUObject * self, PyObject * args)
401404
PyObject *py_parent = nullptr;
402405
if (!PyArg_ParseTuple(args, "Os|O:add_actor_component", &obj, &name, &py_parent))
403406
{
404-
return NULL;
407+
return nullptr;
405408
}
406409

407-
if (!self->ue_object->IsA<AActor>())
408-
{
410+
AActor *actor = ue_py_check_type<AActor>(self);
411+
if (!actor)
409412
return PyErr_Format(PyExc_Exception, "uobject is not an AActor");
410-
}
411-
412-
AActor *actor = (AActor *)self->ue_object;
413413

414-
if (!ue_is_pyuobject(obj))
415-
{
416-
return PyErr_Format(PyExc_Exception, "argument is not a UObject");
417-
}
418-
419-
ue_PyUObject *py_obj = (ue_PyUObject *)obj;
420-
421-
if (!py_obj->ue_object->IsA<UClass>())
422-
{
414+
UClass *u_class = ue_py_check_type<UClass>(obj);
415+
if (!u_class)
423416
return PyErr_Format(PyExc_Exception, "argument is not a UClass");
424-
}
425-
426-
UClass *u_class = (UClass *)py_obj->ue_object;
427417

428418
if (!u_class->IsChildOf<UActorComponent>())
429419
{
@@ -440,24 +430,31 @@ PyObject *py_ue_add_actor_component(ue_PyUObject * self, PyObject * args)
440430
return PyErr_Format(PyExc_Exception, "argument is not a USceneComponent");
441431
}
442432
}
433+
UActorComponent *component = nullptr;
434+
Py_BEGIN_ALLOW_THREADS;
435+
component = NewObject<UActorComponent>(actor, u_class, FName(UTF8_TO_TCHAR(name)), RF_Public);
436+
if (component)
437+
{
443438

444-
UActorComponent *component = NewObject<UActorComponent>(actor, u_class, FName(UTF8_TO_TCHAR(name)), RF_Public);
445-
if (!component)
446-
return PyErr_Format(PyExc_Exception, "unable to create component");
439+
if (py_parent && component->IsA<USceneComponent>())
440+
{
441+
USceneComponent *scene_component = (USceneComponent *)component;
442+
scene_component->SetupAttachment(parent_component);
443+
}
447444

448-
if (py_parent && component->IsA<USceneComponent>())
449-
{
450-
USceneComponent *scene_component = (USceneComponent *)component;
451-
scene_component->SetupAttachment(parent_component);
452-
}
445+
if (actor->GetWorld() && !component->IsRegistered())
446+
{
447+
component->RegisterComponent();
448+
}
453449

454-
if (actor->GetWorld() && !component->IsRegistered())
455-
{
456-
component->RegisterComponent();
450+
if (component->bWantsInitializeComponent && !component->HasBeenInitialized() && component->IsRegistered())
451+
component->InitializeComponent();
457452
}
458453

459-
if (component->bWantsInitializeComponent && !component->HasBeenInitialized() && component->IsRegistered())
460-
component->InitializeComponent();
454+
Py_END_ALLOW_THREADS;
455+
456+
if (!component)
457+
return PyErr_Format(PyExc_Exception, "unable to create component");
461458

462459
Py_RETURN_UOBJECT(component);
463460
}
@@ -472,7 +469,7 @@ PyObject *py_ue_add_python_component(ue_PyUObject * self, PyObject * args)
472469
char *class_name;
473470
if (!PyArg_ParseTuple(args, "sss:add_python_component", &name, &module_name, &class_name))
474471
{
475-
return NULL;
472+
return nullptr;
476473
}
477474

478475
AActor *actor = ue_py_check_type<AActor >(self);
@@ -481,20 +478,27 @@ PyObject *py_ue_add_python_component(ue_PyUObject * self, PyObject * args)
481478
return PyErr_Format(PyExc_Exception, "uobject is not an AActor");
482479
}
483480

484-
UPythonComponent *component = NewObject<UPythonComponent>(actor, FName(UTF8_TO_TCHAR(name)), RF_Public);
485-
if (!component)
486-
return PyErr_Format(PyExc_Exception, "unable to create component");
481+
UPythonComponent *component = nullptr;
482+
Py_BEGIN_ALLOW_THREADS;
483+
component = NewObject<UPythonComponent>(actor, FName(UTF8_TO_TCHAR(name)), RF_Public);
487484

488-
component->PythonModule = FString(UTF8_TO_TCHAR(module_name));
489-
component->PythonClass = FString(UTF8_TO_TCHAR(class_name));
490-
491-
if (actor->GetWorld() && !component->IsRegistered())
485+
if (component)
492486
{
493-
component->RegisterComponent();
487+
component->PythonModule = FString(UTF8_TO_TCHAR(module_name));
488+
component->PythonClass = FString(UTF8_TO_TCHAR(class_name));
489+
490+
if (actor->GetWorld() && !component->IsRegistered())
491+
{
492+
component->RegisterComponent();
493+
}
494+
495+
if (component->bWantsInitializeComponent && !component->HasBeenInitialized() && component->IsRegistered())
496+
component->InitializeComponent();
494497
}
498+
Py_END_ALLOW_THREADS;
495499

496-
if (component->bWantsInitializeComponent && !component->HasBeenInitialized() && component->IsRegistered())
497-
component->InitializeComponent();
500+
if (!component)
501+
return PyErr_Format(PyExc_Exception, "unable to create component");
498502

499503
Py_RETURN_UOBJECT(component);
500504
}
@@ -782,14 +786,18 @@ PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject * args, PyObject *kwar
782786
rotation = py_rotation->rot;
783787
}
784788

789+
AActor *actor = nullptr;
790+
785791
if (kwargs && PyDict_Size(kwargs) > 0)
786792
{
787793
FTransform transform;
788794
transform.SetTranslation(location);
789795
transform.SetRotation(rotation.Quaternion());
790-
AActor *actor = world->SpawnActorDeferred<AActor>(u_class, transform);
791-
if (!actor)
792-
return PyErr_Format(PyExc_Exception, "unable to spawn a new Actor");
796+
Py_BEGIN_ALLOW_THREADS
797+
actor = world->SpawnActorDeferred<AActor>(u_class, transform);
798+
Py_END_ALLOW_THREADS
799+
if (!actor)
800+
return PyErr_Format(PyExc_Exception, "unable to spawn a new Actor");
793801
ue_PyUObject *py_actor = ue_get_python_uobject_inc(actor);
794802
if (!py_actor)
795803
return PyErr_Format(PyExc_Exception, "uobject is in invalid state");
@@ -806,13 +814,17 @@ PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject * args, PyObject *kwar
806814
}
807815
}
808816
Py_DECREF(py_iter);
809-
UGameplayStatics::FinishSpawningActor(actor, transform);
810-
return (PyObject *)py_actor;
817+
Py_BEGIN_ALLOW_THREADS
818+
UGameplayStatics::FinishSpawningActor(actor, transform);
819+
Py_END_ALLOW_THREADS
820+
return (PyObject *)py_actor;
811821
}
812822

813-
AActor *actor = world->SpawnActor(u_class, &location, &rotation);
814-
if (!actor)
815-
return PyErr_Format(PyExc_Exception, "unable to spawn a new Actor");
823+
Py_BEGIN_ALLOW_THREADS
824+
actor = world->SpawnActor(u_class, &location, &rotation);
825+
Py_END_ALLOW_THREADS
826+
if (!actor)
827+
return PyErr_Format(PyExc_Exception, "unable to spawn a new Actor");
816828
Py_RETURN_UOBJECT(actor);
817829

818830
}

0 commit comments

Comments
 (0)