You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python 2.7转Python 3适配Dynamo for Revit 2022时遇TypeError问题求助

Fixing 'instance property must be accessed through a class instance' Error in Python 3 for Dynamo Revit

Let's break down what's causing this error and how to fix it quickly.

The Root Cause

In Python 2.7 with the Revit API, you could retrieve element properties using .GetValue() on the class-level property info object (like Element.Name.GetValue(itm)). But in Python 3 (especially with the updated .NET integration in Dynamo for Revit 2022), this approach no longer works—.NET properties are accessed directly via the element instance, not through the class's property metadata.

The Simple Fix

Replace the problematic line:

Typenaam.append(Element.Name.GetValue(itm))

With this direct property access:

Typenaam.append(itm.Name)

This is the standard way to access properties on Revit element instances in Python 3, aligning with modern .NET API usage patterns.

Modified Full Code

Here's your updated script with the fix applied, plus a small clarity improvement for dictionary initialization:

def Typenaam_Family(itm):
    Typenaam = []
    Collector_Element_And_Typename = []
    Bcat = itm[0]
    Dict_Element_And_Typename = itm[1]
    Collector = FilteredElementCollector(doc).OfCategory(Bcat).WhereElementIsElementType().ToElements()
    for itm in Collector:
        # Directly access the Name property of the element instance
        Typenaam.append(itm.Name)
    Collector_Element_And_Typename.extend(list(zip(Typenaam,Collector)))
    for itm in Collector_Element_And_Typename:
        Dict_Element_And_Typename[itm[0]] = itm[1]

# Initialize dictionaries explicitly (ensure these exist before use)
Dict_Door_Element_And_Typename = {}
Dict_Plumbing_Element_And_Typename = {}
Dict_Wall_Element_And_Typename = {}
Dict_Floor_Element_And_Typename = {}
Dict_Kolommen_Liggers_Element_And_Typename = {}

Benodigde_Categorys = [[BuiltInCategory.OST_Doors,Dict_Door_Element_And_Typename],
[BuiltInCategory.OST_PlumbingFixtures,Dict_Plumbing_Element_And_Typename],
[BuiltInCategory.OST_Walls,Dict_Wall_Element_And_Typename],
[BuiltInCategory.OST_Floors,Dict_Floor_Element_And_Typename],
[BuiltInCategory.OST_StructuralColumns,Dict_Kolommen_Liggers_Element_And_Typename],
[BuiltInCategory.OST_StructuralFraming,Dict_Kolommen_Liggers_Element_And_Typename]]

for itm in Benodigde_Categorys:
    Typenaam_Family(itm)

Quick Side Note

I added explicit dictionary initializations at the top—this ensures you won't hit a NameError if these weren't defined elsewhere in your original script. It's a small touch to make the code more self-contained.

Content of the question comes from Stack Exchange, asked by Wouter

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 17:04:09