Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. from maya import cmds
  2. import maya.api.OpenMaya as om
  3. from avalon.pipeline import AVALON_CONTAINER_ID
  4.  
  5. import avalon.maya.pipeline
  6.  
  7. # Current implementation
  8. ls = avalon.maya.pipeline._ls
  9.  
  10.  
  11. def ls1():
  12. """First query to only object sets and attribute .id
  13.  
  14. Then use API to get string value of the .id
  15. """
  16.  
  17. nodes = cmds.ls("*.id", exactType="objectSet", o=True, recursive=True)
  18.  
  19. ids = {AVALON_CONTAINER_ID,
  20. # Backwards compatibility
  21. "pyblish.mindbender.container"}
  22.  
  23. if not nodes:
  24. return
  25.  
  26. sel = om.MSelectionList()
  27. for node in nodes:
  28. sel.add(node)
  29.  
  30. fn_dep = om.MFnDependencyNode()
  31. for i, node in enumerate(nodes):
  32. dep_node = sel.getDependNode(i)
  33. fn_dep.setObject(dep_node)
  34. if fn_dep.findPlug("id", True).asString() in ids:
  35. yield node
  36.  
  37. def ls2():
  38. """Iterate with API over all set types"""
  39.  
  40. def _maya_iterate(iterator):
  41. """Helper to just iterate a maya iterator"""
  42. while not iterator.isDone():
  43. yield iterator.thisNode()
  44. iterator.next()
  45.  
  46. ids = {AVALON_CONTAINER_ID,
  47. # Backwards compatibility
  48. "pyblish.mindbender.container"}
  49.  
  50. # Getting the string attribute values is much faster through the Maya API
  51. # so the following might seem convoluted but it's much faster.
  52. fn_dep = om.MFnDependencyNode()
  53. iterator = om.MItDependencyNodes(om.MFn.kSet)
  54. for mobject in _maya_iterate(iterator):
  55. if mobject.apiTypeStr != "kSet":
  56. # Only match by exact type
  57. continue
  58.  
  59. fn_dep.setObject(mobject)
  60. try:
  61. plug = fn_dep.findPlug("id", True)
  62. except RuntimeError:
  63. continue
  64.  
  65. value = plug.asString()
  66. if value in ids:
  67. yield fn_dep.name()
  68.  
  69. def ls3():
  70.  
  71. nodes = cmds.ls(exactType="objectSet", recursive=True)
  72.  
  73. ids = {AVALON_CONTAINER_ID,
  74. # Backwards compatibility
  75. "pyblish.mindbender.container"}
  76.  
  77. if not nodes:
  78. return
  79.  
  80. sel = om.MSelectionList()
  81. for node in nodes:
  82. sel.add(node)
  83.  
  84. fn_dep = om.MFnDependencyNode()
  85. for i, node in enumerate(nodes):
  86. mobject = sel.getDependNode(i)
  87. fn_dep.setObject(mobject)
  88. try:
  89. plug = fn_dep.findPlug("id", True)
  90. except RuntimeError:
  91. continue
  92.  
  93. value = plug.asString()
  94. if value in ids:
  95. yield fn_dep.name()
  96.  
  97.  
  98.  
  99. s = time.time()
  100. result = list(ls())
  101. e = time.time()
  102. print "Current:", e-s
  103.  
  104. s = time.time()
  105. result1 = list(ls1())
  106. e = time.time()
  107. print "New 1:", e-s
  108.  
  109. s = time.time()
  110. result2 = list(ls2())
  111. e = time.time()
  112. print "New 2:", e-s
  113.  
  114. s = time.time()
  115. result3 = list(ls3())
  116. e = time.time()
  117. print "New 3:", e-s
  118.  
  119. # Sort all to ensure equality check is consistent
  120. result.sort()
  121. result1.sort()
  122. result2.sort()
  123. result3.sort()
  124.  
  125. assert result1 == result, "ls1() does not match ls()"
  126. assert result2 == result, "ls2() does not match ls()"
  127. assert result3 == result, "ls3() does not match ls()"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement