how to delete selected layers

how to delete selected layers

manue154
Enthusiast Enthusiast
2,998 Views
12 Replies
Message 1 of 13

how to delete selected layers

manue154
Enthusiast
Enthusiast

Hi,

There is an easy way to delete all selected layers and all inside, (like their nested nodes, objs, nested layers...)?

by advance, thanks

0 Likes
Accepted solutions (1)
2,999 Views
12 Replies
Replies (12)
Message 2 of 13

Swordslayer
Advisor
Advisor
Accepted solution
layerExplorer = SceneExplorerManager.GetActiveExplorer()
if layerExplorer != undefined do
(
	local objs = #(), layers = #()

	mapped fn collectObjs item =
		if isKindOf item Base_Layer then
		(
			append layers item
			join objs (refs.dependentNodes item)
			items = (for c = 1 to item.getNumChildren() collect (item.getChild c).layerAsRefTarg)
			collectObjs items
		)
		else append objs item
	
	collectObjs (layerExplorer.SelectedItems())
	delete objs
	for layer in layers where layer.canDelete() do LayerManager.deleteLayerByName layer.name
)
Message 3 of 13

manue154
Enthusiast
Enthusiast

Thank you again, it's magic!!! you know all about maxscript! thank you for your generosity 🙂

0 Likes
Message 4 of 13

denisT.MaxDoctor
Advisor
Advisor
join objs (refs.dependentNodes item)

this line can be a source of mistake. a dependency can be out of hierarchy 

0 Likes
Message 5 of 13

manue154
Enthusiast
Enthusiast

arf.... do you know what can i put instead?

0 Likes
Message 6 of 13

Swordslayer
Advisor
Advisor

@Anonymous wrote:
join objs (refs.dependentNodes item)

this line can be a source of mistake. a dependency can be out of hierarchy 


I'd be curious to see an example for this case.

0 Likes
Message 7 of 13

manue154
Enthusiast
Enthusiast

anyway, i'll test it at work 🙂

0 Likes
Message 8 of 13

denisT.MaxDoctor
Advisor
Advisor
ca = attributes ca 
(
	parameters params
	(
		_layer type:#maxobject
	)
)

delete objects
sp = sphere()

a = LayerManager.newLayer() 
custattributes.add sp ca baseobject:off
sp.ca._layer = a.layerAsRefTarg

format ">> % == %\n" (refs.dependentnodes a.layerAsRefTarg) (a.getNumNodes())
0 Likes
Message 9 of 13

Swordslayer
Advisor
Advisor

@Anonymous wrote:
ca = attributes ca 
(
	parameters params
	(
		_layer type:#maxobject
	)
)

delete objects
sp = sphere()

a = LayerManager.newLayer() 
custattributes.add sp ca baseobject:off
sp.ca._layer = a.layerAsRefTarg

format ">> % == %\n" (refs.dependentnodes a.layerAsRefTarg) (a.getNumNodes())

Hey, that's cheating Smiley Very Happy Yeah in this case getting the nodes with the usual method would of course be better 🙂

 

And because I always liked your challenges and tricks, do you have any snippet in mind where saving a layer this way could be useful (more than this 'just because I can' example)?

0 Likes
Message 10 of 13

denisT.MaxDoctor
Advisor
Advisor

i had a scripted manipulator object that showed nodes in different layers using different specified by user LOOK (color, shape, i don't remember what else) ...

 

that manipulator had to have the same dependency issue

 

 

as you understand the storing layers names couldn't work well because of renaming case and no 'layer reference change' message

 

 

same dependency issue we will have with plugins, modifiers, controllers, etc

0 Likes
Message 11 of 13

manue154
Enthusiast
Enthusiast

heu...i'm not understand anything....

but do you know why if i apply this perfect code to delete a list in an array (and to found inside a specific layer)

i've got this answer in the listener:

 

-- Error occurred in anonymous codeblock; filename: ; position: 878; line: 31
-- Compile error: No outer local variable references permitted here:  Layers
--  In line:                             append layers i

 

 

 

LayerToFoundArr = #("Asset1", "Asset2", "Asset3")
	mainLayerName = "LayerName"	
	mainLayer = LayerManager.getLayerFromName mainLayerName		
									
	function GetObjectsInLayer layer: =
	(
		local objsArr
		layer.nodes &objsArr
		objsArr				
	)	
	numChildLayers = mainLayer.getNumChildren()		
	if numChildLayers != 0 then
	(	
		for i = 1 to numChildLayers do
		(
			ChildLayersTmp = mainLayer.getChild i	
			ChildLayersName = ChildLayersTmp.name
			
			for o = 1 to LayerToFoundArr.count do	
			(				
				selLayersArr = for o in ChildLayersName where (matchpattern o.name pattern:("*" + LayerToFoundArr + "*")) collect o
				if selLayersArr.count != 0 do
				(	
					if layerExplorer != undefined do
					(
						local objs = #(), layers = #()

						mapped fn collectObjs item =
						if isKindOf item Base_Layer then
						(
							append layers item
							join objs (refs.dependentNodes item)
							items = (for c = 1 to item.getNumChildren() collect (item.getChild c).layerAsRefTarg)
							collectObjs items
						)
						else append objs item
									
						collectObjs (layerExplorer.SelectedItems())
						delete objs
						for layer in layers where layer.canDelete() do LayerManager.deleteLayerByName layer.name
					)
				)
			)
		)
	)

 

0 Likes
Message 12 of 13

Swordslayer
Advisor
Advisor

Duly noted, might come in handy some time. Wonder what would happen if the layer was saved in a RefTargContainer instead and opened in a different scene. Not at max, but guess it should still work, properties should be retrievable etc. and maybe it could be even switched with an existing scene layer using replaceReference. Gee, I'd have so many questions...

 

Btw. check your private messages.

0 Likes
Message 13 of 13

denisT.MaxDoctor
Advisor
Advisor

@Anonymous wrote:

Duly noted, might come in handy some time. Wonder what would happen if the layer was saved in a RefTargContainer instead and opened in a different scene. Not at max, but guess it should still work, properties should be retrievable etc.


it was something changed in the layers system since nested layers were added (2016+). i'm not sure now how it works. but by theory it has to be the same ReferenceTarget as was saved and be loaded with scene  

 

as i said... sometimes there is no practical use of storing a layer in paramblock but it can be a solution to receive reference change messages. I've used it many times 

0 Likes