{"id":16136,"date":"2022-11-03T13:32:23","date_gmt":"2022-11-03T12:32:23","guid":{"rendered":"http:\/\/davy.chang.free.fr\/?p=16136"},"modified":"2022-11-03T17:21:06","modified_gmt":"2022-11-03T16:21:06","slug":"maya-python-bake-transforms-to-offset-parent-matrix","status":"publish","type":"post","link":"http:\/\/davy.chang.free.fr\/index.php\/non-classe\/maya-python-bake-transforms-to-offset-parent-matrix\/","title":{"rendered":"Maya &#8211; Python : bake Transforms to Offset Parent Matrix"},"content":{"rendered":"<p><FONT face=\"Monospace, Arial, sans-serif\"><br \/>\n\/\/ source : <a href=\"https:\/\/gist.github.com\/Muream\/b4255ec4f690222d92fe794be249b7f1\" target=\"_blank\" rel=\"noopener\">https:\/\/gist.github.com\/Muream\/b4255ec4f690222d92fe794be249b7f1<\/a><br \/>\n\/\/ A utiliser en PYTHON !<br \/>\n\/\/ Attention, Python ne supporte pas certains encodage de caract\u00e8re Notepad, HTML&#8230;<br \/>\n\/\/ C&rsquo;est pourquoi le code suivant est brut et entre balise &lt;pre&gt;&#8230;&lt;\/pre&gt;<\/p>\n<pre>\r\nimport maya.api.OpenMaya as om\r\nimport maya.cmds as cmds\r\n\r\nTRANSFORM_NODETYPES = [\"transform\", \"joint\"]\r\n\r\ndef has_non_default_locked_attributes(node):\r\n    locked_attributes = []\r\n    for attribute in [\"translate\", \"rotate\", \"scale\", \"jointOrient\"]:\r\n        default_value = 1 if attribute == \"scale\" else 0\r\n        for axis in \"XYZ\":\r\n            if cmds.attributeQuery(attribute + axis, node=node, exists=True):\r\n                attribute_name = \"{}.{}{}\".format(node, attribute, axis)\r\n                current_value = cmds.getAttr(attribute_name)\r\n                if cmds.getAttr(attribute_name, lock=True) and current_value != default_value:\r\n                    return True\r\n\r\n\r\ndef reset_transforms(node):\r\n    for attribute in [\"translate\", \"rotate\", \"scale\", \"jointOrient\"]:\r\n        value = 1 if attribute == \"scale\" else 0\r\n        for axis in \"XYZ\":\r\n            if cmds.attributeQuery(attribute + axis, node=node, exists=True):\r\n                attribute_name = \"{}.{}{}\".format(node, attribute, axis)\r\n                if not cmds.getAttr(attribute_name, lock=True):\r\n                    cmds.setAttr(attribute_name, value)\r\n\r\n\r\ndef bake_transform_to_offset_parent_matrix(node):\r\n    if cmds.nodeType(node) not in TRANSFORM_NODETYPES:\r\n        raise ValueError(\"Node {} is not a transform node\".format(node))\r\n\r\n    if has_non_default_locked_attributes(node):\r\n        raise RuntimeError(\"Node {} has at least one non default locked attribute(s)\".format(node))\r\n\r\n    local_matrix = om.MMatrix(cmds.xform(node, q=True, m=True, ws=False))\r\n    offset_parent_matrix = om.MMatrix(cmds.getAttr(node + \".offsetParentMatrix\"))\r\n    baked_matrix = local_matrix * offset_parent_matrix\r\n    cmds.setAttr(node + \".offsetParentMatrix\", baked_matrix, type=\"matrix\")\r\n\r\n    reset_transforms(node)\r\n\r\ndef bake_transform_to_offset_parent_matrix_selection():\r\n    for node in cmds.ls(sl=True):\r\n        bake_transform_to_offset_parent_matrix(node)\r\n\r\nif __name__ == \"__main__\":\r\n    bake_transform_to_offset_parent_matrix_selection()\r\n<\/pre>\n<p><\/FONT><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\/\/ source : https:\/\/gist.github.com\/Muream\/b4255ec4f690222d92fe794be249b7f1 \/\/ A utiliser en PYTHON ! \/\/ Attention, Python ne supporte pas certains encodage de caract\u00e8re Notepad, HTML&#8230; \/\/ C&rsquo;est pourquoi le code suivant est brut et entre balise &lt;pre&gt;&#8230;&lt;\/pre&gt; import maya.api.OpenMaya as om import maya.cmds as cmds TRANSFORM_NODETYPES = [\u00ab\u00a0transform\u00a0\u00bb, \u00ab\u00a0joint\u00a0\u00bb] def has_non_default_locked_attributes(node): locked_attributes = [] for attribute in [\u00ab\u00a0translate\u00a0\u00bb, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/posts\/16136"}],"collection":[{"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/comments?post=16136"}],"version-history":[{"count":0,"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/posts\/16136\/revisions"}],"wp:attachment":[{"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/media?parent=16136"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/categories?post=16136"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/davy.chang.free.fr\/index.php\/wp-json\/wp\/v2\/tags?post=16136"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}