Skip to content

Commit 8e67df9

Browse files
author
Roberto De Ioris
committed
refactored rotations with FRotator
1 parent 91f89c4 commit 8e67df9

8 files changed

Lines changed: 110 additions & 66 deletions

File tree

‎Source/UnrealEnginePython/Private/UEPyActor.cpp‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -430,15 +430,15 @@ PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject * args) {
430430

431431
ue_py_check(self);
432432

433-
float x = 0, y = 0, z = 0;
434-
float roll = 0, pitch = 0, yaw = 0;
433+
PyObject *py_obj_location = nullptr;
434+
PyObject *py_obj_rotation = nullptr;
435435

436436
UWorld *world = ue_get_uworld(self);
437437
if (!world)
438438
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
439439

440440
PyObject *obj;
441-
if (!PyArg_ParseTuple(args, "O|ffffff:actor_spawn", &obj, &x, &y, &z, &roll, &pitch, &yaw)) {
441+
if (!PyArg_ParseTuple(args, "O|OO:actor_spawn", &obj, &py_obj_location, &py_obj_rotation)) {
442442
return NULL;
443443
}
444444

@@ -448,8 +448,22 @@ PyObject *py_ue_actor_spawn(ue_PyUObject * self, PyObject * args) {
448448

449449
ue_PyUObject *py_obj = (ue_PyUObject *)obj;
450450

451-
FVector const location = FVector(x, y, z);
452-
FRotator const rotation = FRotator(pitch, yaw, roll);
451+
FVector location = FVector(0,0,0);
452+
FRotator rotation = FRotator(0,0,0);
453+
454+
if (py_obj_location) {
455+
ue_PyFVector *py_location = py_ue_is_fvector(py_obj_location);
456+
if (!py_location)
457+
return PyErr_Format(PyExc_Exception, "location must be an FVector");
458+
location = py_location->vec;
459+
}
460+
461+
if (py_obj_rotation) {
462+
ue_PyFRotator *py_rotation = py_ue_is_frotator(py_obj_rotation);
463+
if (!py_rotation)
464+
return PyErr_Format(PyExc_Exception, "location must be an FRotator");
465+
rotation = py_rotation->rot;
466+
}
453467

454468
AActor *actor = world->SpawnActor((UClass *)py_obj->ue_object, &location, &rotation);
455469

‎Source/UnrealEnginePython/Private/UEPyAttaching.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ PyObject *py_ue_get_socket_rotation(ue_PyUObject *self, PyObject * args) {
3535
USceneComponent *component = (USceneComponent *)self->ue_object;
3636

3737
FRotator rot = component->GetSocketRotation(UTF8_TO_TCHAR(socket_name));
38-
return Py_BuildValue("fff", rot.Roll, rot.Pitch, rot.Yaw);
38+
return py_ue_new_frotator(rot);
3939
}
4040

4141
PyObject *py_ue_get_socket_transform(ue_PyUObject *self, PyObject * args) {

‎Source/UnrealEnginePython/Private/UEPyEngine.cpp‎

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -110,29 +110,26 @@ PyObject *py_unreal_engine_print_string(PyObject * self, PyObject * args) {
110110
}
111111

112112
PyObject *py_unreal_engine_get_forward_vector(PyObject * self, PyObject * args) {
113-
float roll, pitch, yaw;
114-
if (!PyArg_ParseTuple(args, "fff:get_forward_vector", &roll, &pitch, &yaw)) {
113+
FRotator rot;
114+
if (!py_ue_rotator_arg(args, rot))
115115
return NULL;
116-
}
117-
FVector vec = UKismetMathLibrary::GetForwardVector(FRotator(pitch, yaw, roll));
116+
FVector vec = UKismetMathLibrary::GetForwardVector(rot);
118117
return py_ue_new_fvector(vec);
119118
}
120119

121120
PyObject *py_unreal_engine_get_right_vector(PyObject * self, PyObject * args) {
122-
float roll, pitch, yaw;
123-
if (!PyArg_ParseTuple(args, "fff:get_right_vector", &roll, &pitch, &yaw)) {
121+
FRotator rot;
122+
if (!py_ue_rotator_arg(args, rot))
124123
return NULL;
125-
}
126-
FVector vec = UKismetMathLibrary::GetRightVector(FRotator(pitch, yaw, roll));
124+
FVector vec = UKismetMathLibrary::GetRightVector(rot);
127125
return py_ue_new_fvector(vec);
128126
}
129127

130128
PyObject *py_unreal_engine_get_up_vector(PyObject * self, PyObject * args) {
131-
float roll, pitch, yaw;
132-
if (!PyArg_ParseTuple(args, "fff:get_up_vector", &roll, &pitch, &yaw)) {
129+
FRotator rot;
130+
if (!py_ue_rotator_arg(args, rot))
133131
return NULL;
134-
}
135-
FVector vec = UKismetMathLibrary::GetUpVector(FRotator(pitch, yaw, roll));
132+
FVector vec = UKismetMathLibrary::GetUpVector(rot);
136133
return py_ue_new_fvector(vec);
137134
}
138135

‎Source/UnrealEnginePython/Private/UEPyMovements.cpp‎

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ PyObject *py_ue_add_movement_input(ue_PyUObject *self, PyObject * args) {
7272

7373
ue_py_check(self);
7474

75-
float x, y, z;
75+
PyObject *py_obj_movement;
7676
float scale = 1;
7777
PyObject *py_force = nullptr;
7878
bool force = false;
79-
if (!PyArg_ParseTuple(args, "fff|fO:add_movement_input", &x, &y, &z, &scale, &py_force)) {
79+
if (!PyArg_ParseTuple(args, "O|fO:add_movement_input", &py_obj_movement, &scale, &py_force)) {
8080
return NULL;
8181
}
8282

@@ -102,7 +102,12 @@ PyObject *py_ue_add_movement_input(ue_PyUObject *self, PyObject * args) {
102102
if (!pawn)
103103
return PyErr_Format(PyExc_Exception, "uobject is not a pawn");
104104

105-
pawn->AddMovementInput(FVector(x, y, z), scale, force);
105+
ue_PyFVector *movement = py_ue_is_fvector(py_obj_movement);
106+
107+
if (!movement)
108+
return PyErr_Format(PyExc_Exception, "movement input must be a FVector");
109+
110+
pawn->AddMovementInput(movement->vec, scale, force);
106111

107112
Py_INCREF(Py_None);
108113
return Py_None;
@@ -131,7 +136,7 @@ PyObject *py_ue_get_control_rotation(ue_PyUObject *self, PyObject * args) {
131136
return PyErr_Format(PyExc_Exception, "uobject is not a pawn");
132137

133138
FRotator rot = pawn->GetControlRotation();
134-
return Py_BuildValue("fff", rot.Roll, rot.Pitch, rot.Yaw);
139+
return py_ue_new_frotator(rot);
135140
}
136141

137142
PyObject *py_ue_jump(ue_PyUObject *self, PyObject * args) {
@@ -251,12 +256,12 @@ PyObject *py_ue_launch(ue_PyUObject *self, PyObject * args) {
251256

252257
ue_py_check(self);
253258

254-
float x, y, z;
259+
PyObject *py_obj_force;
255260
PyObject *py_xy_override = nullptr;
256261
PyObject *py_z_override = nullptr;
257262
bool xy_override = false;
258263
bool z_override;
259-
if (!PyArg_ParseTuple(args, "fff|OO:launch", &x, &y, &z, &py_xy_override, &z_override)) {
264+
if (!PyArg_ParseTuple(args, "O|OO:launch", &py_obj_force, &py_xy_override, &z_override)) {
260265
return NULL;
261266
}
262267

@@ -286,7 +291,12 @@ PyObject *py_ue_launch(ue_PyUObject *self, PyObject * args) {
286291
z_override = true;
287292
}
288293

289-
character->LaunchCharacter(FVector(x, y, z), xy_override, z_override);
294+
ue_PyFVector *force = py_ue_is_fvector(py_obj_force);
295+
296+
if (!force)
297+
return PyErr_Format(PyExc_Exception, "launch force must be a FVector");
298+
299+
character->LaunchCharacter(force->vec, xy_override, z_override);
290300

291301
Py_INCREF(Py_None);
292302
return Py_None;

‎Source/UnrealEnginePython/Private/UEPyPhysics.cpp‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ PyObject *py_ue_destructible_apply_damage(ue_PyUObject * self, PyObject * args)
4141

4242
float damage_amount = 0;
4343
float impulse_strength = 0;
44-
float x = 0, y = 0, z = 0;
45-
float ix = 0, iy = 0, iz = 0;
46-
if (!PyArg_ParseTuple(args, "ffffffff:destructible_apply_damage", &damage_amount, &impulse_strength, &x, &y, &z, &ix, &iy, &iz)) {
44+
PyObject *py_obj_location = nullptr;
45+
PyObject *py_obj_impulse = nullptr;
46+
if (!PyArg_ParseTuple(args, "ffOO:destructible_apply_damage", &damage_amount, &impulse_strength, &py_obj_location, &py_obj_impulse)) {
4747
return NULL;
4848
}
4949

@@ -64,8 +64,25 @@ PyObject *py_ue_destructible_apply_damage(ue_PyUObject * self, PyObject * args)
6464
}
6565
}
6666

67+
FVector location = FVector(0, 0, 0);
68+
FVector impulse = FVector(0, 0, 0);
69+
70+
if (py_obj_location) {
71+
ue_PyFVector *py_location = py_ue_is_fvector(py_obj_location);
72+
if (!py_location)
73+
return PyErr_Format(PyExc_Exception, "location must be a FVector");
74+
location = py_location->vec;
75+
}
76+
77+
if (py_obj_impulse) {
78+
ue_PyFVector *py_impulse = py_ue_is_fvector(py_obj_impulse);
79+
if (!py_impulse)
80+
return PyErr_Format(PyExc_Exception, "impulse must be a FVector");
81+
impulse = py_impulse->vec;
82+
}
83+
6784
if (destructible) {
68-
destructible->ApplyDamage(damage_amount, FVector(x, y, z), FVector(ix, iy, iz), impulse_strength);
85+
destructible->ApplyDamage(damage_amount, location, impulse, impulse_strength);
6986
}
7087
else {
7188
return PyErr_Format(PyExc_Exception, "UObject is not a destructible");

‎Source/UnrealEnginePython/Private/UEPyTimer.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ PyObject *py_ue_set_timer(ue_PyUObject *self, PyObject * args) {
8383
PyObject *py_callable;
8484
PyObject *py_loop = nullptr;
8585
float first_delay = -1;
86-
if (!PyArg_ParseTuple(args, "fO|Of:simple_move_to_location", &rate, &py_callable, &py_loop, &first_delay)) {
86+
if (!PyArg_ParseTuple(args, "fO|Of:set_timer", &rate, &py_callable, &py_loop, &first_delay)) {
8787
return NULL;
8888
}
8989

‎Source/UnrealEnginePython/Private/UEPyTraceAndSweep.cpp‎

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,28 @@ PyObject *py_ue_line_trace_single_by_channel(ue_PyUObject * self, PyObject * arg
66

77
ue_py_check(self);
88

9-
float x1 = 0, y1 = 0, z1 = 0;
10-
float x2 = 0, y2 = 0, z2 = 0;
9+
PyObject *py_obj_start;
10+
PyObject *py_obj_end;
1111
int channel;
1212

1313
UWorld *world = ue_get_uworld(self);
1414
if (!world)
1515
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
1616

1717

18-
if (!PyArg_ParseTuple(args, "ffffffi:line_trace_single_by_channel", &x1, &y1, &z1, &x2, &y2, &z2, &channel)) {
18+
if (!PyArg_ParseTuple(args, "OOi:line_trace_single_by_channel", &py_obj_start, &py_obj_end, &channel)) {
1919
return NULL;
2020
}
2121

22+
ue_PyFVector *start = py_ue_is_fvector(py_obj_start);
23+
ue_PyFVector *end = py_ue_is_fvector(py_obj_end);
24+
25+
if (!start || !end)
26+
return PyErr_Format(PyExc_Exception, "start and end location must be vectors");
27+
2228
FHitResult hit;
2329

24-
bool got_hit = world->LineTraceSingleByChannel(hit, FVector(x1, y1, z1), FVector(x2, y2, z2), (ECollisionChannel)channel);
30+
bool got_hit = world->LineTraceSingleByChannel(hit, start->vec, end->vec, (ECollisionChannel)channel);
2531

2632
if (got_hit) {
2733
return py_ue_new_fhitresult(hit);
@@ -36,25 +42,31 @@ PyObject *py_ue_line_trace_multi_by_channel(ue_PyUObject * self, PyObject * args
3642

3743
ue_py_check(self);
3844

39-
float x1 = 0, y1 = 0, z1 = 0;
40-
float x2 = 0, y2 = 0, z2 = 0;
45+
PyObject *py_obj_start;
46+
PyObject *py_obj_end;
4147
int channel;
4248

4349
UWorld *world = ue_get_uworld(self);
4450
if (!world)
4551
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
4652

4753

48-
if (!PyArg_ParseTuple(args, "ffffffi:line_trace_multi_by_channel", &x1, &y1, &z1, &x2, &y2, &z2, &channel)) {
54+
if (!PyArg_ParseTuple(args, "OOOi:line_trace_multi_by_channel", &py_obj_start, &py_obj_end, &channel)) {
4955
return NULL;
5056
}
5157

58+
ue_PyFVector *start = py_ue_is_fvector(py_obj_start);
59+
ue_PyFVector *end = py_ue_is_fvector(py_obj_end);
60+
61+
if (!start || !end)
62+
return PyErr_Format(PyExc_Exception, "start and end location must be vectors");
63+
5264
TArray<struct FHitResult> hits;
5365
hits.Reset();
5466

5567
PyObject *hits_list = PyList_New(0);
5668

57-
bool got_hits = world->LineTraceMultiByChannel(hits, FVector(x1, y1, z1), FVector(x2, y2, z2), (ECollisionChannel)channel);
69+
bool got_hits = world->LineTraceMultiByChannel(hits, start->vec, end->vec, (ECollisionChannel)channel);
5870

5971
if (got_hits) {
6072
for (int i = 0; i < hits.Num(); i++) {
@@ -110,8 +122,8 @@ PyObject *py_ue_draw_debug_line(ue_PyUObject * self, PyObject * args) {
110122

111123
ue_py_check(self);
112124

113-
float x1 = 0, y1 = 0, z1 = 0;
114-
float x2 = 0, y2 = 0, z2 = 0;
125+
PyObject *py_obj_start;
126+
PyObject *py_obj_end;
115127
uint8 r = 1, g = 0, b = 0;
116128
float duration = 0;
117129

@@ -120,11 +132,17 @@ PyObject *py_ue_draw_debug_line(ue_PyUObject * self, PyObject * args) {
120132
return PyErr_Format(PyExc_Exception, "unable to retrieve UWorld from uobject");
121133

122134

123-
if (!PyArg_ParseTuple(args, "ffffff|iiif:draw_debug_line", &x1, &y1, &z1, &x2, &y2, &z2, &r, &g, &b, &duration)) {
135+
if (!PyArg_ParseTuple(args, "OO|iiif:draw_debug_line", &py_obj_start, &py_obj_end, &r, &g, &b, &duration)) {
124136
return NULL;
125137
}
126138

127-
UKismetSystemLibrary::DrawDebugLine(world, FVector(x1, y1, z1), FVector(x2, y2, z2), FColor(r, g, b), false, duration);
139+
ue_PyFVector *start = py_ue_is_fvector(py_obj_start);
140+
ue_PyFVector *end = py_ue_is_fvector(py_obj_end);
141+
142+
if (!start || !end)
143+
return PyErr_Format(PyExc_Exception, "start and end location must be vectors");
144+
145+
UKismetSystemLibrary::DrawDebugLine(world, start->vec, end->vec, FColor(r, g, b), false, duration);
128146

129147
Py_INCREF(Py_None);
130148
return Py_None;

‎Source/UnrealEnginePython/Private/UEPyTransform.cpp‎

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -117,24 +117,14 @@ PyObject *py_ue_set_actor_rotation(ue_PyUObject *self, PyObject * args) {
117117

118118
ue_py_check(self);
119119

120-
float roll, pitch, yaw;
121-
if (!PyArg_ParseTuple(args, "fff:set_actor_rotation", &roll, &pitch, &yaw)) {
120+
FRotator rot;
121+
if (!py_ue_rotator_arg(args, rot))
122122
return NULL;
123-
}
124-
125-
if (self->ue_object->IsA<AActor>()) {
126-
((AActor *)self->ue_object)->SetActorRotation(FRotator(pitch, yaw, roll));
127-
goto ret;
128-
}
129-
130-
if (self->ue_object->IsA<UActorComponent>()) {
131-
((UActorComponent *)self->ue_object)->GetOwner()->SetActorRotation(FRotator(pitch, yaw, roll));
132-
goto ret;
133-
}
134-
135-
return PyErr_Format(PyExc_Exception, "uobject is not an actor or a component");
136123

137-
ret:
124+
AActor *actor = ue_get_actor(self);
125+
if (!actor)
126+
return PyErr_Format(PyExc_Exception, "uobject is not an actor or a component");
127+
actor->SetActorRotation(rot);
138128
Py_INCREF(Py_None);
139129
return Py_None;
140130

@@ -238,12 +228,11 @@ PyObject *py_ue_set_world_location(ue_PyUObject *self, PyObject * args) {
238228

239229
PyObject *py_ue_set_world_rotation(ue_PyUObject *self, PyObject * args) {
240230
ue_py_check(self);
241-
float roll, pitch, yaw;
242-
if (!PyArg_ParseTuple(args, "fff:set_world_rotation", &roll, &pitch, &yaw)) {
231+
FRotator rot;
232+
if (!py_ue_rotator_arg(args, rot))
243233
return NULL;
244-
}
245234
if (self->ue_object->IsA<USceneComponent>()) {
246-
((USceneComponent *)self->ue_object)->SetWorldRotation(FRotator(pitch, yaw, roll));
235+
((USceneComponent *)self->ue_object)->SetWorldRotation(rot);
247236
Py_INCREF(Py_None);
248237
return Py_None;
249238
}
@@ -280,12 +269,11 @@ PyObject *py_ue_set_relative_location(ue_PyUObject *self, PyObject * args) {
280269

281270
PyObject *py_ue_set_relative_rotation(ue_PyUObject *self, PyObject * args) {
282271
ue_py_check(self);
283-
float roll, pitch, yaw;
284-
if (!PyArg_ParseTuple(args, "fff:set_relative_rotation", &roll, &pitch, &yaw)) {
272+
FRotator rot;
273+
if (!py_ue_rotator_arg(args, rot))
285274
return NULL;
286-
}
287275
if (self->ue_object->IsA<USceneComponent>()) {
288-
((USceneComponent *)self->ue_object)->SetRelativeRotation(FRotator(pitch, yaw, roll));
276+
((USceneComponent *)self->ue_object)->SetRelativeRotation(rot);
289277
Py_INCREF(Py_None);
290278
return Py_None;
291279
}

0 commit comments

Comments
 (0)