Rename Objects By layer name

Looking for a script to rename all objects on a layer to the name of that layer.
This should work for multiple selected layers. Unselected layers should be ignored.

Comments

Comment viewing options

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

Anubis,

is it possible to rename objects by layer name without adding number at the name end?
I seem can't find a way to remove redundant part of the code.

barigazy's picture

...

Yup. By removing "uniquename" function in this line
for n in nodes do n.name = uniquename layer.name
This line need to look like this
for n in nodes do n.name = layer.name

barigazy

strangeneutrino's picture

layer by layer

Could someone please add description to the end of each line to describe how this works. In particular how does the script know to go down each layer one by one.

fn renameLayerNodes considerZeroLayer:on =
(
e = if considerZeroLayer then 0 else 1
local getLay = LayerManager.getLayer, layer
for i = e to layerManager.count-1 where ((layer = getLay i).nodes &nodes ; nodes).count > 0 do
(
for n in nodes do n.name = uniquename layer.name
)
)
renameLayerNodes()

barigazy's picture

...

You need to learn the way how maxscript works ei. execute code.
Max Script Help document will help you a lot to understand this.
Anyway...

-- define function "renameLayerNodes" with one argument
-- argument "considerZeroLayer" represent the state that can be "ON" or "OFF" ei. "true" or "false" 
fn renameLayerNodes considerZeroLayer:on =
(
	-- depending on "considerZeroLayer" variable "e" will hold integer 0 (if "true") or 1 (if "false")
	-- first layer in LayerManager is always "0" and GUI treelist control (.NET) contains items (layers)
	-- where first index is zero-based. 
	e = if considerZeroLayer then 0 else 1
	-- declare local variable "getLay" that will hold LayerManager Interface function
	-- and variable "layer" that will hold actual layer
	local getLay = LayerManager.getLayer, layer
	-- this "for-loop" iterates through a range of numbers from e which is 0 or 1 to number of layers (count) in LayerManager
	-- taking into account only layers that are not empty
	for i = e to layerManager.count-1 where ((layer = getLay i).nodes &nodes ; nodes).count > 0 do
	(
		-- "&nodes" is an array of all nodes (objects) on the layer in the by-reference Out parameter
		-- loop trough all object in the "nodes" array and rename each object with unique name (base name is the object layer name)
		for n in nodes do n.name = uniquename layer.name
	)
)
-- execute above defined function
-- you can use two forms to execute function where argument have state "ON"
renameLayerNodes()
-- or 
renameLayerNodes considerZeroLayer:on
-- but if you decide to change argument state to "OFF" use only this form
renameLayerNodes considerZeroLayer:off

barigazy

strangeneutrino's picture

re-name all objects to each objects layer name

Has anyone worked out how to re-name all object to each objects layer name?

This will help when all objects imported into the scene have the same name.

AttachmentSize
layer_name.jpg 17.8 KB
barigazy's picture

...

Try this fn.
If "considerZeroLayer" argument is off then "0" layer will be skiped (default on)

fn renameLayerNodes considerZeroLayer:on =
(
	e = if considerZeroLayer then 0 else 1
	local getLay = LayerManager.getLayer, layer
	for i = e to layerManager.count-1 where ((layer = getLay i).nodes &nodes ; nodes).count > 0 do
	(
		for n in nodes do n.name = uniquename layer.name
	)
)
renameLayerNodes()

barigazy

strangeneutrino's picture

Bang on, that's cool. How

Bang on, that's cool. How does it add increments on the end of multiple objects in the same layer? Thanks a bunch really saved some time here.

barigazy's picture

...

By using this

uniquename layer.name

barigazy

tuxmask75's picture

Yeah its a strange request,

Yeah its a strange request, Right now I have 154 layers with 100s of objects on some layers. all requiring the same name. would take forever naming a layer than naming all the objects on that layer.

Anubis's picture

Hey Andrew

Thats sound strange to me... For example, the default layer has preserved name "0", and if you have 20 objects inside that layer, you wan to rename all of them to "0"? I never will do that, but never mind...

How to work with layers is very well explained in the mxs docs, however, I not see any way to catch which layer is selected. You can use layer.ON property instead of selection. If that applicable, try this very lightly modification of the code example in the help file:

for i = 0 to layerManager.count-1 do
(
  ilayer = layerManager.getLayer i
  layerName = ilayer.name
  if ilayer.on do
  (
    layer = ILayerManager.getLayerObject i
    layerNodes = refs.dependents layer
    layerNodes.name = layerName
  )
)

my recent MAXScripts RSS (archive here)

Comment viewing options

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