Import, export and transfer
JSON import/export
Export weigths to file:
import os
from ngSkinTools2 import api as ngst_api
output_file_name = os.path.join(target_dir, 'export_sample_mesh.json')
ngst_api.export_json("sample_mesh", file=output_file_name)
Import weights from file:
from ngSkinTools2.api import InfluenceMappingConfig, VertexTransferMode
# prerequisites: mesh with skinCluster attached, "sampleMesh" in our case
# configure how influences described in a file will be matched against the scene
config = InfluenceMappingConfig()
config.use_distance_matching = True
config.use_name_matching = False
# run the import
ngst_api.import_json(
"sampleMesh",
file=source_file_name,
vertex_transfer_mode=VertexTransferMode.vertexId,
influences_mapping_config=config,
)
Transfer
Transfer with default settings: vertices mapped with closestPoint
rule, and influences with default settings for mesh-to-mesh transfer (using InfluenceMappingConfig.transfer_defaults()
).
from ngSkinTools2 import api as ngst_api
# prerequisites: we have two meshes with skinCluster attached ready
source = "sample_source_mesh"
destination = "sample_destination_mesh"
# normally we would assume that source mesh already has skin layers created,
# but for example's sake, just create some layers on the fly
source_layers = ngst_api.init_layers(source)
l1 = source_layers.add("layer1")
l1.set_weights(0, [1.0] * 4)
l1.set_weights(1, [0.2, 0.2, 0.8, 0.8])
l2 = source_layers.add("layer_two")
l2.set_weights(1, [1, 1, 0, 0])
l3 = source_layers.add("layer3", parent=l1)
l4 = source_layers.add("layer4", parent=l1)
# there is no need to init layers on destination,
# but if destination had layers already, then transferred layers
# would be created on top of old ones
# so this line is all you need, if you have source mesh ready with skinning layers,
# and destination mesh with skinCluster attached.
ngst_api.transfer_layers(source, destination)
To customize transfer options for vertex and influences mapping:
infl_config = ngst_api.InfluenceMappingConfig.transfer_defaults()
infl_config.use_label_matching = False
infl_config.use_distance_matching = True
infl_config.use_name_matching = False
ngst_api.transfer_layers(
"sample_source_mesh",
"sample_destination_mesh",
vertex_transfer_mode=ngst_api.VertexTransferMode.closestPoint,
influences_mapping_config=infl_config,
)