{"id":601,"date":"2018-07-17T13:18:37","date_gmt":"2018-07-17T17:18:37","guid":{"rendered":"http:\/\/www.vicdebaie.com\/blog\/?p=601"},"modified":"2018-07-17T13:18:37","modified_gmt":"2018-07-17T17:18:37","slug":"scaling-and-moving-story-clips-with-motionbulider-python","status":"publish","type":"post","link":"http:\/\/www.vicdebaie.com\/blog\/scaling-and-moving-story-clips-with-motionbulider-python\/","title":{"rendered":"Scaling And Moving Story Clips With MotionBulider Python"},"content":{"rendered":"<p><img data-attachment-id=\"602\" data-permalink=\"http:\/\/www.vicdebaie.com\/blog\/scaling-and-moving-story-clips-with-motionbulider-python\/lib_story_together\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/07\/lib_story_together.jpg?fit=920%2C473\" data-orig-size=\"920,473\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/07\/lib_story_together.jpg?fit=300%2C154\" data-large-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/07\/lib_story_together.jpg?fit=678%2C349\" decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-602 \" src=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/07\/lib_story_together.jpg?resize=495%2C281\" alt=\"\" width=\"495\" height=\"281\" data-recalc-dims=\"1\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>After the last two posts (found <a href=\"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-library-aka-my-fav-functions\/\">here<\/a> and <a href=\"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-my-fav-story-functions\/\">here<\/a>), I was able to quickly create a script that would scale all selected takes within a scene to a specific frame length.<\/p>\n<p>This could be handy if data needed to blend or needed to conform to a specific length for game play.<\/p>\n<p>The script is easy enough to use, just execute it within your scene then select the takes you wish it to be applied on and type within the python console\u00a0 ScaleTakes(length).<\/p>\n<p>ie:\u00a0ScaleTakes(15) will scale all selected takes to 15 frames.<\/p>\n<p>As a bonus all layers on the take will be merged together, the clip will be moved to start on frame zero (if it did not already) and your time line length will be adjusted to fit the new length.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport pyfbsdk as fb\r\nimport pyfbsdk_additions as fba\r\nimport decimal\r\n\r\n##Scene Refresh Bug Work Around\r\ndef SceneRefresh(): \r\n    fb.FBPlayerControl().GotoNextKey()\r\n    fb.FBSystem().Scene.Evaluate()\r\n    fb.FBPlayerControl().GotoPreviousKey()\r\n    fb.FBSystem().Scene.Evaluate()\r\n\r\n##Get Length Of Time Line \/ Get Time Line Frame Count            \r\ndef GetTimeSpan():\r\n    return ( fb.FBSystem().CurrentTake.LocalTimeSpan.GetStop().GetFrame() - fb.FBSystem().CurrentTake.LocalTimeSpan.GetStart().GetFrame() )\r\n\r\n##Set Time Line Length ie. SetTimeSpan(150, 200) Will Set The Time Line To Start At Frame 150 And End At Frame 200\r\ndef SetTimeSpan(start, end):\r\n    fb.FBSystem().CurrentTake.LocalTimeSpan = fb.FBTimeSpan(fb.FBTime(0, 0, 0, start, 0), fb.FBTime(0, 0, 0, end, 0))\r\n\r\n##Plot Clip\r\ndef PlotStoryClip():\r\n    ##Deal With The User's Sory Mode Activity\r\n    fb.FBStory().Mute = False\r\n    SceneRefresh()\r\n    \r\n    ##Plot Options\r\n    lPlotClipOptions = fb.FBPlotOptions()\r\n    lPlotClipOptions.ConstantKeyReducerKeepOneKey = False\r\n    lPlotClipOptions.PlotAllTakes = False\r\n    lPlotClipOptions.PlotOnFrame = True\r\n    lPlotClipOptions.PlotPeriod = fb.FBTime( 0, 0, 0, 1 )\r\n    lPlotClipOptions.PlotTranslationOnRootOnly = False\r\n    lPlotClipOptions.PreciseTimeDiscontinuities = False\r\n    lPlotClipOptions.RotationFilterToApply = fb.FBRotationFilter.kFBRotationFilterUnroll\r\n    lPlotClipOptions.UseConstantKeyReducer = False\r\n    ##Plot Story Clip On Current Character\r\n    lChar = fb.FBApplication().CurrentCharacter               \r\n    lChar.PlotAnimation(fb.FBCharacterPlotWhere.kFBCharacterPlotOnControlRig,lPlotClipOptions )\r\n\r\n##Go Through The Story And Delete The Track Created By This Script        \r\ndef CleanStoryTrack():       \r\n    for eachTrack in fb.FBStory().RootFolder.Tracks:\r\n        if &quot;--Del_Me-PythonStoryClip&quot; in eachTrack.Name:\r\n            eachTrack.FBDelete()\r\n        else:\r\n            pass\r\n\r\n\r\ndef ScaleTakes(length):\r\n    lTakeLst = &#x5B;]\r\n    ##Add Every Selected Take Within Our Scene To Our Take List\r\n    for i in range( len(fb.FBSystem().Scene.Takes) ):\r\n        if fb.FBSystem().Scene.Takes&#x5B;i].Selected == True:\r\n            lTakeLst.extend(&#x5B;fb.FBSystem().Scene.Takes&#x5B;i]])\r\n        else: pass\r\n    ogTake = fb.FBSystem().CurrentTake\r\n    ##Delete Every Take Within Our Take List    \r\n    for take in lTakeLst:\r\n        fb.FBSystem().CurrentTake = take\r\n        fb.FBSystem().CurrentTake.MergeLayers(fb.FBAnimationLayerMergeOptions.kFBAnimLayerMerge_AllLayers_CompleteScene, True, fb.FBMergeLayerMode.kFBMergeLayerModeAutomatic)\r\n        lTrack = fb.FBStoryTrack(fb.FBStoryTrackType.kFBStoryTrackCharacter, fb.FBStory().RootFolder)\r\n        lTrack.Label = fb.FBSystem().CurrentTake.Name + &quot;--Del_Me-PythonStoryClip&quot;\r\n        lTrack.Details.append(fb.FBApplication().CurrentCharacter)\r\n        lTrack.CopyTakeIntoTrack( fb.FBSystem().CurrentTake.LocalTimeSpan, fb.FBSystem().CurrentTake )\r\n        for clip in lTrack.Clips:\r\n            ##Get Clips New End Frame\r\n            oClipStopFrame = GetTimeSpan()\r\n            ##Create Our Adjustment Speed (Clips Current Legnth \/ Our Desired Length)\r\n            adjSpeed = round( ( decimal.Decimal(oClipStopFrame) \/ decimal.Decimal(length) ) , 4 )\r\n            ##Set StopFrame So Anim Does Not Get Cut Off\r\n            clip.Stop = fb.FBTime(0,0,0,(length+1))\r\n            ##Set Our Clips New Speed To Be Our Adjustment Speed\r\n            clip.Speed = adjSpeed\r\n            ##Set Our Time Span To Match Our Desired Time Line Length\r\n            SetTimeSpan( 0, length )\r\n        \r\n        PlotStoryClip()\r\n        CleanStoryTrack()\r\n            \r\n    fb.FBSystem().CurrentTake = ogTake\r\n<\/pre>\n<p>I hope this post does two things;<br \/>\n1. Shows how having a library of functions at your disposal can speed up your scripting<br \/>\n2. Gives you a new toy\/tool to play with.<\/p>\n<p>As always, I hope this helps.<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>&nbsp; After the last two posts (found here and here), I was able to quickly create a script that would scale all selected takes within a scene to a specific frame length. This could be handy if data needed to blend or needed to conform to a specific length for game play. The script is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":602,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false}}},"categories":[4],"tags":[5,7],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/07\/lib_story_together.jpg?fit=920%2C473","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8pltq-9H","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":127,"url":"http:\/\/www.vicdebaie.com\/blog\/running-a-python-script-on-all-takes\/","url_meta":{"origin":601,"position":0},"title":"Running A Python Script On All Takes.\u00a0\ufeff","author":"admin","date":"February 18, 2017","format":false,"excerpt":"Being able to run a Python script on all takes within a MotionBuilder scene is a powerful time saver. Below I'm going to show how I learned to create a layer on every take within a MotionBuilder scene using Python. We are going to\u00a0use Python to create an Animation Layer\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"http:\/\/www.vicdebaie.com\/blog\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":794,"url":"http:\/\/www.vicdebaie.com\/blog\/using-motionbuilder-python-to-offset-all-animations-within-a-scene\/","url_meta":{"origin":601,"position":1},"title":"Using MotionBuilder Python To Offset All Animations Within A Scene","author":"admin","date":"January 7, 2021","format":false,"excerpt":"Here is a quick script that will allow you to shift\/offset all animation keys within a scene. Previously I would do this via story mode or use another technique that would skip over props and\/or constraints. This script should shift everything within the scene that is animated. The script is\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"http:\/\/www.vicdebaie.com\/blog\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2021\/01\/OffSet-1.jpg?fit=960%2C540&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2021\/01\/OffSet-1.jpg?fit=960%2C540&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2021\/01\/OffSet-1.jpg?fit=960%2C540&resize=700%2C400 2x"},"classes":[]},{"id":686,"url":"http:\/\/www.vicdebaie.com\/blog\/save-all-story-clips-to-new-takes-with-motionbuilder-python-script\/","url_meta":{"origin":601,"position":2},"title":"Save All Story Clips To New Takes with MotionBuilder Python Script","author":"admin","date":"May 2, 2019","format":false,"excerpt":"UPDATED: A quick update that will now allow the script to maintain all of the original tracks within the story. The story will look exactly as it was pre-processing. Thanks Simon Kay Below is a script that I wrote, It goes through all you Story tracks and clips and then\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"http:\/\/www.vicdebaie.com\/blog\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/05\/StoryTrackClips.png?fit=851%2C304&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/05\/StoryTrackClips.png?fit=851%2C304&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/05\/StoryTrackClips.png?fit=851%2C304&resize=700%2C400 2x"},"classes":[]},{"id":175,"url":"http:\/\/www.vicdebaie.com\/blog\/%ef%bb%bfplotting-selected-properties-using-motionbuilders-python\/","url_meta":{"origin":601,"position":3},"title":"\ufeffPlotting selected properties using MotionBuilder&#8217;s Python.","author":"admin","date":"February 17, 2017","format":false,"excerpt":"I ran\u00a0the script I created in \"Animating and setting keys with MotionBuilder's Python Editor\"\u00a0and decided that I would use the scene it creates\u00a0to learn how to plot on selected properties in MotionBuilder's Python Editor. Update: I found a work around for the bug and it is covered in my \"lock-and-plot-selected-properties-using-motionbuilders-python\"\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"http:\/\/www.vicdebaie.com\/blog\/category\/python\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":253,"url":"http:\/\/www.vicdebaie.com\/blog\/move-a-character-in-motionbuilder-to-the-worlds-center-with-python\/","url_meta":{"origin":601,"position":4},"title":"Move a Character in MotionBuilder to the World&#8217;s Center with Python","author":"admin","date":"March 7, 2018","format":false,"excerpt":"\u200eWith a dozen takes in my Motionbuilder scene and my Character placed at different locations on each take, I decided Python would help me move my Character to the World's Center. With raw Mocap data your Character's Wold Position is all based on the Actors position within the volume during\u2026","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"http:\/\/www.vicdebaie.com\/blog\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/IMG_4056.jpg?fit=800%2C600&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/IMG_4056.jpg?fit=800%2C600&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/IMG_4056.jpg?fit=800%2C600&resize=700%2C400 2x"},"classes":[]},{"id":805,"url":"http:\/\/www.vicdebaie.com\/blog\/write-take-info-to-text-file\/","url_meta":{"origin":601,"position":5},"title":"Write Take info To Text File","author":"admin","date":"March 23, 2021","format":false,"excerpt":"Write out take information to a .txt file using MotionBuilder Python","rel":"","context":"In &quot;Python&quot;","block_context":{"text":"Python","link":"http:\/\/www.vicdebaie.com\/blog\/category\/python\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2021\/03\/Writing_on_the_whiteboard-1.jpg?fit=853%2C640&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2021\/03\/Writing_on_the_whiteboard-1.jpg?fit=853%2C640&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2021\/03\/Writing_on_the_whiteboard-1.jpg?fit=853%2C640&resize=700%2C400 2x"},"classes":[]}],"_links":{"self":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/601"}],"collection":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/comments?post=601"}],"version-history":[{"count":3,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions"}],"predecessor-version":[{"id":606,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/601\/revisions\/606"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media\/602"}],"wp:attachment":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media?parent=601"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/categories?post=601"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/tags?post=601"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}