Can't change material. Object refference not set to instance of object


Trying to simply change material of few elements. Why isn’t it working?

EDIT:
Structural column category

Can you select one column instance and change that parameter in Revit? Or is it a Type parameter?

SetMaterialStructureColumns

'''
Code created by Eden BIM API Solution.
https://www.facebook.com/EdenBIMapiSolution
'''

import clr

# Import Revit API
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import BuiltInParameter

# Import DocumentManager and TransactionManager
clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

# If the input is a single column, wrap it into a list; otherwise, treat it as a list
columns = [UnwrapElement(IN[0])] if not isinstance(IN[0], list) else [UnwrapElement(c) for c in IN[0]]
input_material = UnwrapElement(IN[1])

# Ensure inputs are provided
if not columns or not input_material:
    OUT = "Both columns and material must be provided as inputs."      

# Begin transaction
TransactionManager.Instance.EnsureInTransaction(doc)

for column in columns:
    # Check if the element is a structural column
    if column.Category.Name != "Structural Columns":
        continue  # Skip if not a structural column

    # Get the material parameter for the column
    material_param = column.get_Parameter(BuiltInParameter.STRUCTURAL_MATERIAL_PARAM)
    
    # If the column has a material parameter, set it
    if material_param:
        material_param.Set(input_material.Id)

# End transaction
TransactionManager.Instance.TransactionTaskDone()

OUT = "Material set for all valid columns in the input."

The Python script interfaces with Autodesk Revit via its API to adjust the material of structural columns. Initially, the script imports the necessary Revit and Dynamo libraries and establishes a connection to the current Revit document. To cater to both individual and multiple column inputs, the script checks whether the provided input is a list or a single column, and then processes it accordingly.

For each column, the code first verifies if the element is indeed categorized as a “Structural Column” in Revit. If it is, the script fetches the material parameter of the column. In the case where the column has this material parameter, the script assigns the provided material to the column. All these modifications are made within a transaction to ensure data integrity and consistency within Revit. Finally, the script provides feedback, indicating the successful assignment of the material to the valid columns in the input.

2 Likes