Reading an Atoms Cache

import Atoms
import AtomsCore
from Atoms import GLOBAL_NAMES

def _load_agent_type_from_cache(agent_types_path, agent_type_name):
    archive = AtomsCore.Archive()
    if archive.readFromFile("%s/%s.agentType" % (agent_types_path,
                                                 agent_type_name)):
        agent_types = Atoms.AgentTypes.instance()
        agent_type = Atoms.AgentType()
        agent_type.deserialise(archive)
        agent_types.addAgentType(agent_type_name, agent_type, True)
        return agent_types.agentType(agent_type_name)


cache_path = "path/to/my/cache.atoms"

atoms_cache = Atoms.AtomsCache()
folder = os.path.dirname(cache_path)
atoms_cache.openCache(folder, os.path.splitext(os.path.basename(cache_path))[0])
start_frame = atoms_cache.startFrame()
end_frame = atoms_cache.endFrame()

atoms_cache.loadFrame(start_frame)

# read ids
path = '/'.join([atoms_cache.cachePath(), atoms_cache.cacheName()])
current_ids = set()
for f in range(start_frame, end_frame + 1):
    fh_path = path + ".%04d.header.atoms" % f
    archive = AtomsCore.Archive()
    if not archive.readFromFile(fh_path):
        continue

    m = AtomsCore.MapMetadata()
    m.deserialise(archive)

    agents_created = set(m["agentsCreated"].value())
    current_ids = current_ids | agents_created
    current_ids = current_ids - set(m["agentsDeleted"].value())

# read agent type
atn = atoms_cache.agentType(start_frame, list(current_ids)[-1])
agent_type = Atoms.AgentTypes.instance().agentType(atn)
if not agent_type:
    agent_type = _load_agent_type_from_cache(atoms_cache.cachePath() + "/agentTypes", atn)

for id in current_ids :
    
    # joint names
    skeleton = agent_type.skeleton()
    for i in range(skeleton .numJoints()):
        joint = skeleton[i]
        print joint.name()
        # bind matrix
        mat = joint.matrix()  

# read animation
id = current_ids.pop()
for f in range(start_frame, end_frame + 1):
    atoms_cache.loadFrame(f)
    # use the pose object to get the joint trans
    pose = AtomsCore.Pose()
    atoms_cache.loadAgentPose(f, id, pose)

    for ji in range(pose.numJoints()):
       	atoms_joint = agent_type.skeleton().joint(ji)
        name = atoms_joint.name()
        jp = pose.jointPose(ji)
        
        print jp.translation, jp.rotation, jp.scale

        # for world positions use the Poser class	
        poser = AtomsCore.Poser(agent_type.skeleton())
        world_mtx = poser.getWorldMatrix(pose, ji)

    # read metadata
    metadatas = AtomsCore.MapMetadata()
    atoms_cache.loadAgentMetadata(f, id, metadatas)






Copyright © 2017, Toolchefs LTD.