Properties from Python

Hi All,

I want to get a Property in the same way that I call a Method, but I don’t know what the syntax is…

E.G. if I wanted to return the category of a Wall…
http://www.revitapidocs.com/2018/ae418d5c-0708-b81c-faf1-e3750ea84b21.htm

Thanks,

Mark

Hi @Mark.Ackerley ,
you can have access to all those propetries, y’oull just need to unwrap the element
image

1 Like

This could be one way:
image

Credit to “Element.Category+” from Clockwork

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

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

def GetCategory(item):
	returnID = item.Category.Id
	try: return Revit.Elements.Category.ById(returnID.IntegerValue)
	except: return None

items = UnwrapElement(IN[0])

if isinstance(IN[0], list): OUT = [GetCategory(x) for x in items]
else: OUT = GetCategory(items)

Fantastic guys, thanks for the speedy response!

Apologies for the follow up dumb question…

Am I reading it right, to get the ‘name’ Johnathan is returning the Category ID?

As a beginner, how the heck am I supposed to work that out for myself? :smiley: Is there a tutorial anywhere that you can recommend? I’ve done the Lynda Python course…

Thanks,

Mark

I’ve learned most of my Python from the forum and some of the great packages out there (clockwork and DanEDU (now Orchid) especially have been great for me.

But this material is definitely also nice to have:

2 Likes

Thanks,

I just seem to hit a wall at every turn… For example…

import clr

#Import Revit Nodes
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

viewSchedule = UnwrapElement(IN[0])
definition = viewSchedule.Definition
param = definition.GetSchedulableFields()
params = param.GetName()

OUT = param

fails…
image

But the docs say that a ScheduleableField does have the method GetName

http://www.revitapidocs.com/2018/8c866024-8d20-8446-502c-a06057bf4185.htm

Any suggestions why? :slight_smile:

Mark

No idea as to why, but out of curiosity why the desire to use Python here? Over use of this method can cause significant management issues as you’ll have to update every graph in your library one by one should the syntax, functions or names change.

1 Like

You are trying to apply a method to a list of elements as a whole, instead you must apply it to each single element in that list
(http://www.revitapidocs.com/2018.1/66f9648b-dd64-6ff5-ef89-4f1d6b5c4a23.htm
GetSchedulableFields() returns a list of scheduable fields)
When I’m coding in python I often print out some variables to see what is happening:


in this case definition.GetSchedulableFields() gives me many items, so I know I must change the code a bit:

import clr

#Import Revit Nodes
clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

# Import RevitAPI
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import *

# Import DocumentManager and TransactionManager
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

#document stuff:
doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application

viewSchedule = UnwrapElement(IN[0])
definition = viewSchedule.Definition
param = definition.GetSchedulableFields()
#I create a new empty list:
newlist=[]
#i use a for loop to "do things" on each element of the list "param"
for p in param:
name=p.GetName(doc) #getName takes the document as an argument
newlist.append(name)

OUT = newlist

image

I don’t know if this is what you were looking for but I hope it’s a start

2 Likes

@jacob.small ,

All things strive :slight_smile:

My prompt for this was not being able to find a node which created sheet lists, from there I made a massive graph for something which should be doable in a few lines of code. So I hoped that I could make a simple script and learn a bit more python while doing so.

Apologies if this clogs up the forum, I would prefer to learn entirely from tutorials and the like, unfortunately (perhaps because I’m not that bright) I keep running out of references. I’ll go back and do some more reading before asking any more questions

@GregX thanks, I’ve been returning variables as i go, but i tidied up the code to show around,Thank you for showing the doc, that makes a lot more sense now :slight_smile:

1 Like

Great tutorials from @Jeremy_Graham https://www.lynda.com/Dynamo-Studio-tutorials/Dynamo-Revit-Python-Scripting/647661-2.html

and link to the topic in the forum - Python Scripting Video Course there you can find another great resources with links to them.

1 Like

Yeah, they are really good, I guess I’m just dumb, I’ll go watch them again!

Practice is the key! I am sure you will start to put everything together after a while = )
Cheers!

1 Like