{"id":336,"date":"2017-04-25T09:34:26","date_gmt":"2017-04-25T13:34:26","guid":{"rendered":"http:\/\/www.vicdebaie.com\/blog\/?p=336"},"modified":"2017-11-06T09:16:50","modified_gmt":"2017-11-06T14:16:50","slug":"how-to-use-functions-with-motionbuilder-python","status":"publish","type":"post","link":"http:\/\/www.vicdebaie.com\/blog\/how-to-use-functions-with-motionbuilder-python\/","title":{"rendered":"How to use functions with MotionBuilder Python"},"content":{"rendered":"<p><img data-attachment-id=\"338\" data-permalink=\"http:\/\/www.vicdebaie.com\/blog\/how-to-use-functions-with-motionbuilder-python\/c2c827b3a6ea16d024b6875b5610651c\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/c2c827b3a6ea16d024b6875b5610651c-e1493127532666.gif?fit=251%2C300\" data-orig-size=\"251,300\" 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\/c2c827b3a6ea16d024b6875b5610651c-e1493127532666.gif?fit=251%2C300\" data-large-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/c2c827b3a6ea16d024b6875b5610651c-e1493127532666.gif?fit=251%2C300\" decoding=\"async\" loading=\"lazy\" src=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/04\/c2c827b3a6ea16d024b6875b5610651c.gif?resize=251%2C300\" alt=\"\" width=\"251\" height=\"300\" class=\"aligncenter size-medium wp-image-338\" data-recalc-dims=\"1\" \/><br \/>\nUsing <strong><em>functions<\/em><\/strong> will save you from re-writing and maintaining many of the same lines of code over and over.<\/p>\n<p>What is a Python Function?<\/p>\n<p>&#8220;A <b>function<\/b> is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. As you already know, <b>Python<\/b> gives you many built-in functions like print(), etc. but you can also create your own functions.&#8221; &#8211;\u00a0<a href=\"https:\/\/www.google.ca\/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=2&amp;cad=rja&amp;uact=8&amp;ved=0ahUKEwiFsoe71r_TAhWS0YMKHZCNCoQQFgglMAE&amp;url=https%3A%2F%2Fwww.tutorialspoint.com%2Fpython%2Fpython_functions.htm&amp;usg=AFQjCNFl1GRhQGc5uvwMe_F1SdRrp81tIg&amp;sig2=kHTRXZamCLhpEHYPpuEtqw\">Python Functions @ TutorialsPoint<\/a><\/p>\n<p>How can we use a Python Function within MotionBuilder?<\/p>\n<p>Functions were pretty simple to figure out, we define (&#8220;def&#8221;) the name of our function, followed by parentheses and then within those parentheses we place any parameters or arguments:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef PrintHiTake():\r\n<\/pre>\n<p>In the above line we have defined the name of our function and we didn&#8217;t use any parameters or arguments. Note the &#8220;:&#8221; at the end as this will be the start our code block (no different from using the &#8220;if&#8221; and &#8220;else&#8221; functions within Python).<\/p>\n<p>Now let&#8217;s set up our code block to print a greet message and then the current take&#8217;s name.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\ndef PrintHello():\r\n    print &quot;Hello, I'm on&quot;, FBSystem().CurrentTake.LongName\r\n<\/pre>\n<p>If you ran the above script little would happen, once the function is defined we then need to call it within our script.<\/p>\n<p>So, lets call our function within a loop &#8211; a loop that goes through every take within your scene and prints &#8220;Hello, I&#8217;m on (insert take name here)&#8221;.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pyfbsdk import *\r\n\r\nfor take in FBSystem().Scene.Takes:\r\n    FBSystem().CurrentTake = take\r\n    PrintHello() #here we are calling our function\r\n\r\ndef PrintHello():\r\n    print &quot;Hello, I'm on&quot;, FBSystem().CurrentTake.LongName\r\n<\/pre>\n<p>Now if you run that script you should get a print out (in the Python Editor&#8217;s Console window) of &#8220;Hello, I&#8217;m on (insert take name here)&#8221; for each and every take within your scene.<\/p>\n<p>Try adding and removing takes and running the script again to see it update.<\/p>\n<p>I hope this helps.<br \/>\n&nbsp;<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Using functions will save you from re-writing and maintaining many of the same lines of code over and over. What is a Python Function? &#8220;A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":338,"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\/c2c827b3a6ea16d024b6875b5610651c-e1493127532666.gif?fit=251%2C300","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8pltq-5q","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":77,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-where-to-start\/","url_meta":{"origin":336,"position":0},"title":"MotionBuilder Python &#8211; Where to start","author":"admin","date":"February 6, 2017","format":false,"excerpt":"Finding information for Python scripting in MotionBuilder can be an uphill battle. Here is a list of some of the resources that I have been able to find (thanks google). A lot of the listed links have valuable scripts in which one can hack through and decipher what each line\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\/16697171583_7c33584c4b.jpg?fit=500%2C333&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":596,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-my-fav-story-functions\/","url_meta":{"origin":336,"position":1},"title":"MotionBuilder Python My Fav Story Functions","author":"admin","date":"July 17, 2018","format":false,"excerpt":"After posting \"MotionBuilder Python Library aka. My Fav Functions\" I thought I would follow up with some breakdown on manipulating Story Tracks, Clips and Folders with MotionBuilder's Python modules. I briefly covered this a while ago here but that was really just an example of how to put characters into\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\/07\/Storycheatsheet.jpg?fit=782%2C474&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/07\/Storycheatsheet.jpg?fit=782%2C474&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2018\/07\/Storycheatsheet.jpg?fit=782%2C474&resize=700%2C400 2x"},"classes":[]},{"id":63,"url":"http:\/\/www.vicdebaie.com\/blog\/hello-world-a-k-a-let-us-begin\/","url_meta":{"origin":336,"position":2},"title":"Hello World! &#8211; A.k.a &#8220;Let us begin&#8221;","author":"admin","date":"February 6, 2017","format":false,"excerpt":"Here is my first posting for this experiment - \"the blog\". For the last two weeks I have been mucking around with Python scripting within MotionBuilder. It has been an uphill battle. With the limited resources out there all I can really do is hack away at bits and pieces\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_3934.jpg?fit=526%2C492&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":345,"url":"http:\/\/www.vicdebaie.com\/blog\/finding-a-characters-namespace-with-motionbuilder-python\/","url_meta":{"origin":336,"position":3},"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":566,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-parent-constraint-tool\/","url_meta":{"origin":336,"position":4},"title":"MotionBuilder Python Parent Constraint Tool","author":"admin","date":"June 18, 2018","format":false,"excerpt":"Here is a quick post to share a tool I created a little while back. It is designed to help to quickly create Parent Constraints between two objects as well as easily allow the user to bake that constraint down. The script can be found HERE and there are 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\/02\/img_3937.jpg?fit=443%2C364&resize=350%2C200","width":350,"height":200},"classes":[]},{"id":794,"url":"http:\/\/www.vicdebaie.com\/blog\/using-motionbuilder-python-to-offset-all-animations-within-a-scene\/","url_meta":{"origin":336,"position":5},"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":[]}],"_links":{"self":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/336"}],"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=336"}],"version-history":[{"count":7,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions"}],"predecessor-version":[{"id":344,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/336\/revisions\/344"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media\/338"}],"wp:attachment":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media?parent=336"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/categories?post=336"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/tags?post=336"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}