Skip to content

Instantly share code, notes, and snippets.

@MitchellKehn
Last active January 27, 2021 06:36
Show Gist options
  • Save MitchellKehn/e46123a13d835a0384791dbf1ede72c0 to your computer and use it in GitHub Desktop.
Save MitchellKehn/e46123a13d835a0384791dbf1ede72c0 to your computer and use it in GitHub Desktop.
[Replace Alembic Filepath] replaces the filepath of an alembic readgeo node without popping up a dialog, and preserving the selection and imports with sensible rules #nuke
from altpipe.nuke.utils import replaceReadGeoAbcFilepath
for readgeo in nuke.selectedNodes("ReadGeo2"):
replaceReadGeoAbcFilepath(readgeo, readgeo["file"].getValue().replace("sh010", "sh020"))
def replaceReadGeoAbcFilepath(readgeo, newFilepath):
"""
Replaces the filepath of an alembic ReadGeo node. The item selection is preserved in the new tree.
How good the match is between the old tree and the new tree isn't always a given, so we
use the following rules to determine the output:
IMPORTED:
- if all items are imported in the old tree, all items are imported in the new tree
- if only some items are imported in the old tree, any matching items are imported in
the new tree, plus any new items
- if no items are imported in the old tree, all items are imported in the new tree
SELECTION:
- if all items are selected in the old tree, then all items will be selected in
the new tree, including any new ones
- if some items are selected in the old tree, but no items match in the
new tree, all items are selected in the new tree
- if no items are selected in the old tree, then only new/different items are selected
in the new tree
"""
oldTree = set(readgeo["scene_view"].getAllItems())
oldImport = set(readgeo["scene_view"].getImportedItems())
oldSelection = set(readgeo["scene_view"].getSelectedItems())
# setting the file knob via TCL is the only way to stop the alembic dialog from popping up
nuke.tcl('knob {node}.file "{filepath}"'.format(node=readgeo.name(), filepath=newFilepath))
# make the node force-load the scenegraph
readgeo.forceValidate()
# apply scenegraph transfer rules
newTree = set(readgeo["scene_view"].getAllItems())
if oldTree == oldImport:
newImport = newTree
elif oldImport:
newImport = newTree.intersection(oldImport) | newTree.difference(oldTree)
else:
newImport = newTree
if oldTree == oldSelection:
newSelection = newTree
elif oldSelection and oldSelection.isdisjoint(newTree):
newSelection = newTree
elif not oldSelection:
newSelection = newTree.difference(oldTree)
else:
newSelection = oldSelection
readgeo["scene_view"].setImportedItems(sorted(list(newImport)))
readgeo["scene_view"].setSelectedItems(sorted(list(newSelection)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment