unreal.SkinWeightModifier
¶
- class unreal.SkinWeightModifier(outer: Object | None = None, name: Name | str = 'None')¶
Bases:
Object
API used to modify skin weights on a Skeletal Mesh asset.
To use: 1. Instantiate an instance of USkinWeightModifier 2. Call “SetSkeletalMesh(MyMeshAsset)”, passing in the skeletal mesh you want to edit weights on 3. Use Get/Set weights functions, and Normalize/Prune to edit the weights as desired 4. When ready to commit to the asset, call CommitWeightsToSkeletalMesh() and save the asset if desired
This API can be used from C++, Blueprint or Python. Here is a sample usage of the API in Python:
import unreal
# create a weight modifier for a given skeletal mesh skel_mesh = unreal.EditorAssetLibrary().load_asset(“/Game/Characters/Wolf/Meshes/SK_Wolf”) weight_modifier = unreal.SkinWeightModifier() weight_modifier.set_skeletal_mesh(skel_mesh)
# get weight of vertex 1234 vertex_weights = weight_modifier.get_vertex_weights(1234) print(vertex_weights)
# remove neck2 as an influence on this vertex vertex_weights.pop(“neck2”) weight_modifier.set_vertex_weights(vertex_weights, True) print(vertex_weights)
# commit change to the skeletal mesh weight_modifier.commit_weights_to_skeletal_mesh()
OUTPUT: {“head”: 0.6, “neck1”: 0.3, “neck2”: 0.1} {“head”: 0.6, “neck1”: 0.3}
In Python, the per-vertex weights are stored as a dictionary mapping Bone Names to float weight values.
The “SetVertexWeights()” function expects the same data structure. You can add/remove/edit influences as needed. The SetVertexWeights() function does not normalize the weights. So you can make multiple modifications and call NormalizeVertexWeights() or NormalizeAllWeights() as desired.
The PruneVertexWeights() and EnforceMaxInfluences() functions can be used to trim small influences and clamp the total number of influences per vertex as needed.
Note that it is not required to normalize the weights by calling any of the normalize functions. Or manually before calling SetVertexWeights(). Committing the weights to the skeletal mesh will always enforce normalization.
Though it may be useful to normalize while editing.
C++ Source:
Plugin: MeshModelingToolsetExp
Module: SkeletalMeshModifiers
File: SkinWeightModifier.h
- commit_weights_to_skeletal_mesh() bool ¶
Actually applies the weight modifications to the skeletal mesh. This action creates an undo transaction. The skeletal mesh asset will be dirtied, but it is up to the user to save the asset if required.
- Returns:
true if weights were applied to a skeletal mesh, false otherwise
- Return type:
- enforce_max_influences(max_influences=-1) bool ¶
Strips out smallest influences to ensure each vertex does not have weight on more influences than MaxInfluences. Influences with the smallest weight are culled first.
- Parameters:
max_influences (int32) – The maximum number of influences to allow for each vertex in the mesh. If -1 is passed, will use the project-wide MaxInfluences amount.
- Returns:
true if influences were removed, false otherwise
- Return type:
- get_num_vertices() int32 ¶
Get the total number of vertices in the skeletal mesh.
- Returns:
int, number of vertices
- Return type:
int32
- get_skeletal_mesh() SkeletalMesh ¶
Get a reference to the skeletal mesh that was loaded
- Returns:
USkeletalMesh* - the mesh that was loaded, or null
- Return type:
- normalize_all_weights() bool ¶
Normalize weights on all vertices in the mesh.
- Returns:
true if normalization was performed, false otherwise
- Return type:
- normalize_vertex_weights(vertex_id) bool ¶
Normalize weights on the specified vertex.
- Parameters:
vertex_id (int32) – the index of the vertex to normalize weights on
- Returns:
true if normalization was performed, false otherwise
- Return type:
- prune_all_weights(weight_threshold) bool ¶
Remove all weights below the given threshold value, on all vertices.
- prune_vertex_weights(vertex_id, weight_threshold) bool ¶
Remove all weights below the given threshold value, on the given vertex. Influences that are pruned will no longer receive weight from normalization.
- set_skeletal_mesh(mesh) bool ¶
Call this first to load the weights for a skeletal mesh for fast editing.
- Parameters:
mesh (SkeletalMesh) – The skeletal mesh asset to load for weight editing
- Returns:
bool - true if the mesh weights were successfully loaded.
- Return type: