public static byte[] encodeAnimationBytes(CHREditorModel modelRef){
        // numAnimations (1 byte)
        // for each animation 
        // Num Frames (1 byte)
        // for each frame: 0 for no change, N for the index of the keyframe data to use  (N bytes)
        
        byte b[] = new byte[4096];
        int numAnimations  = modelRef.getNumAnimations();
        int animDuration = 0;
        if(numAnimations > 0){
            animDuration = modelRef.getAnimationDuration(0);
        }        
        
        Vector<AnimationKeyframeData> uniqueKFs = new Vector<AnimationKeyframeData>();
        HashMap<AnimationKeyframeData,Byte> kfMap = new HashMap<AnimationKeyframeData,Byte>();
        
        int cnt = 0;
        b[cnt++] = (byte)numAnimations;
        b[cnt++] = (byte)animDuration;
        
        for(int i=0;i<numAnimations;i++) {
            for(int j=0;j<animDuration;j++){
                if(modelRef.getKeyFrame(i,j) == null){
                    b[cnt++] = 0;                    
                } else {
                    AnimationKeyframeData kf = modelRef.getKeyFrame(i,j);
                    if(kfMap.containsKey(kf)){
                        b[cnt++] = ((Byte)kfMap.get(kf)).byteValue();
                    } else {
                        // new entry
                        uniqueKFs.add(kf);
                        Byte val = new Byte((byte)uniqueKFs.size());
                        kfMap.put(kf, val);
                        b[cnt++] = val.byteValue();
                    }
                }
            }
        }
        
        // now to store the keyframes
        b[cnt++] = (byte)uniqueKFs.size();      
        System.out.println("Unique frames:" + uniqueKFs.size());
        Iterator<AnimationKeyframeData> itor = uniqueKFs.iterator();
        while(itor.hasNext()){
            // lets make em 4 bytes each
            AnimationKeyframeData kf = itor.next();
            b[cnt++] = kf.yPos;                    
            b[cnt++] = kf.spriteIndex;                    
            b[cnt++] = kf.getSpriteAttributesByte();
            b[cnt++] = kf.xPos; 
        }
        
        byte b2[] = new byte[cnt];
        System.arraycopy(b,0,b2,0,cnt);
  
        return b2;
    }