How to use magic methods and Attribudetes?

Hello,

I try to learn stuff regarding python, is there any usage for “magic methods and attributes” ?

import clr

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Structure import *

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit
clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument
uidoc=DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument


wall = UnwrapElement(IN[0])
nam = IN[1]

def __getattr__(self, name):
	return self.AsValueString(name)
	
	
OUT = __getattr__(wall,nam)

can i call a name of wall with that? how can i use this kind of magic ?

KR

Andreas

This method __getattr__ will allow you to “catch” references to attributes that don’t exist in your object, and/or it can also be used to generate an attribute dynamically

In all cases the object must be constructed from a Python class, (not Net Class)

however, you can use the getattr() method to set the default value if the attribute no exist

import sys
import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

pt = Point.ByCoordinates(4,5,0)

# example 1 
# try to get 'XY' attribute, 
# if attribute no exist return a default value 'None' instead of an error
a = getattr(pt, "XY", None)
# example 2 
# try to get 'XYZ' attribute, 
# if attribute no exist return each coordinates instead of an error
b = getattr(pt, "XYZ", (pt.X, pt.Y, pt.Z))

OUT = a, b
1 Like

Hello @c.poupin ,

that means all my elements in Revit are .Net elements?

this kind methods want work on relular categories and instances…

Even when they are expoxed to the UnwrapedElement list…?

KR

Andreas

yes

UnwrapedElement() allows finding the native Revit API objects (that DynamoAPI has wrapped beforehand)

if you want use __getattr__ you need to build your own class

1 Like