{"id":274,"date":"2017-04-06T14:19:49","date_gmt":"2017-04-06T18:19:49","guid":{"rendered":"http:\/\/www.vicdebaie.com\/blog\/?p=274"},"modified":"2017-11-06T09:16:50","modified_gmt":"2017-11-06T14:16:50","slug":"using-python-to-import-motionbuilder-characters-into-story","status":"publish","type":"post","link":"http:\/\/www.vicdebaie.com\/blog\/using-python-to-import-motionbuilder-characters-into-story\/","title":{"rendered":"Using Python to import MotionBuilder Characters into story.\u00a0"},"content":{"rendered":"<p>&nbsp;<\/p>\n<p><img data-attachment-id=\"323\" data-permalink=\"http:\/\/www.vicdebaie.com\/blog\/using-python-to-import-motionbuilder-characters-into-story\/mobupython_storyscript\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/MobuPython_StoryScript.jpg?fit=1011%2C224\" data-orig-size=\"1011,224\" 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\/2017\/04\/MobuPython_StoryScript.jpg?fit=300%2C66\" data-large-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/MobuPython_StoryScript.jpg?fit=678%2C150\" decoding=\"async\" loading=\"lazy\" class=\"aligncenter size-full wp-image-323\" src=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/MobuPython_StoryScript.jpg?resize=678%2C150\" alt=\"\" width=\"678\" height=\"150\" srcset=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/MobuPython_StoryScript.jpg?w=1011 1011w, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/MobuPython_StoryScript.jpg?resize=300%2C66 300w, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/MobuPython_StoryScript.jpg?resize=768%2C170 768w, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/MobuPython_StoryScript.jpg?resize=940%2C208 940w\" sizes=\"(max-width: 678px) 100vw, 678px\" data-recalc-dims=\"1\" \/><\/p>\n<p>MotionBuilder comes with a great script that will place your selected character into a Character Track within the Story, I wanted to write one that would put all of the scene&#8217;s Characters into the story.<\/p>\n<p>Once again I will use the &#8220;Mia_Rigged.fbx&#8221; file to test my script. I created a <strong><em>NEW\u00a0<\/em><\/strong>scene and merged the\u00a0&#8220;Mia_Rigged.fbx&#8221; file twice into that new scene. I checked &#8220;Apply NameSpace&#8221; and then checked &#8220;Use New NameSpace&#8221;. For my NameSpace I used &#8220;Mia01&#8221; for the first Character and &#8220;Mia02&#8221; on my second merge.<\/p>\n<p><img data-attachment-id=\"321\" data-permalink=\"http:\/\/www.vicdebaie.com\/blog\/using-python-to-import-motionbuilder-characters-into-story\/miamerged\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/miamerged.jpg?fit=692%2C892\" data-orig-size=\"692,892\" 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=\"miamerged\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/miamerged.jpg?fit=233%2C300\" data-large-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/miamerged.jpg?fit=678%2C874\" decoding=\"async\" loading=\"lazy\" class=\"aligncenter wp-image-321\" src=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/miamerged.jpg?resize=482%2C602\" alt=\"\" width=\"482\" height=\"602\" data-recalc-dims=\"1\" \/><\/p>\n<p>Once I had both my Characters within my scene (Mia01 and Mia02) I did a very quick animation of them sliding back and forth through the world &#8211; this allowed me to have some test animation data to work with.<\/p>\n<p>Now let&#8217;s start scripting!!! \ud83d\ude42<\/p>\n<p>First we need to set up the script to find all our Characters within the scene and put them into a list.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pyfbsdk import *\r\nimport re\r\n\r\n# Variables --------------------\r\nlSystem = FBSystem()\r\nlScene = FBSystem().Scene\r\nlTakeName = lSystem.CurrentTake.Name\r\nlCurChar = FBApplication().CurrentCharacter\r\n\r\n# This variable will split &quot;lCurChar&quot; into only the first word before the &quot;:&quot; in the name = our NameSpace \r\nlCurCharNameSpace = lCurChar.LongName.rsplit(':', 1)&#x5B;0]\r\n\r\n# the characters within our scene\r\nlCharactersInScene = lScene.Characters\r\n\r\n# story\r\nlStory = FBStory()\r\n\r\n# Print &quot;hello world!&quot; for every character within the scene \r\nprint &quot;These are the Names of the Characters within your scene:&quot;\r\nfor lCurChar in lCharactersInScene:\r\n    print &quot;hello world!&quot;\r\n<\/pre>\n<p>Ok, above we have\u00a0some variables that we will be using as well as a simple script that will print &#8220;hello world!&#8221; once for every character within our scene.<\/p>\n<p>Now that we have the Characters listed let&#8217;s put it to actual use and see what info we can get Python to tell us about our Characters.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pyfbsdk import *\r\nimport re\r\n\r\n# Variables --------------------\r\nlSystem = FBSystem()\r\nlScene = FBSystem().Scene\r\nlTakeName = lSystem.CurrentTake.Name\r\nlCurChar = FBApplication().CurrentCharacter\r\n\r\n# This variable will split &quot;lCurChar&quot; into only the first word before the &quot;:&quot; in the name = our NameSpace \r\nlCurCharNameSpace = lCurChar.LongName.rsplit(':', 1)&#x5B;0]\r\n\r\n# the characters within our scene\r\nlCharactersInScene = lScene.Characters\r\n\r\n# story\r\nlStory = FBStory()\r\n\r\n# Print the total number of characters within the scene -----------\r\nlNumberOfChar = len (lCharactersInScene)\r\nprint &quot;There Are A Total Of&quot;, lNumberOfChar, &quot;Characters within Your Scene&quot;\r\nprint &quot;--------------------&quot;\r\n\r\n# Print a range for the numeber of characters within the scene\r\nlCharactersList = range (lNumberOfChar)\r\nprint &quot;The Characters Listed In The Scene Are:&quot;, lCharactersList\r\nprint &quot;--------------------&quot; \r\n\r\n# Print Names of all the characters within our scene --------------\r\nprint &quot;These are the Names of the Characters within your scene:&quot;\r\nfor lCurChar in lCharactersInScene:\r\n    lCurCharNameSpace = lCurChar.LongName.rsplit(':', 1)&#x5B;0]\r\n    print lCurCharNameSpace\r\nprint &quot;--------------------&quot;\r\n<\/pre>\n<p>So with the above script we found out:<\/p>\n<ol>\n<li>The total number of characters within our scene<\/li>\n<li>The range of numbers for those characters<\/li>\n<li>The NameSpace for each character<\/li>\n<\/ol>\n<p>The part to really take notice is the variable &#8220;lCurCharNameSpace&#8221;, note that this is much different from using &#8220;lCurChar.Name&#8221; and &#8220;lCurChar.LongName&#8221; which would give us different strings:<\/p>\n<ul>\n<li>lCurChar.Name = &#8220;Mia&#8221;<\/li>\n<li>lCurChar.LongName = &#8220;Mia01:Mia&#8221; or &#8220;Mia02:Mia&#8221;<\/li>\n<li>lCurCharNameSpace =\u00a0&#8220;Mia01&#8221; or &#8220;Mia02&#8221; (this is achieved by taking the LongName version and splitting it to 1 word before the &#8220;:&#8221;)<\/li>\n<\/ul>\n<p>Now that\u00a0we have all of our Character&#8217;s info (more than we need, but that&#8217;s o.k.), let&#8217;s get to dealing with putting the Character&#8217;s Data into MotionBuilder&#8217;s\u00a0Story Mode.<\/p>\n<p>We make the same type of list we used to get the Character&#8217;s info and we create a loop that will produce our Story setup.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pyfbsdk import *\r\nimport re\r\n\r\n# Variables --------------------\r\nlSystem = FBSystem()\r\nlScene = FBSystem().Scene\r\nlTakeName = lSystem.CurrentTake.Name\r\nlCurChar = FBApplication().CurrentCharacter\r\n\r\n# This variable will split &quot;lCurChar&quot; into only the first word before the &quot;:&quot; in the name = our NameSpace \r\nlCurCharNameSpace = lCurChar.LongName.rsplit(':', 1)&#x5B;0]\r\n\r\n# the characters within our scene\r\nlCharactersInScene = lScene.Characters\r\n\r\n# story\r\nlStory = FBStory()\r\n\r\n# Story Folder will be names after the take the data was one\r\nlParentFolder = FBStoryFolder()\r\nlParentFolder.Label = lSystem.CurrentTake.Name + &quot; StoryFolder&quot;\r\n\r\nfor lCurChar in lCharactersInScene:\r\n    lCurCharNameSpace = lCurChar.LongName.rsplit(':', 1)&#x5B;0]\r\n    print lCurCharNameSpace+&quot; is being processed -- &quot;\r\n    \r\n    #Select your Character's Hips: \r\n    '''\r\n    MAKE SURE THAT YOU KNOW THE NAMESPACE OF YOUR CHARACTER!!!!\r\n    &quot;Mia_Ctrl:HipsEffector&quot; is used because I know that the Character's hips are labled as &quot;NameSpace:Mia_Ctrl:HipsEffector&quot;\r\n    You may need to edit the below line to ensure the script works for your Characters. Select the hips of your Character and see what is needed for you.\r\n    Standardizing your Character's NameSpace will help you creat a fluid pipe line.\r\n    '''\r\n    lCurChar_ChrHips = FBFindModelByLabelName(lCurCharNameSpace+&quot;:Mia_Ctrl:HipsEffector&quot;) \r\n    lCurChar_ChrHips.Selected = True\r\n    lCurChar_Track = FBStoryTrack(FBStoryTrackType.kFBStoryTrackCharacter, lParentFolder)\r\n    \r\n    # assign our CurrentCharacter to the track\r\n    lCurChar_Track.Details.append(FBApplication().CurrentCharacter)\r\n    lCurChar_Track.Label = lCurCharNameSpace\r\n    # Insert current take in the newly created track\r\n    lCurChar_Clip = lCurChar_Track.CopyTakeIntoTrack( lSystem.CurrentTake.LocalTimeSpan, lSystem.CurrentTake )\r\n<\/pre>\n<p>The end result is a Python Script that will create a\u00a0Story Folder (labeled the same as the take that the script was executed on) and put each Character within the Scene onto their own Character Track (Labeled after the Character&#8217;s NameSpace).<\/p>\n<p>The thing to really take note of is NameSpaces. If you do not have a NameSpace naming convention that you stick with then your pipeline will be impossible to maintain and at the very least the script won&#8217;t work. \ud83d\ude42<\/p>\n<p>Once you under stand all this is should be very easy for you to re-create this script to run on all takes (or even selected takes) within your scene. \ud83d\ude09<\/p>\n<p>This was a really fun script for me to figure out.<\/p>\n<p>I hope this helps.<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>&nbsp; MotionBuilder comes with a great script that will place your selected character into a Character Track within the Story, I wanted to write one that would put all of the scene&#8217;s Characters into the story. Once again I will use the &#8220;Mia_Rigged.fbx&#8221; file to test my script. I created a NEW\u00a0scene and merged the\u00a0&#8220;Mia_Rigged.fbx&#8221; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":323,"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":false,"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\/2017\/04\/MobuPython_StoryScript.jpg?fit=1011%2C224","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8pltq-4q","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":345,"url":"http:\/\/www.vicdebaie.com\/blog\/finding-a-characters-namespace-with-motionbuilder-python\/","url_meta":{"origin":274,"position":0},"title":"Finding a Character&#8217;s NameSpace with MotionBuilder Python","author":"admin","date":"April 25, 2017","format":false,"excerpt":"Here is a snippet of script that I use a lot when dealing with Character's Name Spaces within MotionBuilder. [code language=\"python\"] ##Setup variables to get all characters within the scene lAllChars = FBSystem().Scene.Characters ##Setup a variable to get the length of lAllChars - this will be used to creat a\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\/04\/12463-nametag600.jpg?fit=950%2C534&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/12463-nametag600.jpg?fit=950%2C534&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/12463-nametag600.jpg?fit=950%2C534&resize=700%2C400 2x"},"classes":[]},{"id":276,"url":"http:\/\/www.vicdebaie.com\/blog\/saving-motionbuilder-character-animation-with-python\/","url_meta":{"origin":274,"position":1},"title":"Saving MotionBuilder Character Animation with Python","author":"admin","date":"April 5, 2017","format":false,"excerpt":"When I get Mocap it's usually in a messy scene. Optics, Markets, strange NameSpace, etc. they all can bog down a scene. One of the first thing I do is save out the Character Animation and load it back into a clean scene. Using Python to save out Character Animation\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\/03\/img_4059-1.jpg?fit=300%2C200&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":528,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-clean-character-from-scene-with-fbdelete\/","url_meta":{"origin":274,"position":2},"title":"MotionBuilder Python Clean Character From Scene with FBDelete()","author":"admin","date":"April 23, 2018","format":false,"excerpt":"This is a script that came about while researching and writing examples for my previous post on FBDelete(). This bit of script will go through a number of constructors within the FBSystem() class. Here is a list of the classes in which our list will go through: fb.FBSystem().Scene.Constraints fb.FBSystem().Scene.Handles fb.FBSystem().Scene.UserObjects\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\/2018\/04\/delete.jpg?fit=768%2C512&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/04\/delete.jpg?fit=768%2C512&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/04\/delete.jpg?fit=768%2C512&resize=700%2C400 2x"},"classes":[]},{"id":794,"url":"http:\/\/www.vicdebaie.com\/blog\/using-motionbuilder-python-to-offset-all-animations-within-a-scene\/","url_meta":{"origin":274,"position":3},"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":747,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-mirror-character-animation-through-story-mode\/","url_meta":{"origin":274,"position":4},"title":"MotionBuilder Python &#8211; Mirror Character Animation Through Story Mode","author":"admin","date":"November 21, 2019","format":false,"excerpt":"A quick script that will mirror animations via Story Mode.","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\/11\/Mirror-Cap.jpg?fit=960%2C640&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/11\/Mirror-Cap.jpg?fit=960%2C640&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/11\/Mirror-Cap.jpg?fit=960%2C640&resize=700%2C400 2x"},"classes":[]},{"id":81,"url":"http:\/\/www.vicdebaie.com\/blog\/plotting-character-animation-with-a-motionbuilder-python-script\/","url_meta":{"origin":274,"position":5},"title":"Plotting Character Animation With A MotionBuilder Python Script .\u00a0","author":"admin","date":"February 9, 2017","format":false,"excerpt":"Let's go over how to use Python to plot animation onto a character's Control Rig. The interesting part of this is\u00a0the \"plot options\" and how we call back on them when we plot. \u00a0 [code language=\"python\"] from pyfbsdk import * # Defining our Characater as the currnetly selected one lCharacter\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":[]}],"_links":{"self":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/274"}],"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=274"}],"version-history":[{"count":3,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions"}],"predecessor-version":[{"id":324,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/274\/revisions\/324"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media\/323"}],"wp:attachment":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media?parent=274"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/categories?post=274"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/tags?post=274"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}