You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 11, 2024. It is now read-only.
Copy file name to clipboardExpand all lines: tutorials/SnippetsForStaticAndSkeletalMeshes.md
+165-2Lines changed: 165 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -854,6 +854,169 @@ Note, that this example use the original skeleton for the mesh. For real-world c
854
854
855
855
## SkeletalMesh: Building from Collada
856
856
857
+
There is already a tutorial for importing collada files as static meshes (https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/WritingAColladaFactoryWithPython.md). This snippet shows how to import skeletal meshes. You can combine both to build a full-featured importer.
858
+
859
+
The main topic here is how to deal with matrices. Collada files expose bone infos as 4x4 column major matrices. FTransform objects can be built by passing them a 4x4 matrix in the UE4 convention (row-major). Collada obviously follows the OpenGL conventions (column-major), so we need to transpose all matrices. The pycollada modules returns matrices as numpy arrays, so in addition to transposing we need to flatten them (FTransform expects a simple iterator of 16 float elements).
860
+
861
+
Remember to install the pycollada module:
862
+
863
+
```
864
+
pip install pycollada
865
+
```
866
+
867
+
Here is the code, note that this time we do not save assets. All of the objects are transient (storing them is left as exercise).
868
+
869
+
Check how we need to fix UVs too, as UE4 do not use the OpenGL convention (damn, this is starting to become annoying ;) of texcoords origin on the left-bottom.
870
+
871
+
```python
872
+
import unreal_engine as ue
873
+
from unreal_engine.classes import Skeleton, SkeletalMesh
874
+
from unreal_engine import FTransform, FSoftSkinVertex, FVector, FRotator, FQuat
875
+
876
+
from collada import Collada
877
+
import numpy
878
+
879
+
classColladaLoader:
880
+
881
+
def__init__(self, filename):
882
+
self.dae = Collada(filename)
883
+
self.controller =self.dae.controllers[0]
884
+
# while i love quaternions, building them from euler rotations
885
+
# looks generally easier to the reader...
886
+
# this rotation is required for fixing the inverted Z axis on
887
+
# bone positions. As bind pose should have rotations set to 0
888
+
# we will apply this quaternion directly to bone positions
filename = ue.open_file_dialog('Choose a Collada file', '', '', 'Collada|*.dae;')[0]
988
+
loader = ColladaLoader(filename)
989
+
ue.open_editor_for_asset(loader.skeleton)
990
+
```
991
+
992
+
A note about joints management in UE4.
993
+
994
+
Unreal expects each vertex weights to be normalized. It means their sum must be 255 (maximum value for FSoftSkinVertex). If the sum
995
+
is not 255, UE4 will automatically add the required weight to the first bone influence.
996
+
997
+
This behaviour could lead to annoying errors generated by float errors (Collada exposes bone weights as float value in the range 0..1).
998
+
999
+
As an example, most of the vertices will result in a total weight of 254 (instead of 255), and (more bad), Collada does not enforce the first influence to be the one with the highest value.
1000
+
1001
+
For both reasons we add 1 to 0 weights (collada files does not report bone influences with a weight of 0, so we are safe) and, more important, we reorder bone influences in a way that the first one is always the one with the higher weight (more infos in the code comments):
Morph Targets (or Blend Shapes), are a simple form of animation where a specific vertex (and eventually its normal) is translated to a new position. By interpolating the transition from the default position to the new one defined by the morph target, you can generate an animation. Morph Targets are common in facial animations or, more generally, whenever an object must be heavily deformed.
@@ -1046,8 +1209,8 @@ This method is how we build the skeleton:
0 commit comments