Python Methods Syntaxes

Hello, everybody,
I am new to python scripting in dynamo. I want to ask a question that may be ridiculous.
My question is: when I want to use a method from Revit API, how can I know its syntax to write in the script? I believe the answer is clear but I cannot find it out.
Thanks.

From the absolute basics, it depends on whether the class is static or non-static and the same again based on its members. For example a class can be non-static but have static methods or properties. How you make a call to a static or non-static class or member is different conceptually, but the syntax is the same in some respect - using the dot operator - which makes the learning curve low.

Enums are used widely in the Revit API and are called in a similar fashion to static classes (just remember an enum is not a class).

So to know what the correct syntax is you first need to know whether the member you want to call is static or non-static or a type such as an enum, and Revit API docs is a good place to start. Then its easy to get it right each time without trial and error.

1 Like

@Thomas_Mahon Thanks very much for your reply. I have understood the concept but I still get lost. For example, if I want to get the origin of a sheet, when I use the Revit API docs, it states that the origin under the viewsheet class is a public property. Now, how can I get the syntax to use the property?

One of the biggest things to get used to is that Python doesn’t require the declaration of a variable. The variable is created as soon as you assign something to it and is smart enough to know that it’s the object type being assigned to it. This streamlines a lot of code when translating from something like C or VB to Python.

2 Likes

You need an instance of that object and then you can call any method or get any property from it.

Methods have brackets () and often, but not always have arguments, inputs if you will, that need to be passed in. For example the ViewSheet.Create(Document, ElementId) method requires you to pass in instances of both Document and rhe ElementId if the TitleBlock Type, whereas, ViewSheet.CanBeLocked() doesn’t require anything to be passed in, but is still a method.

Methods also can, but don’t always return anything. If the Method has void preceding its method name…

public void MyVoidMethod()

then this does not return anything and you cannot do something like…

myMethodReturnValue = myObject.MyVoidMethod()

instead you would simply call…

myObject.MyVoidMethod()

If a Method does return an object, then the method will explicitely say this…

public XYZ SomeMethodThatReturnsAPoint()

Properties on the otherhand do not need brackets and can be accessed simply by having an instance of that type (say an instance of a ViewSheet) and then you can write…

origin = viewSheet.Origin

Remember though, when using the Revit API you will get back a Revit API Type, and if you want to use it in Dynamo Nodes, then you have to convert it to DS Type.

Here is some example code…

import clr

clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
doc =  DocumentManager.Instance.CurrentDBDocument

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

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

def to_list(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]
	
	
viewSheets = to_list(UnwrapElement(IN[0]))

if IN[0] == None:
	viewSheets = FilteredElementCollector(doc).OfClass(ViewSheet).WhereElementIsNotElementType().ToElements()
	

OUT = [vs.Origin.ToPoint() for vs in viewSheets]

Note how we need to unwrap any revit elements coming into the node, and we are using the .ToPoint() method to convert from Autodesk.Revit.DB.XYZ to Autodesk.DesignScript.Geometry.Point.

Hope that sheds some more light on methods/properties.

2 Likes

Thanks, @Nick but I don’t have problems with variables, I just was asking how to know the syntax of the methods and properties in the Revit API dealing with the objects.

Thanks, @Daniel_Woodcock1 . You made it very clear and I really appreciate your long detailed answer. I am starting to realize that it makes sense. I am making your answer as a solution.

1 Like

Gotcha. I was just pointing out that the syntax shown in RevitAPIDocs for other languages is essentially the same for Python, just without the need for declarations.
image

1 Like

Aaah. Do all the syntaxes follow the same rule? I mean, I have found some methods that are strange for me to get their syntaxes. Especially, those are not “Create” ones.

This for example.

image

What you are seeing here is a method that returns an ICollection of type ElementId. C# has something called generics which uses another thing called interfaces (which is a much longer story but interfaces almost always begin with the letter I, say IList or ICollection), but essentially this is just a generic list of ElementId. The angle brackets contain the Type that the list is a list of, say IList<FamilyInstance> would be a List of FamilyInstances. This would translate into a python array where all the elements in that array are FamilyInstances.

2 Likes

The magenta is the method or property. The greenish is the object class being returned or used as an argument.

In your example, GetFilters() is a method with no arguments and returns an ICollection (special list) of ElementIds.

Edit: What @Daniel_Woodcock1 said. :point_up:

2 Likes

That’s great, @Nick_Boyts and @Daniel_Woodcock1 .
I am very grateful.
A big thank you.

1 Like

The correct answer is already been given. But just to add an extra option, or perhaps a head start.

While learning Python with focus on Dynamo. And finding my way through the RevitAPI. I created some API helper nodes for direct use in Dynamo. I still like it and use it. Although I use the APIDocs more and more often. (These API helper Nodes have limits while building specific things.)
You can check the IkLeerBIM package with 3 API helper nodes. And there are some examples of use in the extra dir. And on my blog is also some explanation. http://ikleerbim.blogspot.com/search/label/Package

3 Likes

Hello, @T_V_werk,
I have visited your blog and gotten your idea. In fact, it is really fantastic. Especially for Revit users rather than professional programmers.
Surely, I will take advantage of it. I am installing your package right now.
Thanks and keep the good work :))

1 Like