Skip to content

Commit 87ffb46

Browse files
author
Roberto De Ioris
authored
Update SnippetsForStaticAndSkeletalMeshes.md
1 parent f2affef commit 87ffb46

1 file changed

Lines changed: 90 additions & 0 deletions

File tree

tutorials/SnippetsForStaticAndSkeletalMeshes.md

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,97 @@ This is a pretty crazy example, but will focus on a really important requirement
612612

613613
Here we take a SkeletalMesh and we "double it", paying attention to renaming each bone. The new tree will have a root, followed by two "relative roots" each with the whole bone tree. As it is pretty complex to explain, this time it is better to show the screenshot before:
614614

615+
![Merged Skel](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/SnippetsForStaticAndSkeletalMeshes_Assets/merged_skel.PNG)
615616

617+
```python
618+
import unreal_engine as ue
619+
from unreal_engine.classes import Skeleton, SkeletalMesh
620+
from unreal_engine import FTransform
621+
622+
original_mesh = ue.get_selected_assets()[0]
623+
624+
if not original_mesh.is_a(SkeletalMesh):
625+
raise DialogException('the script only works with Skeletal Meshes')
626+
627+
new_path = ue.create_modal_save_asset_dialog('Choose destination path', '/Game')
628+
package_name = ue.object_path_to_package_name(new_path)
629+
object_name = ue.get_base_filename(new_path)
630+
631+
original_skeleton = original_mesh.Skeleton
632+
633+
new_skeleton = Skeleton('{0}_Skeleton'.format(object_name), ue.get_or_create_package('{0}_Skeleton'.format(package_name)))
634+
635+
root_bone = new_skeleton.skeleton_add_bone('root', -1, FTransform())
636+
637+
# add a whole new skeleton tree to the main one
638+
# all of the old roots of sub-skeletons are attached to the main root
639+
def add_tree(suffix):
640+
for bone_index in range(0, original_skeleton.skeleton_bones_get_num()):
641+
# get the bone name
642+
bone_name = original_skeleton.skeleton_get_bone_name(bone_index)
643+
# fix name
644+
bone_name = '{0}_{1}'.format(bone_name, suffix)
645+
# get the transform of the bone
646+
bone_transform = original_skeleton.skeleton_get_ref_bone_pose(bone_index)
647+
# get the index of the parent bone
648+
parent_index = original_skeleton.skeleton_get_parent_index(bone_index)
649+
# if the parent is -1, use 0, otherwise search in the current tree
650+
if parent_index == -1:
651+
parent_index = root_bone
652+
else:
653+
original_parent_name = original_skeleton.skeleton_get_bone_name(parent_index)
654+
parent_name = '{0}_{1}'.format(original_parent_name, suffix)
655+
parent_index = new_skeleton.skeleton_find_bone_index(parent_name)
656+
new_skeleton.skeleton_add_bone(bone_name, parent_index, bone_transform)
657+
658+
659+
# add firt skeleton
660+
add_tree('OfFirstModel')
661+
# add the second skeleton
662+
add_tree('OfSecondModel')
663+
664+
new_skeleton.save_package()
665+
666+
new_mesh = SkeletalMesh(object_name, ue.get_or_create_package(package_name))
667+
668+
new_mesh.skeletal_mesh_set_skeleton(new_skeleton)
669+
670+
vertices = original_mesh.skeletal_mesh_get_lod()
671+
merged_vertices = []
672+
673+
# given the original index and a suffix, return the new bone index
674+
def get_new_bone_index(index, suffix):
675+
bone_name = original_skeleton.skeleton_get_bone_name(index)
676+
# fix name
677+
bone_name = '{0}_{1}'.format(bone_name, suffix)
678+
return new_skeleton.skeleton_find_bone_index(bone_name)
679+
680+
# fix bones indices for the first mesh
681+
for vertex in vertices:
682+
bones = list(vertex.influence_bones)
683+
for i in range(0, 8):
684+
bones[i] = get_new_bone_index(vertex.influence_bones[i], 'OfFirstModel')
685+
v = vertex.copy()
686+
v.influence_bones = bones
687+
merged_vertices.append(v)
688+
689+
# fix bones indices for the second mesh
690+
for vertex in vertices:
691+
bones = list(vertex.influence_bones)
692+
for i in range(0, 8):
693+
bones[i] = get_new_bone_index(vertex.influence_bones[i], 'OfSecondModel')
694+
v = vertex.copy()
695+
v.influence_bones = bones
696+
merged_vertices.append(v)
697+
698+
new_mesh.skeletal_mesh_build_lod(merged_vertices)
699+
700+
new_mesh.save_package()
701+
702+
ue.open_editor_for_asset(new_mesh)
703+
```
704+
705+
Note that all of the resulting bones will be "in use" when opening the Skeleton editor. If you want to mark only active bones, pass a list with all the valid indices to the skeletal_mesh_set_active_bones() method of a SkeletalMesh object.
616706

617707
## SkeletalMesh: Building from Collada
618708

0 commit comments

Comments
 (0)