FamilyType.ByName in Python

I’m probably going to be really embarrassed after someone answers… but it’s Friday and I’m thinking of the weekend…

I’m trying to recreate the Dynamo in the blue box using Python.

Are you trying to use the Revit API directly (returning the native API object) or the Dynamo node (returning the Dynamo wrapped object)?

I’m after the Revit object.

So you want to use the Revit API in a Python way, not the Dynamo node in a Python way?

Do I? :person_shrugging:
Whoosh

I’ve done it like this… but was wondering how to get rid of the two nodes feeding into IN[2]

This will do the selection for you:

### standard imports ###
import clr #add the CLR (common language runtime) class to the Python environment so we can work with .net libraries

### Dynamo Revit imports ###
clr.AddReference("RevitNodes") #add Dynamo's Revit nodes library to the clr
import Revit #import Dynamo's Revit node class
clr.AddReference("RevitServices") #add the Revit services library to the CLR
import RevitServices #import the Revit services class to the Python environment
from RevitServices.Persistence import DocumentManager #import the document manager class to the Python environment 

### Revit API imports ###
clr.AddReference("RevitAPI") #add the Revit API to the CLR
import Autodesk #add the Autodesk class to the Python environment 
from Autodesk.Revit.DB import * #import every class of the Revit API to the Python environment

doc = DocumentManager.Instance.CurrentDBDocument #the current Revit document
targetName = IN[0] #the name of the family type you're looking for
revitAPI = [i for i in FilteredElementCollector(doc).OfClass(FamilySymbol).ToElements() if i.get_Name() == targetName] #uses a Python list comprehension method to return all instances in the list of family symbols who's names match the target name from the list of family symbols returned by the Revit API
dynamoNodes = Revit.Elements.FamilyType.ByName(targetName) #sues the Dynamo node to return the first family type who's name matches the target name

OUT = revitAPI, dynamoNodes #return the two sets of results to the Dynamo environment

Note that if you use the Dynamo Nodes method I think you’ll have to unwrap before you can really do stuff with it.

1 Like

Ahh… so I needed to add Revit.Elements in front of the thing.

dynamoNodes = Revit.Elements.FamilyType.ByName(“A1 Landscape”)

:smiley:

So simple!

Thanks

1 Like