Create materials (from Excel to Revit)

Hi,
I need help with this. I am trying to create a lot of materials in Revit. I just need the name of the material and the color. I have started with a simple case without considering Excel, but it doesn’t work because it only creates the last material of the list. I attach the dynamo file


Following is presented the Python Script used in this case.
import clr
clr.AddReference(“RevitAPI”)
from Autodesk.Revit.DB import *
clr.AddReference(“RevitServices”)
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference(“RevitNodes”)
import Revit
clr.ImportExtensions(Revit.Elements)

def ToRevitColor(dynamoColor):return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)

def ToDynamoObject(revitObject, isRevitOwned=False):return revitObject.ToDSType(isRevitOwned)

doc = DocumentManager.Instance.CurrentDBDocument

names = IN[0]
colors = IN[1]

newMaterials =

for mat_name, color in zip(names, colors):TransactionManager.Instance.EnsureInTransaction(doc)
new_mat_id = Material.Create(doc, mat_name)
new_mat = doc.GetElement(new_mat_id)
new_mat.Color = ToRevitColor(color)
TransactionManager.Instance.TransactionTaskDone()
newMaterials.append(ToDynamoObject(new_mat))
#Asigne la salida a la variable OUT.
OUT = newMaterials

Thank you very much

I believe it’s due to improper formatting.

for mat_name, color in zip(names, colors):
    TransactionManager.Instance.EnsureInTransaction(doc)
    new_mat_id = Material.Create(doc, mat_name)
    new_mat = doc.GetElement(new_mat_id)
    new_mat.Color = ToRevitColor(color)
    TransactionManager.Instance.TransactionTaskDone()
    newMaterials.append(ToDynamoObject(new_mat))

Honestly not sure why it ran in the first place, but moving your transaction to the next line and indenting everything fixes the problem.

Hi Nick_Boyts,
I’ve changed the code because the last one just produces the last material of all the list.
Now I would like to assign the same color to both, the color of the material and the rendering color in order to make easy to identify the materials in the Revit material manager. Do you know how can I do that?. I mean, tho you know how to attach the color to the rendering color?
Thank you so much.
This is the last code:

import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

#Creación de variables de inicio

def ToRevitColor(dynamoColor):return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)
def ToDynamoObject(revitObject, isRevitOwned=False):return revitObject.ToDSType(isRevitOwned)
doc = DocumentManager.Instance.CurrentDBDocument
names = IN[0]
colors = IN[1]
i = IN[2]
newMaterials = []

# -----CREACIÓN DE MATERIALES

while i > 0:
 TransactionManager.Instance.EnsureInTransaction(doc)#Función ionicial

 # Llamada a cada elemento de los vectores (arrays) de names y colors

 mat_name = names[i-1]
 color = colors[i-1]

 # Proceso de generación de materiales

 new_mat_id = Material.Create(doc, mat_name)
 new_mat = doc.GetElement(new_mat_id)
 new_mat.Color = ToRevitColor(color)
 newMaterials.append(ToDynamoObject(new_mat))
 i = i - 1
 TransactionManager.Instance.TransactionTaskDone()#siguiente función

#Extracción de materiales

OUT = newMaterials

Hi Jesus,

Thanks very much for the code (+Nick), it worked nicely. I’m also looking to quickly create materials in a project from excel.

But I would also very much like to set the realistic/rendering material to the same colour, did you make any progress on this?

I’ve noticed that with each newly created material it makes a new generic asset: ‘Generic(x)’ under the appearance tab.

Does anyone know how to modify the material assets post-creation? Or set the parameters of the asset when the material is created? I can’t find any resources Dynamo/Python wise.

Many thanks!