PDA

View Full Version : [MEL] Compare strings


Jerakin
01-20-2011, 05:27 PM
Hi,

I am new to MEL so I could use some help,
I would like to compare two strings. Like this

p, li { white-space: pre-wrap; }


string $stringTwo = "pSphere9";
string $getTheCurentMesh[] = `ls -selection`;



for ($mesh in $getTheCurentMesh)
if ($getTheCurentMesh == $stringTwo)
{
print True;
}
else
{
print False;
}



But of course I get
// Error: Line 5.36: Illegal operation "==" on data of type string[]. //

As it is a string.



Do anyone know of a way to compare two text strings?




Regards,
Jerakin

theazz
01-21-2011, 09:15 AM
you haven't specified the correct object in your IF statement. you've indicated the array and not the object the loop is operating on. also you need to wrap your for statement commands in there own curly brackets

try this


string $stringTwo = "pSphere9";
string $getTheCurentMesh[] = `ls -selection`;

for ($mesh in $getTheCurentMesh)
{
if ($mesh == $stringTwo)
{ print True; }
else
{ print False; }
}

Jerakin
01-21-2011, 10:01 AM
Edit, mulitpost. Sorry

Jerakin
01-21-2011, 10:45 AM
Thanks for your reply. It does solve that problem, that code I just wrote because I didn't have access to my code, but now I have.


This is the code, what I wanted to do is to Delete the unused UVmaps on the selected Objects.

Beware it is VERY messy and a lot of unnecessary variables is still in there.

If someone could take a look so that you can see what I want to do it would be great. I want to learn so please just don't post "Do like this" please state why also :D

Regards,
Jerakin



/*
Author: Jerakin

Version: 0.1

Description:
Deletes the unused UVmaps.


*/


//Make the window if it doesn’t exist
if( `window -exists "mh_DeleteUVWindow"`)
deleteUI "mh_DeleteUVWindow";

window
-title "Del Unused UVSets"
"mh_DeleteUVWindow";

columnLayout -adj 0 "mh_DeleteUVLayout";

windowPref -remove "mh_DeleteUVWindow";
window -edit -s false -mxb false -wh 250 100 "mh_DeleteUVWindow";
columnLayout -adjustableColumn false;
button -label "Selection" -w 247 -command "SelectionButtonPush";
button -label "All" -w 247 -command "AllButtonPush";
button -label "Cancel" -w 247 -command "CancelButtonPush";
showWindow;

showWindow "mh_DeleteUVWindow";


//The Selection button
proc SelectionButtonPush()
{

//Get selected Objectname

string $getTheCurentMesh[] = `ls -selection`;

//Get selected ObjectShapename

string $getTheCurentShapeMesh[] = `ls -sl -dag -lf`;

//save it with a new name.

string $theCurentMapname = ($getTheCurentMesh[0] + ".map[1]");

string $activeUVTest[0];

string $activeUV[];

//Store the UVsets
string $UVSetsList[] = `polyUVSet -q -allUVSets`;

//For-loop, checks if there is any UVs in the UVset.
//Deletes the UVset if there isn't any.
for ($UVSet in $UVSetsList)
{
hilite $getTheCurentMesh ;
select -r $theCurentMapname;
string $activeUV[] = `ls -selection`;
}
// $theCurentMapname will be meshname.map[1]
// and $activeUV[0] will be meshname.map[1]
// if there is any UVs
if ($activeUV == $theCurentMapname)
{
print "Something was deleted";
polyUVSet -currentUVSet -uvSet $UVSet $getTheCurentShapeMesh;
polyUVSet -delete;
}
else
{
// Do nothing
print $activeUV;
print $theCurentMapname;
}
}

proc CancelButtonPush()
{
deleteUI -window "mh_DeleteUVWindow";
}

djx
01-22-2011, 12:35 PM
I have not tried to run your script, but here are a few observations about your code.

This line is bad syntax
string $activeUVTest[0];

should be
string $activeUVTest[];

but actually it doesnt seem to be needed at all.

You declared string $activeUV[] and then redeclared it inside the for loop. I think this redeclared variable will only exist inside the for loop. I think you want the line in the loop to look like this (ie drop the "string" and the square brackets)
$activeUV = `ls -selection`;

The if statement will fail because you cant compare an array to a string. As theazz suggested, you need to compare one of the array variables. So your if statement should probably look like this
if ($activeUV[0] == $theCurentMapname)

and in the else block I think you want to
print $activeUV[0]

good luck
David

Jerakin
01-22-2011, 02:55 PM
Thanks for you reply, but unfortunately it didn't help.

I still get
// Error: if ($activeUV[0] == $getTheCurentMesh) //
// Error: Line 57.42: Illegal operation "==" on data of type string[]. //


Do anyone know how to convert a string to a binary number or decimal number?

I was thinking that if you convert a "pCube.map[1]" to a unique number (based on letters) I could use two INTs or floats to compare them instead of two strings.

Thanks,
Jerakin

NaughtyNathan
01-22-2011, 07:54 PM
because $getTheCurentMesh is an array too!!!
string comparison in MEL is done using == exactly like this. e.g.

if ($string1 == $string2) print "the same!\n";You just have to understand the difference between a string an an array.

(there is also the strcmp command but that is for something a little different)
:nathaN

Jerakin
01-22-2011, 11:45 PM
Thanks a lot Nathan that gave me the thinking that I needed.


I used that thinking to rewrite my code a little, this is what I got now.

/* Author: Jerakin

Version: 0.2

Description:
Deletes the unused UVmaps.


*/
//Make the window if it doesn’t exist
if( `window -exists "mh_DeleteUVWindow"`)
deleteUI "mh_DeleteUVWindow";

window
-title "Del Unused UVSets"
"mh_DeleteUVWindow";

columnLayout -adj 0 "mh_DeleteUVLayout";

windowPref -remove "mh_DeleteUVWindow";
window -edit -s false -mxb false -wh 250 100 "mh_DeleteUVWindow";
columnLayout -adjustableColumn false;
button -label "Selection" -w 247 -command "SelectionButtonPush";
button -label "All" -w 247 -command "AllButtonPush";
button -label "Cancel" -w 247 -command "CancelButtonPush";
showWindow;

showWindow "mh_DeleteUVWindow";
//The Selection button
proc SelectionButtonPush()
{
//Get selected Objectname
string $getTheCurentMesh[] = `ls -selection`;
//Get selected ObjectShapename
string $getTheCurentShapeMesh[] = `ls -sl -dag -lf`;
// string $activeUV;
for ($currentMesh in $getTheCurentMesh)
{
//remake it so it it will be read as a UV
string $theCurentMapname = ($currentMesh + ".map[1]");
//Store the UVsets
string $UVSetsList[] = `polyUVSet -q -allUVSets`;
//For-loop, checks if there is any UVs in the UVset.
//Deletes the UVset if there isn't any.
for ($UVSet in $UVSetsList)
{
hilite $getTheCurentMesh ;
select -r $theCurentMapname;
$activeUV = `ls -selection`;

// int $stringCompare = `strcmp $activeUV $theCurentMapname`;
// $theCurentMapname will be meshname.map[1]
// and $activeUV[0] will be meshname.map[1]
// if there is any UVs
if ($activeUV == $theCurentMapname)
{
print "Something can be deleted";
// polyUVSet -currentUVSet -uvSet $UVSet $getTheCurentShapeMesh;
// polyUVSet -delete;
}
else
{
print $activeUV;
print $theCurentMapname;
}
}
deleteUI -window "mh_DeleteUVWindow";
}
}
proc CancelButtonPush()
{
deleteUI -window "mh_DeleteUVWindow";
}



I looks fine to me but I get the error
// Error: line 63: Cannot use data of type string[] in a scalar operation. //

I have googeld it (a lot!). It only seems to be there when mr gives someone problem. You Nathan answered one of the threads I read, you said it could be related to the "if (x) ? 1 : 0; construct..." I don't use that in the code though (I use an If statement but not compressed with ?).

I tried to use the strcmp command also but didn't get that to work as I thought it should when I read about it.

I tried to replace

if ($activeUV == $theCurentMapname)

with

int $stringCompare = `strcmp $activeUV $theCurentMapname`;

if ($stringCompare == 0)


that gave me
//Error: line 56: Invalid call to "strcmp". Check number and types of arguments expected by the procedure. //


If someone know how to fix and why this happens I would be very happy.

I am sorry if I am being a nuisance, but you people really seems to know what you speak of and I would really like to get this to work.

Best regards,
Jerakin

NaughtyNathan
01-23-2011, 03:00 PM
lol! I don't know why you are having such difficulty with this issue, it's the same thing every time... $activeUV is an ARRAY.

you cannot compare an array to a string in MEL (as by now I think you know). But the ls command always returns an array, even if it is only returning a single item, it is still an array. (how do we know? because the ls docs tell us)

$activeUV = `ls -selection`; // this creates an array
an array is not defined only when you put [] on a variable name, it is defined by what the result of the captured command is. and ls always returns an array. For this reason it is not considered good practice to auto-type your variables as it makes it very unclear (especially to a beginner) what you are creating. e.g.

$active = `someFunction`;no-one is going to have any idea what type of variable $active is going to be, without going through some other code or docs to find out.
float $active[3] = `someFunction`;is far better as we can immediately see that $active is supposed an array of 3 float values. And if `someFunction` didn't return a float array we would get an immediate error on that line that told us exactly what the specific issue was, not something far more esoteric later on in the code.

Don't bother trying to use strcmp, it's a red herring in your case.

Finally, don't worry about or apologise for being a "nuisance", you're far from that atm and this forum is just as much for noobs trying to learn as it is for anyone else and sometimes it just takes a while longer to "get" certain stuff.

:nathaN

Jerakin
01-23-2011, 09:45 PM
Yet again thank you Nathan, we didn't learn that in class or I had just forgot it.

Thanks to you help I got the code "working", just to see that doing it that way didn't work.

Felt like a slap in my face.

Well I learned a lot and that was the main purpose and a lot is thanks to you.

Cheers,
Jerakin

CGTalk Moderation
01-23-2011, 09:45 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.