{"id":74,"date":"2009-07-05T18:50:31","date_gmt":"2009-07-05T18:50:31","guid":{"rendered":"http:\/\/sugarandcyanide.com\/blog\/?p=74"},"modified":"2009-07-05T18:50:31","modified_gmt":"2009-07-05T18:50:31","slug":"a-quick-note-on-object-snapping","status":"publish","type":"post","link":"http:\/\/sugarandcyanide.com\/blog\/2009\/07\/05\/a-quick-note-on-object-snapping\/","title":{"rendered":"a quick note on object snapping"},"content":{"rendered":"<p>A few weeks ago I got stuck while writing a foot snapping tool &#8212; the snapping code I used to use didn&#8217;t work on the rigs we inherited because there were a lot of pivot changes.  I tried a bunch of more elegant ways to fix this but ended up doing the old standby:<\/p>\n<p><code>delete `orientConstraint`;<br \/>\ndelete `pointConstraint`;<\/code><\/p>\n<p>Now, that solution wasn&#8217;t a satisfying one to me, so I kept looking for a better one.<\/p>\n<p>Turns out there&#8217;s an easy fix.  If you use <b>getAttr<\/b> on a transform node&#8217;s .worldMatrix, you get back the proper 4&#215;4 set of float values that represents its final position in world space.  Afterwards you can set the matrix of another object using <b>xform<\/b>.  It&#8217;s pretty simple in either MEL or Python.<\/p>\n<p><code><br \/>\n\/\/ this snaps one object to another using worldSpace matrix coordinates<br \/>\nstring $sel[] = `ls -sl`;<br \/>\nfloat $m[] = `getAttr ($sel[1] + \".worldMatrix\")`;<br \/>\nxform -ws -m $m[0] $m[1] $m[2] $m[3]  $m[4] $m[5] $m[6] $m[7]  $m[8] $m[9] $m[10] $m[11]  $m[12] $m[13] $m[14] $m[15]  ($sel[0]);<br \/>\n<\/code><\/p>\n<p>The MEL code is a bit ugly because of having to specifically reference each item in the array of floats, but it works. Here&#8217;s the Python equivalent. I keep it now as a button my shelf.<\/p>\n<p><code><br \/>\n# uses matrices to snap one object to another<br \/>\nimport maya<br \/>\n# yes, I put all my commands into the main namespace<br \/>\nfrom maya.cmds import *<\/p>\n<p>sel = ls(sl=True)<br \/>\nmat = getAttr(sel[1] + \".worldMatrix\") <br \/>\nxform(sel[0], ws=True, m=mat)<br \/>\n<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago I got stuck while writing a foot snapping tool &#8212; the snapping code I used to use didn&#8217;t work on the rigs we inherited because there were a lot of pivot changes. I tried a bunch of more elegant ways to fix this but ended up doing the old standby: delete [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4,13],"tags":[],"class_list":["post-74","post","type-post","status-publish","format-standard","hentry","category-3d","category-maya"],"_links":{"self":[{"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/posts\/74","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/comments?post=74"}],"version-history":[{"count":0,"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/posts\/74\/revisions"}],"wp:attachment":[{"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/media?parent=74"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/categories?post=74"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/sugarandcyanide.com\/blog\/wp-json\/wp\/v2\/tags?post=74"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}