Set Material Parameters from Python Script

Hi everyone, I’m a newcomer to the Bim/Revit/Dynamo world, so please excuse my ingenuity beforehand.
My new task is to prepare about 5000 materials, which are pretty similar one to another.
After a few days creating everything manually, decided give Dynamo a chance to do that for me.
Did my (quick) research on internets, found the script for creating materials with python, modified it to suit my needs.
It creates the materials in revit, but I can’t find the way to set material properties from within it,
gives me following error "TypeError: 'ParameterSet' object is unsubscriptable"

Here’s the script:

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
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 ToDynamoObject(revitObject, isRevitOwned=False): 
     # isRevitOwned should be False if Dynamo script created the object.
     return revitObject.ToDSType(isRevitOwned)



doc = DocumentManager.Instance.CurrentDBDocument
#Las entradas de este nodo se almacenan como lista en las variables IN.
dataEnteringNode = IN

#Asigne la salida a la variable OUT.
OUT = 0
newMaterials = []

for mat_list in dataEnteringNode:
    for id, fmt, gr, din, mat_name, aspect_name, comments, url in mat_list:
        TransactionManager.Instance.EnsureInTransaction(doc)
        mat_name_r = mat_name.replace('"','')
        mat_name_t = mat_name_r.strip()
        new_mat_id = Material.Create(doc, mat_name_t)
        new_mat = doc.GetElement(new_mat_id)
        #new_mat.MaterialClass("Wood")
        #new_mat.Transparency = transparency
        #new_mat.URL = url
        new_mat.Parameters['URL'] =  url
        #elem = UnwrapElement(new_mat)
        #elem.Parameters['URL'] = url
        TransactionManager.Instance.TransactionTaskDone()
        #newMaterials.append(ToDynamoObject(new_mat))
        newMaterials.append(new_mat)
        #newMaterials.append(ToDynamoObject(new_mat.Properties))        
OUT = newMaterials

As you can see, I’ve tried a few possibilities, but none of them worked.

Or setting material properties member is similar to following snippet (found here: modify-the-material-thermalconductivity-parameter) :

thermalAssetID = material.ThermalAssetId
propertySetElement = doc.GetElement(thermalAssetID)
thermalass = propertySetElement.GetThermalAsset()
thermalass.ThermalConductivity = thermalcond 

Any link (to) manual, tutorial or a few lines of code will by highly appreciated.

What Revit version are you in?

Did you also see this thread?

It’s Revit 2015

I was able to access the parameters with:

for param in new_mat.Parameters:
    newMaterials.append(param.URL)
    #or
    newMaterials.append(param['URL']) 

But

'Parameter' object has no attribute 'URL' or
TypeError: 'Parameter' object is unsubscriptable

happens. The Revit 2015 API does not enumerate parameter names :confused:

It was one of the firsts post I followed,
the thing is I would like to set the properties in the Identity tab first (Descriptive Information and Product Information), .
From the Graphics Tab check the ‘use render appereance’ checkbox,
the Appereance Tab name and comments , the image can be/should be managed manually.

Still not sure what exactly parameters you’re trying to manage with dynamo, but there are a bunch of new API functions for materials in Revit 2018.1.

May be that you need to upgrade a bit to accomplish this.

You’re right, I didn’t explain myself clearly.

I’d like to set these with PythonScript:

Saw one user setting them with Element.SetElementByName here:

(how-to-set-the-material-parameters-class-and-keywords)

and I thought it will be possible with python.

Identity tab stuff is doable. Well maybe not all of it but I know some of it is as that was my first ever script.

Element.setparameterbyname is all you need so long as you feed the right elements (materials) and value types.

The appearance tab you can’t touch unless you are in Revit 2018.1.

Trying to break through some mysteries here, I discovered some interesting things. I am on R2017 at the moment:

  • First, the color of a material (the one from the Graphics tab) can be set with a simple Element.SetParameterByName node. The awaited value is an integer but I don’t know how this number is defined. I have been able to set a value from 0 up to 16777215.
  • Second, the whole Appearance tab can be set by switching the material asset element with Material.SetMaterialAssetByAssetId or Material.SetMaterialAssetByMaterial from Archi-lab_Grimshaw, but I assume that this has already been exposed somewhere.

It’s based on the RGB values; 256 options per variable.
image

r = dynColor.Red;
g = dynColor.Green;
b = dynColor.Blue;

integer = r+256*g+65536*b;

Any link to an additional resource about that, by chance?

Afraid not. It was just from experimenting with integers and the resulting RGB values.
Here’s how it breaks down:

Red takes up integers 0-255. Makes sense.
Green starts at 256 but begins counting with the Red values. So 256 = [R=0,G=1,B=0] and 257 = [R=1,G=1,B=0] and so on.
Blue starts at 65536 and follows the same pattern of counting the Red value first, followed by Green, then finally Blue.

I think springnodes has a custom node for this.

1 Like

using element unique id, you can set all parameter of materials

Were you able to complete this script? I am trying to accomplish something similar where our material database is in excel!

Sorry for late response, but I’ve abandoned the idea of doing this with python and switched to c#. With VS you can connect directly to RevitSDK and contextual help there works very well.

1 Like

Hello,

I have created few project parameters by “Parameter.CreateProjectParameter” for the category “Materials”. now I’m trying to set these parameter values and I cant achieve it through “Element.SetPararmeterByName”.

You’re feeding the parameter a string, and it wants something else (not sure what as I don’t see where you made the parameter here). Try a double or integer, if that fails start a new topic and include your graph and a sample rvt file.

Thanks Jacob. You were right. I was setting these new parameters type to “Number” and that’s why it was not taking input of a “String”. Now I have the parameter type to “Text” and it takes input from both numbers and strings.

Thanks a lot.

1 Like

Is there a way to set multiple parameters of multiple elements?

Just Like this but with multiple elements