theatre icon indicating copy to clipboard operation
theatre copied to clipboard

How to get all keyframes

Open vimlesh1975 opened this issue 2 years ago • 8 comments

I need to have all keyframes and its value array to convert it to threejs standard AnimationClip. Please help.

vimlesh1975 avatar Oct 29 '22 05:10 vimlesh1975

It would be a hack but you can use studio.createContentOfSaveFile to get the underlying structure.

Out of curiosity, why do you need to convert the keyframes?

AriaMinaei avatar Oct 29 '22 10:10 AriaMinaei

Big thank you. 😊 It is working. I want to export it with gltf. Is there any other way I can do this without createcontestofsavefile method?

vimlesh1975 avatar Oct 29 '22 13:10 vimlesh1975

I second this request - or a nice way to get the value at a given time, please!

akre54 avatar Nov 04 '22 22:11 akre54

Here's a simple helper, with the typescript type spelled out (would be nice to include this instead of Record<string, unknown>).

function getKeyframes<T extends UnknownShorthandCompoundProps>(sheet: ISheetObject<T>, tracks: string[]) {
  const {projectId, sheetId, objectKey, sheetInstanceId} = sheet.address
  
  type TrackId = `["${keyof T extends string ? keyof T : never}"]`;
  type Keyframe = {value: number};
  type StudioSaveFile = {
    sheetsById: {
      [sId: typeof sheetId]: {
        sequence: {
          tracksByObject: {
            [oKey: typeof objectKey]: {
              trackData: {
                [propPath: typeof sheetInstanceId]: {
                  keyframes: Keyframe[]
                }
              },
              trackIdByPropPath: {
                [t in TrackId]: typeof sheetInstanceId
              },
            }
          }
        }
      }
    }
  };

  const json = studio.createContentOfSaveFile(projectId) as StudioSaveFile;

  const {trackData, trackIdByPropPath} = json.sheetsById[sheetId].sequence.tracksByObject[objectKey];

  return tracks.map(track =>
    trackData[trackIdByPropPath[`["${track}"]`]].keyframes.map(k => k.value);
  );
}

And then using it:

getKeyframes(positionSheet, ['x', 'y']);

akre54 avatar Dec 21 '22 17:12 akre54

Looks like there's already a type for OnDiskState which does what my StudioSaveFile does. Awesome! I just opened a pull to fix the type.

Also the new createRafDriver helper looks great for my use case, but it would still be nice to be able to read values at a given timestamp without mutating the global clock, for things I want to do down the line. Any hope of supporting this?

akre54 avatar Jan 31 '23 20:01 akre54

@akre54 something like val(prop, position) ?

clementroche avatar Jan 31 '23 21:01 clementroche

@clementroche yes correct.

My use case is that I want the first and last value of a track, but it would be nice to be able to pull arbitrary values too.

akre54 avatar Jan 31 '23 21:01 akre54

@akre54 something like val(prop, position) ?

This would be very helpful for our use case where we're using objects in the timeline to generate geometry. Animating a point's position, but wanting to draw a trailing line for wherever it's been, for example.

chrisgervang avatar Feb 13 '23 19:02 chrisgervang