PDA

View Full Version : Explicit variable definitions - tokenize still fails?!?!


DeadlyNightshade
01-22-2013, 08:40 PM
So I have this script that finds and selects the shading group belonging to a selected material:
int $bufferSize;
string $buffer, $fileNode, $temp[], $temp[0];

// Select the current mat, find it's connections and tokenize the result
select $currentMat;
$temp = `listConnections -connections true` ;
tokenize ((string)$temp[0], ".", (string)$buffer);
$bufferSize = `size($temp)`;

// Now match each array position towards the "file" object type
for ($i = 0; $i <= $bufferSize; $i++)
{
if (`objectType -isType "file" $temp[$i]` == true)
{
$fileNode = $temp[$i];
break;
}
}
select $fileNode;
...The script works but it generates an error on the tokenize-row and I don't understand why. I've read that tokenize is sensitive and needs to be fed variables with explicit definitions. But I don't understand this - how the heck can you define it EVEN MORE explicit than I already have? I define $temp[] as a string array - and even define the array position $temp[0] AND I even tell tokenize that it's a string (string) so what the hell is wrong here? I'm seriously getting pissed because this doesn't make any sense.

I've restarted Maya ofc and none of my variables are defined as globals somewhere else or anything.

rgkovach123
01-22-2013, 08:55 PM
this doesn't answer your question, but this should help. You are doing way more work than necessary to retrieve the shading Groups.


global proc string[] findShadingGroups(string $material)
{
// Even though materials and shadingGroups are usually paired one-to-one, this is not
// always the case.
string $connections[] = `listConnections -type "shadingEngine" $material`;
return $connections;
}
string $sgs[] = `findShadingGroups("lambert1")`;
// Result: "initialShadingGroup", "initialParicleSE"
select -replace $sgs;


What is the specific error message you get?


Edit: Fixed syntax errors... going back to MEL from Python is really hard!

DeadlyNightshade
01-22-2013, 10:28 PM
Invalid call to "tokenize". Check number and types of arguments expected by the procedure

DeadlyNightshade
01-22-2013, 10:30 PM
My stuff gives this error:
Invalid call to "tokenize". Check number and types of arguments expected by the procedure.

And your code gives me errors as well:

global proc string[] findShadingGroups(string $currentMat)
{
// Even though materials and shadingGroups are usually paired one-to-one, this is not
// always the case.
string $connections = `listConnections -type "shadingEngine" $currentMat`;
return $connections;
}
string $sgs = `findShadingGroups($currentMat)`;
select -replace $sgs;
// Error: return $connections;
//
// Error: Line 6.21: Cannot cast data of type string to string[]. //
// Error: string $sgs = `findShadingGroups($currentMat)`;
//
// Error: Line 8.47: Cannot cast data of type string[] to string. //

Cannot cast data of type string to string[]

rgkovach123
01-22-2013, 10:56 PM
Sorry, i don't write MEL anymore, so i just pull stuff out of my head from memory. I'll fix my original post.

To figure out why tokenize is failing, you should capture and print the string you are trying to tokenize.

what do you get when you print out $temp? it is possible for a Material to have NO shading Groups at all... in which case you would passing an invalid string to tokenize....

Nyro
01-22-2013, 11:51 PM
It's quite simple. You declared $buffer as a string, while it should have been a string array:


string $buffer, $fileNode, $temp[], $temp[0]; // this is wrong

string $buffer[]; // this is right

If you correct that, get rid of the (string) identifiers. Using those makes syntax alot more complicated and confusing (you'd have to surround the whole expression in parantheses, each time). Otherwise you just get an error.

Also, why are you declaring temp[] twice. Both declarations are identical, as they both create an empty array, or array of size 0.

Lastly, I don't understand your code, though I may be only looking at a portion of it.
Why do you tokenize $temp[0] into $buffer, but you don't use buffer. You use the size of $temp for your iterator maximum value (buffersize). Where does $buffer even come into play?

DeadlyNightshade
01-23-2013, 11:33 AM
It's quite simple. You declared $buffer as a string, while it should have been a string array:


string $buffer, $fileNode, $temp[], $temp[0]; // this is wrong

string $buffer[]; // this is right

If you correct that, get rid of the (string) identifiers. Using those makes syntax alot more complicated and confusing (you'd have to surround the whole expression in parantheses, each time). Otherwise you just get an error.

Also, why are you declaring temp[] twice. Both declarations are identical, as they both create an empty array, or array of size 0.

Lastly, I don't understand your code, though I may be only looking at a portion of it.
Why do you tokenize $temp[0] into $buffer, but you don't use buffer. You use the size of $temp for your iterator maximum value (buffersize). Where does $buffer even come into play?
Okay thanks.
The reason I made those declarations was to get rid of the error: I googled this before I made the thread and then I got into explicit declarations. Someone wrote that you should also declare the array+array position and not just the array. Same goes with the (string)-declarations.

Regarding $temp:
I looked through my code and I understand your confusion. Tokenizing $temp[0] isn't needed - so why is it there? Because I used the same piece of code for extracting the material name from a SG node. So there I used tokenize to get rid of the .outColor suffix (material.outColor) in order to get the true material name on position one in the array. Sorry for the confusion.

Nyro
01-23-2013, 01:51 PM
In my own scripts, I always use explicit declarations. That way I always know what type to expect when using a variable. If I happen to use the wrong type, Maya will warn me when loading the script; if I had used implicit type-casting, then the script would have failed while it was being run because it was using a wrong value. And I may have missed that.

What I seldomly do though is create arrays of fixed size. I'm not sure what the benefit of this is. Obviously, Maya only has to allocate a certain ammount of memory for the array, but I can still go ahead an increase the array's size any time by adding an element beyond the bounds of its defined size.

In this particular case, to use the tokenize command, you have to use explicit declaration. The tokenize commadn expects a string array as its last argument, and that has to exist beforehand

rgkovach123
01-23-2013, 02:03 PM
So there I used tokenize to get rid of the .outColor suffix (material.outColor) in order to get the true material name on position one in the array. Sorry for the confusion.

If have the time to learn python you will be so much happier in the long run.
python has great string manipulation that makes tokenize look like a wooden club.

nodePlusAttr = 'lambert1.outColor'
material = nodePlusAttr.split('.')[0]
attr = nodePlusAttr.split('.')[-1]

# or split from the right side of the string instead of the left side
attr = nodePlusAttr.rsplit('.')[0]

CGTalk Moderation
01-23-2013, 02:03 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.