{"id":645,"date":"2018-11-14T11:50:40","date_gmt":"2018-11-14T16:50:40","guid":{"rendered":"http:\/\/www.vicdebaie.com\/blog\/?p=645"},"modified":"2018-11-21T12:22:51","modified_gmt":"2018-11-21T17:22:51","slug":"motionbuilder-python-a-better-way-to-create-constraints","status":"publish","type":"post","link":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-a-better-way-to-create-constraints\/","title":{"rendered":"MotionBuilder Python &#8211; A Better Way To Create Constraints"},"content":{"rendered":"<p><img decoding=\"async\" loading=\"lazy\" class=\"aligncenter \" src=\"https:\/\/upload.wikimedia.org\/wikipedia\/commons\/4\/4e\/...Think%5E_Is_There_a_Better_Way_to_Do_it%5E_-_NARA_-_534256.jpg\" width=\"400\" height=\"204\" \/><\/p>\n<h1><strong>Update<\/strong><\/h1>\n<p>Thanks to\u00a0<strong>kilianeczka\u00a0<\/strong>for the heads up, there is even an EASIER easy way to create a constraint using MotionBuilder and Python<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\nimport pyfbsdk as fb\r\n\r\nfb.FBConstraintManager().TypeCreateConstraint('Parent\/Child')\r\n\r\n<\/pre>\n<p>This omits the need to find the number of constraints and then search all the constraints for one that has the name\u00a0&#8216;Parent\/Child&#8217;.<\/p>\n<hr \/>\n<p>In my previous post about constraint (<a href=\"http:\/\/www.vicdebaie.com\/blog\/motionbuilders-constraint-system-and-python\/\">link<\/a>), I was creating different constraint types by using the predefined numeric value that represented them.<\/p>\n<p>example:\u00a0\u00a0lMyCons = FBConstraintManager() .TypeCreateConstraint(10)<\/p>\n<p>The number &#8220;10&#8221; would represent the &#8220;Parent\/Child&#8221; constraint type.<\/p>\n<p>The above method worked for me but it didn&#8217;t for many others and the reason being was that the number did <em><strong>not<\/strong><\/em> <em><strong>always<\/strong><\/em> represent the specific constraint type &#8220;Parent\/Child&#8221;.\u00a0 Depending on the release version of MotionBuilder you are using and if you have unique constraints created for your project &#8211; the numbers will vary, so we can agree that using &#8220;lMyCons = FBConstraintManager() .TypeCreateConstraint(10)&#8221; to create a &#8220;Parent\/Child&#8221; constraint is not the smartest of way . With each release of MotionBuider relying solely on a numerical value to represent our constraint type is not future proof .<\/p>\n<p>On to a better way!!<\/p>\n<p>In all honesty we will still be using a numerical value to tell our Python script which constraint we desire, but we will be doing it in a different way &#8211; we will use the name of the desired constraint as our final condition in our script.\u00a0 Let&#8217;s make a script that goes through every constraint within MotioBuilder and when the constraint&#8217;s name matches our desired name, then and only then will we have MotionBuilder create the constraint.<\/p>\n<p>So to do this we will have to cycle through every constraint, which is an easy enough task once we know the amount of constraint that exist within MotionBuilder &#8211; to do this we will use the class FBConstraintManager().TypeGetCount().<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport pyfbsdk as fb\r\n\r\nfb.FBConstraintManager().TypeGetCount()\r\n<\/pre>\n<p>This will return the number of constraint MotionBuilder has available and that number will be used in a range to list all our constraints with the help of FBConstraintManager().TypeGetName().\u00a0 \ud83d\ude42<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport pyfbsdk as fb\r\n\r\nfor i in range( fb.FBConstraintManager().TypeGetCount() ):\r\n    print fb.FBConstraintManager().TypeGetName(i)\r\n<\/pre>\n<p>With the above script we now can get a list of all the constraint names within our scene.<\/p>\n<p>Since we now know how many constraints there are and what their names are &#8211; we can now isolate the constraint we want by its name.<\/p>\n<p>Let&#8217;s use the name &#8220;Parent\/Child&#8221; as an example:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\n\r\nimport pyfbsdk as fb\r\n\r\n##For each entry in the range of 0 to the total # of constraints\r\n## i will represent the current number being processed within the range 0 to the total # of constraints\r\nfor i in range( fb.FBConstraintManager().TypeGetCount() ):\r\n    \r\n    ## Look for the string &quot;Parent\/Child&quot; in the constraints name\r\n    if &quot;Parent\/Child&quot; in fb.FBConstraintManager().TypeGetName(i):\r\n    \r\n        ##When a match occurs then create that constraint\r\n        fb.FBConstraintManager().TypeCreateConstraint(i)\r\n<\/pre>\n<p>By finding the total number of constraint and then searching all the constraints for a name is kind of the long way around but it is the most reliable that I have come across within MotionBuilder.<\/p>\n<p>So now lets write a little function that takes all of the above and puts it to good use.<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nimport pyfbsdk as fb\r\n\r\n'''\r\nUsage Example: CreateParentConstraint(&quot;Marker&quot;, &quot;Marker 1&quot;, True, 100)\r\n\r\nThis will create a Parent\/Child constraint with the object named &quot;Marker&quot; as the parent, the object names &quot;Marker 1&quot; as the child. The child will maintain its original position and the weight of the constraint will be set to 100% \r\n'''\r\n\r\ndef CreateParentConstraint(parent, child, offset, weight): \r\n    ##Get Object By Name\r\n    def GetObjectByName(name):\r\n        lModel = fb.FBFindModelByLabelName(name) \r\n        return lModel\r\n    \r\n    ##Set Parent Object    \r\n    objParent = GetObjectByName(parent)\r\n    ##Set Child Object\r\n    objChild = GetObjectByName(child)\r\n    \r\n    ##Create &quot;Parent\/Child&quot; Constraint \r\n    for i in range( fb.FBConstraintManager().TypeGetCount() ):\r\n        if &quot;Parent\/Child&quot; in fb.FBConstraintManager().TypeGetName(i):\r\n            lMyConstraint = fb.FBConstraintManager().TypeCreateConstraint(i)\r\n        \r\n    #for index, element in enumerate(selected_objects):\r\n    lMyConstraint.ReferenceAdd (0, objChild)\r\n    lMyConstraint.ReferenceAdd (1, objParent)\r\n    \r\n    #Snap if user desires\r\n    if offset == True:\r\n        lMyConstraint.Snap()\r\n    \r\n    #weight of the constraint\r\n    lMyConstraint.Weight = weight\r\n    \r\n    ##Activate Constraint\r\n    lMyConstraint.Active = True\r\n    \r\n<\/pre>\n<p>So there we have it, this is a better way to create a constraint than my previous post has show.<\/p>\n<p>As always I hope this helps.<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>Update Thanks to\u00a0kilianeczka\u00a0for the heads up, there is even an EASIER easy way to create a constraint using MotionBuilder and Python import pyfbsdk as fb fb.FBConstraintManager().TypeCreateConstraint(&#8216;Parent\/Child&#8217;) This omits the need to find the number of constraints and then search all the constraints for one that has the name\u00a0&#8216;Parent\/Child&#8217;. In my previous post about constraint (link), [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":651,"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\/11\/Think^_Is_There_a_Better_Way_to_Do_it^_-_NARA_-_534256.jpg?fit=400%2C204","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8pltq-ap","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":566,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-parent-constraint-tool\/","url_meta":{"origin":645,"position":0},"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":167,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilders-constraint-system-and-python\/","url_meta":{"origin":645,"position":1},"title":"MotionBuilder&#8217;s Constraint system and Python.","author":"admin","date":"April 7, 2017","format":false,"excerpt":"MotionBuilder's Constraint system is a must for animators. Weather you are rigging, transferring data or animating - you will want to use a Constraint system to make your life easier. Let's see how Python can access MotionBuilder's Constraint system. Using Python to set up a Constraint system in MotionBuilder took\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":711,"url":"http:\/\/www.vicdebaie.com\/blog\/using-motionbuilder-python-to-cast-real-time-shadows\/","url_meta":{"origin":645,"position":2},"title":"Using MotionBuilder Python To Cast Real-Time Shadows","author":"admin","date":"September 18, 2019","format":false,"excerpt":"Using Python To Quickly Add A Real Time Shadow To Your MotionBuilder Scene.","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\/09\/ShadowCube.png?fit=1157%2C526&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/09\/ShadowCube.png?fit=1157%2C526&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/09\/ShadowCube.png?fit=1157%2C526&resize=700%2C400 2x, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2019\/09\/ShadowCube.png?fit=1157%2C526&resize=1050%2C600 3x"},"classes":[]},{"id":596,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-my-fav-story-functions\/","url_meta":{"origin":645,"position":3},"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":77,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-where-to-start\/","url_meta":{"origin":645,"position":4},"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":336,"url":"http:\/\/www.vicdebaie.com\/blog\/how-to-use-functions-with-motionbuilder-python\/","url_meta":{"origin":645,"position":5},"title":"How to use functions with MotionBuilder Python","author":"admin","date":"April 25, 2017","format":false,"excerpt":"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? \"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\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\/c2c827b3a6ea16d024b6875b5610651c-e1493127532666.gif?fit=251%2C300&resize=350%2C200","width":350,"height":200},"classes":[]}],"_links":{"self":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/645"}],"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=645"}],"version-history":[{"count":5,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/645\/revisions"}],"predecessor-version":[{"id":654,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/645\/revisions\/654"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media\/651"}],"wp:attachment":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media?parent=645"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/categories?post=645"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/tags?post=645"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}