Skip to content

Commit 24ce588

Browse files
author
Roberto De Ioris
authored
Update SnippetsForStaticAndSkeletalMeshes.md
1 parent 184fa97 commit 24ce588

File tree

1 file changed

+85
-0
lines changed

1 file changed

+85
-0
lines changed

tutorials/SnippetsForStaticAndSkeletalMeshes.md

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,91 @@ This snippet shows how to build a new StaticMesh by combining multiple ones. It
197197

198198
Instead of automatically generating the asset name, a dialog will open asking for the path. Note: all of the selected Static Meshes will be merged in a single new one.
199199

200+
The objective is to merge the following static meshes:
201+
202+
![Static Meshes to merge](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/SnippetsForStaticAndSkeletalMeshes_Assets/static_meshes_to_merge.PNG)
203+
204+
```python
205+
import unreal_engine as ue
206+
from unreal_engine import FRawMesh
207+
from unreal_engine.classes import StaticMesh
208+
from unreal_engine.structs import StaticMeshSourceModel, MeshBuildSettings
209+
210+
def mesh_merge(meshes, merge_materials=False):
211+
new_path = ue.create_modal_save_asset_dialog('Choose destination path', '/Game')
212+
package_name = ue.object_path_to_package_name(new_path)
213+
object_name = ue.get_base_filename(new_path)
214+
215+
materials = []
216+
vertices = []
217+
texcoords = []
218+
normals = []
219+
indices = []
220+
material_indices = []
221+
for mesh in meshes:
222+
if not mesh.is_a(StaticMesh):
223+
raise DialogException('only StaticMeshes can be merged')
224+
225+
raw_mesh = mesh.get_raw_mesh()
226+
227+
# manage wedges
228+
original_indices = raw_mesh.get_wedge_indices()
229+
for i, index in enumerate(original_indices):
230+
original_indices[i] += len(vertices)
231+
indices += original_indices
232+
233+
# manage materials
234+
original_material_indices = raw_mesh.get_face_material_indices()
235+
for i, index in enumerate(original_material_indices):
236+
if original_material_indices[i] >= 0:
237+
original_material_indices[i] += len(material_indices)
238+
material_indices += original_material_indices
239+
240+
vertices += raw_mesh.get_vertex_positions()
241+
texcoords += raw_mesh.get_wedge_tex_coords()
242+
normals += raw_mesh.get_wedge_tangent_z()
243+
materials += mesh.StaticMaterials
244+
# give back control to ue to not block the interface too much
245+
ue.slate_tick()
246+
247+
# assemble the new mesh
248+
sm = StaticMesh(object_name, ue.get_or_create_package(package_name))
249+
new_raw_mesh = FRawMesh()
250+
new_raw_mesh.set_vertex_positions(vertices)
251+
new_raw_mesh.set_wedge_indices(indices)
252+
new_raw_mesh.set_wedge_tex_coords(texcoords)
253+
new_raw_mesh.set_wedge_tangent_z(normals)
254+
if not merge_materials:
255+
new_raw_mesh.set_face_material_indices(material_indices)
256+
257+
lod0 = StaticMeshSourceModel(BuildSettings=MeshBuildSettings(bRecomputeNormals=False, bRecomputeTangents=True, bUseMikkTSpace=True, bBuildAdjacencyBuffer=True, bRemoveDegenerates=True))
258+
259+
new_raw_mesh.save_to_static_mesh_source_model(lod0)
260+
261+
sm.SourceModels = [lod0]
262+
if merge_materials:
263+
materials = [materials[0]]
264+
sm.StaticMaterials = materials
265+
# rebuild the whole mesh
266+
sm.static_mesh_build()
267+
# re-do the body setup (collisions and friends)
268+
sm.static_mesh_create_body_setup()
269+
# notify the editor about the changes (not strictly required)
270+
sm.post_edit_change()
271+
272+
sm.save_package()
273+
274+
return sm
275+
276+
277+
new_mesh = mesh_merge(ue.get_selected_assets())
278+
279+
ue.open_editor_for_asset(new_mesh)
280+
281+
```
282+
283+
![Merged](https://github.com/20tab/UnrealEnginePython/blob/master/tutorials/SnippetsForStaticAndSkeletalMeshes_Assets/mesh_merged.PNG)
284+
200285
## Skeleton: Building a new tree
201286

202287
A Skeleton is an asset describing the tree of bones that influences a SkeletalMesh. While building a new skeleton (or adding a bone to it) is pretty easy, modifying or destroying a skeleton is always a risky operation. You should always generate a new Skeleton and the related SkeletalMesh whenever you need to change the bones tree.

0 commit comments

Comments
 (0)