PDA

View Full Version : Array problem


leandrooliveira
12-03-2012, 11:53 AM
Hi,

Why the script:

string $Sel[] = `ls -sl`;

for ( $Obj in $Sel ) {
print ($Obj);
}

works in 2013 version of Maya, but not in 2012? 2012 version returns me the following error:

// Error: Cannot convert data of type string to type string[]. //
// Error: Invalid array expression type in "foreach" statement. //

Any idea?

Thanks.

Panupat
12-04-2012, 08:33 AM
Works for me.

nookie
12-04-2012, 09:19 AM
If you have already declared the variable $Sel before you cannot re-declare it as $Sel[].
I donīt know if you can delete it, I remember I used to restart Maya xD

Azrail
12-04-2012, 09:54 AM
nookie is right - if you are not sure for the type of variables you need, and since Maya doesn't allow to change the variable type once it is declared* - then it's better to enclose everything into a procedure:

//Define some dummy proc
proc testProc() {
string $Sel[] = `...`;
}
// Call it to execute the code:
testProc;

This way you can change variable types without using different names every time.

* this means anywhere in the current maya session, so for example some scripts can even stop working if they use lets say $i as iterator (for $i=0;..etc), but you declare it as string somewhere else, and then try running the mentioned script.

leandrooliveira
12-04-2012, 11:06 AM
But, guys, the code works in 2013! The problem is: why itīs working in 2013, and itīs not working in 2012?

nookie
12-04-2012, 11:19 AM
I am not on Maya here at work, but you can try to re-declare the variables in 2012 and 2013 and see if it lets you or not.

string $Sel = "something";
string $Sel[] = `ls -sl`;

(if the code works... has been a while since I used MEL!)

leandrooliveira
12-04-2012, 12:54 PM
nookie, this didnīt work. Iīm not a progammer. Iīm trying to follow a tutorial about array. When I used the same code included in the tutorial, it didnīt work in 2012 version.

Iī, trying to make a script that catches the name of the selected objects and then change a certain attribute of them.

Maybe, it could be a bug.

But, thanks for your attention.

eebling
12-09-2012, 12:05 AM
Hi,

Why the script:

string $Sel[] = `ls -sl`;

for ( $Obj in $Sel ) {
print ($Obj);
}

works in 2013 version of Maya, but not in 2012? 2012 version returns me the following error:

// Error: Cannot convert data of type string to type string[]. //
// Error: Invalid array expression type in "foreach" statement. //

Any idea?

Thanks.


I copied and pasted your exact code into maya 2012 and it executed just fine.?! Not sure why it would give you a problem. I tried it on a windows version. Not sure if it would fail on say a linux or mac version...

leandrooliveira
12-10-2012, 11:22 AM
Very strange, eebling, 'cause iīve tried in several maya 2012 and it didinīt work. In Maya 2011 and 2013 it works fine...

zaskar
12-10-2012, 03:07 PM
Do you try to execute mel code in a python interpreter tab? Besides if you copied those lines from a website etc it could be that some invisible characters (formatting) break the script editor. Just type every character into a fresh maya2012 mel script editor tab and run it.

DeadlyNightshade
12-10-2012, 03:37 PM
Uhm... maybe try and simplify the script:

string $Sel = `ls -sl`;

for ($Obj in $Sel ){
print $Obj;
}


After all, Maya works in mysterious ways :)

leandrooliveira
12-10-2012, 04:15 PM
Deadly, this was the error when I execute your code on Maya 2012:

// Error: for ($Obj in $Sel ){
//
// Error: Expression in "for <id> in <expr>" statement must be an array type. //

DeadlyNightshade
12-10-2012, 04:25 PM
Okay I guess you can't do that with arrays.
Maya is usually pretty smart at figuring out what datatype is used when you do declarations.

leandrooliveira
12-10-2012, 04:56 PM
A friend of mine (he writes MEL scripts) says that certainly is a 2012 bug. The same code runs in other versions... A shame...

zaskar
12-10-2012, 08:03 PM
does this run in your maya2012

{
string $Sel[] = `ls -sl`;
for($Obj in $Sel)
print($Obj);
};

?

Besides if the mel loop "for($element in $array[])" wouldnt work your whole maya app wouldnt even start because of those thousands of lines of mel script using this expression there and there.

leandrooliveira
12-11-2012, 12:05 PM
Thanks, for help, Zaskar, but this didnīt work. Maya returnud the following erros:

// Error: Cannot convert data of type string to type string[]. //
// Error: Invalid array expression type in "foreach" statement. //

DeadlyNightshade
12-11-2012, 01:33 PM
Reinstall Maya
Everything about this is just plain WEIRD

zoharl
12-11-2012, 04:50 PM
Thanks, for help, Zaskar, but this didnīt work. Maya returnud the following erros:

// Error: Cannot convert data of type string to type string[]. //
// Error: Invalid array expression type in "foreach" statement. //

I tried @zakar's code on maya 2012 x64 and I had no problems.

Before you reinstall maya ( :rolleyes: ), please copy/paste the output for each of the following tests:

1. Please try the @zakar's code on a new empty scene.
2. Before running the code, try:

print `ls -sl`;

3. Try:

{
string $Sel[] = `ls -sl`;
}

4. Try:

{
string $Sel2[] = `ls -sl`;
}

zoharl
12-11-2012, 04:59 PM
Ho, and make sure that you copy/paste the code into maya, and not type it by yourself (and mistakenly type a tick instead of a back-tick for example). Also your output for @zakar code doesn't make sense. Mel should have failed and stopped on the first line, and shouldn't have continued with the for.

Azrail
12-11-2012, 09:23 PM
Do you have custom scripts in your User/maya/.../scripts folder. I am quite sure there is no such bug in maya, otherwise all our tools would have been broken. So open a "vanilla" maya (simply move/rename your maya prefs folder) and then try again some of the examples. Or better yet try them first with different variable names.

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