Some stuff regarding classes, i do right now.
Does anybody have some practical use regarding architecture?
I have these small examples:
import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import * #The inputs to this node will be stored as a list in the IN variables.
name = IN[0]
age = IN[1]
class Person:
def init(mysillyobject, name, age):
mysillyobject.name = name
mysillyobject.age = age
def myfunc(abc):
return ("Hello my name is " + abc.name)
p1 = Person(name, age)
OUT = p1.myfunc()
How can I use that for lists? And for what can I use that in my projects?
I really like Coreys tutorials. Here’s one of them on Python classes. https://www.youtube.com/watch?v=ZDa-Z5JzLYM
The short story is - if you have no idea what Python class is and how you could use one - you probably do not need to use them as for now. Learning Python is still the way to go.
A common application of classes is in object-oriented programming (OOP). Much of how you already interact with the Revit API uses similar concepts (methods and properties). See below:
class Person:
def __init__(self, name, age):
self.name = name
self.age = age
def is_older(self, age):
"""Determines whether person is older than a given age.
Args:
age (int): Age to compare against, in years.
Returns:
bool: True if Person is older than the given age.
"""
return self.age > age
names = IN[0]
ages = IN[1]
# Construct our Person objects
people = [Person(name, age) for name, age in zip(names, ages)]
test = []
for p in people:
# Test whether each person is older than 40 years old.
test.append(p.is_older(40))
OUT = test
You need to instantiate the class rather than simply passing the class itself. i.e. OUT should be MyClass() rather than MyClass. Both of the following are valid (although the first is a better approach):