{"id":167,"date":"2017-04-07T10:08:53","date_gmt":"2017-04-07T14:08:53","guid":{"rendered":"http:\/\/www.vicdebaie.com\/blog\/?p=167"},"modified":"2018-11-13T16:23:06","modified_gmt":"2018-11-13T21:23:06","slug":"motionbuilders-constraint-system-and-python","status":"publish","type":"post","link":"http:\/\/www.vicdebaie.com\/blog\/motionbuilders-constraint-system-and-python\/","title":{"rendered":"MotionBuilder&#8217;s Constraint system and Python."},"content":{"rendered":"<p><a href=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/img_3937.jpg\"><img data-attachment-id=\"134\" data-permalink=\"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-parent-constraint-tool\/img_3937-jpg\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/img_3937.jpg?fit=443%2C364\" data-orig-size=\"443,364\" 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;1&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\/02\/img_3937.jpg?fit=300%2C247\" data-large-file=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/img_3937.jpg?fit=443%2C364\" decoding=\"async\" loading=\"lazy\" class=\"size-full wp-image-134 aligncenter\" src=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/img_3937.jpg?resize=443%2C364\" alt=\"\" width=\"443\" height=\"364\" srcset=\"https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/img_3937.jpg?w=443 443w, https:\/\/i0.wp.com\/www.vicdebaie.com\/blog\/wp-content\/uploads\/2017\/02\/img_3937.jpg?resize=300%2C247 300w\" sizes=\"(max-width: 443px) 100vw, 443px\" data-recalc-dims=\"1\" \/><\/a><\/p>\n<p>MotionBuilder&#8217;s Constraint system is a must for animators. Weather you are rigging, transferring data or animating &#8211; you will want to use a Constraint system to make your life easier. Let&#8217;s see how Python can access MotionBuilder&#8217;s Constraint system.<\/p>\n<p>Using Python to set up a Constraint system in MotionBuilder took a lot of searching for me. I wanted to perform a task that used a Parent Child Constraint and when I figured out how to get Python to do a Parent Child Constraint it felt amazing!<\/p>\n<p>Below I will show you what I figured out as well as list out all the different MotionBuilder Constraints you can have Python access for you.<\/p>\n<p>First we will set up a script that will create a scene for us with two markers for us to use and then apply a &#8220;Parent Child&#8221; constraint to them:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pyfbsdk import *\r\nfrom pyfbsdk_additions import *\r\n\r\nlSystem = FBSystem()\r\nlScene = FBSystem().Scene\r\n\r\n# Creating the two objects that will be used for our constraint scipt\r\n\r\n#Creat our &quot;lParentMarker&quot; and names it 'ParentMarker'\r\nlParentMarker = FBModelMarker('ParentMarker')\r\n#Set our Marker to be visible\r\nlParentMarker.Show = True\r\n#Set the size of the lParentMarker\r\nlParentMarker.Size = 2500\r\n#Set the &quot;Look&quot; property of the lParentMarker, the end # is the visual stle. See list below to find numeric values for each visual style\r\nlParentMarker.PropertyList.Find('LookUI').Data=1\r\n#Set the color value for the lParentMarker\r\nlParentMarker.Color = FBColor(0,1,1)\r\n#Define the Transaltion to be represented as lParentMarker.Translation\r\nxTrans, yTrans, zTrans = lParentMarker.Translation\r\n#Declare that lParentMarker.Translation can be animated\r\nlParentMarker.Translation.SetAnimated(True)\r\n#postition lParentMarker in the world\r\nlParentMarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,0), &#x5B;-200, 150, 0])\r\n\r\n#Creat our &quot;lChildMarker&quot; and names it 'ChildMarker'\r\nlChildMarker = FBModelMarker('ChildMarker')\r\n#Set our Marker to be visible\r\nlChildMarker.Show = True\r\n#Set the size of the lChildMarker\r\nlChildMarker.Size = 2500\r\n#Set the &quot;Look&quot; property of the lChildMarker, the end # is the visual stle. See list below to find numeric values for each visual style\r\nlChildMarker.PropertyList.Find('LookUI').Data=1\r\n#Set the color value for the lChildMarker\r\nlChildMarker.Color = FBColor(1,1,0.3)\r\n#Define the Transaltion to be represented as lChildMarker.Translation\r\nxTrans, yTrans, zTrans = lChildMarker.Translation\r\n#Declare that lChildMarker.Translation can be animated\r\nlChildMarker.Translation.SetAnimated(True)\r\n#postition lChildMarker in the world\r\nlChildMarker.Translation.GetAnimationNode().KeyAdd(FBTime(0,0,0,0), &#x5B;0, 50, 0])\r\n\r\n\r\n# Go to last frame and then back to the first frame - this is a HACK to update the viewport\r\n'''\r\nIf I don't up date the display the objects will stay at 0, 0 ,0 of the world and\r\nwhen we apply the constraint the two objects will be on top of one and other\r\n'''\r\nFBPlayerControl().GotoEnd()\r\nFBPlayerControl().GotoStart()\r\n\r\n\r\n# Selecting objects for the Parent Child Constraint\r\nlChildMdl = FBFindModelByLabelName(&quot;ChildMarker&quot;)\r\nlChildMdl.Selected = True\r\n\r\nlParentMdl = FBFindModelByLabelName(&quot;ParentMarker&quot;)\r\nlParentMdl.Selected = True\r\n\r\ndef constraint(CONSTRAINT_TYPE = 10, Weight = 100, SNAP = False, child = None , parent = None  ):\r\n    #assign child and parents variables and check if the right amount of items are selected\r\n    if child == None or parent == None:\r\n        selectedModels = FBModelList()\r\n        FBGetSelectedModels (selectedModels, None, True, True)\r\n        if len(selectedModels) &amp;lt;= 1 or len(selectedModels) &amp;gt; 2 :\r\n           FBMessageBox( &quot;Error&quot;, &quot;Please select two items&quot;, &quot;OK&quot; )\r\n           return\r\n    \r\n    selected_objects = &#x5B;FBFindObjectByFullName(model.FullName) for model in selectedModels]\r\n    child = selected_objects&#x5B;0]\r\n    parent = selected_objects&#x5B;1]\r\n    #setting the prefix for the name displayed on the constraint based on its type\r\n    lConstraint_name = &quot;MyPythonScript-Parent_Constraint&quot;\r\n    \r\n    #constraint creation with type and name\r\n    lMyManager = FBConstraintManager()\r\n    #note &quot;10&quot; is being called as it is assigned to &quot;CONSTRAINT_TYPE_PARENT_CHILD&quot;\r\n    lMyCons = lMyManager.TypeCreateConstraint(10)\r\n    lMyCons.Name = lConstraint_name\r\n    \r\n    #adding elements to the constraint slots\r\n    #for index, element in enumerate(selected_objects):\r\n    lMyCons.ReferenceAdd (0, child)\r\n    lMyCons.ReferenceAdd (1, parent)\r\n    \r\n    #setting up the snap option, so the elements will keep their original position\r\n    if SNAP == False:  \r\n      lMyCons.Snap()\r\n    #weight of the constraint\r\n    lMyCons.Weight = 100\r\n    lMyCons.Active = True\r\n\r\n#note &quot;10&quot; is being called as it is assigned to &quot;CONSTRAINT_TYPE_PARENT_CHILD&quot; \r\nconstraint(CONSTRAINT_TYPE = 10, Weight = 100, SNAP = False, child = None , parent = None  )\r\n<\/pre>\n<p>Things to note are the Constraint types list, FBConstraintManager, needing to have the Constraint Active set to &#8220;True&#8221;, the Weight set to 100 and the Snap set to &#8220;False&#8221;.<\/p>\n<p>Now that we have all that out of the way we can go on to shortening that above script into a useful tool that will just take our selected objects and apply a Parent Child Constraint to them:<\/p>\n<pre class=\"brush: python; title: ; notranslate\" title=\"\">\r\nfrom pyfbsdk import *\r\n\r\ndef constraint(CONSTRAINT_TYPE = 10, Weight = 100, SNAP = False, child = None , parent = None  ):\r\n    #assign child and parents variables and check if the right amount of items are selected\r\n    if child == None or parent == None:\r\n        selectedModels = FBModelList()\r\n        FBGetSelectedModels (selectedModels, None, True, True)\r\n        #if we do not have the correct amount of objects selected then warn the usser\r\n        if len(selectedModels) &amp;amp;lt;= 1 or len(selectedModels)&amp;amp;gt; 2 :\r\n           FBMessageBox( &quot;Error&quot;, &quot;Please select two items&quot;, &quot;OK&quot; )\r\n           return\r\n    \r\n    selected_objects = &#x5B;FBFindObjectByFullName(model.FullName) for model in selectedModels]\r\n    \r\n    # define which objects your selected objects are child and which are parent\r\n    '''\r\n    currently the script works as so that\r\n    firt object selected = child\r\n    second object selected = parent\r\n        \r\n    first object selected = selected_object&#x5B;0]\r\n    second object selected = selected_objectp&#x5B;1]\r\n        \r\n    so if you have\r\n    child = selected_objects&#x5B;0] then the first obeject selected will be used as your child\r\n    parent = selected_objects&#x5B;0] then the first obeject selected will be used as your parent\r\n    \r\n    it is all personal taste - I perfer thinking like &quot;I want this object to be the child of this other object&quot;\r\n    when I select my objectw within my scene\r\n    '''\r\n    child = selected_objects&#x5B;0]\r\n    parent = selected_objects&#x5B;1]\r\n\r\n    #setting the prefix for the name displayed on the constraint based on its type\r\n    lConstraint_name = &quot;MyPythonScript-Parent_Constraint&quot;\r\n    \r\n    #constraint creation with type and name\r\n    lMyManager = FBConstraintManager()\r\n    #note &quot;10&quot; is being called as it is assigned to &quot;CONSTRAINT_TYPE_PARENT_CHILD&quot;\r\n    lMyCons = lMyManager.TypeCreateConstraint(10)\r\n    lMyCons.Name = lConstraint_name\r\n    \r\n    #adding elements to the constraint slots\r\n    #for index, element in enumerate(selected_objects):\r\n    lMyCons.ReferenceAdd (0, child)\r\n    lMyCons.ReferenceAdd (1, parent)\r\n    \r\n    #setting up the snap option, so the elements will keep their position\r\n    if SNAP == False:  \r\n      lMyCons.Snap()\r\n    #weight of the constraint\r\n    lMyCons.Weight = 100\r\n    lMyCons.Active = False\r\n    lMyCons.Snap()\r\n\r\n#note &quot;10&quot; is being called as it is assigned to &quot;CONSTRAINT_TYPE_PARENT_CHILD&quot; \r\nconstraint(CONSTRAINT_TYPE = 10, Weight = 100, SNAP = False, child = None , parent = None  ) \r\n<\/pre>\n<p>You can easily set this script up to be triggered by a button within your scene, to learn that you can go <a href=\"http:\/\/www.vicdebaie.com\/blog\/creat-a-button\/\">here <\/a>and read my <a href=\"http:\/\/www.vicdebaie.com\/blog\/creat-a-button\/\">Creat a Button in MotionBuilder with Python<\/a>.<\/p>\n<p>As I mentioned at the start of this post, figuring out how to get Constraints to work through Python took quite a bit of research for me and it was awesome when I was able to finally get the script to work.<\/p>\n<p>I hope this helps.<\/p>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>MotionBuilder&#8217;s Constraint system is a must for animators. Weather you are rigging, transferring data or animating &#8211; you will want to use a Constraint system to make your life easier. Let&#8217;s see how Python can access MotionBuilder&#8217;s Constraint system. Using Python to set up a Constraint system in MotionBuilder took a lot of searching for [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":134,"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\/02\/img_3937.jpg?fit=443%2C364","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8pltq-2H","jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":566,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-parent-constraint-tool\/","url_meta":{"origin":167,"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":645,"url":"http:\/\/www.vicdebaie.com\/blog\/motionbuilder-python-a-better-way-to-create-constraints\/","url_meta":{"origin":167,"position":1},"title":"MotionBuilder Python &#8211; A Better Way To Create Constraints","author":"admin","date":"November 14, 2018","format":false,"excerpt":"Update Thanks to\u00a0kilianeczka\u00a0for the heads up, there is even an EASIER easy way to create a constraint using MotionBuilder and Python [code language=\"python\"] import pyfbsdk as fb fb.FBConstraintManager().TypeCreateConstraint('Parent\/Child') [\/code] This omits the need to find the number of constraints and then search all the constraints for one that has the\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\/11\/Think^_Is_There_a_Better_Way_to_Do_it^_-_NARA_-_534256.jpg?fit=400%2C204&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":167,"position":2},"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":288,"url":"http:\/\/www.vicdebaie.com\/blog\/getting-information-on-motionbuilders-python-with-dir\/","url_meta":{"origin":167,"position":3},"title":"Getting information on MotionBuilder&#8217;s Python with dir","author":"admin","date":"March 13, 2017","format":false,"excerpt":"Trying to figure how things work in MotionBuilder's Python editor is tough. Google and the SDK Help that comes along with MotionBuilder are great, but there is still more help out there - the \"dir\"! :) What does \"dir\" do? The definition I was able to find is \"It gives\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":63,"url":"http:\/\/www.vicdebaie.com\/blog\/hello-world-a-k-a-let-us-begin\/","url_meta":{"origin":167,"position":4},"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":175,"url":"http:\/\/www.vicdebaie.com\/blog\/%ef%bb%bfplotting-selected-properties-using-motionbuilders-python\/","url_meta":{"origin":167,"position":5},"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":[]}],"_links":{"self":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/167"}],"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=167"}],"version-history":[{"count":13,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions"}],"predecessor-version":[{"id":390,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/posts\/167\/revisions\/390"}],"wp:featuredmedia":[{"embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media\/134"}],"wp:attachment":[{"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/media?parent=167"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/categories?post=167"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.vicdebaie.com\/blog\/wp-json\/wp\/v2\/tags?post=167"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}