Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. # Python API example for skin weight access
  2. # Place this file into an lxserv folder
  3. #
  4. # Creates a command that takes the names of a mesh and a connected joint locator and inverts the weight values
  5. #
  6. # Example command usage:
  7. # example.printJointWeights locator:"Skeleton_Joint (4)" mesh:MyCube
  8.  
  9. import lx
  10. import lxu
  11. import lxifc
  12.  
  13.  
  14. def printJointWeights(jointLocatorName, meshName):
  15.  
  16. scn_svc = lx.service.Scene()
  17.  
  18. # Get the scene and the two items by name
  19. scene = lxu.select.SceneSelection().current()
  20. mesh_item = scene.ItemLookup(meshName)
  21. locator_item = scene.ItemLookup(jointLocatorName)
  22.  
  23. # Set the mesh up
  24. channelWrite = lx.object.ChannelWrite(scene.Channels(lx.symbol.s_ACTIONLAYER_SETUP, 0.0))
  25. channelRead = lx.object.ChannelRead(scene.Channels(None, 0.0))
  26. write_mesh_obj = channelWrite.ValueObj(mesh_item, mesh_item.ChannelLookup(lx.symbol.sICHAN_MESH_MESH))
  27. mesh = lx.object.Mesh(write_mesh_obj)
  28.  
  29. # We want to find the connected influence deformer using the 'deformers' graph
  30. graph = lx.object.ItemGraph(scene.GraphLookup('deformers'))
  31. desired_type = scn_svc.ItemTypeLookup('genInfluence')
  32.  
  33. # In the simplest case we can assume that the first found deformer is the influence
  34. deformer = graph.FwdByIndex(locator_item, 0)
  35.  
  36. # We need the internal name of the weight map to look it up using the meshmap accessor below
  37. mapname = lx.object.WeightMapDeformerItem(deformer).GetMapName(channelWrite)
  38. ui_name = deformer.UniqueName()
  39.  
  40. lx.out('-' * 80)
  41. lx.out(ui_name)
  42. lx.out(mapname)
  43.  
  44. # Set up a storage buffer to read the value into
  45. storageBuffer = lx.object.storage('f', 1)
  46.  
  47. # Loop through all points
  48. for index in range(mesh.PointCount()):
  49.  
  50. # Create a point- and meshmap accessor respectively and select the relevant component
  51. point_accessor = mesh.PointAccessor()
  52. point_accessor.SelectByIndex(index)
  53.  
  54. meshmap_accessor = mesh.MeshMapAccessor()
  55. meshmap_accessor.SelectByName(lx.symbol.i_VMAP_WEIGHT, mapname)
  56.  
  57. # Read and print the value
  58. if point_accessor.MapEvaluate(meshmap_accessor.ID(), storageBuffer):
  59. value = storageBuffer.get()
  60. lx.out(index, value)
  61.  
  62. # Write the inverted value back
  63. storageBuffer.set((1.0 - value[0],))
  64. point_accessor.SetMapValue(meshmap_accessor.ID(), storageBuffer)
  65.  
  66. # Update the mesh
  67. mesh.SetMeshEdits(lx.symbol.f_MESHEDIT_MAP_OTHER)
  68.  
  69.  
  70. class CmdPrintJointWeights(lxu.command.BasicCommand):
  71. def __init__(self):
  72. lxu.command.BasicCommand.__init__(self)
  73.  
  74. # Add two string arguments
  75. self.dyna_Add('locator', lx.symbol.sTYPE_STRING)
  76. self.dyna_Add('mesh', lx.symbol.sTYPE_STRING)
  77.  
  78. def cmd_Flags(self):
  79. return lx.symbol.fCMD_MODEL | lx.symbol.fCMD_UNDO
  80.  
  81. def basic_Enable(self, msg):
  82. return True
  83.  
  84. def cmd_Interact(self):
  85. pass
  86.  
  87. def basic_Execute(self, msg, flags):
  88. if self.dyna_IsSet(0) and self.dyna_IsSet(1):
  89.  
  90. locatorName = self.dyna_String(0, None)
  91. meshName = self.dyna_String(1, None)
  92.  
  93. printJointWeights(locatorName, meshName)
  94.  
  95. def cmd_Query(self, index, vaQuery):
  96. lx.notimpl()
  97.  
  98.  
  99. lx.bless(CmdPrintJointWeights, "example.printJointWeights")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement