Skip to main content
Inspiring
August 19, 2019
Answered

jsfl Add keyframe to non selected layer

  • August 19, 2019
  • 2 replies
  • 2413 views

Is it possible to add a keyframe to a non selected layer using jsfl? Or does it need to be selected first with setSelectedLayers(index)?

This topic has been closed for replies.
Correct answer JoãoCésar17023019

Hi.

If I'm understanding correctly, the currentLayer approach is what you want.

But please notice that selecting a layer and selecting a keyframe are different things.

The code below should select the second layer and create a keyframe at index 5. Then it should reselect the original layer and the original keyframe.

var doc = fl.getDocumentDOM();

var timeline = doc.getTimeline();

var currentLayer = timeline.currentLayer;

var selectedFrames = timeline.getSelectedFrames();

var targetLayer = 1;

var targetFrame = 5;

timeline.currentLayer = targetLayer;

timeline.convertToKeyframes(targetFrame);

timeline.currentLayer = currentLayer;

timeline.setSelectedFrames(selectedFrames);

Please let us know if this is helpful somehow.

Regards,

JC

2 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
August 19, 2019

Hi.

If I'm understanding correctly, the currentLayer approach is what you want.

But please notice that selecting a layer and selecting a keyframe are different things.

The code below should select the second layer and create a keyframe at index 5. Then it should reselect the original layer and the original keyframe.

var doc = fl.getDocumentDOM();

var timeline = doc.getTimeline();

var currentLayer = timeline.currentLayer;

var selectedFrames = timeline.getSelectedFrames();

var targetLayer = 1;

var targetFrame = 5;

timeline.currentLayer = targetLayer;

timeline.convertToKeyframes(targetFrame);

timeline.currentLayer = currentLayer;

timeline.setSelectedFrames(selectedFrames);

Please let us know if this is helpful somehow.

Regards,

JC

Inspiring
August 20, 2019

JoãoCésar Yes, you're right about having to re-select the current frames after converting the target frame to a keyframe. Thanks for taking a look.

JoãoCésar17023019
Community Expert
Community Expert
August 20, 2019

You're welcome!

Vladin M. Mitov
Inspiring
August 19, 2019

Hi,

Both commands - convertToKeyframes() and inserttKeyframe() work on the current layer.

So, you don't need to select the layer, just make it current.

var tml = fl.getDocumentDOM().getTimeline();

tml.currentLayer = 2;

tml.convertToKeyframes();

- Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
Inspiring
August 19, 2019

Thanks. But setting the current layer focuses that layer to be selected in the main Animate timeline, whereas we want the current selection to remain and only add a keyframe to another layer, not select it. Here's some code:

var cFrame = tl.currentFrame;

var tml = fl.getDocumentDOM().getTimeline();

tml.currentLayer = 1; //childIndex

tml.convertToKeyframes(cFrame);

fl.getDocumentDOM().getTimeline().currentFrame = cFrame

With layer 3 selected, this adds a keyframe to the correct layer index, 1 but the timeline in Animate shifts and stays selected on layer 1 when it needs to remain on layer 3. If we add tml.currentLayer = 3 after line 04, it still remains on layer 1..

The reason for line 05 is to return the playhead back to the current frame (cFrame) because when you run convertToKeyframes, the playhead ends up moving one forward. The above routine is in a "mouseMove" eventListener so the playhead needs to stay put.

Legend
August 19, 2019

kilopop2000  wrote

The above routine is in a "mouseMove" eventListener so the playhead needs to stay put.

You're adding keyframes when the user moves the mouse?!