Setting material id and material

Hey guys,

First off, I'm a newbie. sorry.

I'm trying to learn maxscript and to create standard parametric geometries for archviz. Currently I'm working on a curtain wall plugin similar to the one that comes with Max but with more functionality for what I need. I have a pretty good handle on creating the geometry itself but I'm stuck on how to assign materials and material ids to specific faces and establish uv coordinates.

I created the following script for testing. I would like to assign a different material for each polygon. Can anybody help?

(
parameters main rollout: params(
WidthA type:#float ui:WidthA default:10
LengthA type:#float ui:LengthA default:10
)

rollout params "Test Plane"(
spinner WidthA"Width "range:[0,10000,10] tooltip:"Width"
spinner LengthA"Length "range:[0,10000,10] tooltip:"Length"
)

on buildMesh do (
vert_array = #()
face_array = #()

append vert_array[0,0,0]
append vert_array[(LengthA/2),0,0]
append vert_array[LengthA,0,0]
append vert_array[0,WidthA,0]
append vert_array[(LengthA/2),WidthA,0]
append vert_array[LengthA,WidthA,0]

append face_array[4,1,2]
append face_array[2,5,4]
append face_array[5,2,3]
append face_array[3,6,5]

setmesh mesh vertices:vert_array faces:face_array
for face = 1 to mesh.numfaces do setEdgeVis mesh face 3 false
)

tool create
(
on mousePoint click do (
case click of (
1: coordsys grid (nodeTM.translation = gridPoint)
)
)
on mouseMove click do (
case click of (
2: (LengthA = abs(gridDist.x); WidthA = abs(gridDist.y))
3: (#stop)
)
)
)
)

Thanks in advance,
Gary

Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
garyb's picture

Duh

Oh wow! That is so much simpler. I was thinking i would have to cycle through the mesh after it was made. This is exactly what i needed. Thank you!

barigazy's picture

I'm glad that this code

I'm glad that this code help.
By the way, next time when you post some code use tags "< code> your code here < /code>"
Cheers!

barigazy

garyb's picture

thanks

thanks

barigazy's picture

And this is your plugin

And this is your plugin (place it if you want in script>startup directory)
You don't need my function for plugin

AttachmentSize
matassignplane.ms 1.27 KB

barigazy

barigazy's picture

Set Unique Mesh Polygon Material ID

Hope this help

fn setPolygonMtlID obj = 
(
	local obj = selection[1]
	local uniqueFaces = #()
 
	for f in 1 to obj.numfaces do appendifunique uniqueFaces ((meshop.getPolysUsingFace obj #{f}) as array)[1]
	meshPolygonsArr = for i in uniqueFaces collect ((meshop.getPolysUsingFace obj #{i}) as array)
	for mid in 1 to meshPolygonsArr.count do 
	(
		for f in meshPolygonsArr[mid] do setFaceMatID obj f mid
	)
	free meshPolygonsArr ; free uniqueFaces ; update obj
)
for obj in selection where iskindof obj.baseobject Editable_Mesh do
(
	setPolygonMtlID obj.baseobject
)

barigazy

garyb's picture

Hey, thanks for the help and

Hey, thanks for the help and the quick reply! :)

I tried it in my script but it didn't seem to do anything. I'm not entirely sure exactly what your code does so I probably didn't apply it correctly.

Attached is the script if you'ld like to have a look.

Thank you. :)

AttachmentSize
testmatscript.ms 1.71 KB
barigazy's picture

Explanation

Run the code below and you'll find out how my function works

fn setPolygonMtlID obj &polycount = 
(
	local uniqueFaces = #()
	for f in 1 to obj.numfaces do appendifunique uniqueFaces ((meshop.getPolysUsingFace obj #{f}) as array)[1]
	meshPolygonsArr = for i in uniqueFaces collect ((meshop.getPolysUsingFace obj #{i}) as array)
	polycount = meshPolygonsArr.count
	for mid in 1 to polycount do 
	(
		for f in meshPolygonsArr[mid] do setFaceMatID obj f mid
	)
	free meshPolygonsArr ; free uniqueFaces ; update obj
)
testmesh = converttomesh (Teapot segs:3)
setPolygonMtlID testmesh &polycount
multimat = Multimaterial name:"TestMat" materialList:(for m in 1 to polycount collect standardmaterial diffuse:(random (color 0 0 0) (color 255 255 255)))
testmesh.material = multimat

barigazy

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.