Hacking the Flash CS4 Motion Model to Create New Extensions

Justin | Animate/Flash,ExtendScript,jsfl,Misc,tips,Tutorials | Wednesday, March 4th, 2009

Here’s a quote from an email I sent to Chris Georgenes and David Stiller on 9/29/08 about an extension idea that I had using the new motion tweens in Flash CS4 (before I’d even used the application):

Given that the new motion tween creates a bezier path, I’m guessing that we’ll have access to creating such a path with JSFL. Which led me to think about creating that path in real-time, similar to the real-time drawing that I’d seen from ScribbleBot on Chris’s blog. Instead of scribbling a line in real-time, I’d be sketching a motion path. There would be a variable to determine “smoothness” so that it doesn’t create an unmananageable amount of new motion keyframes. And it could probably be done with a custom tool rather than a panel, so that the sketching could be done directly on the stage.

That also leads me to wonder if there’s a way to record the armature animation in real-time and convert it to a motion tween, similar to the feature that already exists in After Effects CS3.

I know, I’m a nerd. The first idea there became MotionSketch. I haven’t yet found a way to accomplish the second idea with the armature, but I have a few ideas.

Prior to the release of Flash CS4, I had gotten a little hooked on creating Flash extensions. It’s quite easy to create a classic motion tween with JSFL (Flash Javascript), you can simply make the following call: timeline.createMotionTween(). It was likely that createMotionTween() would remain for legacy support even though the name of the tween had changed from “motion” to “classic.” I’d seen all of the great new motion features demonstrated online, and I expected there might be an equivalent call for a new motion tween. When Lee Brimelow linked to the CS4 documentation, I went right to the Extending Flash section to see what had been added. I didn’t see any additions for the motion features, but I had also previously seen Lee’s post on Flash Camp in San Francisco. Fortunately, I live close to San Francisco and I decided to head over try out the application early, and maybe get a chance to talk with some of the Flash team about any undocumented features.

While at Flash Camp, I spoke with Richard Galvan and he directed me to John Mayhew, the engineer behind the new motion model in Flash. John was very affable and willing to follow up with me, but he didn’t know of any undocumented features offhand. Unfortunately, adding features to the JSFL API is usually somewhat of an afterthought because so much of the development time is spent on the new features themselves. I had a few ideas to use the new 3D features while at Flash Camp as well, but they were similarly left out of the JSFL API.

So I let those ideas go for awhile and focused on a different extension as my Flash Camp project. A few weeks later, I got a little antsy and I starting digging around in the Flash configuration directory for clues. I noticed two files in the Javascript directory named MotionXML.jsfl and MotionClipboard.xml. I started looking around in the JSFL file for undocumented commands that I could use, but to little avail. I think I even put some trace calls into the JSFL to spit out info when copying motion from the Flash IDE. I noticed that even though copying and pasting a new motion tween appeared to work the same way in the Flash interface as copying and pasting a classic tween, a new motion tween was not triggering the calls in the JSFL file. Looking at the XML file, I found that even though copying a motion tween wasn’t using the same JSFL calls, it was saving XML to the same file…just different XML. The classic tweens were generating XML with a root tag of <Motion> and the new tweens were generating a root tag of <AnimationCore>.

Flash seemed to be able to discern which type of motion it was copying or pasting and acting accordingly. So it occurred to me that I could grab the data from the motion clipboard and alter it before pasting it back. Additionally, if I wanted data from an existing tween, I could run a Copy Motion command, available to JSFL as of Flash CS3 thanks to Robert Penner’s Copy Motion feature, then go about altering and pasting. These are the extensions that I’ve created thus far using that very technique:

Below is a rundown of the steps I used in the extensions mentioned above. This technique can be used in lieu of the non-existent timeline.createMotionObjectTween() command. I refer to this as a “hack” because it’s an unsupported method, but given the complexity of what can be created with this new motion model, editing the XML is actually a fairly efficient way to go about creating/editing a tween. If the XML is not properly formed, it can cause Flash to crash when you try to paste the motion, or it can create some funky bugs in the Motion Editor. Be sure to test your code thoroughly before releasing anything for public consumption. The language is JSFL (used to automate and manipulate the Flash authoring environment). There’s a link to documentation for the AnimationCore XML listed in step 3.

Step 1: Store the motion clipboard so you can restore it when you’re done. In some cases, I store it as a file. In this example, I store it as a variable.

var MOTION_CLIPBOARD = fl.configURI + "Javascript/MotionClipboard.xml";
var tl = fl.getDocumentDOM().getTimeline();
var storedMotion = FLfile.read(MOTION_CLIPBOARD);

Step 2(a): Copy motion to clipboard, retrieve the new clipboard contents and convert to XML. Use this step if you want alter an existing motion tween. The MotionBlur extension uses this step.

var xmlstr = FLfile.read(MOTION_CLIPBOARD);
var motionXML = new XML(xmlstr)

Step 2(b): Instead of copying the motion, you can create a new tween by loading a more or less blank motion XML template and adding content to it. The MotionSketch extension uses this step.

Step 3: The motion XML can then be altered using E4X. This portion of the code is different for each extension, depending on what the extension is trying to accomplish. Here’s the documentation on CS4 Motion XML that Flash engineer John Mayhew graciously put together.

Step 4: Write the altered XML to the motion clipboard.

FLfile.write(MOTION_CLIPBOARD, motionXML.toXMLString());

Step 5: Paste the altered motion.


Step 6: Restore the motion clipboard for the user.

FLfile.write(MOTION_CLIPBOARD, storedMotion);

If you want to know if a motion tween has already been applied, you can retrieve the tweenType property from the current frame. A classic tween will return a value of “motion” and a new motion tween will return a value of “motion object“. If you use Step 2(a), you’ll want to check and make sure you have a “motion object” selected before proceeding.

And there you have it.

There are other ways to go about this process. You could, since motion presets are just motion XML files, run this process through the Motion Presets panel’s JSFL calls, but this would require more steps than the process laid out above.

Update (1/12/10): Flash CS5 now has direct JSFL access to the calls documented in this post. Check out this video tutorial that I produced on ActiveTuts for more info.

Get updates from Ajar Productions


Sign up today and get the InDesign Split Text premium extension for free!

Unsubscribe at any time. Powered by ConvertKit

Related Articles

8 thoughts on “Hacking the Flash CS4 Motion Model to Create New Extensions”

  1. samBrown says:

    wow, simply awesome work. I look forward to taking trying these components out. Thanks!

  2. Justin says:

    Thanks for the kind words, Sam!

  3. thanks for sharing that Justin. I cant wait to get my hands dirty with them. the net could always do with more people like you 🙂

    1. Justin says:

      Thanks, Parisha! Happy to share.

  4. Brilliant post! Tons of handy things there! Thanks Justin!

    1. Justin says:

      Thanks for the feedback, George! Glad you found it useful.

  5. You wouldn’t happen to know if there is a method burried in JSFL that does the same thing as right-clicking a Motion Object and choosing “Convert to Frame by Frame Animation”? It would be very useful for me.

    I’ve already tried Timeline.convertToKeyframes() and that doesn’t work the same.

    If you know anything, please give me a shout!


  6. Justin says:

    Hi Pierre,

    Sorry I don’t know of another function to convert to frame-by-frame animation.

Leave a Reply

Your email address will not be published. Required fields are marked *