Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save BigRoy/d6ac706e46d0800c15a0f26f9c18ed7d to your computer and use it in GitHub Desktop.
Save BigRoy/d6ac706e46d0800c15a0f26f9c18ed7d to your computer and use it in GitHub Desktop.
An example gist that shows how to compare different the hierarchy paths between published Alembic files and print out the differences. This is a more elaborate example of a prototype I wrote here: https://github.com/getavalon/core/issues/101#issuecomment-516337513
from avalon import api
from avalon import io
import alembic.Abc
def get_alembic_paths(filename):
"""Return all full hierarchy paths from alembic file"""
# Normalize alembic path
path = os.path.normpath(filename)
path = path.replace("\\", "/")
path = str(path) # path must be string
archive = alembic.Abc.IArchive(path)
root = archive.getTop()
paths = []
iterator = list(root.children)
for obj in iterator:
path = obj.getFullName()
paths.append(path)
# Include children's children
iterator.extend(obj.children)
return paths
def get_representations(asset_name, subset_name, representation_name):
"""Get all version's representations for a specific asset's subset."""
# Get asset and its subset by name
asset = io.find_one({"name": asset_name,
"type": "asset"})
subset = io.find_one({"name": subset_name,
"type": "subset",
"parent": asset["_id"]})
versions = io.find({"type": "version",
"parent": subset["_id"]})
versions_to_rep = {}
for version in versions:
rep = io.find_one({"name": representation_name,
"type": "representation",
"parent": version["_id"]})
if rep:
versions_to_rep[version["name"]] = rep
return versions_to_rep
# Get all .abc representations of all version for an asset's subset
representations = get_representations("characters_poo", "modelDefault", "abc")
# Compare each version with the previous version
previous = None
for version, representation in sorted(representations.items()):
path = api.get_representation_path(representation)
hierarchies = set(get_alembic_paths(path))
if previous is not None:
previous_version, previous_hierarchies = previous
print("Comparing v{0:03d} with v{1:03d}".format(previous_version, version))
created = sorted(hierarchies - previous_hierarchies)
for entry in created:
print("\tCreated {0}".format(entry))
removed = sorted(previous_hierarchies - hierarchies)
for entry in removed:
print("\tRemoved {0}".format(entry))
previous = (version, hierarchies)
# Example result:
# Comparing v001 with v002
# Comparing v002 with v003
# Comparing v003 with v004
# Created /poo_GRP/eye_L_GES
# Created /poo_GRP/eye_L_GES/eye_L_GESShape
# Created /poo_GRP/eye_R_GES
# Created /poo_GRP/eye_R_GES/eye_R_GESShape
# Comparing v004 with v005
# Comparing v005 with v006
# Comparing v006 with v007
# Created /poo_GRP/tongue_GES
# Created /poo_GRP/tongue_GES/tongue_GESShape
# Removed /poo_GRP/mouth_GRP/tongue_GES
# Removed /poo_GRP/mouth_GRP/tongue_GES/tongue_GESShape
# Comparing v007 with v008
# Comparing v008 with v009
# Comparing v009 with v010
# --
# Time taken: 0.3059 seconds (including database queries and alembic parsing)
@BigRoy
Copy link
Author

BigRoy commented Jul 30, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment