// =========================================================================== // Copyright 2018 Autodesk, Inc. All rights reserved. // // Use of this software is subject to the terms of the Autodesk license // agreement provided at the time of installation or download, or which // otherwise accompanies this software in either electronic or hard copy form. // =========================================================================== // // // // containerAutopublishRoot // // // string: name of root if the operation was successful // // // This script will check if the selected container contains dag // nodes that are all within a single dag hierarchy. If it does, then the // highest object within that hierarchy will be published as the // root transform and parent/child anchor. If the container // has no dag nodes, or consists of multiple hierarchies, this // method will do nothing. // // // string $objs[], the selection to be considered when choosing a root // int $val if 1, publish the translate, rotate and scale for the root // int $preview if 1, print the name of the node that would be // published as root. // // // // auto publish the root for container1, and publish its TRS attributes // select -r container1; // containerAutopublishRoot 1 0; // // // global proc string containerAutopublishRoot(string $objs[], int $val, int $preview) { string $rtn = ""; if (size($objs) == 0) { return $rtn; } string $container = ""; if( !$preview ) { string $sel[] = `ls -sl`; if (size($sel) > 0 && `objectType -isa container $sel[0]`) { string $root = ""; $container = $sel[0]; $root = `container -q -publishAsRoot $container`; if (size($root) > 0) { // The container already has a root. This can happen if you create a container // by nesting an object that is published as a root in an existing container. // container -e -publishAsRoot $root 0 $container; } } else { error((uiRes("m_containerAutopublishRoot.kNoContainerSelected"))); } } string $xformsLong[] = `ls -long -type transform $objs`; string $xforms[] = `ls -type transform $objs`; int $xformCount = size($xforms); if ($xformCount > 0) { // Find the transform at the highest level in the dag. // It will be our "possible parent". Just knowing it is the highest // up doesn't guarantee all the others are below it so we have to // check that too. // int $minDepth = 0; int $possibleParent = 0; for ($ii = 0; $ii < $xformCount; $ii++) { string $buff[]; tokenize($xformsLong[$ii],"|",$buff); int $pathDepth = size($buff); if ($ii == 0) { $minDepth = $pathDepth; } else if ($pathDepth < $minDepth) { $possibleParent = $ii; $minDepth = $pathDepth; } } // Now validate whether the others all live under the highest one. // int $commonParent = 1; string $children[] = `listRelatives -allDescendents -path $xforms[$possibleParent]`; for ($ii = 0; $ii < $xformCount; $ii++) { if ($ii != $possibleParent) { if (!stringArrayContains($xforms[$ii],$children)) { $commonParent = 0; break; } } } if ($commonParent ) { // A common parent was found. Now let's just check to see if its // parent is also in the container. This can happen if the user // created a container using the "Include Hierarchy Above" option. // string $root = $xforms[$possibleParent]; string $parents[] = `listRelatives -parent -path $root`; while (size($parents) == 1) { string $parentContainer = `container -q -findContainer $parents[0]`; if ($parentContainer == $container) { $root = $parents[0]; $parents = `listRelatives -path -parent $root`; } else { break; } } if( $preview ) { string $fmt = (uiRes("m_containerAutopublishRoot.kPreviewRoot")); print( `format -s $root $fmt` ); } else { container -e -publishAsRoot $root 1 $container; container -e -publishAsParent $root "rootParentAnchor" $container; container -e -publishAsChild $root "rootChildAnchor" $container; if ($val) { string $tr = ($root+".translate"); string $rot = ($root+".rotate"); string $scale = ($root+".scale"); string $vis = ($root+".visibility"); container -e -publishAndBind $tr "translate" $container; container -e -publishAndBind $rot "rotate" $container; container -e -publishAndBind $scale "scale" $container; container -e -publishAndBind $vis "visibility" $container; } $rtn = $root; } } else { if( $preview ) { print( (uiRes("m_containerAutopublishRoot.kPreviewNoRoot")) ); } } } return $rtn; }