Skip to content

Instantly share code, notes, and snippets.

@renaudll
Last active January 23, 2020 03:13
Show Gist options
  • Select an option

  • Save renaudll/cc1105fbe37eca4326145478c7ae85f4 to your computer and use it in GitHub Desktop.

Select an option

Save renaudll/cc1105fbe37eca4326145478c7ae85f4 to your computer and use it in GitHub Desktop.
"""
Remove the "Bebe Magic (tm)" reference edits from selected meshes.
"""
from contextlib import contextmanager
from maya import cmds
def _get_selected_meshes(sel):
meshes = []
for obj in sel:
for child in cmds.listRelatives(obj, fullPath=True, allDescendents=True):
if cmds.nodeType(child) == "mesh" and not cmds.getAttr(child + ".intermediateObject"):
meshes.append(child)
return meshes
def _clean_mesh_reference_edits(dagpath):
for editCommand in ("connectAttr", "disconnectAttr"):
cmds.referenceEdit("%s.inMesh" % dagpath, failedEdits=True, successfulEdits=True, editCommand=editCommand, removeEdits=True)
def _get_references_from_objs(sel):
return {cmds.referenceQuery(obj, referenceNode=True) for obj in sel}
@contextmanager
def _references_unloaded(references):
""" Context manager that temporary unload references.
:raises RuntimeError: If the selected nodes are not referenced.
"""
for reference in references:
cmds.file(unloadReference=reference)
yield
for reference in references:
cmds.file(loadReference=reference)
sel = cmds.ls(selection=True, long=True)
meshes = _get_selected_meshes(sel)
references = _get_references_from_objs(sel)
with _references_unloaded(references):
for mesh in meshes:
_clean_mesh_reference_edits(mesh)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment