CGTalk > Software > Autodesk Maya > Maya Programming
Login register
Thread Closed share thread « Previous Thread | Next Thread »  
 
Thread Tools Search this Thread Display Modes
Old 01-22-2013, 09:40 PM   #1
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 922
Explicit variable definitions - tokenize still fails?!?!

So I have this script that finds and selects the shading group belonging to a selected material:
Code:
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.
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 01-22-2013, 09:55 PM   #2
rgkovach123
Know-it-All
robert
austin, USA
 
Join Date: Nov 2006
Posts: 364
this doesn't answer your question, but this should help. You are doing way more work than necessary to retrieve the shading Groups.

Code:
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!

Last edited by rgkovach123 : 01-22-2013 at 11:57 PM.
 
Old 01-22-2013, 11:28 PM   #3
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 922
Invalid call to "tokenize". Check number and types of arguments expected by the procedure
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 01-22-2013, 11:30 PM   #4
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 922
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:
Code:
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[]
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 01-22-2013, 11:56 PM   #5
rgkovach123
Know-it-All
robert
austin, USA
 
Join Date: Nov 2006
Posts: 364
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....

Last edited by rgkovach123 : 01-23-2013 at 12:05 AM.
 
Old 01-23-2013, 12:51 AM   #6
Nyro
Lurking
 
Nyro's Avatar
portfolio
A. D.
Vertex Slinger
Munich, Germany
 
Join Date: Dec 2006
Posts: 1,436
It's quite simple. You declared $buffer as a string, while it should have been a string array:

Code:
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?
 
Old 01-23-2013, 12:33 PM   #7
DeadlyNightshade
Plz...
 
DeadlyNightshade's Avatar
portfolio
Martin Dahlin
Jr. Technical Artist
King
Stockholm, Sweden
 
Join Date: Jul 2012
Posts: 922
Quote:
Originally Posted by Nyro
It's quite simple. You declared $buffer as a string, while it should have been a string array:

Code:
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.
__________________
Nightshade UV Editor (script for Maya) - Extends Maya's default UV Texture Editor with additional tools.
 
Old 01-23-2013, 02:51 PM   #8
Nyro
Lurking
 
Nyro's Avatar
portfolio
A. D.
Vertex Slinger
Munich, Germany
 
Join Date: Dec 2006
Posts: 1,436
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
 
Old 01-23-2013, 03:03 PM   #9
rgkovach123
Know-it-All
robert
austin, USA
 
Join Date: Nov 2006
Posts: 364
Quote:
Originally Posted by DeadlyNightshade
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]
 
Old 01-23-2013, 03:03 PM   #10
CGTalk Moderation
Expert
CGTalk Forum Leader
 
Join Date: Sep 2003
Posts: 1,066,478
Thread automatically closed

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.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 02:32 AM.


Powered by vBulletin
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.