ScriptSpot

Forum topic · General Scripting

Setting material id and material

By garyb · 2012-05-24

Description

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 (8)

Duh

garyb · 2012-05-28

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 · 2012-05-28

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!

garyb · 2012-05-29

thanks

barigazy · 2012-05-25

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

Attachments

Set Unique Mesh Polygon Material ID

barigazy · 2012-05-24

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
)

garyb · 2012-05-25

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. :)

Attachments

Explanation

barigazy · 2012-05-25

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