Revit 2022 python code to change all wall colors

Revit 2022 python code to change all wall colors

lemonskade
Explorer Explorer
549 Views
1 Reply
Message 1 of 2

Revit 2022 python code to change all wall colors

lemonskade
Explorer
Explorer

Hi so I'm trying to use python and Dynamo to change all the wall colors to a certain color. I've created the script but am getting an error and not sure why. I'm new to this so any help would be appreciated.

 

import clr
# import ProtoGeometry
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
# import Revit Node
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)
# import Revit Services
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
# import Revit API
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

#Override graphics
def OverrideElement(element, color, fill, fillpatt):
        ogs = OverrideGraphicSettings()
        ogs.SetProjectionLinePatternId(fill.Id)
        ogs.SetProjectionFillColor(color)
        ogs.SetProjectionFillPatternId(fillpatt.Id)
        doc.ActiveView.SetElementOverrides(element.Id, ogs)

#Convert the Dynamo color to Revit color.
def ConvertColor(element):
        return Autodesk.Revit.DB.Color(element.Red, element.Green, element.Blue)


doc = DocumentManager.Instance.CurrentDBDocument

#Collect all the line patterns in the project.
patterns = FilteredElementCollector(doc).OfClass(LinePatternElement).ToElements()

fillPatSelected = list()

#search for line pattern
for i in range(len(patterns)):
    namepick = patterns[i].ToDSType(True)
    if namepick.Name == "Dash":
        fillPatSelected.append(patterns[i])

elements = UnwrapElement(IN[0]) #Element to change
colors = ConvertColor(IN[1]) #Color.ByARGB node from Dynamo
fillPat = UnwrapElement(IN[2]) #Fill Pattern node from Dynamo
count = 0 #count to change color if elements are differnt color.

for i in elements:
    TransactionManager.Instance.EnsureInTransaction(doc)
    #Using def to override graphics.
    OverrideElement(i, colors[count], fillPatSelected[0], fillPat)
    count += 1
    TransactionManager.Instance.TransactionTaskDone()

The error is,

Warning: IronPythonEvaluator.EvaluateIronPythonScript operation failed.
Traceback (most recent call last):
File "<string>", line 50, in <module>
TypeError: iteration over non-sequence of type Wall

Screenshot 2023-07-25 084558.png 

 

0 Likes
550 Views
1 Reply
Reply (1)
Message 2 of 2

jeremy_tammik
Alumni
Alumni

I am not a Python expert, so I cannot help much. However, my guess is that calling UnwrapElement(IN[0]) produces exactly what the subsequent comment says, an "Element to change". Later, in the loop, `for i in elements` requires a sequence of objects to iterate over. Instead, it receives one single Wall element. Hence the error message, 

TypeError: iteration over non-sequence of type Wall. So, the error message already explains exactly what the problem is.

  

Seeing that this message is not yet clear to you, I would recommend investing a bit little bit more time and having a little bit more more fun with a basic Python tutorial, learning about sequences and iteration and stuff, before trying to struggle and suffer with more advanced topics such as Revit API and BIM.

  

Good luck, much success, have fun!

  

Jeremy Tammik Developer Advocacy and Support + The Building Coder + Autodesk Developer Network + ADN Open
0 Likes