PDA

View Full Version : my basic scripts for you


iLegacy
04-29-2006, 09:50 PM
Hi guys,

here i will present you my basic MEL scripts. Comments & critique are very welcome!

Index (3 Scrips)
-----------------
1. rotate Objects
2. move only 2 axis
3. change culling (double sided)
4. mass uv transfer
5. vertex mode



1. rotate Objects
------------------

Explaination
-------------

The script rotates and scales selected objects by random values. Simply put the values
in the textfields and press the "ENTER"-key at your NUMLOCK block til you are satisfied!

x,y,z = the axis
r = rotate (the rotate result is a random valiu between 0 and the entered angel)
smin = min. scale value
smax = max. scale value (the scale result is a random value between smin and smax)

http://www.simont.de/downloads/robj_for_cgtalk.jpg

The Code
----------


proc rotateObj()
{
//Hole alle Werte aus den Textfeldern in die Variablen
float $objx = `textField -q -text txt_rx`;
float $objy = `textField -q -text txt_ry`;
float $objz = `textField -q -text txt_rz`;

float $obj_sxmin = `textField -q -text txt_sxmin`;
float $obj_sxmax = `textField -q -text txt_sxmax`;

float $obj_symin = `textField -q -text txt_symin`;
float $obj_symax = `textField -q -text txt_symax`;

float $obj_szmin = `textField -q -text txt_szmin`;
float $obj_szmax = `textField -q -text txt_szmax`;

//Rausfinden welche Objekte selektiert sind
string $objselected[] = `ls -sl`;
$objall = size($objselected);
$obji=0;

while ($obji < $objall)
{
//OBJEKTE ROTIEREN
if ($objx>0)
{
$objrx = rand($objx);
setAttr ($objselected[$obji]+".rotateX") $objrx;
}

if ($objy>0)
{
$objry = rand($objy);
setAttr ($objselected[$obji]+".rotateY") $objry;
}

if ($objz>0)
{
$objrz = rand($objz);
setAttr ($objselected[$obji]+".rotateZ") $objrz;
}

//OBJEKTE SKALEN
//X SCALEN
if (($obj_sxmin>0) || ($obj_sxmax>0)) //nur eins der beiden muss 377ber 0 sein damit rand in kraft treten kann
{
//print("test!\n");
$scale_rand = rand($obj_sxmin,$obj_sxmax);
//print($scale_rand+"\n");
setAttr ($objselected[$obji]+".scaleX") $scale_rand;
}

//Y SCALEN
if (($obj_symin>0) || ($obj_symax>0)) //nur eins der beiden muss 377ber 0 sein damit rand in kraft treten kann
{
//print("test!\n");
$scale_rand = rand($obj_symin,$obj_symax);
//print($scale_rand+"\n");
setAttr ($objselected[$obji]+".scaleY") $scale_rand;
}

//Z SCALEN
if (($obj_szmin>0) || ($obj_szmax>0)) //nur eins der beiden muss 377ber 0 sein damit rand in kraft treten kann
{
//print("test!\n");
$scale_rand = rand($obj_szmin,$obj_szmax);
//print($scale_rand+"\n");
setAttr ($objselected[$obji]+".scaleZ") $scale_rand;
}

$obji++;
}
}

int $checked;
string $huhu = `window -menuBar false -title "rObj" -mnb 1 -tlb 1 -resizeToFitChildren 1`;
gridLayout -numberOfColumns 4 -cellWidth 30 -cellHeight 20;
text " ";
text "r";
text "smin";
text "smax";

text "x";
string $objtext_rx = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_rx`; //rotateX
string $objtext_sxmin = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_sxmin`; //scaleX MIN
string $objtext_sxmax = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_sxmax`; //scaleX MAX

text "y";
string $objtext_ry = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_ry`; //rotateY
string $objtext_symin = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_symin`; //scaleY MIN
string $objtext_symax = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_symax`; //scaleY MAX

text -l "z";
string $objtext_rz = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_rz`; //rotateZ
string $objtext_szmin = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_szmin`; //scaleZ MIN
string $objtext_szmax = `textField -text "0" -width 30 -enterCommand "rotateObj" txt_szmax`; //scaleZ MAX

window -e -w 127 -h 105 $huhu;
showWindow;



[b]2. move only 2 axis
------------------

Explaination
-------------

Locks x,y or z axis in succession of one selected object. Now objects can be
moved only along two axis.

First execute: lock x
Second execute: lock y
Third execute: lock z
Fourth execute: unlock all

It's a good way if you set a hotkey (in my case "l" for "lock").

The Code
----------


string $all[] = `ls -sl -type mesh`;
int $i,$x,$y,$z;

//get state of lock/unlock and put it to x,y or z
if (`getAttr -lock ($all[0]+".tx")` == 1) {$x=1;} else {$x=0;}
if (`getAttr -lock ($all[0]+".ty")` == 1) {$y=1;} else {$y=0;}
if (`getAttr -lock ($all[0]+".tz")` == 1) {$z=1;} else {$z=0;}

if (($x==0) && ($y==0) && ($z==0))
{
setAttr -lock true ($all[0]+".tx");
}
else
if ($x==1)
{
setAttr -lock false ($all[0]+".tx");
setAttr -lock true ($all[0]+".ty");
}
else
if ($y==1)
{
setAttr -lock false ($all[0]+".ty");
setAttr -lock true ($all[0]+".tz");
}
else
if ($z==1)
{
setAttr -lock false ($all[0]+".tz");
}



[B]3. change culling
------------------

Explaination
-------------

The scripts changes the state of "double sided" of all objects i the scene to 1 (first execution of the script) or 0 (second execution of the script). You can very fast check if the objects normals are correct.

It's a good way if you set a hotkey.

The Code
----------


string $all[] = `ls -l -type mesh`;

int $i = 0;
int $onoff = getAttr ($all[0]+".doubleSided");
if ($onoff == 0) $onoff=1; else $onoff=0;

while ($i < size($all))
{
setAttr ($all[$i]+".doubleSided") $onoff;
$i++;
}



4. mass uv transfer
------------------

Explaination
-------------

With this script you are able to transfer the uv set of the last selected to all other selected objects. Thats very usefull if you have a huge amount of equal (for example) stones or something and like to change the uv set for all of them.
Simply select all objects containing the old and last but not least the object with the new
uv set.

It's a good way if you set a hotkey.

The Code
----------



//This script will replace the uv sets of all selected objects with the uv set of the last selected object
string $list[] = `ls -sl`;
int $i = 0;

while ($i < (size($list)-1))
{
polyTransfer -vc 0 -uv 1 -v 0 -ao $list[size($list)-1] $list[$i];
$i++;
}



5. vertex mode
------------------

Explaination
-------------

When you attach this script to a hotkey, you can change the selection mode to "vertex" for the current selection. Replace "vertex" with "facet" (for face selection) or "egde" (for edge selection).

The Code
----------


string $list[] = `ls -sl`;
int $i = 0;

while ($i <= (size($list)-1))
{
print $list[$i];
doMenuComponentSelection($list[$i], "vertex");
$i++;
}

seth0x2b
04-30-2006, 12:26 AM
Nice...
I was looking for something to lock one of the axis so the second script really hits the spot.

Cheers

Nicool
04-30-2006, 08:19 AM
For locking an axis in Maya transformers, you just gotta CTRL+click on the axis you want to lock and then move. To unlock, CTRL+click on the yellow square at the axis gizmo.

seth0x2b
04-30-2006, 12:28 PM
Really?! I didn't know that (and I'm NOT being sarcastic..I really didn't know :D )

Cheers

iLegacy
04-30-2006, 01:37 PM
@ Nicool (member.php?u=10394)

Hey, that was a very nice tip :) Thank you.

stallion151
04-30-2006, 11:05 PM
you can also use the attribute spread sheet to turn on or off the double sided attribute on each selected object. its under the render tab.

sorry man, don't mean to rain on your parade.

iLegacy
05-01-2006, 12:11 PM
@ stallion151

Yes, you are completely right. But don't forget: to change the double sided attribute you have to click 3 times (open attribute editor, open render states tab, change the state) and you have to do this for every object in the whole scene.

With the script, you press ONE button and ALL objects in the scene changes their state.

ACamacho
05-01-2006, 01:00 PM
I think what he meant was that you can change all the objects double sided attributes at once with the spreadsheet editor. Not to say the script isn't useful.....just that there is another way to change the render stats for all objects. :)

iLegacy
05-01-2006, 01:56 PM
@ ACamacho

Ah ok :)

Mikademius
05-02-2006, 09:21 AM
Hi there iLegacy :)
Nice of you to share your scripts. A couple of things though:
-> When you plan to share your scripts you have to comment them in english. I understood it, but that's just because norwegian is a german language ;)
-> Why don't you use floatFields in the first script when the input are floats and not text?

iLegacy
05-02-2006, 09:48 AM
@ Mikademius

Hey thank you for your response! You are right, i will comment the scripts in English :)
Hm i didn't knew that there are float fields ... thank you for the tip! I named the Thread
"my basic scripts" because i'm very basic in programming MEL :)

Thank you.

iLegacy
05-14-2006, 12:11 AM
4. mass uv transfer
------------------

Explaination
-------------

With this script you are able to transfer the uv set of the last selected to all other selected objects. Thats very usefull if you have a huge amount of equal (for example) stones or something and like to change the uv set for all of them.
Simply select all objects containing the old and last but not least the object with the new
uv set.

It's a good way if you set a hotkey.

The Code
----------



//This script will replace the uv sets of all selected objects with the uv set of the last selected object
string $list[] = `ls -sl`;
int $i = 0;

while ($i < (size($list)-1))
{
polyTransfer -vc 0 -uv 1 -v 0 -ao $list[size($list)-1] $list[$i];
$i++;
}

iLegacy
05-15-2006, 03:14 PM
5. vertex mode
------------------

Explaination
-------------

When you attach this script to a hotkey, you can change the selection mode to "vertex" for the current selection. Replace "vertex" with "facet" (for face selection) or "egde" (for edge selection).

The Code
----------


string $list[] = `ls -sl`;
int $i = 0;

while ($i <= (size($list)-1))
{
print $list[$i];
doMenuComponentSelection($list[$i], "vertex");
$i++;
}

Mikademius
05-16-2006, 08:29 AM
You tend to write scripts that are already in maya :)
However, these scripts are good training for you I guess.
The convert to vertex doesn't actually select the vertices accordingly to the selection. Isn't that what it's supposed to do?

Und ich liebe deine Avatar :D

iLegacy
05-16-2006, 01:20 PM
Hm no i wrote the script because i hate it to hold down the left mouse button and choose "vertex" from the marking menu. I told the problem to a friend and he gave me the advice to use F9 - F12. With the help these Keys you can change the current selection mode to "edge" and "faces" - not to vertex!

And now i can select an object, press "<" and *zing* i can select the verteces without waiting for the marking menu. :) I hope i answered your question :)

Vielen Dank :) Ich kann mir kein Wacom leisten und muss mir behelfen :)

CoolDuck
05-16-2006, 01:38 PM
Nice scripts. I like move only 2 axis.
I was looking for something like this... without having to click on one of the axis. But maybe some visual feedback is needed to show what is locked, channel box is not open.

I think you should put an Apply button on that rotate script.

@ vertex mode MEL...
Cool! I like this one a lot. This is the same as right click marking menu.
Currently I use SelectVertexMask etc, found in status line section of hotkey editor for this, but yours works slightly different.

Do you also have a script for NURBS?

I was trying to figure this out myself, but couldn't figure it out.

Mikademius
05-16-2006, 01:58 PM
So your script/hotkey isn't supposed to select the vertices?
I made a markingMenu with all the "convert to" options, but maya's default isn't really that bad.
Try this:
->Create a cube
->Select a face
->Ctrl click + RMB and drag left. ( You don't have to wait for the menu to pop up. Just drag in the direction you know the option is and voila. Done in less than a second.)

I have almost stopped using hokeyes after I set the markingMenus up :) Now I try to use Maya's default markingMenus more and more because they are so extremely handy.

iLegacy
05-16-2006, 02:14 PM
@ CoolDuck

Thank you for your tips! They are very helpful. When i have a little bit more time i will try to update the scripts.

@ Mikademius

Yeah i know this marking menu but it's to slow for me, i like to press only one key. And several times i hit "vertex faces" instead of "faces" and i hate that. If it's necessary i can code that all verteces are selected when you change to vertex mode :)

Mikademius
05-16-2006, 02:31 PM
oh just flipping to vertex mode?
The RMB-drag left is too slow for you? Even if you don't wait for it? Do you have one hotkey for every type then? Because then this script works just like f9 doesn't it? I understand that f9 is way to far away, people mostly have their hand around left alt to esc so if that's the way it's supposed to work then I understand it. :) But then it should deselect the face/edge you have selected when you switch into vertexmode...

iLegacy
05-16-2006, 02:56 PM
@ Mikademius

Your are completely right! :)

Hm, it's right that the script doesn't deselect faces/egdes but why should that be necessary? If you change the mode and select multiple verteces, the face/egde will be deselected. That's enough for me :)

azshall
05-16-2006, 08:13 PM
you can also use the attribute spread sheet to turn on or off the double sided attribute on each selected object. its under the render tab.

sorry man, don't mean to rain on your parade.

Well.. You may be right, .. However, any seasoned Maya User should know there is quite a few ways of doing the same thing ... Some ways are more efficient than others (like this) .. Just depends on your workflow ...

With that said, I'm all for reducing redundancy, clicks or repetitiveness ... Good stuff iLegacy ..

az

iLegacy
05-16-2006, 10:24 PM
@ azshall

Thank you ;)

Mikademius
05-17-2006, 08:00 AM
I see what you mean. I just found it confusing to have the selection there after changing mode :)

CGTalk Moderation
05-17-2006, 08:00 AM
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.