Skip to content

Instantly share code, notes, and snippets.

@viktorasm
Created January 23, 2019 15:36
Show Gist options
  • Save viktorasm/6eeb670f28bfaa8e9cc5ecf3bccddd0e to your computer and use it in GitHub Desktop.
Save viktorasm/6eeb670f28bfaa8e9cc5ecf3bccddd0e to your computer and use it in GitHub Desktop.
Example of JSON import into ngSkinTools by skipping UI settings screen.
from ngSkinTools.importExport import JsonImporter
from ngSkinTools.influenceMapping import InfluenceMapping
from ngSkinTools.mllInterface import MllInterface
from ngSkinTools.ui.events import LayerEvents
from ngSkinTools.ui.layerDataModel import LayerDataModel
def ngSkinToolsImport(fileName, targetMesh):
'''
loads a JSON file (previously exported with ngSkinTools exporter) and loads data onto provided mesh.
:param fileName: json file to load ngSkinTools data from
:param targetMesh: skinned mesh (without ngSkinTools layers initialized yet)
'''
with open(fileName, 'r') as f:
sourceModel = JsonImporter().process(f.read())
# save loaded data to in-memory temporary mesh
sourceModel.saveTo(MllInterface.TARGET_REFERENCE_MESH)
sourceMll = MllInterface(mesh=MllInterface.TARGET_REFERENCE_MESH)
# initialize ngSkinTools data to target mesh
targetMll = MllInterface(mesh=targetMesh)
targetMll.initLayers()
# configure how source influences map to destination influences
# in transfer function we need to supply a source:destination map, where
# influences are represented by their index in source and destination skin cluster, e.g.
# {0:1, 1:0, 2:2} , etc.
# ngSkinTools internally uses a mapper that tries it's best to find related influences
# by their position in space and their names.
mapper = InfluenceMapping()
mapper.mirrorMode = False
mapper.distanceMatchRule.maxThreshold = 0.1
mapper.nameMatchRule.ignoreNamespaces = True
mapper.sourceInfluences = sourceModel.influences
mapper.destinationInfluences = targetMll.listInfluenceInfo()
mapper.calculate()
# other valid values: 'uvSpace', 'vertexId'
vertexTransferMode = 'closestPoint'
with targetMll.batchUpdateContext():
sourceMll.transferWeights(targetMesh, influencesMapping=mapper.mapping,
vertexTransferMode=vertexTransferMode)
# optional section: signal UI about changes in data
LayerDataModel.getInstance().updateLayerAvailability()
LayerEvents.layerListModified.emit()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment