How can I get material IDs?

Trying to get a list of material Ids so I can call GetMaterialVolume() but can’t get the ElementId objects. I’ve tried the instance method and static (might not be a real method) method. The first returns an empty list, the second says there’s no method matching the arguments.

— Static method attempt —
Code

Autodesk.Revit.DB.Element.GetMaterialIds(False)

Error: No method matches given arguments for GetMaterialIds.

The API doc: ApiDocs.co

— Instance method attempt —

UnwrapElement(wall).GetMaterialId(True) #wall is a Wall object

Returns

Edit: I managed to get two ElementIds by checking all the walls, was expecting more. But, the returned class ElementId is not a MaterialId so Autodesk.Revit.Element.GetMaterialVolume(<id>) doesn’t work.

Thanks

Sounds like there is something else at play as MaterialId isn’t a class in the API, at least not which I can find. Post your full code and a small sample model and the community can have a look.

Yeah, that’s on me. The GetMaterialVolume method takes an ElementIi. The error message is still the one in the original post.

That’s all the code. The error is the same.

Updated the original post.

Thanks

So you are trying to call the method GetMaterialVolume on the class Autodesk.Revit.DB.Elements instead of an element itself? That won’t work as you’re referring to the class rather than the element which has the material assigned to it.

First you want to get an element - in your case this is the UnwrapElement(wall) bit as you’re transferring data from the Dynamo environment to the Python environment. Set this as a variable as you’ll want to use this a few times, something like elem = UnwrapElement(IN[0]).

Next we’ll pull the SOLID materials from the element using the method GetMaterialIds on the element stored in the variable - something like solidMaterialIds = elem.GetMaterialIds(False). The False override on this method means we’re not going to pull the materials assigned via the geometry or via the compound layer structure, but not pull materials assigned via the Paint tool.

From there we should have a list of materials IDs, so we can’t just pull the data from them but will have to iterate over the values. For this I prefer list comprehension in Python, but you could use a for loop too. The GetMaterialVolume method once again applies to an Element class object, so we’ll go back to the wall: elem.GetMaterialVolume(materialId), where materialId is the objects contained in the solidMaterialIds list. Applying full list comprehension we should get something like this: materialVolumes = [ elem.GetMaterialVolume(matId) for matId in solidMaterialIds ]

Considering what you’re after, you’ll likely want the material names or material elements as well.

solidMaterialElems = [ doc.GetElement(matId) for matId in solidMaterialIds ]
solidMaterialNames = [ mat.Name for mat in solidMaterialElems ]

And now we can send the element, name, volume lists back to the Dynamo environment.

OUT = zip(solidMaterialElems, solidMaterialNames, materialVolumes)

6 Likes

Thanks Jacob. Can’t stress enough how much you’ve helped. This worked perfectly.

Edit: added a credit to the source, if that counts for anything.

1 Like