Created
June 30, 2022 19:19
-
-
Save rBrenick/189f3889763e5700681013ce5af7ccef to your computer and use it in GitHub Desktop.
Maya script for a combined Separate/Extract
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
FancySeparate by Richard Brenick | |
(written for JKeys) | |
A combined Separate/Extract script. | |
Also does some clean up of groups created by seperate function. | |
""" | |
import maya.cmds as cmds | |
import maya.mel as mel | |
def extract_selected_faces_as_duplicate_objects(): | |
selected_faces = cmds.ls(sl=True, flatten=True) | |
selected_meshes = list(set([f.split(".f[")[0] for f in selected_faces])) | |
new_meshes = [] | |
faces_to_delete = [] | |
for mesh in selected_meshes: | |
new_mesh = cmds.duplicate(mesh)[0] | |
new_meshes.append(new_mesh) | |
new_mesh_faces = [f.replace(mesh, new_mesh) for f in selected_faces] | |
for face in cmds.ls("{}.f[:]".format(new_mesh), flatten=True): | |
face_on_org_mesh = face.replace(new_mesh, mesh) | |
if face_on_org_mesh not in selected_faces: | |
faces_to_delete.append(face) | |
faces_to_delete.extend(selected_faces) | |
cmds.delete(faces_to_delete) | |
# center pivot | |
[cmds.xform(m, cpc=True) for m in new_meshes] | |
# select for convenience | |
cmds.select(new_meshes) | |
def separate_full_meshes(): | |
sel_meshes = cmds.ls(sl=True, type="transform") | |
new_meshes = [] | |
to_delete = [] | |
for sel in sel_meshes: | |
sel_parent = cmds.listRelatives(sel, parent=True) | |
sel_short_name = sel.split("|")[-1] | |
separate_result = cmds.polySeparate(sel, ch=True) | |
separated_meshes = cmds.ls(separate_result, type="transform") | |
renamed_sel = cmds.rename(sel, sel_short_name+"_TO_DELETE") | |
to_delete.append(renamed_sel) | |
mel.eval("DeleteHistory;") | |
for i, separated_mesh in enumerate(separated_meshes): | |
if sel_parent: | |
cmds.parent(separated_mesh, sel_parent) | |
else: | |
cmds.parent(separated_mesh, world=True) | |
new_mesh = cmds.rename(separated_mesh, sel_short_name) | |
new_meshes.append(new_mesh) | |
# these should now be fully seperated, so we can delete the empty node | |
cmds.delete(to_delete) | |
# center pivot | |
[cmds.xform(m, cpc=True) for m in new_meshes] | |
# select for convenience | |
cmds.select(new_meshes) | |
if __name__ == "__main__": | |
if cmds.ls(sl=True, type="transform"): | |
separate_full_meshes() | |
else: | |
extract_selected_faces_as_duplicate_objects() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment