Last active
January 10, 2021 22:57
-
-
Save BigRoy/bffef577089c4c0dca8f02f39b70499f to your computer and use it in GitHub Desktop.
Example using Alembic python api to get the full inclusive matrix for objects in the .abc file.
This file contains hidden or 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
import alembic.Abc | |
from alembic.AbcGeom import IXform | |
kWrapExisting = alembic.Abc.WrapExistingFlag.kWrapExisting | |
def get_matrix(obj): | |
if not IXform.matches(obj.getHeader()): | |
raise TypeError("Object is not an xform: {0}".format(obj)) | |
x = IXform(obj, kWrapExisting) | |
xs = x.getSchema().getValue() | |
return xs.getMatrix() | |
def get_inclusive_matrix(obj): | |
"""Return inclusive matrix of Alembic Object""" | |
matrix = get_matrix(obj) | |
parent = obj.getParent() | |
while parent: | |
try: | |
parent_matrix = get_matrix(parent) | |
except TypeError: | |
# Parent is not an xform | |
break | |
matrix *= parent_matrix | |
parent = parent.getParent() | |
return matrix | |
def get_alembic_transforms(filename): | |
"""Return transform matrices from the Alembic hierarchy | |
Arguments: | |
filename (str): Full path to Alembic archive to read. | |
Returns: | |
dict: Mapping of node full path with its world-space matrix. | |
""" | |
# 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() | |
node_matrices = {} | |
iterator = list(root.children) | |
for obj in iterator: | |
# Include children children | |
iterator.extend(obj.children) | |
node_path = obj.getFullName() | |
# Check if is transform | |
if not IXform.matches(obj.getHeader()): | |
continue | |
matrix = get_inclusive_matrix(obj) | |
node_matrices[node_path] = matrix | |
return node_matrices | |
path_matrices = get_alembic_transforms(path) | |
for node_path, matrix in path_matrices.items(): | |
print(node_path) | |
print("\t{0}".format(matrix)) | |
# Example output: | |
# /character_GRP/eye_L_GES | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP/mouth_GRP/back_mouth_GES | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP/tongue_GES | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP/stand_GES | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP/eye_R_GES | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP/body_GES | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP/mouth_GRP | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
# /character_GRP/mouth_GRP/teeth_GES | |
# M44d((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, 1)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment