Advertisement
Guest User

Untitled

a guest
Mar 5th, 2013
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.05 KB | None | 0 0
  1. def separateFaces(faces):
  2.     """ Separates a list of faces and returns a mesh for each face, in the same order """
  3.    
  4.     faces = cmds.filterExpand(faces, sm=34, expand=True)
  5.     cmds.polyChipOff(faces, kft=False, dup=False, ch=False)
  6.  
  7.     # Can't figure out how to get the -rs flag to work properly unless the faces
  8.     # to separate are selected
  9.     cmds.select(faces, r=True)
  10.     mesh = faces[0].split('.')[0]
  11.     sep_meshes = cmds.polySeparate(mesh, rs=True)
  12.     polysep = sep_meshes.pop()
  13.     # The number of shells in the inputMesh
  14.     shell_count = cmds.getAttr('%s.icount' % polysep)
  15.     # An array of shellIds specifying the shells to separate
  16.     # If None, all shells are separated
  17.     separated_shells = cmds.getAttr('%s.remShells' % polysep)
  18.  
  19.     # mapping of {mesh.f[#] : #}
  20.     face_map = {}
  21.     for f in faces:
  22.         faceid = int(f.split('[')[-1].split(']')[0])
  23.         face_map[f] = faceid
  24.     faces_sort = [k for k, v in sorted(face_map.iteritems(), key=lambda x:x[1])]
  25.  
  26.     if separated_shells is None:
  27.         # All shells were separated
  28.         # The returned meshes are ordered based on shell id
  29.         if len(sep_meshes) > len(face_map):
  30.             # sep_meshes and face_map will only be the same length if every face in the mesh
  31.             # is selected and separated. Otherwise, there will be one extra mesh that
  32.             # contains all the unselected faces, and we need to find out at which index
  33.             # that mesh is located and remove it.
  34.            
  35.             # Since the returned meshes will be ordered by the lowest face id of each shell,
  36.             # the lowest face id that isn't in our selection will be the lowest face id of
  37.             # the "extra", unseparated mesh. That face id's ordered position in our face_ids
  38.             # list will tell what it's index in sep_meshes is.
  39.             # e.g. face_ids = [0, 1, 3, 6, 12]. The lowest id not in that list is 2, which
  40.             # would occupy the 2-index ([0, 1, 2, 3, 6, 12]) Thus the 2-index of sep_meshes
  41.             # is the leftover mesh
  42.             face_ids = face_map.values()
  43.             # +2 to account for the possibility that the other mesh's first faceid is
  44.             # higher than the highest faceid of the separated faces
  45.             face_id_range = set(range(max(face_ids) + 2))
  46.             remaining_ids = face_id_range.difference(face_ids)
  47.             lowest_rem_id = min(remaining_ids)
  48.             face_ids.append(lowest_rem_id)
  49.             face_ids.sort()
  50.             idx = face_ids.index(lowest_rem_id)
  51.             # idx is the leftover mesh, remove it from our separated meshes
  52.             del sep_meshes[idx]
  53.            
  54.         meshes = dict(zip(faces_sort, sep_meshes))
  55.     else:
  56.         # Not all shells were separated, meaning one of the resulting meshes contains
  57.         # multiple shells. The mesh with multiple shells will be the last of the returned meshes;
  58.         # the rest will be ordered by shell id
  59.         meshes = dict(zip(faces_sort, sep_meshes[:-1]))
  60.        
  61.     return [meshes[face] for face in faces]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement