Skip to content

Commit 61da86d

Browse files
author
Roberto De Ioris
committed
fixed quaternion and transform interactions
1 parent 8756b0b commit 61da86d

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

Source/UnrealEnginePython/Private/UObject/UEPyAnimSequence.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,8 @@ PyObject *py_ue_anim_sequence_get_raw_animation_track(ue_PyUObject * self, PyObj
5252
if (!anim_seq)
5353
return PyErr_Format(PyExc_Exception, "UObject is not a UAnimSequence.");
5454

55+
if (index < 0 || index >= anim_seq->GetAnimationTrackNames().Num())
56+
return PyErr_Format(PyExc_Exception, "invalid track index %d", index);
57+
5558
return py_ue_new_fraw_anim_sequence_track(anim_seq->GetRawAnimationTrack(index));
5659
}

Source/UnrealEnginePython/Private/Wrappers/UEPyFQuat.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ static PyObject *ue_py_fquat_seq_item(ue_PyFQuat *self, Py_ssize_t i) {
236236
PySequenceMethods ue_PyFQuat_sequence_methods;
237237

238238
static int ue_py_fquat_init(ue_PyFQuat *self, PyObject *args, PyObject *kwargs) {
239-
float x = 0, y = 0, z = 0, w = 0;
239+
float x = 0, y = 0, z = 0, w = 1;
240240
if (!PyArg_ParseTuple(args, "|ffff", &x, &y, &z, &w))
241241
return -1;
242242

Source/UnrealEnginePython/Private/Wrappers/UEPyFTransform.cpp

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,24 @@ static PyGetSetDef ue_PyFTransform_getseters[] = {
5454
{ NULL } /* Sentinel */
5555
};
5656

57+
static PyObject *ue_PyFTransform_str(ue_PyFTransform *self)
58+
{
59+
FVector vec = self->transform.GetTranslation();
60+
FRotator rot = self->transform.Rotator();
61+
FVector scale = self->transform.GetScale3D();
62+
63+
return PyUnicode_FromFormat("<unreal_engine.FTransform {'translation': (%S, %S, %S), 'rotation': (%S, %S, %S), 'scale': (%S, %S, %S)}>",
64+
PyFloat_FromDouble(vec.X),
65+
PyFloat_FromDouble(vec.Y),
66+
PyFloat_FromDouble(vec.Z),
67+
PyFloat_FromDouble(rot.Roll),
68+
PyFloat_FromDouble(rot.Pitch),
69+
PyFloat_FromDouble(rot.Yaw),
70+
PyFloat_FromDouble(scale.X),
71+
PyFloat_FromDouble(scale.Y),
72+
PyFloat_FromDouble(scale.Z)
73+
);
74+
}
5775

5876

5977
static PyTypeObject ue_PyFTransformType = {
@@ -72,7 +90,7 @@ static PyTypeObject ue_PyFTransformType = {
7290
0, /* tp_as_mapping */
7391
0, /* tp_hash */
7492
0, /* tp_call */
75-
0, /* tp_str */
93+
(reprfunc)ue_PyFTransform_str, /* tp_str */
7694
0, /* tp_getattro */
7795
0, /* tp_setattro */
7896
0, /* tp_as_buffer */
@@ -119,6 +137,9 @@ static int ue_py_ftransform_init(ue_PyFTransform *self, PyObject *args, PyObject
119137
return -1;
120138
}
121139
}
140+
else {
141+
self->transform.SetRotation(FQuat::Identity);
142+
}
122143

123144
// ensure scaling is set to 1,1,1
124145
FVector scale(1, 1, 1);
@@ -136,11 +157,31 @@ static int ue_py_ftransform_init(ue_PyFTransform *self, PyObject *args, PyObject
136157
return 0;
137158
}
138159

160+
static PyObject *ue_py_ftransform_mul(ue_PyFTransform *self, PyObject *value) {
161+
FTransform t = self->transform;
162+
if (ue_PyFQuat *py_quat = py_ue_is_fquat(value)) {
163+
t *= py_quat->quat;
164+
}
165+
else if (ue_PyFTransform *py_transform = py_ue_is_ftransform(value)) {
166+
t *= py_transform->transform;
167+
}
168+
else {
169+
return PyErr_Format(PyExc_TypeError, "FTransform can be multiplied only for an FQuat or an FTransform");
170+
}
171+
return py_ue_new_ftransform(t);
172+
}
173+
174+
PyNumberMethods ue_PyFTransform_number_methods;
175+
139176
void ue_python_init_ftransform(PyObject *ue_module) {
140177
ue_PyFTransformType.tp_new = PyType_GenericNew;
141178

142179
ue_PyFTransformType.tp_init = (initproc)ue_py_ftransform_init;
143180

181+
memset(&ue_PyFTransform_number_methods, 0, sizeof(PyNumberMethods));
182+
ue_PyFTransformType.tp_as_number = &ue_PyFTransform_number_methods;
183+
ue_PyFTransform_number_methods.nb_multiply = (binaryfunc)ue_py_ftransform_mul;
184+
144185
if (PyType_Ready(&ue_PyFTransformType) < 0)
145186
return;
146187

0 commit comments

Comments
 (0)