[MEL] Assign Existing Material as popup in custom UI?


#1

Hi guys,

Any help would super awesome since i am trying for a while now but can’t figure it out…huh.
What i am basically trying to do is to put “Assign Existing Material” as popup in my custom UI; the one when you select the object(s) and RMB+click for marking menu (image).

It doesn’t have to be exactly like that in marking menu, all i need is a list of all materials and you can select one on the list to assign it on selected object(s).
Ideal would be if possible to have button, when LMB+click on it does one thing and when RMB+click it shows this list of all materials and to be able to assign it to selected object(s).

Any ideas how would be possible to do this? At least any hint would be great.

Thanks!


#2

Ok i have managed to do somehow most of it by myself except one problem.
What i have is window with just:

  1. “textFiled” that shows material of currently selected object + rename it by typing new name and press enter, and
  2. “optionMenu” that shows list of all materials + when you choose one option assigns it to the selected object(s).

The problem i have now is that once i rename one of the materials using “textField”, the “optionMenu” doesn’t show or updates to a new name. Would anyone maybe know if it is possible somehow update the “optionMenu” with the new name?

The only thing i thought was to somehow refresh the window once i type new name in “textField” and press enter. I figured maybe i could use > evalDeferred “deleteUI MATren”; but then i don’t know how to call window back.

Thanks!!


#3

It sounds as if you have to rebuild the optionMenu after changing a shader name.
Well, I’d try to add a change callback to the textField which triggers a rebuild of the optionMenu.
Or:
In the optionMenu (since Maya2015 Extension Pack) you have the possibility to add a -beforeShowPopup callback. I did not try it but it may allow you to add all needed menu items in the callback.


#4

Thanks haggi for info,

I was already trying out that -beforeShowPopup in optionMenu but doesn’t seem to work in this case. It show the menus but after change material name it would still show the old name. Perhaps i am doing something wrong.

Here is the whole script (without -beforeShowPopup) Just make few objects and assign different mats to them.


if (`window -q -ex MATren`) deleteUI MATren;

string $window = `window -t "Material Rename" -tlb 1 MATren`;
    columnLayout -adj true;

        global string $sel[];
        textField  -bgc 0.22 0.22 0.22 -ed true -w 100 -h 27 -font "boldLabelFont" -cc "RnamE"  -tx $sel[0] selObject;
        scriptJob  -parent selObject -e "SelectionChanged" UpdateProc;
     
separator -style "none" -h 3;

    string $allSEL[] = `ls -type "lambert" -type "VRayMtl"`;
        optionMenu -h 27 -cc matsASSs MATlist;
   for ($oneSEL in $allSEL)
   {
      menuItem -label ("  " + $oneSEL);
      menuItem -en false ( "         > " + `nodeType $oneSEL` + "
") ;
   }
 
separator -style "none" -h 1;

showWindow MATren;


proc matsASSs(){
   string $objASS[] = `ls -sl`;
     if ( size($objASS) == 0 ) {
        inViewMessage -amg ("<span style=\"color:#ff0038;\"> NONE </span> - objects selected!" ) -fade -pos botLeft -fontSize 9 -frameOffset 50;
     }   
     if ( size($objASS) >= 1 ) {  
        string $matASS = `optionMenu -q -value MATlist`;
        select -r $objASS;
        hyperShade -a $matASS;
        select -cl;
     }
}

proc RnamE() {
     string $mySels[] = `ls -sl`;
       hyperShade -smn "";
     string $sels = `textField -query -tx selObject`;
     rename $sels;
     select -r $mySels;
     inViewMessage -amg ("Material - <span style=\"color:#bfff00;\"> Renamed </span>" ) -fade -pos botLeft -fontSize 9 -frameOffset 50;

}

global proc UpdateProc (){
    string $obj[] = `ls -sl`;
      if ( size($obj) == 0 ) { print ""; }
      if ( size($obj) >= 1 ) { 
    string $sel[] = stringArrayRemoveDuplicates(ls("-mat",listConnections(listConnections("-type","shadingEngine",`ls -sl -o -dag -s`))));
    textField -e -tx $sel[0] selObject;	
       }
}

#5

Ok seems like i managed to do it somehow!!

I just added this part at the end of “RnamE” procedure :



/// delete optionMenu
string $optionList[] = `optionMenu -q -itemListLong MATlist`;
for($option in $optionList)
deleteUI -mi $option;

// rebuild optionMenu with new names
     string $allSELs[] = `ls -type "lambert" -type "VRayMtl"`;
     for ($oneSELs in $allSELs)
     {
      menuItem -parent MATlist -label ("  " + $oneSELs);
      menuItem -parent MATlist -en false ( "         > " + `nodeType $oneSELs` + "
");
     }