PDA

View Full Version : copy value from an array to another one


Ptichat
11-21-2010, 08:36 AM
Hi,
I'm facing this problem ( maybe I'm doing it wrong, I'm a mel beginner ! )

string $listlight[] = `ls -type "light"`;
int $numlight = size($listlight);
int $count = 0;

for ($k=0 ; $k < $numlight; $k++)
{
select $listlight[$k];
string $currentlightname[] = `ls -sl`;
string $lightNameSplitted[] = stringToStringArray($currentlightname[0] , "_" );
print ($lightNameSplitted [0] + "\n" );
string $arraylight[$k] = $lightNameSplitted[0];

$count = $count +1;

}
This line
string $arraylight[$k] = $lightNameSplitted[0];
gives me an error.

What I'm trying to do is basically listing my lights, taking first part of the name to display it, then display all the light
and so on.

Ex :
if my scene contains those lights :

Destiny_sky
Zarya_window
Zarya_lamp

I want to be able to print this :
Destiny
|sky
Zarya
|window|lamp


Thanks for any ideas or solutions :)

haggi
11-21-2010, 02:40 PM
This could be a very good example where python could be a bit more effective than mel. I'm not sure if it is useful at all for you, but this could be a python solution (untested):

import maya.cmds as cmds
lightList = cmds.ls(type = "light")
lightDict = {}
for light in lightList:
groupName, lightName = light.split("_")
if not lightDict.has_key(groupName):
lightDict[groupName] = []
lightDict[groupName].append(lightName)

for lightGroup in lightDict:
print lightGroup
print "|".join(lightDict[lightGroup])

NaughtyNathan
11-21-2010, 02:57 PM
you have to initialise an array variable before you add individual elements to it:

string $myArray[] = {}; // initialise an EMPTY string array
...
$mayArray[$n] = $someVariable; // insert a single entry into the array at a given cell: $n
similarly, you cannot initialise an array as a single item:

string $myArray[5] = $someVariable; // initialise a string array of size 5 with a single variable
// syntax error!
string $myArray[5] = { $someVariable }; // this works, as we've specified an array using { }
:nathaN

Ptichat
11-21-2010, 07:38 PM
you have to initialise an array variable before you add individual elements to it:

string $myArray[] = {}; // initialise an EMPTY string array
...
$mayArray[$n] = $someVariable; // insert a single entry into the array at a given cell: $n
similarly, you cannot initialise an array as a single item:

string $myArray[5] = $someVariable; // initialise a string array of size 5 with a single variable
// syntax error!
string $myArray[5] = { $someVariable }; // this works, as we've specified an array using { }
:nathaN

Thanks Nathan,

I went for this solution :





string $listlight[] = `ls -type "light"`;
int $numlight = size($listlight);

for ($k=0 ; $k < $numlight; $k++)
{
select $listlight[$k];
string $currentlightname[] = `ls -sl`;
string $lightNameSplitted[] = stringToStringArray($currentlightname[0] , "_" );
string $group = $lightNameSplitted[0];
string $arraylight[] = {};
string $arraylight[] = {$group};
print ($arraylight[$k]);
}


Having those following lights in my scene :
Destiny_sky
Zarya_window
Zarya_lamp

The result is just the first print :
Destiny.
That's sound normal, because I'm not increasing the array of arraylight.
But if I write
string $arraylight[$k] = {$group};
instead of
string $arraylight[] = {$group};
I'm getting an error :(

CGTalk Moderation
11-21-2010, 07:38 PM
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.