How to properly use parameters in functions

How to properly use parameters in functions

Anonymous
Not applicable
707 Views
4 Replies
Message 1 of 5

How to properly use parameters in functions

Anonymous
Not applicable

I'm trying to define a function to fetch the number of materials applied to a mesh:

 

function getMatCount meshName = 
(
	mat = $meshName.material
	if mat.count != undefined then
	(
		return mat.count
	)
	1
)

getMatCount "body"
getMatCount "head"

Using $body.material works fine, but using $meshName inside the function doesn't work... as I would expect. What is the proper way to access the mesh?

 

Also, if there's a better way to fetch the number of materials by mesh name, please tell me 😉

 

0 Likes
Accepted solutions (1)
708 Views
4 Replies
Replies (4)
Message 2 of 5

Swordslayer
Advisor
Advisor

@Anonymous wrote:

I'm trying to define a function to fetch the number of materials applied to a mesh:

 

function getMatCount meshName = 
(
	mat = $meshName.material
	if mat.count != undefined then
	(
		return mat.count
	)
	1
)

getMatCount "body"
getMatCount "head"

Using $body.material works fine, but using $meshName inside the function doesn't work... as I would expect. What is the proper way to access the mesh?

 

Also, if there's a better way to fetch the number of materials by mesh name, please tell me 😉

 


Variables in MAXScript don't have the dollar sign at the beginning, that's reserved for pathName access, i.e. accessing nodes by their name/hierarchical name. So instead of $meshName use just the variable name, meshName.

 

As for the material.count, that will report how many materials are there in case there's a multisub material applied to the object, not all of them have to be assigned to the object, either. Is that what you wanted, or do you instead want to know how many material IDs are used by the object?

0 Likes
Message 3 of 5

Anonymous
Not applicable

Ah, yes, actually I want to know how many material IDs are used by the object 🙂

0 Likes
Message 4 of 5

Swordslayer
Advisor
Advisor
Accepted solution

That's a bit more involved as you basically have to loop all the faces and check their ID. The method used depends on the geometry type, it's different for editable poly object edit poly modifier (obj.getFaceMaterial faceIndex) and editable mesh object and modifier (getFaceMatID obj faceIndex). So you either have to check for the type and choose accordingly or get a in-memory mesh snapshot and use that.

Message 5 of 5

Anonymous
Not applicable

Alright, at least I know where to start digging now 🙂 Thanks!

0 Likes