Skip to main content
Inspiring
Aug 19, 2019 12:21 AM UTC
Answered

jsfl Add keyframe to non selected layer

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 is closed to new replies. Start a new post to keep the conversation going.
JoãoCésar17023019
JoãoCésar17023019Correct answer
Community Expert
Aug 19, 2019 2:12 PM UTC

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

8 replies

JoãoCésar17023019
Community Expert
JoãoCésar17023019Community ExpertCorrect answer
Community Expert
Aug 19, 2019 2:12 PM UTC

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

Translate
Inspiring
Aug 20, 2019 1:15 AM UTC

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.

Translate
JoãoCésar17023019
Community Expert
Community Expert
Aug 20, 2019 2:05 AM UTC

You're welcome!

Translate
Vladin M. Mitov
Inspiring
Aug 19, 2019 6:24 AM UTC

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();

Translate
- Vlad: UX and graphic design, Flash user since 1998Member of Flanimate Power Tools team - extensions for character animation
Inspiring
Aug 19, 2019 7:06 AM UTC

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.

Translate
Legend
Aug 19, 2019 2:30 PM UTC

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?!

Translate