import sys
import clr
# Import Revit API
clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
# Import Revit Services (for Dynamo)
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# Import DesignScript Geometry (for Dynamo)
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
def substitute_material_appearance(element, source_material_name, target_material_name):
# Get Revit document
doc = DocumentManager.Instance.CurrentDBDocument
# Get the source and target materials
materials = FilteredElementCollector(doc).OfClass(Material)
source_material = None
target_material = None
for material in materials:
if material.Name == source_material_name:
source_material = material
if material.Name == target_material_name:
target_material = material
if source_material and target_material:
break
if source_material is None or target_material is None:
missing_materials = []
if source_material is None:
missing_materials.append(f"Source material '{source_material_name}' not found.")
if target_material is None:
missing_materials.append(f"Target material '{target_material_name}' not found.")
raise Exception("\n".join(missing_materials))
# Substitute appearance settings from source_material to target_material
source_appearance_asset_id = source_material.AppearanceAssetId
source_appearance_asset_elem = doc.GetElement(source_appearance_asset_id)
# Start a transaction (required for modifying the document)
TransactionManager.Instance.EnsureInTransaction(doc)
# Assign the source material's appearance asset to the target material
target_material.AppearanceAssetId = source_appearance_asset_id
# Commit the transaction
TransactionManager.Instance.TransactionTaskDone()
return "Material appearance substituted successfully."
# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN
if len(dataEnteringNode) < 3:
OUT = "Error: Please provide an element and both source and target material names as inputs."
else:
element = UnwrapElement(IN[0]) # Unwrap the element to access the Revit element
source_material_name = IN[1]
target_material_name = IN[2]
try:
result = substitute_material_appearance(element, source_material_name, target_material_name)
OUT = result
except Exception as e:
OUT = str(e)
You’re not declaring your variables. The first line, dataEnteringNode = IN
, is telling you that inputs for the node are defined by IN
. A specific input is then defined by the port it’s using, i.e. IN[0]
, IN[1]
, IN[2]
.