Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def separateFaces(faces):
- """ Separates a list of faces and returns a mesh for each face, in the same order """
- faces = cmds.filterExpand(faces, sm=34, expand=True)
- cmds.polyChipOff(faces, kft=False, dup=False, ch=False)
- # Can't figure out how to get the -rs flag to work properly unless the faces
- # to separate are selected
- cmds.select(faces, r=True)
- mesh = faces[0].split('.')[0]
- sep_meshes = cmds.polySeparate(mesh, rs=True)
- polysep = sep_meshes.pop()
- # The number of shells in the inputMesh
- shell_count = cmds.getAttr('%s.icount' % polysep)
- # An array of shellIds specifying the shells to separate
- # If None, all shells are separated
- separated_shells = cmds.getAttr('%s.remShells' % polysep)
- # mapping of {mesh.f[#] : #}
- face_map = {}
- for f in faces:
- faceid = int(f.split('[')[-1].split(']')[0])
- face_map[f] = faceid
- faces_sort = [k for k, v in sorted(face_map.iteritems(), key=lambda x:x[1])]
- if separated_shells is None:
- # All shells were separated
- # The returned meshes are ordered based on shell id
- if len(sep_meshes) > len(face_map):
- # sep_meshes and face_map will only be the same length if every face in the mesh
- # is selected and separated. Otherwise, there will be one extra mesh that
- # contains all the unselected faces, and we need to find out at which index
- # that mesh is located and remove it.
- # Since the returned meshes will be ordered by the lowest face id of each shell,
- # the lowest face id that isn't in our selection will be the lowest face id of
- # the "extra", unseparated mesh. That face id's ordered position in our face_ids
- # list will tell what it's index in sep_meshes is.
- # e.g. face_ids = [0, 1, 3, 6, 12]. The lowest id not in that list is 2, which
- # would occupy the 2-index ([0, 1, 2, 3, 6, 12]) Thus the 2-index of sep_meshes
- # is the leftover mesh
- face_ids = face_map.values()
- # +2 to account for the possibility that the other mesh's first faceid is
- # higher than the highest faceid of the separated faces
- face_id_range = set(range(max(face_ids) + 2))
- remaining_ids = face_id_range.difference(face_ids)
- lowest_rem_id = min(remaining_ids)
- face_ids.append(lowest_rem_id)
- face_ids.sort()
- idx = face_ids.index(lowest_rem_id)
- # idx is the leftover mesh, remove it from our separated meshes
- del sep_meshes[idx]
- meshes = dict(zip(faces_sort, sep_meshes))
- else:
- # Not all shells were separated, meaning one of the resulting meshes contains
- # multiple shells. The mesh with multiple shells will be the last of the returned meshes;
- # the rest will be ordered by shell id
- meshes = dict(zip(faces_sort, sep_meshes[:-1]))
- return [meshes[face] for face in faces]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement