Notifications
Clear all

[Closed] Automatic Material by Layer

Hey guys,

I’m having a hell of a time finding information on this. Basically i want to create a script that will automatically assign a material to a layer (all of its objects) which has the same name.

So “Bricks” material would be assigned to all the objects on the “Bricks” layer and so on.

I’ve tried the process with the listener on but to no avail. Also looked for some reference but couldn’t seem to find anything appropriate. Any help would be greatly appreciated!

10 Replies

I think i may have looked at this one when i was searching for reference. I have pre-exisiting layers and pre-exisiting materials though. Basically, just trying to combine them based on similar names…exact names actually.

I’ll have to look at this one more closely…there may be bits inside there that will help.

Still open for input/direction though…

My method would be :

for m = 0 to layerManager.count-1 do  -- loop on each layer (include layer "0" default)
	(
		lmLayer = layerManager.getLayer m  -- get current layer
		layerName = lmLayer.name	         -- get current layer name

		layer = ILayerManager.getLayerObject m
		for n in (refs.dependents layer) where superClassof n!=ReferenceTarget do (  -- loop through all data on layer and skip things on layer that are not geometryClass
			
                        -- try to do stuff: here we don't check if named material exists
                        try(
				n.material = meditMaterials[layerName]  -- assign to current node, material in the materialEditor which has same name that current layer
			)catch()

                        /*
                        -- we can check named material exists :
                        idx = findItem (for m in meditMaterials collect m.name) layerName
			if idx != 0 then n.material = meditMaterials[idx]
                        */

		)-- end loop on nodes in current layer

		
	)--end loop on each layer
(
	matlib = meditmaterials   -- or any another material library
	for k=1 to LayerManager.count-1 do
	(
		layer = LayerManager.getLayer k
		if (mat = matlib[layer.name]) != undefined do
		(
			nodes
			layer.nodes &nodes
			if nodes.count > 0 do nodes.material = mat
		)
	)
)
1 Reply
(@denist)
Joined: 10 months ago

Posts: 0
for n in (refs.dependents layer) where superClassof n!=ReferenceTarget

if you use it to find nodes in a layer this is not a right method.

ps. i’ve just found – it’s my 7,000th post

Yesss denisT,

I was just waiting for that !

Happy 7000 !! :applause:

Thanks for sharing your thoughts since 2009.
I’m here since 2002, and … I’ll make a party for my 100 posts !

Code:

for n in (refs.dependents layer) where superClassof n!=ReferenceTarget

if you use it to find nodes in a layer this is not a right method.

Some lazy stuff from me, I proceeded by elimination. I saw two elements that were not geometryClass so I excluded them.

Should I filter nodes like this ?

for n in (refs.dependents layer) where superClassof n==geometryclass and canconvertto n editable_mesh do

But, in your code, you don’t filter objects (or I don’t understand how). I don’t get those two lines :

nodes
layer.nodes &nodes

Are you declaring an empty variable “nodes” ?
I don’t see any reference of that in layerManger documentation…

What is the “&” before your variable ? (sometimes I see this in codes but I don’t know the meaning of this mecanism)

1 Reply
(@denist)
Joined: 10 months ago

Posts: 0

it’s documented. see Interface: LayerProperties

nodes <&node array>layerNodes

layerNodes is Out parameter

Returns true on success, and an array of all nodes on the layer in the by-reference Out parameter.

the symbol & means that the following variable is an out parameter. (in some methods it can be in parameter as well)

hehh… i forgot that the .nodes method returns true if nodes array is not empty. so my code can be modified to look more ‘pro’:

if layer.nodes &nodes do nodes.material = mat

Out parameters make a lot more sense when you use them in a function that has multiple out parameters. There are a few inbuilt max functions which have them, and you can also write your own function with out parameters by using the % symbol.

fn thisFunction input &valA &valB =
(
valA = input * 10
valB = input * 100
)

testValA
testValB
thisFunction 10 &testValA &testValB

–testValA = 100
–testValB = 1000

So from thisFunction you get two variables output, rather than having something like
testValA = thisFunctionA 10
testValB = thisFunctionB 10

@ Dave & Denis
Thanks gentlemen, I’ve learned something new !