vtkjs_helper.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from paraview import simple
  2. import os, json
  3. # -----------------------------------------------------------------------------
  4. def getAllNames():
  5. actorNameMapping = {}
  6. srcs = simple.GetSources()
  7. duplicates = {}
  8. for key, val in srcs.items():
  9. # Prevent name duplication
  10. nameToUse = key[0]
  11. if nameToUse in duplicates:
  12. count = 1
  13. newName = "%s (%d)" % (nameToUse, count)
  14. while newName in duplicates:
  15. count += 1
  16. newName = "%s (%d)" % (nameToUse, count)
  17. nameToUse = newName
  18. duplicates[nameToUse] = True
  19. representation = simple.GetRepresentation(val)
  20. if representation:
  21. vtkRepInstance = representation.GetClientSideObject()
  22. if "GetActiveRepresentation" in dir(vtkRepInstance):
  23. actorRep = vtkRepInstance.GetActiveRepresentation().GetActor()
  24. actorNameMapping[nameToUse] = actorRep
  25. return actorNameMapping
  26. # -----------------------------------------------------------------------------
  27. def getRenameMap():
  28. renameMap = {}
  29. names = getAllNames()
  30. view = simple.GetActiveView()
  31. renderer = view.GetClientSideObject().GetRenderer()
  32. viewProps = renderer.GetViewProps()
  33. volumes = renderer.GetVolumes()
  34. idx = 1
  35. for viewProp in viewProps:
  36. if not viewProp.IsA("vtkActor"):
  37. continue
  38. if not viewProp.GetVisibility():
  39. continue
  40. # The mapping will fail for multiblock that are composed of several blocks
  41. # Merge block should be used to solve the renaming issue for now
  42. # as the id is based on the a valid block vs representation.
  43. strIdx = "%s" % idx
  44. # Prop is valid if we can find it in the current sources
  45. for name, actor in names.items():
  46. if viewProp == actor:
  47. renameMap[strIdx] = name
  48. idx += 1
  49. break
  50. for volume in volumes:
  51. if not volume.IsA("vtkVolume"):
  52. continue
  53. if not volume.GetVisibility():
  54. continue
  55. strIdx = "%s" % idx
  56. for name, actor in names.items():
  57. if viewProp == actor:
  58. renameMap[strIdx] = name
  59. idx += 1
  60. break
  61. return renameMap
  62. # -----------------------------------------------------------------------------
  63. def applyParaViewNaming(directoryPath):
  64. renameMap = getRenameMap()
  65. scene = None
  66. filePath = os.path.join(directoryPath, "index.json")
  67. with open(filePath) as file:
  68. scene = json.load(file)
  69. for item in scene["scene"]:
  70. if item["name"] in renameMap:
  71. item["name"] = renameMap[item["name"]]
  72. with open(filePath, "w") as file:
  73. file.write(json.dumps(scene, indent=2))