PDA

View Full Version : Mel scripting- Do something to selected


cwisbg
11-17-2011, 07:51 PM
I got the basics of how to tell something to do something, but i want to be able to select a bunch of stuff and apply the attribute/ change to selected.
i dont know how to wright this in mell
so how would i wright- change scale of all selected objects (cubes) to 2

NaughtyNathan
11-17-2011, 08:03 PM
you need to make a variable array and fill it with items, then iterate over those items and do something to them. In your case the array of items is the current selection, and the something is set an attribute. In very simple terms it's something like this:
string $array[] = `ls -sl`;
for ($item in $array)
{
setAttr ($item + ".scale") 2.0 2.0 2.0;
}
which will set the scale to 2 in the X, Y and Z for all selected objects. Bear in mind though, that only transform nodes have .scale attribute, so if you don't select the right things, your code won't work correctly.
:nathaN

cwisbg
11-18-2011, 07:40 PM
Awesome!

I got one more i cant wrap my head around.

how do i replace a group of selected objects with the last selected object. and have the replaced new object to have the scale,translate and rotation of the objects in the selected group

so i want to replace a low polly treebranch with high polly branch. pretty much just swap out the low polly for high.

NaughtyNathan
11-18-2011, 08:26 PM
...how do i replace a group of selected objects with the last selected object.

Maya > Modify > Replace Objects

if you really want to re-script this yourself, just open up Maya's /scripts/others/replaceObject.mel and you'll find the appropriate code inside.
:nathaN

cwisbg
11-23-2011, 12:34 AM
I am now trying to read text from a file and apply an instance with translate, rotate, and scale (0,0,0,0,0,0,0,0,0)

so my data will be stored in $dataArray
so how would i instance a cube( or in my case the selected object) for all the data in the array
This is the code i found that im trying to use, i added some stuff at the end, but i dont think its right

p, li { white-space: pre-wrap; }
// Reads A Text File And Returns A String Array Of Each Line
global proc string[] jgTextFileToStringArray (int $skipFirstLine, string $filePath) {
string $filePath = "E:/Scripts/TreePlaces.txt" ;
// Open File
$fileId = `fopen $filePath "r"` ;

// Define String Array
string $dataArray[] ;

// Get The First Line
string $nextLine = `fgetline $fileId` ;

// Loop Until The String Size Is Zero (No Data On That Line)
while (size($nextLine) > 0) {

// Strip Whitespace From The Beginning And End Of The Line
string $cleanLine = strip($nextLine) ;

// Add To Array
$dataArray[size($dataArray)] = $cleanLine ;

// Get Next Line And Continue
$nextLine = `fgetline $fileId` ;

}

// Remove First Line
if($skipFirstLine) stringArrayRemoveAtIndex(0,$dataArray) ;

// Return Array
return $dataArray ;

//---------------------------------------------------This is my stabb at the part im trying to right
for ($item in $dataArray);
instance cube;

}

CGTalk Moderation
11-23-2011, 12:35 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.