-- **************************************************
-- Provide Moho with the name of this script object
-- **************************************************

ScriptName = "SS_Ae_CameraExport"


    Derived work based on: 'DS Camera to AE v1.1' by
      (C) 2013 David F. Sandberg (aka. ponysmasher)
      https://www.dauid.com - with thanks

    Derived by: Sam Cogheil (SimplSam) 2022-Jan

    Primary Changes:
        - Save to Clipboard instead of file
        - Run as Menu or Button Tool (w/ PNG)
        - Optimize/Minimize: Keyframe-less properties
        - Functionalise


-- **************************************************
-- General information about this script
-- **************************************************

SS_Ae_CameraExport = {}
function SS_Ae_CameraExport:Name()
	return "SS/DS Camera to AE+"

function SS_Ae_CameraExport:Version()
	return "2.0 #520114"

function SS_Ae_CameraExport:Description()
	return "Export Camera data to Clipboard for After Effects"

function SS_Ae_CameraExport:Creator()
	return "dauid.com / SimplSam"

function SS_Ae_CameraExport:UILabel()
	return("Camera to AE+")

-- **************************************************
-- The guts of this script
-- **************************************************

function SS_Ae_CameraExport:Run(moho)

    local mohodoc = moho.document
    local width = mohodoc:Width()
    local height = mohodoc:Height()
    local aspect = mohodoc:AspectRatio()
    local newX = mohodoc:Width()/2
    local newY = mohodoc:Height()/2
    local newZ = mohodoc:Height()/2
    local fps = mohodoc:Fps()
    local startframe = mohodoc:StartFrame()
    local endframe = mohodoc:EndFrame()
    local zoomFactor = (1/ math.tan(math.pi/(2 * 6)))/2 * newZ  -- i.e. 3.7321 /2 *540 for 1080p
    local cliptext = {} --tabl

    local function AddText(s)
        table.insert(cliptext, s .."\r\n")

    local function AddHead(s1, s2)
        AddText("\r\n" .. s1 .. "\r\n" .. s2)

    local function ssRound4(n)
        return (LM.Round(n * 10^4)/10^4)

    local function CallbackAngle(_val)
        return ssRound4(math.deg(_val))

    local function CallbackAngleX(_val)
        return CallbackAngle(_val.x)

    local function CallbackAngleY(_val)
        return CallbackAngle(_val.y)

    local function CallbackZoom(_val)
        return ssRound4(_val * zoomFactor)

    local function CallbackCamXYZ(_val, _zoffset)
        _zoffset = _zoffset or 0
        if (aspect >0) then
            _val.x = ((_val.x + 1) * newY) + newX -newY
            _val.y = (-_val.y + 1) * newY
            _val.z = (-_val.z * newZ) + _zoffset
            _val.x = (_val.x + 1) * newX
            _val.y = ((-_val.y + 1) * newX) + newY -newX
            _val.z = (-_val.z * newX) + _zoffset
        return ssRound4(_val.x) .."\t".. ssRound4(_val.y) .."\t".. ssRound4(_val.z)

    local function CallbackCamXYZee(_val)
        return CallbackCamXYZ(_val, 0.1)

    local function AddKeys(animChannel, Callback)
        local keyCount = animChannel:CountKeys()
        if (keyCount > 1) then
            local theend = math.min(animChannel:GetKeyWhen(keyCount -1), endframe)
            for i = startframe, theend do
                AddText("\t".. i .."\t".. Callback(animChannel:GetValue(i)) .."")
            AddText("\t\t".. Callback(animChannel:GetValue(0)) .."")

    --= Header
    AddText("Adobe After Effects 8.0 Keyframe Data")

    --= Doc props
    AddHead("\tUnits Per Second\t"..fps, "\tSource Width\t"..width)
    AddText("\tSource Height\t"..height)
    AddText("\tSource Pixel Aspect Ratio\t1")
    AddText("\tComp Pixel Aspect Ratio\t1")

    --= Zoom
    AddHead("Camera Options\tZoom", "\tFrame\tpixels")
    AddKeys(mohodoc.fCameraZoom, CallbackZoom)

    --= Point of interest (2 Node Cam)
    AddHead("Transform\tPoint of Interest", "\tFrame\tX pixels\tY pixels\tZ pixels")
    AddKeys(mohodoc.fCameraTrack, CallbackCamXYZee)

    --= Position
    AddHead("Transform\tPosition", "\tFrame\tX pixels\tY pixels\tZ pixels")
    AddKeys(mohodoc.fCameraTrack, CallbackCamXYZ)

    --= Z rotation
    AddHead("Transform\tRotation", "\tFrame\tdegrees")
    AddKeys(mohodoc.fCameraRoll, CallbackAngle)

    --= X rotation
    AddHead("Transform\tX Rotation", "\tFrame\tdegrees")
    AddKeys(mohodoc.fCameraPanTilt, CallbackAngleX) -- SS: Todo Split Dims?

    --= Y rotation
    AddHead("Transform\tY Rotation", "\tFrame\tdegrees")
    AddKeys(mohodoc.fCameraPanTilt, CallbackAngleY)

    --= Footer
    AddHead("", "End of Keyframe Data")

    --= Copy to Clipboard

SS - After Effects Camera Export

Script type: Button/Menu

Uploaded: Jan 14 2022, 14:58

Export Camera property & keyframe data to clipboard, ready for pasting to After Effects
SimplSam's AE Camera Export tool allows you to export Camera property & keyframe data to the clipboard, ready for pasting directly to Adobe After Effects.


It was designed as an upgrade / companion to the excellent earlier DS Camera to AE tool - created by David F. Sandberg (aka. ponysmasher) - https://www.dauid.com - discussed here: https://lostmarble.com/forum/viewtopic.php?f=12&t=18091

... and complements it with these new & alternate features:

- Instantly Saves Camera data to the Clipboard (instead of a file); ready for pasting
- Can be run as a Button or Menu Tool
- Reduces data size by eliminating redundant keyframes (does not bake multiple keyframes if the property only has a single value)
- Has a modular code refactor


So ... Why do you need AE Camera Export ?

The tool allows the efficient sharing of Camera configuration and keyframed positioning information from Moho to After Effects, which helps greatly when your animation workflow (finalizing, SFXing or compositing) includes one or more trips from Moho -> Ae. Thus allowing Camera movement & properties to be easily synchronized (one-way).


How do I get set up ?

* To use:

  - Run the AE Camera Export+ tool from the Tools palette or Menu location
  - If you need to view/amend the data before passing to AE, you can paste into a Excel/Spreadsheet or Text editor

Options & Features

** Currently there are no options

Notes & Limitations

There appears to be a bug in AS11 which limits the size of text passed to the Clipboard to 4096 bytes. Thus AS11 is not fully compatible with this tool.

Because the 'DS Camera to AE' tool fully baked the Camera movement (wrote every possible keyframe), it would always overwrite all previous settings. This tool is keyframe data optimized and only writes required keyframes. So you may occasionally need to clear old-keyframes (deselect the stop-watches or delete keyframes) before use.

Additional Notes:

* Compatible/Tested with MH12+
* Optimized for MH12+

Special Thanks to:

* David F. Sandberg (ponysmasher) -- https://www.dauid.com
* Stan (and the team): MOHO Scripting -- https://mohoscripting.com
* The friendly faces @ Lost Marble Moho forum -- https://www.lostmarble.com/forum/

Installation Options:

This script, and all other scripts on this site are distributed as free software under the GNU General Public License 3.0 or later.
Downloads count: 23