如何将新的元数据添加到 maya 中的 alembic 文件?

How to add new meta data to alembic file in maya?

我想在现有的 abc 文件中添加一些额外的信息,或者如果可能的话,在使用 pyhon 的 maya 或任何 cg 应用程序中创建带有一些额外信息的 alembic 缓存时。 我很感激任何人都可以帮助我编辑 alembic 文件。

输入示例

meta_data = {'name': 'Hero', 'tag': 'test_show'}
abc_file = '/show/test_show/scene/hero.abc'

设置元数据?

from alembic import Abc

获取元数据

from alembic import Abc

archive = Abc.IArchive(abc_file)
top = archive.getTop()
meta_data = top.getMetaData()
print meta_data__str()

您不能只写任意数据,但可以设置描述和应用程序字符串:

from alembic import Abc

MY_APP = 'My cool application'


def write(abc_file, description):
    archive = Abc.CreateArchiveWithInfo(abc_file, MY_APP, description) 

def read(abc_file):
    archive = Abc.IArchive(abc_file)
    top = archive.getTop()
    return Abc.GetArchiveInfo(archive)

abc_file = 'alembic.abc'
write(abc_file, 'An abc file cool description')
archive_info = read(abc_file)
print(archive_info.get('appName'))
print(archive_info.get('userDescription'))
print(archive_info.get('whenWritten'))

这是一个完成复制源 alembic 文件并插入一些元数据的完整脚本:


import os
import alembic


def copy_props(i_props, o_props):
    '''
    Copy properties
    '''
    for index in range(i_props.getNumProperties()):
        header = i_props.getPropertyHeader(index)
        if header.isArray():
            i_prop = alembic.Abc.IArrayProperty(
                i_props,
                header.getName())
            prop_name = i_prop.getName()
            prop_meta = i_prop.getMetaData()
            o_prop = alembic.Abc.OArrayProperty(
                o_props,
                prop_name,
                i_prop.getDataType(),
                prop_meta,
                0)
            o_prop.setTimeSampling(i_prop.getTimeSampling())
            for i in range(i_prop.getNumSamples()):
                o_prop.setValue(i_prop.getValue(i))
        elif header.isScalar():
            i_prop = alembic.Abc.IScalarProperty(
                i_props,
                header.getName())
            prop_name = i_prop.getName()
            prop_meta = i_prop.getMetaData()
            o_prop = alembic.Abc.OScalarProperty(
                o_props,
                prop_name,
                i_prop.getDataType(),
                prop_meta,
                0)
            o_prop.setTimeSampling(i_prop.getTimeSampling())
            for i in range(i_prop.getNumSamples()):
                o_prop.setValue(i_prop.getValue(i))
        elif header.isCompound():
            i_prop = alembic.Abc.ICompoundProperty(
                i_props,
                header.getName())
            prop_name = i_prop.getName()
            prop_meta = i_prop.getMetaData()
            o_prop = alembic.Abc.OCompoundProperty(
                o_props,
                prop_name,
                prop_meta)
            copy_props(i_prop, o_prop)


def copy_object(i_obj, o_obj):
    '''
    Recursively copy object data
    '''
    if o_obj is None:
        return
    i_props = i_obj.getProperties()
    o_props = o_obj.getProperties()
    copy_props(i_props, o_props)
    for index in range(i_obj.getNumChildren()):
        i_child = i_obj.getChild(index)
        i_child_name = i_child.getName()
        i_child_meta = i_child.getMetaData()
        o_child = alembic.Abc.OObject(o_obj, i_child_name, i_child_meta)
        copy_object(i_child, o_child)


def copy_abc(i_path, o_path, app, description):
    '''
    Copy alembic file from i_path to o_path
    '''
    arc_in = alembic.Abc.IArchive(i_path)
    arc_out = alembic.Abc.OArchive(o_path, asOgawa=True)
    arc_out = alembic.Abc.CreateArchiveWithInfo(o_path, app, description)
    top_in = arc_in.getTop()
    top_out = arc_out.getTop()
    copy_object(top_in, top_out)


def read(abc_file):
    archive = alembic.Abc.IArchive(abc_file)
    return alembic.Abc.GetArchiveInfo(archive)


if __name__ == '__main__':
    i_path = os.path.join(
        os.path.dirname(__file__),
        'itest.abc'
    )
    o_path = os.path.join(
        os.path.dirname(__file__),
        'otest.abc'
    )
    copy_abc(i_path, o_path, 'Cool app', 'Cool description')
    print('Created archive: ' + o_path)
    archive_info = read(o_path)
    print('App name: ' + archive_info.get('appName'))
    print('Description: ' + archive_info.get('userDescription'))
    print('Written: ' + archive_info.get('whenWritten'))