Export by material

Hi All
I’m following the forum from a long time but didn’t write anything because you have been really useful and all my problems have always found a solution.

But now I’m encountering a problem I really can’t find a way to solve.

I have a big model coming from a client and I need to export the model in separate 3DM files for each material. Unfortunately the file have all the (many) materials correctly applied but they do not match with the layer.

So I decided to write a script to do this… welll I’m not so good in that so I tried to copy/paste from other scripts I found but the resul is not good at all.

Can anyone help me on this?

Thanks to all in advance.

I’m sure there are many folks here that could help you, but they will require better details about what you are trying to accomplish and what your approach has been so far.

May I suggest that you export enough of your model to show what you are starting with and post it here along with more details about what you want as a result? It might also help to post your script.

Hi AIW
you’re right maybe you need some more information :slight_smile:

At this link you can find a test scene I’m working on where all the spheres are on layer “Default” but every column have a different material:

What I need is to export a 3DM file for each of the materials (so one file per column)

The script I’m working on is the following (please forgive me for the confusion on it but I’m definitely not a programmer :slight_smile: )

Option Explicit

Call ExportMaterialObjects()

Sub ExportMaterialObjects

' Declare local variables
Dim strPath, strFile
Dim arrMaterials, intMaterial
Dim strMaterial

Rhino.ClearCommandHistory()

' Disable redrawing
Rhino.EnableRedraw False

' Get the path to and name of the current document. Surround with double-quotes in case path includes spaces.
strPath = Chr(34) & Rhino.DocumentPath & Rhino.DocumentName & Chr(34)

' Get names of all materials
arrMaterials = Rhino.MaterialIndices

Rhino.Print Rhino.MaterialCount


If IsArray(arrMaterials) Then

	For Each intMaterial In arrMaterials
				
		strMaterial = Rhino.MaterialName(intMaterial)
		Rhino.Print("strMaterial:" + Rhino.MaterialName(intMaterial))
		
		Rhino.Command "_-SelNone", 0
		Rhino.Command "_-SelMaterialName " & Chr(34) & strMaterial & Chr(34), 0
		

		' Generate a modified path string that includes the material name
		strFile = Replace(strFile, ".3dm", "_" & strMaterial & ".3dm")
		
		'Rhino.Print("strFile:" + strFile)
		
		' Export the selected objects
		Rhino.Command "_-Export " & strFile, 0
		
		' Unselect all
		Rhino.Command "_-SelNone", 0
	Next

End If


' Enable redrawing
Rhino.EnableRedraw True

End Sub