{"id":4168,"date":"2020-03-05T09:33:09","date_gmt":"2020-03-05T17:33:09","guid":{"rendered":"https:\/\/trikingo.com\/?p=4168"},"modified":"2020-03-15T22:51:10","modified_gmt":"2020-03-16T05:51:10","slug":"using-the-new-calculatedistance-tool-for-maya","status":"publish","type":"post","link":"https:\/\/trikingo.com\/using-the-new-calculatedistance-tool-for-maya\/","title":{"rendered":"Using the new CalculateDistance tool"},"content":{"rendered":"\n<h2>A few notes<\/h2>\n\n\n\n<p>After some more implementations, this tool is officially ready to use. I added a new &#8220;developer&#8221; mode, so that it can be used within another script rather than using it as a standalone.<\/p>\n\n\n\n<p>Previously this tool only displayed the current frame distance between the selected objects through a prompt box, and that was it.<\/p>\n\n\n\n<p>In order to make it more useful, I decided to add a few more functions that return the actual value of the distance as a string. This string can then be used within any script. <\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2>Usage of the tool<\/h2>\n\n\n\n<p>To install, simply download the tool here:<a href=\"https:\/\/trikingo.com\/product\/calculatedistance-for-maya\/\"> CalculateDistance<\/a> and unzip it to your \/maya\/scripts\/ folder. Inside Maya, in a Script Editor python tab you can type the following commands depending on the mode you want to use. You can map these commands to a hot key or to a shelf button.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h4>Regular Mode<\/h4>\n\n\n\n<p>1. Calculate distance from current camera to selected object&#8217;s pivot:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">import CalculateDistance;CalculateDistance.camToPivot(); <\/pre><\/div>\n\n\n\n<p>2. Distance from current camera to closest point in a selected geometry:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">import CalculateDistance;CalculateDistance.camToSurface(False);<\/pre><\/div>\n\n\n\n<p> 3. Same usage than previous point, but creating a locator in closest point. <\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">import CalculateDistance;CalculateDistance.camToSurface(True);<\/pre><\/div>\n\n\n\n<p>4. Distance between 2 selected objects (No camera involved)<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">import CalculateDistance;CalculateDistance.between2Objects();<\/pre><\/div>\n\n\n\n<h4>Developer mode<\/h4>\n\n\n\n<p>This mode returns the distance as a string instead of displaying it. The usage is the same than the regular mode, with these functions available:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">import CalculateDistance\nCalculateDistance.camToPivotDev()\nCalculateDistance.camToSurfaceDev()\nCalculateDistance.between2ObjectsDev()<\/pre><\/div>\n\n\n\n<p>Example<br>If at some point you want to hook the distance between the camera and an object\u2019s closest vertex to an attribute in a certain frame range, you can do the following:<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"python\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\"># Import maya commands library\nimport maya.cmds as cmds\n# Import the tool\nimport CalculateDistance\n# Get the start frame of the current timeline\nstartFrame = cmds.playbackOptions( q=True,min=True )\n# Get the last inclusive frame\nendFrame = cmds.playbackOptions( q=True,max=True )+1.0\n# Get the current selected item\ngetSelection=cmds.ls(selection=True)\n# Check if there is anything selected.\nif getSelection:\n    # Iterate through each frame\n\tfor frame in range(int(startFrame), int(endFrame)):\n\t    # Select the item again in case it gets de-selected during the process\n\t\tcmds.select(getSelection)\n\t\t# Jump to the current frame\n\t\tcmds.currentTime(frame)\n\t\t# Calculate the distance and store it in a variable\n\t\tdistance=CalculateDistance.camToSurfaceDev()\n        # Set the focal length of the camera to the distance value\t\t\n\t\tcmds.setAttr(\"perspShape.focalLength\", distance)\n\t\t# Add a keyframe to \"bake\" it\n\t\tcmds.setKeyframe('perspShape.focalLength')<\/pre><\/div>\n\n\n\n<p>This snippet will &#8220;bake&#8221; the distance into the perspective camera&#8217;s focalLength attribute. You can use that loop and adapt it to your needs.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>After some more implementations, this tool is officially ready to use. I added a new &#8220;developer&#8221; mode, so that it can be used within another script rather than using it as a standalone.<\/p>\n","protected":false},"author":2,"featured_media":4304,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[122],"tags":[],"jetpack_featured_media_url":"https:\/\/i1.wp.com\/trikingo.com\/wp-content\/uploads\/2020\/03\/cd2-2.png?fit=1920%2C559&ssl=1","_links":{"self":[{"href":"https:\/\/trikingo.com\/api\/wp\/v2\/posts\/4168"}],"collection":[{"href":"https:\/\/trikingo.com\/api\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/trikingo.com\/api\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/comments?post=4168"}],"version-history":[{"count":12,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/posts\/4168\/revisions"}],"predecessor-version":[{"id":4425,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/posts\/4168\/revisions\/4425"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/media\/4304"}],"wp:attachment":[{"href":"https:\/\/trikingo.com\/api\/wp\/v2\/media?parent=4168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/categories?post=4168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/tags?post=4168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}