Data Input Type Conflict

Hoping someone can point me in the right direction here with this one as I’m fairly sure its a basic issue, but I continually get stuck on it anytime python comes up. I need to understand what the difference is between the category node output and the python node output that is shown. The category node works fine, but the python version does not.

My best guess is that the python node is returning an object, not the actual category that can be used downstream. What concept can I research here to understand this?

# Import Revit API and Dynamo modules
import clr
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager

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

# Get the current document
doc = DocumentManager.Instance.CurrentDBDocument

# Get all categories
categories = doc.Settings.Categories

# Prepare output: list of [name, category object] pairs
output = []

for cat in categories:
    output.append([cat.Name, cat])

# Output: list of [Category Name, Category Object]
OUT = output

This is just a basic dummy script that I am just messing with to learn. There isn’t a goal here at the moment.

Hi @matt.wna - What you are seeing is the raw Revit API results being returned into the Dynamo Graph.

The View.SetCategoryOverrides node requires a wrapped version of that API call, which is what the world of Dynamo understands. Right now it kind of like a conversation where you are expecting Spanish, and getting French, so it’s getting confused.

Amended code here that works for your use case:

# Import Revit API and Dynamo modules
import clr
clr.AddReference('RevitServices')
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

clr.AddReference('RevitAPI')
from Autodesk.Revit.DB import Category as RevitCategory # Renamed to avoid confusion with Dynamo Category

# We also need to import the Dynamo Category class
clr.AddReference('RevitNodes')
import Revit
from Revit.Elements import Category

# Get the current document
doc = DocumentManager.Instance.CurrentDBDocument

# Get all categories
# In Revit, categories are part of the document's settings.
categories_revit_db = doc.Settings.Categories

# Prepare output: list of [name, Dynamo Category object] pairs
output = []

# Loop through each Revit DB Category
for cat_db in categories_revit_db:
    # IMPORTANT STEP: Convert the Revit DB Category to a Dynamo Category
    # Dynamo has its own way of representing Revit elements, and for categories,
    # it often involves a conversion or a specific constructor.
    # The most common way to get a Dynamo Category from a Revit DB Category
    # is to use the 'Category.ById' or 'Category.ByName' after getting the name/ID.
    # Or, in some cases, a direct conversion like ToDSType() might work,
    # but for Categories, instantiating the Dynamo Category is safer.

    # Option 1: Using Category.ById (more robust if names aren't unique, but requires ID)
    # Dynamo Category objects are often created using their integer ID.
    # cat_dynamo = Category.ById(cat_db.Id.IntegerValue)

    # Option 2: Using Category.ByName (simpler for this case, assuming unique names in Dynamo's context)
    # We can get the name from the Revit DB Category and then use Dynamo's Category.ByName
    try:
        cat_dynamo = Category.ByName(cat_db.Name)
        output.append([cat_db.Name, cat_dynamo])
    except Exception as e:
        # Handle cases where a Revit DB Category might not have a direct Dynamo equivalent
        # or if Category.ByName fails for some reason (e.g., system categories that aren't exposed directly)
        # For beginners, it's good to be aware that not ALL Revit DB objects have a direct Dynamo wrapper.
        # print(f"Could not convert category '{cat_db.Name}': {e}")
        pass # We'll just skip categories that can't be converted easily for this example

# Output: list of [Category Name, Dynamo Category Object]
OUT = output
1 Like

Fun fact, you can get this answer by using Gemini by Google :slight_smile: Full prompt for your scenario below.

You are a Revit API and Dynamo Python expert, with deep and rich knowledge about both domains. You gravitate towards answers that are easy for a Beginner Dynamo Python user to understand.

For the following code, the results are coming through as the Revit category type of “Autodesk.Revit.DB.Category” instead of the Dynamo version, where I want to use them in the Dynamo graph with nodes like “Category.ByName”.

Please resolve the code to achieve this task - think hard, I know you can do it!

"# Import Revit API and Dynamo modulesimport clr
clr.AddReference(‘RevitServices’)from RevitServices.Persistence import DocumentManager

clr.AddReference(‘RevitAPI’)from Autodesk.Revit.DB import Category# Get the current document

doc = DocumentManager.Instance.CurrentDBDocument# Get all categories

categories = doc.Settings.Categories# Prepare output: list of [name, category object] pairs

output = for cat in categories:

output.append([cat.Name, cat])# Output: list of [Category Name, Category Object]

OUT = output"

1 Like

Hey, thanks for the quick response. Wrapping is definitely something I overlook / forget about, thanks very much. I’ll review and see if I can start putting some things together.

1 Like