Simple layer script

Hey guys,

Sorry to keep posting simple little questions. I am trying to learn maxscript but I keep getting stuck on dumb little things.

I want a script that will loop through a selection and move the objects to a new layer with the layer name matching the object name. But also change the wire color to colors inside of a defined array.

So object 201.MDL:brck will be moved to a newly created layer named "201.MDL:brck"
with the wire color matching the first color in the "sColor" array. And so on and son.

The way I have it written now, the script will create and move the objects to the new layers. But the colors are all the same. It won't cycle through the array and move on to the next color.

__________________________________________________

sColor = #(red, green, blue, yellow, brown, orange)

(
for o in selection do
(
oName = o.name
oLayer = LayerManager.newLayerFromName oName
for c in sColor do
(
oLayer.wirecolor = c
)
oLayer.addNode o
)
)

___________________________________________________

Comments

Comment viewing options

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

...

Optimized version

fn place_Obj_In_NewLayer objs clrID: =
(
	local sColor = #(red, green, blue, yellow, brown, orange)
	local getLayer = LayerManager.getLayerFromName, newLayer = LayerManager.newLayerFromName
	for o in objs where o.layer.name != o.name do
	(
		layer = if (lay = getLayer o.name) == undefined then newLayer o.name else lay
		layer.addNode o
		if clrID <= sColor.count then layer.wirecolor = o.wirecolor = sColor[clrID]
		else layer.wirecolor = o.wirecolor = sColor[random 1 sColor.count]
	)
)
place_Obj_In_NewLayer selection clrID:1

bga

scottparris's picture

Is there anyway for the clrID

Is there anyway for the clrID to loop to the next color so I don't have to manually change that number?

barigazy's picture

...

fn place_Obj_In_NewLayer objs =
(
	local sColor = #(red, green, blue, yellow, brown, orange), clrID = 1
	local getLayer = LayerManager.getLayerFromName, newLayer = LayerManager.newLayerFromName
	for o in objs where o.layer.name != o.name do
	(
		if (node = getLayer o.name) != undefined then (node.addNode o ; o.wirecolor = node.wirecolor) else
		(
			node = newLayer o.name
			clrID = if clrID > sColor.count then 1 else clrID
			node.wirecolor = o.wirecolor = sColor[clrID]
			node.addNode o ; clrID += 1
		)
	)
)
place_Obj_In_NewLayer selection

bga

scottparris's picture

Works great!!! Thank you!

Works great!!! Thank you! Still got a lot to learn.

barigazy's picture

...

Use integer value for "clrID" argument. This number represent ordinal number of item in sColor array

fn place_Obj_In_NewLayer objs clrID: =
(
	local sColor = #(red, green, blue, yellow, brown, orange)
	local getLayer = LayerManager.getLayerFromName, newLayer = LayerManager.newLayerFromName
	for o in objs where o.layer.name != o.name do
	(
		if (layer = getLayer o.name) == undefined then
		(
			layer = newLayer o.name ; layer.addNode o
			if clrID <= sColor.count then layer.wirecolor = o.wirecolor = sColor[clrID]
			else layer.wirecolor = o.wirecolor = sColor[random 1 sColor.count]
 
		)
		else
		(
			layer.addNode o
			if clrID <= sColor.count then layer.wirecolor = o.wirecolor = sColor[clrID]
			else layer.wirecolor = o.wirecolor = sColor[random 1 sColor.count]
		)
	)
)
place_Obj_In_NewLayer selection clrID:1

bga

Comment viewing options

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