{"id":3862,"date":"2020-02-28T13:56:04","date_gmt":"2020-02-28T21:56:04","guid":{"rendered":"https:\/\/trikingo.com\/?p=3862"},"modified":"2020-03-06T17:43:30","modified_gmt":"2020-03-07T01:43:30","slug":"distance-between-two-points","status":"publish","type":"post","link":"https:\/\/trikingo.com\/distance-between-two-points\/","title":{"rendered":"Distance between two points"},"content":{"rendered":"\n<h2>Measure distance with OpenMaya <\/h2>\n\n\n\n<p>Recently I received a request from a layout artist, he asked me about a script to get the distance from the camera to any selected object in the Maya viewport. His specific case was to set the depth of field of the camera based on the distance value. <\/p>\n\n\n\n<p>His second request was for the script to be able to be mapped into a hotkey for quick access.<\/p>\n\n\n\n<p>While there are several ways to approach this request, I decided to build a small tool to solve this issue using OpenMaya instead of cmds. If you are here just looking for the tool, you can find it for free here: <a href=\"https:\/\/trikingo.com\/product\/calculatedistance-for-maya\/\">CalculateDistance<\/a><\/p>\n\n\n\n<p>If you are more curious and want to have a sneak peak of the code, here is a python snippet showing how to get the distance between the current active camera and any selected object&#8217;s pivot point. <\/p>\n\n\n\n<p>This snippet only prints the distance from the camera to the object&#8217;s pivot (worldspace xform), while the full version of the tool has this extra features:<\/p>\n\n\n\n<ul><li> Allows to get the closest point from the camera to the surface of a geometry. (Vertex based instead of xform)<\/li><li>Create a locator in the closest point on the surface of the object.<\/li><li>Measure distance between 2 selected objects.<\/li><li>Shows a prompt box to copy the distance without opening the script editor.<\/li><\/ul>\n\n\n\n<p>To test this snippet, select any object (geometry, locator, group, etc), and run it. <\/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\"># Imports\nimport maya.OpenMayaUI as omui\nimport maya.OpenMaya as om\nimport maya.cmds as cmds\nfrom math import pow,sqrt\n\n# Get selection from vieport\ngetSelection=cmds.ls(selection=True)\n\n# Get current active camera\nview = omui.M3dView.active3dView()\ncam = om.MDagPath()\nview.getCamera(cam)\n\n# Get the parent of the camera shape\ngetCamera = cmds.listRelatives(cam.fullPathName(), p=True)\n\n# Wodlspace xform of the camera\ncamXForm = cmds.xform(getCamera[0], q=True, t=True, ws=True)\n\n# Worldspace xform of the selected object\nobjXform = cmds.xform(getSelection[0], q=True, t=True, ws=True)\n\n# Distance formula : \u221a((x2 - x1)2 + (y2 - y1)2 + (z2 - z1)2)\ndistance = sqrt(pow(camXForm[0]-objXform[0],2)+pow(camXForm[1]-objXform[1],2)+pow(camXForm[2]-objXform[2],2))\n\n# Just print the distance\nprint distance<\/pre><\/div>\n\n\n\n<p>If you find this useful, please leave a comment with some feedback or requests!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Recently I ran across a colleague who asked me about a script to get the distance from the camera to any selected object in the Maya viewport. His specific case was to set the depth of field of the camera based on the distance value. <\/p>\n","protected":false},"author":2,"featured_media":4008,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":""},"categories":[73],"tags":[79,75,77,76],"jetpack_featured_media_url":"https:\/\/i2.wp.com\/trikingo.com\/wp-content\/uploads\/2020\/02\/distance-1.png?fit=1813%2C469&ssl=1","_links":{"self":[{"href":"https:\/\/trikingo.com\/api\/wp\/v2\/posts\/3862"}],"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=3862"}],"version-history":[{"count":14,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/posts\/3862\/revisions"}],"predecessor-version":[{"id":4426,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/posts\/3862\/revisions\/4426"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/media\/4008"}],"wp:attachment":[{"href":"https:\/\/trikingo.com\/api\/wp\/v2\/media?parent=3862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/categories?post=3862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/trikingo.com\/api\/wp\/v2\/tags?post=3862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}