Community
Maya Programming
Welcome to Autodesk’s Maya Forums. Share your knowledge, ask questions, and explore popular Maya SDK topics.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Mel why won't gmatch ever find text in my array?

4 REPLIES 4
SOLVED
Reply
Message 1 of 5
malcolm_341
734 Views, 4 Replies

Mel why won't gmatch ever find text in my array?

malcolm_341
Collaborator
Collaborator

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";
		}
	}

 

0 Likes

Mel why won't gmatch ever find text in my array?

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";
		}
	}

 

4 REPLIES 4
Message 2 of 5
mspeer
in reply to: malcolm_341

mspeer
Consultant
Consultant

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?

 

Message 3 of 5
malcolm_341
in reply to: mspeer

malcolm_341
Collaborator
Collaborator

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?

Message 4 of 5
mspeer
in reply to: malcolm_341

mspeer
Consultant
Consultant
Accepted solution

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";
	}

 

0 Likes

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";
	}

 

Message 5 of 5
malcolm_341
in reply to: mspeer

malcolm_341
Collaborator
Collaborator

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.

Post to forums  

Autodesk Design & Make Report