MEL Scripting Help

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
  10 October 2012
MEL Scripting Help

Hey everyone,

So I'm super new to MEL scripting (about a week into it), and any kind of coding in general. I'm working on 'baby' scripts right now to attempt to slowly build my knowledge and better understanding. I've been kind of copying, pasting and picking apart scripts and reading up on MEL and I've come up with this so far.


 global proc togNorm() {
 if ( `window -exists togNorm` ) {
 deleteUI togNorm;
 }
 window
 -title "Normals v1.1" togNorm;
 
 columnLayout
 -columnAttach "both" 2
 -rowSpacing 10
 -columnWidth 200;
 
 text -label "All Geometry Objects";
 
 button -label "Select All / Unify Normals On" -command "SelectAll()";
 
 button -h 50 -label "Toggle Face Normals Off/On" -command "NormToggle()";
 
 separator
 -height 10
 -style "in";
 
 text -label "Selected Geometry Objects";
 
 button -label "Unify Normals On" -command "Selected()";
 
 button -height 50 -label "Toggle Selected Normals Off/On" -command "NormToggleSel()";
 
 separator
 -height 10
 -style "in";
 
 text -label "Normal Size";
 
 floatSlider -minValue 0 -maxValue 1 -value .5 -step .1 -dragCommand "NormalSize" NormalSize;
 
 showWindow togNorm;
 }
 
 global proc NormalSize()
 {
 $normalSize = `floatSlider -query -value NormalSize` ;
 setNormalsSize $normalSize;
 print ("Normal Size: " + $normalSize);
 }
 
 global proc SelectAll() {
 select -r `listTransforms "-type mesh"`;
 print "All objects are selected and normals turned on.";
 string $sel[] = `ls -sl`;
 for ($s in $sel)
 setAttr ($s +".displayNormal") 1;
 }
 
 global proc Selected() {
 string $sel[] = `ls -sl`;
 for ($s in $sel)
 setAttr ($s +".displayNormal") 1;
 }
 
 global proc NormToggle() {
 string $sel[] = `ls -sl`;
 if (size ($sel) < 1)
 {
 error "All objects have not been selected!!";
 }
 for ($s in $sel) {
 string $sAttr = $s +".displayNormal";
 int $Check = `getAttr $sAttr`;
 if ($Check == 0) {
 setAttr $sAttr 1;
 print "Normals toggled  ON ";
 }
 else {
 setAttr $sAttr 0;
 print "Normals toggled  OFF ";
 }
 }
 }
 
 global proc NormToggleSel() {
 string $sel[] = `ls -sl`;
 if (size ($sel) < 1)
 {
 error "No objects have been selected!!";
 }
 for ($s in $sel) {
 string $sAttr = $s +".displayNormal";
 int $Check = `getAttr $sAttr`;
 if ($Check == 0) {
 setAttr $sAttr 1;
 print "Normals toggled  ON ";
 }
 else {
 setAttr $sAttr 0;
 print "Normals toggled  OFF ";
 }
 }
 }
 


The questions I have right now are, for the 'Toggle All Normals Off/On' is there a way I can have it register an error in the command line if all the geometry objects aren't select? If you click on the button and all the geometry objects in the scene aren't selected, its errors.

The other is, for the Normal Size slider, is there a way to limit the amount of printed decimals in the command line? It's more of an aesthetic.

Also any other critiques, tips/pointers would be greatly appreciated!!

Thanks!!
 
  10 October 2012
Hi,

First of all, I must warn you, I'm not a good mel scripter myself. I'll try to help though.

For your first question, two ways of doing that (at least).
First, you can force the selection. Maybe the best way, the less the user does, the happier he is.

//If you type ls -visible -type "transform";, it lists all kinds of objects, 
//including lights, cameras, etc.
//-type "mesh" selects polygons, but only the shape
string $allObects [] = `ls -visible -type "mesh"`;
select -cl;

for ($eachObj in $allObjects)
{
	string $transformParent [] = `listRelatives -p $eachObj`;
	select -add $transformParent [0];
}


If you don't want to do it automatically, you can compare the visible objects in the scene and the current selection.

string $test [] = `ls -visible -type "mesh"`; //lists all the visible geometry

string $testSel [] = `ls -sl`; //lists the selection

if (size ($test) != size ($testSel)) //compares the arrays
	warning "Noooooooo!";
else
	print "yes!\n";


Actually, you only compare the size of two arrays, so if you have [pCubeShape1,pCubeShape2] in the first array and [pCube2,pCube3] in the second one, it returns true, although they are different.

for the second question, you could do something like this :

float $longFloat = 3.123456789;
int $shorterInt = ($longFloat*100);
float $shorterFloat = $shorterInt;
$shorterFloat /= 100;


But I don't recommend you to show this feedback, rather useless.

For the third point, I'd tell you to be aware of the way of writing your code (tabulations, comments, etc.). This script is quite simple but already hard to read. Don't hesitate to declare long names variables ($selectedObj instead of $s). In a long script, it's much easier to write and, above all, to read. Try to adopt a kind of normalization in the variables (like: no upperCase for the first letter and an upperCase to seperate words). Try to be sure that your procs' name can't be redundant to avoid conflicts. In other words, find unique words (something like: global proc Tog_Norm_Selected () instead of just global proc Selected () ).

//--------------------------------------
//This proc toggles Normals display for selected obects
//--------------------------------------

global proc NormToggle()
{
	string $selectedObects [] = `ls -sl`;
	if (size ($selectedObects) < 1)
		warning "All objects have not been selected!!";
	else
	{
		for ($each in $selectedObects)
		{
			string $displayNormal = $each +".displayNormal";
			int $check = `getAttr $displayNormal`;

			if ($check)
			{
				setAttr $displayNormal 0;
				print "Normals toggled OFF\n";
			}
			else
			{
				setAttr $displayNormal 1;
				print "Normals toggled ON\n";
			}
		}
	}
}


Hope it helps!
 
  10 October 2012
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 06:07 AM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.