maya2011_thumbMaya 2011 adds PyQt bindings and dockControl command

…and this enables the docking of any custom window you create to the main maya interface. The UI can then be moved docked and undocked from anywhere TO anywhere. I immediately have embraced this godsend to the hellish Maya UI coding world. This isn’t really new news since Maya 2011 has been around for a little while now but I wanted to quickly share my code for making windows dockable. I have expanded my floating tools shelf to have a button that makes the UI dockable when you press it. The old window gets created in my userSetup and is not dockable until now that I added this command as a button to it.

The command gets called only when pressing the button and the command also stores the UI object’s names and specifics so that I can use it to modularly make more buttons on other UIs so that they can also be dockable. The whole key is being modular and not storing global variables which I consider messy.

If you’re gonna make a proc, that is easy

//Given a window name, a name to call the control, and placement in the main maya window,
//add the ability to dock the UI to the main maya UI.
global proc string jaDockUiCommand (string $winName, string $ctrlName, string $placement) {
 global string $gMainWindow;
 string $userPrefs = `internalVar -upd`;
 string $mayaVersion;
 string $buffer[];
 int $numTokens = `tokenize $userPrefs "/" $buffer`;
 $mayaVersion = `substring $buffer[5] 0 4`;
 if ($mayaVersion == "2011")
 {
 string $layout1 = `paneLayout -configuration "single" -parent $gMainWindow`;
 string $myDC = `dockControl
 -allowedArea "all"
 -area $placement
 -content $layout1
 -label $ctrlName`
 ;
 control -e -p $layout1 $winName;
 return 1;
 }
 return 0;
}

If you’re gonna call this proc to make one of your windows dockable just copy/paste it into your script editor and then run it by typing:

jaDockUiCommand “your_window_name” “control_name_you_like” “where_you_want_it”;

“your_window_name” is what you named your window when you made it. “control_name_you_like” is any ‘ol thing you please, I find keeping the “_” in the name much more reliable for naming dockConrols (Maya tends to wanna drop characters that are spaced with empty spaces for some reason. “where_you_want_it” can be “top”, “bottom”, “left”, “right”.

Next, if you are going to make a command that gets run from a button command, then it’s a little different especially if you want pass the result command to a simple button that you can add to any of your floating windows. It looks like:

//Given a window name, a name to call the control, and placement in the main maya window,
//add the ability to dock the UI to the main maya UI. Only works with Maya 2011+.
global proc string jaDockUiCommand (string $winName, string $ctrlName, string $placement) {
 string $command = ("//\n\n// Result: Dock UI //\r"
 + "global proc jaDockThisUI (){\r\n"
 + "\tglobal string $gMainWindow;\r\n"
 + "\tstring $userPrefs = `internalVar -upd`;\r\n"
 + "\tstring $mayaVersion;\r\n"
 + "\tstring $buffer[];\r\n"
 + "\tint $numTokens = `tokenize $userPrefs \"/\" $buffer`;\r\n"
 + "\t$mayaVersion = `substring $buffer[5] 1 4`;\r\n"
 + "\tif ($mayaVersion == \"2011\")\r\n"
 + "\t{\r\n"
 + "\t\tstring $layout1 = `paneLayout -configuration \"single\" -parent $gMainWindow`;\r\n"
 + "\t\tstring $myDC = `dockControl\r\n"
 + "\t\t\t-allowedArea \"all\"\r\n"
 + "\t\t\t-area " + $placement + "\r\n"
 + "\t\t\t-content $layout1\r\n"
 + "\t\t\t-label " + $ctrlName + "`\r\n"
 + "\t;\r\n"
 + "\r\n"
 + "\tcontrol -e -p $layout1 " + $winName + ";\r\n"
 + "\t}\r\n"
 + "\telse\r\n"
 + "\t{\r\n"
 + "\t\tprint \"This verion of Maya doesn't support the dockControl command.\";\r\n"
 + "\t}\r\n"
 + "}\r\n"
 + "jaDockThisUI;")
 ;
 return $command;
}

And here’s the button. (notice the

-command `jaDockUiCommand $myWinName $ui_name $ui_placement`

) The button is in the shelf code below.

A little info: If you were to save the shelf using the saveShelf Maya command, you’ll see it looks a lot different because the button stores the command and all it’s variables and you can run it as a standalone proc or use it to deploy the saved shelf to a team of other artists.

I use shelves like this that are floating to house lots of tools that I use daily. The shelves are made dynamically using userSetup.mel and maya env. I find it’s easier to keep everything organized if I have everything installed to one specific repository. I hate hunting and sourcing scripts. This way it finds them all automatically, and I have tools that will search and make buttons if there’s a tool I haven’t used in a while and it can’t be sourced as a button on a shelf like this:

//A shelf that gets put into a floating window.
//It can contain a plethora of useful Maya tools.
global proc jaMyShelf () {
 string $myWinName = "myWindow";
 string $myWinTitle = "My_Shelves";
 string $ui_name = $myWinTitle;
 string $ui_placement = "bottom";
 string $sh_loc = "C:/myCustomShelves/";
 int $developing = false;

 if ($developing && `window -exists $myWinName`)
 deleteUI -window $myWinName;
 if ( !`window -exists myWindow` )
 {
 if ( $developing )
 windowPref -remove myWindow;
 }

 window
 -title $myWinTitle
 -rtf 1
 -tlb 1
 $myWinName;

 tabLayout;

 //Begin adding shelves

 string $sh_name = "My_Custom";
 string $ja_sh = `shelfLayout -style "iconOnly" -backgroundColor .5 .5 .5 $sh_name`;

 shelfButton
 -enableCommandRepeat 1
 -enable 1
 -width 34
 -height 34
 -manage 1
 -visible 1
 -preventOverride 0
 -align "center"
 -label "Dock This UI"
 -labelOffset 0
 -font "tinyBoldLabelFont"
 -imageOverlayLabel "dock"
 -image "commandButton.xpm"
 -image1 "commandButton.xpm"
 -style "iconOnly"
 -marginWidth 1
 -marginHeight 1
 -annotation "Use this button to dock this UI to a desired location.\n"
 -command `jaDockUiCommand $myWinName $ui_name $ui_placement`
 ;
setParent ..;

 showWindow;

}jaMyShelf;

I hope you enjoy coding your dockable windows! I’m learning how to code PyQt and use QtDesigner to make UI’s now, and hopefully I will share more about that in the near future.