Request for script to create layers from object base name

Hello,

I'm in need of a script that could create layers based on names of CAD objects imported into Max, Max brings the CAD geometry and names it based on on its CAD layer, however when adding multiple of the same, it add a numeric sequence to those object.

Example: 3 objects of the same CAD objects.

CAD layer:
AFUPA-3D-TL-GL

Max Imported Object names:
AFUPA-3D-TL-GL.001, AFUPA-3D-TL-GL.002, AFUPA-3D-TL-GL.003

I would like to have a script to create a single layer named "AFUPA-3D-TL-GL" fo that sequence of objects and move all those objects to that layer and do the same for the next.

I found a script that is pretty close, but creates a layer for each individual object instead. I have attached the script if someone could help me modify it you would be a life saver! :)

Thanks!

AttachmentSize
object2layer.ms1.05 KB

Comments

Comment viewing options

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

.

Try this one:

(
	objNamesArr = #()
	filterStr = "."
	for o in geometry do 
	(
		namesArr = filterString o.name filterStr
		if namesArr.count > 1 do appendifunique objNamesArr namesArr[1]
	)
 
	for nameStr in objNamesArr do 
	(
		newLayer = if LayerManager.getLayerFromName nameStr == undefined then
				LayerManager.newLayerFromName nameStr
			else 
				LayerManager.getLayerFromName nameStr	
 
		for o in geometry where (matchPattern o.name pattern:(nameStr + "*")) do
		(
			newLayer.addNode o
		)
	)
	objNamesArr = undefined
	free objNamesArr
	gc()
)

filterStr = "." is important. I assume that all objects have the suffix separated by the dot. If other symbol is used, then you have to change the character inside the quotes. For example if the name of the objects are:
AFUPA-3D-TL-GL_001, AFUPA-3D-TL-GL_002, AFUPA-3D-TL-GL_003
Then the filterStr = "_".
But be sure that the same charater(filterStr) is not in the names twice.

eyepiz's picture

Help!

Miauu,

I have an issue were not all my objects are being moved to the newly created layer. The script is making all the appropriate layers, but then it keeps some of them empty, certain objects are being moved to another layer.

Can you take a look at the script?

Thank you!

-E

AttachmentSize
layers.jpg 73.76 KB
layers.max 704 KB
miauu's picture

.

Hi! The problems comes from the names of the objects and the layers. I will explain:

You have a layer with name AFUFU-3D-SV, and layer with name AFUFU-3D-SV-DF. As you see the second layer have the same name as the first layer with -DF added to the end. When the script loops through all objects and compare their names with the names of the layers this command is used for comparison:

(matchPattern o.name pattern:(nameStr + "*"))

Which means:

- when the current layer name is AFUFU-3D-SV and the script loops through all objects to find those of them that have the same name and to put them to that layer and

- there are object with name AFUFU-3D-SV-DF  the script will put that object to the AFUFU-3D-SV layer, because the object name contains all the characters from the layers name in correct order. This is the reason some objects to go to the wrong layers. In fact, first they are placed in correct layer, but then the script compares the object names against the layer name that is shorter than the previous and have the same characters: first the script use AFUFU-3D-SV-DF  and all objects with the same name are added to that layer, but then the script use AFUFU-3D-SV  and the same objects are moved to the wrong layer.  

To avoid this problem replace the line above with this one:

(matchPattern o.name pattern:(nameStr + ".*"))

As you see the addition is only one character.

Other way to avoid such a problems is to keep the names fo your layers unique.

 

 

 

 

eyepiz's picture

Thank you! That did the trick

Thank you! That did the trick :)

You rock!

eyepiz's picture

Miauu! It worked just like a

Miauu!

It worked just like a charm. :) your awesome!

Here's another one I'm trying to figure out. You have something similar done in another thread.

http://www.scriptspot.com/forums/3ds-max/general-scripting/assign-materi...

Now that I have a layer with all the objects inside I want to assign a material to that layer.

Example: Layer: AFUPA-3D-TL-GL assign Material "black plastic 135" an so on, I cant seem to get the script to work in my scenario. my object are inside a group can I do this with out opening the group?

miauu's picture

.

I can't tell why the script not works for you, because I don't have the scene that you use.
The material "black plastic 135" - where is it - in the Material Editor, in a .mat file? The script below works with the materials assigned to the material slots in the Material Editor.
There is no need to open the group. You can assign material to selection of objects, like the script below does it.

(
	local layersNameAndMatArr = #(#("AFUPA-3D-TL-GL", meditMaterials[1]), \
		#("*2D-concrete", meditMaterials[2]), \
		#("1D-concrete", meditMaterials[3])	)
 
	for i = 0 to LayerManager.count - 1 do
	(
		local layer = LayerManager.getLayer i
		for j = 1 to layersNameAndMatArr.count do
		(
			if matchPattern layer.name pattern:layersNameAndMatArr[j][1] do
			(
				local theNodes
				layer.nodes &theNodes
				theNodes.material = layersNameAndMatArr[j][2]
			)
		)
	)
)
eyepiz's picture

gotcha!. the script picks up

gotcha!. the script picks up the material in the material editors slot number. i.e, [1] and so on.

Is there a way it could pick up the material by name? My ultimate goal would be to pull it from a ".mat" file in a local directory by material name.

Thanks you so much help! Much appreciated. :)

miauu's picture

.

Load the .mat file from a directory and print all materials. You can see their names. Then add the approriate material, using its name to the desired layer.

Comment viewing options

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