I've seen countless times on the internet people suggesting gmatch to find an item in an array and return true if it finds the value, except this has never worked once for me and I've confirmed the value is in the array. Is there some hidden trick to using gmatch this is seriously frustrating. I have a single cube selected and that cube is skinned to a bone. Here's my code that never returns true even though I know it's true.
string $tempNode[] = `ls -sl`;
int $sizeTempNode = `size $tempNode`;
int $gmatchCounter = 0;
select $tempNode[$gmatchCounter];
string $shapeName[] = `listRelatives -shapes -fullPath $tempNode[$gmatchCounter]`;
string $connections[] = `listConnections $shapeName[0]`;
for ($itemGM in $connections)
{
if (`gmatch $itemGM "*skinCluster*"`)
{
print "Found cluster";
}
else
{
print "Nothing";
}
}
Solved! Go to Solution.
I've seen countless times on the internet people suggesting gmatch to find an item in an array and return true if it finds the value, except this has never worked once for me and I've confirmed the value is in the array. Is there some hidden trick to using gmatch this is seriously frustrating. I have a single cube selected and that cube is skinned to a bone. Here's my code that never returns true even though I know it's true.
string $tempNode[] = `ls -sl`;
int $sizeTempNode = `size $tempNode`;
int $gmatchCounter = 0;
select $tempNode[$gmatchCounter];
string $shapeName[] = `listRelatives -shapes -fullPath $tempNode[$gmatchCounter]`;
string $connections[] = `listConnections $shapeName[0]`;
for ($itemGM in $connections)
{
if (`gmatch $itemGM "*skinCluster*"`)
{
print "Found cluster";
}
else
{
print "Nothing";
}
}
Solved! Go to Solution.
Solved by mspeer. Go to Solution.
Hi!
I tried this here with slightly modified print values and this is what I get for a cube with one joint.
----shapes:
|pCube1|pCubeShape1
|pCube1|pCubeShape1Orig
----connections:
initialShadingGroup
skinCluster1Set
skinCluster1GroupId
skinCluster1Set
tweakSet1
groupId2
tweakSet1
skinCluster1
tweak1
----script print output:
Nothing
Found cluster
Found cluster
Found cluster
Nothing
Nothing
Nothing
Found cluster
Nothing
What Maya version are you using?
Hi!
I tried this here with slightly modified print values and this is what I get for a cube with one joint.
----shapes:
|pCube1|pCubeShape1
|pCube1|pCubeShape1Orig
----connections:
initialShadingGroup
skinCluster1Set
skinCluster1GroupId
skinCluster1Set
tweakSet1
groupId2
tweakSet1
skinCluster1
tweak1
----script print output:
Nothing
Found cluster
Found cluster
Found cluster
Nothing
Nothing
Nothing
Found cluster
Nothing
What Maya version are you using?
Thanks for your reply, I'm using 2020.1, I think my issue is it's finding true and then also finding false. I had assumed the way gmatch would work is if it found true it would return true, but I think in my case it's returning both true and then also returning else causing it to not work. Your example is helpful because it's showing multiple return values which is what I was getting tripped up on. I think the trick might be you can only use gmatch to return true or false you can't really use it to return true or else because it's looping through the array and it might find true in the first item, but then be false by the end of the loop. Is that how it works?
Thanks for your reply, I'm using 2020.1, I think my issue is it's finding true and then also finding false. I had assumed the way gmatch would work is if it found true it would return true, but I think in my case it's returning both true and then also returning else causing it to not work. Your example is helpful because it's showing multiple return values which is what I was getting tripped up on. I think the trick might be you can only use gmatch to return true or false you can't really use it to return true or else because it's looping through the array and it might find true in the first item, but then be false by the end of the loop. Is that how it works?
Hi!
Every connection is checked any you get a result per check:
initialShadingGroup - Nothing
skinCluster1Set - Found cluster
skinCluster1GroupId - Found cluster
skinCluster1Set - Found cluster
tweakSet1 - Nothing
groupId2 - Nothing
tweakSet1 - Nothing
skinCluster1 - Found cluster
tweak1 - Nothing
If you only want one single bool value as result you can try the following:
string $tempNode[] = `ls -sl`;
int $sizeTempNode = `size $tempNode`;
int $gmatchCounter = 0;
select $tempNode[$gmatchCounter];
string $shapeName[] = `listRelatives -shapes -fullPath $tempNode[$gmatchCounter]`;
string $connections[] = `listConnections $shapeName[0]`;
int $nFound = 0;
for ($itemGM in $connections)
{
if (`gmatch $itemGM "*skinCluster*"`)
{
$nFound = 1;
break;
}
}
if ($nFound == true) {
print "Found cluster";
}
else {
print "Nothing";
}
Hi!
Every connection is checked any you get a result per check:
initialShadingGroup - Nothing
skinCluster1Set - Found cluster
skinCluster1GroupId - Found cluster
skinCluster1Set - Found cluster
tweakSet1 - Nothing
groupId2 - Nothing
tweakSet1 - Nothing
skinCluster1 - Found cluster
tweak1 - Nothing
If you only want one single bool value as result you can try the following:
string $tempNode[] = `ls -sl`;
int $sizeTempNode = `size $tempNode`;
int $gmatchCounter = 0;
select $tempNode[$gmatchCounter];
string $shapeName[] = `listRelatives -shapes -fullPath $tempNode[$gmatchCounter]`;
string $connections[] = `listConnections $shapeName[0]`;
int $nFound = 0;
for ($itemGM in $connections)
{
if (`gmatch $itemGM "*skinCluster*"`)
{
$nFound = 1;
break;
}
}
if ($nFound == true) {
print "Found cluster";
}
else {
print "Nothing";
}
Oh that's a great solution thanks, yeah that is what I was looking for I ended up solving it a different way, but your way works better, thanks!
Oh that's a great solution thanks, yeah that is what I was looking for I ended up solving it a different way, but your way works better, thanks!
Can't find what you're looking for? Ask the community or share your knowledge.