Looping RGB Values from csv file to Create Materials

python
dynamo

#1

rgb.txt (107 Bytes)

i have a txt file with about 1700 + material names and RBG values ( ive attached a small sample)


#2

Haven’t seen the script but have you tried turning it into a custom node and then plugging the list into it afterwards? Custom nodes tend to do repeated things that normally only work for 1 item.


#3

    import clr
        clr.AddReference
        from Autodesk.Revit.DB import *
        clr.AddReference
        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

        newMaterials = []

        TransactionManager.Instance.EnsureInTransaction(doc)

        mat_name = IN[0]
        color = IN[1]
        transparency = IN[2]

        for i in mat_name:

        new_mat_id = Material.Create(doc, mat_name)
        new_mat = doc.GetElement(new_mat_id)
        new_mat.Color = ToRevitColor(color)
        new_mat.SurfacePatternColor = ToRevitColor(color)
        new_mat.CutPatternColor = ToRevitColor(color)
        new_mat.Transparency = transparency
        newMaterials.append(ToDynamoObject(new_mat))

        TransactionManager.Instance.TransactionTaskDone()

        OUT = newMaterials

#4

Tip for taking pictures of graphs, if you zoom in until you can fully read a single node, then click the camera button, it will take a picture of the entire graph at that zoom level, you don’t need to worry about zooming out to see everything.


#5

Your inputs for the ARGB node should be numbers [int] not strings


thats the first error solved
the python script is another thing


#6

You can use the zip function in the python 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)


def ToRevitColor(dynamoColor):return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)
def ToDynamoObject(revitObject, isRevitOwned=False):return revitObject.ToDSType(isRevitOwned)
doc = DocumentManager.Instance.CurrentDBDocument

newMaterials = []

TransactionManager.Instance.EnsureInTransaction(doc)

mat_name = IN[0]
color = IN[1]
transparency = IN[2]

for i,y in zip (mat_name, color):
	new_mat_id = Material.Create(doc, i)
	new_mat = doc.GetElement(new_mat_id)
	new_mat.SurfacePatternColor = ToRevitColor(y)
	new_mat.CutPatternColor = ToRevitColor(y)
	new_mat.Color = ToRevitColor(y)
	new_mat.Transparency = transparency
	newMaterials.append(ToDynamoObject(new_mat))

TransactionManager.Instance.TransactionTaskDone()

OUT = newMaterials

Create RGB Materials2.dyn (15.8 KB)


#7

Hi Alban,

Very interesting :slight_smile: if you wouldn’t mind humouring me…

Say we wanted to make 3 black materials, 2 red, 4 orange… is it possible to do a version of the zip which has 2 different length lists? Perhaps with a range? Or do we always need to match the number of colours to the number of names?

Thanks,

Mark


#8

When using the zip() function, you need lists of equal lengths, like when you use list.transpose :slightly_smiling_face:


#9

It changes the color in the Graphics but not in the appearance tab.


#10

First, you can thank for the help already provided.
Second, you can look at yourself in the API.
Third, you can create an appareance asset from an existing rendering asset, duplicate an asset, and set it to a material.
AppearanceAssetElement Class


#11

Yea i looked at the API. but is there a way to access the Image (Texture) of the appearance asset. I Created Jpeg of those textures in Python itself using the PIL library so maybe i can assign the texture directly?.


#12

I forgot to mention it but I created all the nodes needed for the workflow a fews weeks ago.


#13

Hmm… I just saw this video, it seesm to be possible But it would only work with Revit 2018.1. i have to Update Revit and Try tomorrow.