# Modifying material id for DirectShape/Python

**URL:** <https://forum.dynamobim.com/t/modifying-material-id-for-directshape-python/104740>\
**Category:** Developers\
**Tags:** revit, dynamo\
**Created:** [September 29, 2024, 11:20am UTC](https://forum.dynamobim.com/t/modifying-material-id-for-directshape-python/104740 "2024-09-29T11:20:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![desmond679679](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@desmond679679](https://forum.dynamobim.com/u/desmond679679)\
**Post date:** [September 29, 2024, 11:20am UTC](https://forum.dynamobim.com/t/modifying-material-id-for-directshape-python/104740/1 "2024-09-29T11:20:40Z")

</div>

Hi All,

A colleague of mine has imported 3D shapes (Brep as known by Dynamo) and I wish to modify the material on the surface. However, I have no luck in doing so. Hoping someone can help me with this.

ps revit’s paint option is out of the box as there are many elements in different locations.

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/3/0395cda7b767217a32568f978c3d75a798e29d53.png)

`import clr  
clr.AddReference(‘RevitAPI’)  
clr.AddReference(‘RevitServices’)

from Autodesk.Revit.DB import FilteredElementCollector, ElementWorksetFilter, Options, Solid, OverrideGraphicSettings  
from RevitServices.Persistence import DocumentManager  
from RevitServices.Transactions import TransactionManager

# Input: Workset ID and Material ID

workset\_id = IN[0]  
material\_id = IN[1]

# Get the current document

doc = DocumentManager.Instance.CurrentDBDocument

# Create a filter for the workset

workset\_filter = ElementWorksetFilter(workset\_id)

# Collect all elements in the workset

elements\_in\_workset = FilteredElementCollector(doc).WherePasses(workset\_filter).WhereElementIsNotElementType().ToElements()

def GetFaces(direct\_shape):  
“”“Get all faces from a DirectShape element.”“”  
faces =   
geometry = direct\_shape.get\_Geometry(Options())  
if geometry:  
for geom in geometry:  
if isinstance(geom, Solid):  
for face in geom.Faces:  
faces.append(face)  
return faces

def SetMaterialOverride(element, material\_id):  
“”“Set the material override for an element.”“”  
ogs = OverrideGraphicSettings()  
ogs.SetSurfaceMaterialId(material\_id)

```
TransactionManager.Instance.EnsureInTransaction(doc)
doc.ActiveView.SetElementOverrides(element.Id, ogs)
TransactionManager.Instance.TransactionTaskDone()

```

# Initialize lists to store DirectShapes and their faces

direct\_shapes =   
faces\_list =

# Filter DirectShape elements and get their faces

for element in elements\_in\_workset:  
if element.Name == ‘DirectShape’:  
direct\_shapes.append(element)  
faces\_list.append(GetFaces(element))

# Set material override for each DirectShape element

for element in direct\_shapes:  
SetMaterialOverride(element, material\_id)

# Output the number of faces

OUT = len(faces\_list)  
`

---

<div class="post-metadata">

**Author:** ![Draxl\_Andreas](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/draxl_andreas/32/145744_2.png) [@Draxl\_Andreas](https://forum.dynamobim.com/u/Draxl_Andreas)\
**Post date:** [September 29, 2024, 2:31pm UTC](https://forum.dynamobim.com/t/modifying-material-id-for-directshape-python/104740/2 "2024-09-29T14:31:54Z")

</div>

```py
import clr
clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import FilteredElementCollector, ElementWorksetFilter, Options, Solid, OverrideGraphicSettings
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

# Input: Workset ID and Material ID
workset_id = IN[0]
material_id = IN[1]

# Get the current document
doc = DocumentManager.Instance.CurrentDBDocument

# Create a filter for the workset
workset_filter = ElementWorksetFilter(workset_id)

# Collect all elements in the workset
elements_in_workset = FilteredElementCollector(doc).WherePasses(workset_filter).WhereElementIsNotElementType().ToElements()

def GetFaces(direct_shape):
"""Get all faces from a DirectShape element."""
    faces = []
    geometry = direct_shape.get_Geometry(Options())
    if geometry:
        for geom in geometry:
            if isinstance(geom, Solid):
                 for face in geom.Faces:
                    faces.append(face)
    return faces

def SetMaterialOverride(element, material_id):
"""Set the material override for an element."""
    ogs = OverrideGraphicSettings()
    ogs.SetSurfaceMaterialId(material_id)

TransactionManager.Instance.EnsureInTransaction(doc)
doc.ActiveView.SetElementOverrides(element.Id, ogs)
TransactionManager.Instance.TransactionTaskDone()
# Initialize lists to store DirectShapes and their faces
direct_shapes = []
faces_list = []

# Filter DirectShape elements and get their faces
for element in elements_in_workset:
    if element.Name == "DirectShape":
        direct_shapes.append(element)
        faces_list.append(GetFaces(element))

# Set material override for each DirectShape element
for element in direct_shapes:
    SetMaterialOverride(element, material_id)

# Output the number of faces
OUT = len(faces_list)

```

paste your code via \</\>

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [September 30, 2024, 12:48am UTC](https://forum.dynamobim.com/t/modifying-material-id-for-directshape-python/104740/3 "2024-09-30T00:48:22Z")

</div>

Hi Desmond, welcome to the forum 👋  
Your code is trying to do two things which are not connected - override an elements graphic settings in a view and change the material painted on the geometry faces

Based on your use case painting the faces is probably the best solution. I haven’t tried the code but it might look something like this

```python
import clr

clr.AddReference("RevitAPI")
clr.AddReference("RevitServices")

from Autodesk.Revit.DB import (
    ElementWorksetFilter,
    FilteredElementCollector,
    Options,
    Solid,
    ViewDetailLevel,
)
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

def GetFaces(direct_shape):
    """Get all faces from a DirectShape element."""
    options = Options()
    options.DetailLevel = ViewDetailLevel.Fine
    faces = []
    geometry = direct_shape.get_Geometry(options)
    if geometry:
        for geom in geometry:
            if isinstance(geom, Solid):
                for face in geom.Faces:
                    faces.append(face)
    return faces

# Input: Workset ID and Material ID
workset_id = IN[0]
material_id = IN[1]

# Get the current document
doc = DocumentManager.Instance.CurrentDBDocument

# Create a filter for the workset
workset_filter = ElementWorksetFilter(workset_id)

# Collect all elements in the workset
elements_in_workset = (
    FilteredElementCollector(doc)
    .WherePasses(workset_filter)
    .WhereElementIsNotElementType()
    .ToElements()
)

# Initialize lists to store DirectShapes and their faces
faces_list = []

TransactionManager.Instance.EnsureInTransaction(doc)
for element in elements_in_workset:
    if element.Name == "DirectShape":
        faces = GetFaces(element)
        for face in faces:
            doc.Paint(element.Id, face, material_id)
            faces_list.append(face)
TransactionManager.Instance.TransactionTaskDone()

# Output the number of faces
OUT = len(faces_list)

```

---

<div class="post-metadata">

**Author:** ![desmond679679](https://avatars.discourse-cdn.com/v4/letter/d/e95f7d/32.png) [@desmond679679](https://forum.dynamobim.com/u/desmond679679)\
**Post date:** [September 30, 2024, 9:06am UTC](https://forum.dynamobim.com/t/modifying-material-id-for-directshape-python/104740/4 "2024-09-30T09:06:36Z")

</div>

thanks, you are a legend

---

<div class="post-metadata">

**Author:** ![c.poupin](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/c.poupin/32/89152_2.png) [@c.poupin](https://forum.dynamobim.com/u/c.poupin)\
**Post date:** [September 30, 2024, 10:45am UTC](https://forum.dynamobim.com/t/modifying-material-id-for-directshape-python/104740/5 "2024-09-30T10:45:52Z")

</div>

Hi @desmond679679 and welcome

For your information, here are the forum rules

([How to get help on the Dynamo forums](https://forum.dynamobim.com/t/how-to-get-help-on-the-dynamo-forums/4677)) and the community guidelines ([https://forum.dynamobim.com/faq](https://forum.dynamobim.com/faq))

Note  
_remember to specify if it’s AI-generated code in your next posts ([see point 8](https://forum.dynamobim.com/t/how-to-get-help-on-the-dynamo-forums/4677#h-8-honor-other-peoples-work-8) )_
