A while ago I was asked by how you can set MASH points using JSON – a fairly specific request, but it’s pretty easy because the MASH Placer node uses a JSON dictionary to store it’s painted points, so we can tap into that to set the positions (and scale, and rotation and Id).
The JSON data is just some nonsense I made up, you can obviously use whatever you’d like here and make this as complex or simple as you’d like.
An alternative approach to this would be using the Python node and translating Python to MASH points in there, the disadvantage of this though is that the Python node recalculates every frame, which is unnecessary for this task (note: you can disconnect the Python node from the Time node to stop this behaviour so as is often the case with MASH, there’s more then one way to do this).
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
import json import MASH.api as mapi import maya.api.OpenMaya as nom import maya.cmds as cmds # Dummy JSON data from your file JSON = [{"assetName": "CoolAssetName", "matrix": [0.8345780997697541, 0.0, -0.5508896399322704, 386.43025219775876, 0.0, 1.0, 0.0, 1.7867262738341623, 0.5508896399322704, 0.0, 0.8345780997697541, 31148.033345175758, 0.0, 0.0, 0.0, 1.0], "filePath": "path/to/asset/asset.ma"}] # dummy shape for the MASH network dummyShape = cmds.polyCube(h=10) # make a new MASH network mashNetwork = mapi.Network() mashNetwork.createNetwork() # zero points from MASH cmds.setAttr(mashNetwork.distribute+".pointCount", 0) # add a Placer node (this accepts JSON to specify MASH point positions) placerNode = mashNetwork.addNode("MASH_Placer") finalJSON = {"positions":[], "rotations":[],"scale":[], "id":[]} # loop through the imported JSON data for entry in JSON: # nom.MMatrix requires 4 lists of 4 values, rather then just 16 entries as in C++ one = entry["matrix"][0:4] two = entry["matrix"][4:8] three = entry["matrix"][8:12] four = entry["matrix"][12:16] matrixList = [one, two, three, four] matrix = nom.MMatrix([one, two, three, four]) # create a transformation matrix tm = nom.MTransformationMatrix(matrix) # get the transform values position = tm.translation(nom.MSpace.kWorld) qrotation = tm.rotation(nom.MSpace.kWorld) rotation = qrotation.asEulerRotation() rotation *= 57.3 # convert to degrees scale = tm.scale(nom.MSpace.kWorld) # write these values to the JSON finalJSON["positions"].append([position.x, position.y, position.z]) finalJSON["rotations"].append([rotation.x, rotation.y, rotation.z]) finalJSON["scale"].append(scale) finalJSON["id"].append(0) #set the final JSON cmds.setAttr(placerNode.name+".paintJson", json.dumps(finalJSON), type='string') # you should now have a cube, at the origin, slightly rotated. |