MEL - Maya UI Building


#241

Hi all!
I am newbe on mel. This is simple i would like to tear off a window panel with a camera in the scene, it is a camera for the face of a character, any idea? Thanks


#242

Have you tried the modelEditor command? I think it does what you are looking for. Check the docs for details


#243

the problem is there will be a undeclared number of rows. The user will generate new ones by clicking a button. i have discovered i must use a class to create instances and some form of array iterator to create unique names for each asset. row[i].


#244

collect the names in a global array variable or one that is unique within your class. Then you can access them for refreshing deleting, adding etc.


#245

I’m trying to code a simple UI to use to re-orient joins so they behave properly as a FK chain. It consists of three sliders (X Y Z), however I can’t get it to apply the changes to the joint and it is unable to tell that I have anything selected, being knew at coding mel I’m quite stumped as to where I should go now, and any help would be appreciated.

global proc rotateJoint ()
{
	//Checks to see if the window already exists, if it does it closes it
	
	if (`window -q -ex RJwindow`) deleteUI RJwindow;
	
		window 
		-title "Joint Reorient" 
		RJwindow;

	//Command used to add a layout to the window
	columnLayout;
	//The 'text' command prints text into your UI
	
	floatSliderGrp -label "X" -field true
		-minValue -360.0 -maxValue 360.0
		-fieldMinValue -360.0 -fieldMaxValue 360.0
		-value 0;	

	floatSliderGrp -label "Y" -field true
		-minValue -360.0 -maxValue 360.0
		-fieldMinValue -360.0 -fieldMaxValue 360.0
		-value 0;	

	floatSliderGrp -label "Z" -field true
		-minValue -360.0 -maxValue 360.0
		-fieldMinValue -360.0 -fieldMaxValue 360.0
		-value 0
		-cc "Zvalue";
		

		
		
	//Command line to create the window
	
	
		showWindow RJwindow;
}

global proc Zvalue ()
{
float $Slider = `floatSliderGrp -q -v`;
string $zstring =`ls -sl`;
setAttr $zstring.rotateZ $Slider;
}

#246
global proc rotateJoint ()
 {
 	//Checks to see if the window already exists, if it does it closes it
 	
 	if (`window -q -ex RJwindow`) deleteUI RJwindow;
 	
 		window 
 		-title "Joint Reorient" 
 		RJwindow;
 
 	//Command used to add a layout to the window
 	columnLayout [b][b](Give you column a name) mainCL[/b][/b];
 	//The 'text' command prints text into your UI
 	
 	floatSliderGrp -label "X" -field true
 		-minValue -360.0 -maxValue 360.0
 		-fieldMinValue -360.0 -fieldMaxValue 360.0
 		-value 0 [b][b](Give you slider a name)[/b]xFSG[/b];	
 
 	floatSliderGrp -label "Y" -field true
 		-minValue -360.0 -maxValue 360.0
 		-fieldMinValue -360.0 -fieldMaxValue 360.0
 		-value 0 [b][b](Give you slider a name)[/b]yFSG[/b];	
 
 	floatSliderGrp -label "Z" -field true
 		-minValue -360.0 -maxValue 360.0
 		-fieldMinValue -360.0 -fieldMaxValue 360.0
 		-value 0
 		-cc "Zvalue" [b][b](Give you slider a name)zFSG[/b][/b];
 
 	//Command line to create the window
 	
 	
 		showWindow RJwindow;
 }
 
 global proc Zvalue ()
 {
 float $Slider = `floatSliderGrp -q -v [b](Give you slider a name) zFSG[/b]`;
 string $zstring[b][][/b] =`ls -sl`; [b]// You need to make this item into an array.  Any time that you have a list in a variable.  It has to be in an array[/b]
 setAttr [b]([/b]$zstring[b][0] +[/b][b] "[/b].rotateZ[b]")[/b] $Slider;
 }

#247

Hi, I have a question about deferring the change command for checkboxes.

I am working on an UI project (pretty advanced at this point) wich was keeping crashing Maya randomly when the -cc flag was triggered on a checkbox (banghead and stuff). I seem to finally have found a fix by using the evalDeferred command on top. It’s the first time I’m finding a use for deferred code.

Why is it, that when the same change command is used on an other control type no crash happen, while it was NOT deferred ?

What can you said about UI drawing and deferred code (callbacks specially) ?

Thanks


#248

no idea Adam. without seeing your -cc code it could be any number of things. I’d suggest submitting it as a bug to Autodesk though if the same code crashes checkBox but not other control types.

:nathaN


#249

Are you changing the UI at all in your changeCommand callback? The usual problem, and the one that evalDeferred is usually used to get around, is when you want to create/delete UI in the callback. So you end up deleting the UI control that is currently calling your code and when you’re done it will crash if it tries to do anything.


#250

Yes goleafsgo, the change command is calling a procedure wich will return a list of concatenated strings (often through supplementary embedded procedures), list wich is then sent to an interpreter procedure wich will destroy and rebuild the same window based on the concatenated information. These concatenated strings often contain raw UI code to be evaluated after the window was recreated, when they are not simply designed to feed automatic controls creation with attributes and labels (see dead excerpt below, not relevant to the current issue). Pretty much the case you are mentioning I suspect, the control is causing its own destruction with the window hosting it. Should I defer every control -cc’s code triggering this redraw mechanism ? Is it even safer the defer the raw UI code to be evaluated ? Simply put, why use eval() in place of evalDeferred() for UI controls, if the question makes sense ?

I read somewhere that slow UI creation could end after Maya started on the next execution steps, in other words, drawing an UI can take longer than Maya is expecting (question mark). If that’s true then I for one am finding that this important information is underexposed to beginners, it costed me about one year and the related nerves to put a finger on this, deferring code is not natural to noobs :slight_smile:

I am guessing that the problem comes with the unhandled error (crash), therefore the issue should be exposed better.


All in all, this first UI project of mine was very experimental (read: from an unexperimented coder, still unsure on how dynamic UI should be done) and that’s why, Nathan, I didn’t send a bug report to Autodesk: my code had to be faulty. You would not believe what I fed the -cc flag with, looking for a work around when I found that using -cc with enum was executing the code (the very last line was my concern) while the attribute itself remained unchanged… well, I used scriptJobs then (not anymore) but that never fixed the crash. I have put ANYTHING in the -cc flag of enums for one year

/*
DISCLAIMER:
DO NOT USE, COUNTER-EXAMPLE !!!

eg: the received string (among a list,
created by 'EDI_list()' below) would be:

"enum¤label¤node¤attribute"

tokenized using "¤" in $args[], gives

$args[0] >> enum
$args[1] >> label
$args[2] >> node
$args[3] >> attribute

Then comes the -cc flag for this very enum control,
referencing to self
(the '#1' callback returns a string for enums argh,
I had to feed in the name of possible attributes
passing there):
*/

	"-cc (	"string $sEnum0[]={"
		+	"\"Gaussian\",\"ior\",\"Linear\",\"None\","
		+	"\"blackbody\",\"Diffuse\",\"Basic\","
		+	"\"Circular\",\"Uniform\"}; "//,\"Camera\"
	+	"string $sEnum1[]={"
		+	"\"Box\","
		+	"\"specular reflectivity\",\"Reinhard\","
		+	"\"PhysicalSunSky\",\"uniform\",\"Phong\","
		+	"\"Dermis\",\"Generated\",\"Henyey-Greenstein\"}; "//,\"Tonemapping\"
	+	"string $sEnum2[]={"
		+	"\"MN_Cubic\",\"nkData\",\"Camera\","
		+	"\"BackgroundColour\",\"peak\","
		+	"\"Oren-Nayar\",\"Epidermis\",\"Image\"};"
	+	"string $sEnum3[]={\"LatLong\",\"Specular\"};"
	+	"string $sEnum4[]={\"Spherical\",\"Glossy Transparent\"};"
	+	"string $sEnum5[]={\"Null\",\"Renderer\"}; "
	+	"if(`stringArrayContains \"#1\" $sEnum0`)"
	+		"setAttr "+$args[2]+"."+$args[3]+" 0; "
	+	"else if(`stringArrayContains \"#1\" $sEnum1`)"
	+		"setAttr "+$args[2] + "." + $args[3]+" 1; "
	+	"else if(`stringArrayContains \"#1\" $sEnum2`)"
	+		"setAttr "+$args[2] + "." + $args[3]+" 2; "
	+	"else if(`stringArrayContains \"#1\" $sEnum3`)"
	+		"setAttr "+$args[2]+"."+$args[3]+" 3; "
	+	"else if(`stringArrayContains \"#1\" $sEnum4`)"
	+		"setAttr "+$args[2]+"."+$args[3]+" 4; "
	+	"else if(`stringArrayContains \"#1\" $sEnum5`)"
	+		"setAttr "+$args[2]+"."+$args[3]+" 5; "
	+
	
/*
command of the year: evalDeferred
*/

		"evalDeferred(\"EDI_list "+$target+" 0\");"
						)

laughs. This is dead code, as you can see at this point I was forced to overcome a control functionality drop. The eval (now deferred) was executed but the attribute wasn’t changing with the control anymore. ? Fixed anyway, can not reproduce… pipe was probably broken. Feel free to comment.

Got similar UI caveheats to depict ? I’m getting too old to wait one year a crash :arteest: need white marks from your experience, to all.

Thanks already !

PS: multiple edits, I’m done now


#251

Hi

So I feel really stupid about this but here goes anyway. I’m building a UI with MEL. Now the structure and layout I can do now problem. The only thing that I can’t figure out is how to connect input information. For example if I have a intSliderGrp, how do I pull the values out of it and plug it into somewhere else.

Here’s an example of my script, please help.

global proc showMyWindow()
{
if ( window -exists myWindow ) {
deleteUI myWindow;
}
window
-title “Multiple texture bake”
-resizeToFitChildren true
-width 300
myWindow;

columnLayout;

     columnLayout -adjustableColumn true -rowSpacing 8;
         text -label "Setup render settings to required render specs";
         button -label "Render Globals"
         -command unifiedRenderGlobalsWindow;
 setParent ..;
 setParent ..;


 
 
     columnLayout;
         text -l "Texture Resolution";
         intSliderGrp -label "Width"
                 -value 1024
                 -field true
                 -minValue 512 -maxValue 4096
                 -fieldMinValue 512 -fieldMaxValue 4096
         textureWidth;
         intSliderGrp -label "Height" 
                 -value 1024
                 -field true
                 -minValue 512 -maxValue 4096
                 -fieldMinValue 512 -fieldMaxValue 4096
         textureHeight;



 setParent ..;
 setParent ..;

showWindow myWindow;
}

showMyWindow();


#252

hi @splinta: You just query the named controls. Beware that control names are always global! That means… I could well think that someone else or any control in Maya comes up with the name “textureWidth”. So I’d always suggest to name things like “splinta_textureWidth” “splinta_textureHeight” … you get me.

now in your “run-command” you’d just call:
$width = intSliderGrp -q -value textureWidth;

If you want to pass a value to another control or even put it in optionVars I always use the change or drag command on creation with the #-operator that passes the current value into the command string:

intSliderGrp -changeCommand “optionVar -iv splinta_textureWidth #1;” textureWidth;

or

intSliderGrp -dragCommand “intField -e -v #1 splinta_extraTextureHeightControl” textureHeight;

or

intSliderGrp -dragCommand “myCommandThatDoesSomethingWithTheValueOnDrag #1” textureHeight;

global proc myCommandThatDoesSomethingWithTheValueOnDrag (int $value)
{
print $value;
}

instead of #1 you could always put in “intSliderGrp -q -v nameOfControl”. But in the moment you create it it knows its name so #1 is possible. Even you would’t have to know its name that way!


#253

Thanks ewerybody

That really cleared things up. All the stuff I’ve read just assumes that I would know how to do it.

:smiley:


#254

Ok cool. So I’ve got 2 more questions: 1. Do I have to define my variables in every proc or will they run through the whole script?
2. Will the variables only be activated / defined once the proc is called?

The reason I’m asking is because I have a variable: string $selection = ls - sl; in a procedure that runs when I click a button when I run the script, I get an error saying :

Error: sting $selection = ls -sl;
//
// Error: line 41.19: “$selection” is an undeclared variable. //

Very confused!!


#255

variables only exist inside the scope they are declared in, and in MEL, scope is started with a { and ended with a }.

for example:

global proc something()
{	// this is the start of a new scope!
	string $var = "hello!";
	string $sel[] = `ls -sl`;
	for ($obj in $sel)
	{ // this is the start of another new scope!!
		string $newName = ($obj+"_new");
		print ($newName+"
");
	} // this is the END of the most recent scope!
	// the variable "$newName" no longer exists!
}	// this is the end of the proc scope!
// now NO variables exist (apart from any that are global!)
 

if you want your procs to refer to variables defined OUTSIDE of them you should ideally pass them in as parameters:

global proc something(string $sel[])
{
	for ($obj in $sel)
	{
		string $newName = ($obj+"_new");
		print ($newName+"
");
	}
}
 

:nathaN


#256

thanks every1 for the help.

learning slowly but surely. can any1 tell me how can I call up the material that is assigned to an object? For renaming but it needs to be open ended, i.e. no matter what material or name. I found a way to do it by making another material but selecting 1 from an object is proving difficult. I tried a pick walk approach but with no luck.


#257

Try checking the connections. Open up the hyper graph and graph all connections. This will tell you what your node is connected to and how.


#258

Hi
I attached an example of what I’m trying to achieve. I understand the flow of the nodes but how to put it into code is a mystery. The other part is that I need the Mel to be independent of naming as the names will not always be the same. Was thinking of using variables from this purpose unless there is another way of doing it. What I have managed to achieve is renaming the original object and its shape.


#259

Hi try this:

window;
panelayout;
modelPanel -cam perspective; // or whatever camera name you have
showWindow;

for info on each command type “help” then the command in script Editor for the flags


#260

He Splinta! Variables are great! :smiley:

you don’t know how often this question already arised? Does it have to be Mel?
here we posted a python solution. But okok here its with Mel as well.

But actually this is not much about UI peeps ;] open up a new thread if you have specific non UI things please!! This thread was actually more about insights and tips than questions.