Create a view filter for a dropdown parameter

I am trying to use Dynamo to make a view filter, using the below nodes.

This will create a filter but assigns no value to the filter, the issue I believe is that Dynamo does not like working with dropdown parameters. I have decided to try hardcoding this process using Python, but I have never used Python before and don’t know how to go about fixing what I have to actually work.

import clr
clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
import System
from System.Collections.Generic import List

# The inputs to this node will be stored as a list in the IN variables.
dataEnteringNode = IN

# Declare all inputs
cats = IN[0]
pValue = IN[1]

# Start a transaction
TransactionManager.Instance.EnsureInTransaction(doc)

try:
	pName = "Reference Level"
	param = None
	collector = FilteredElementCollector(doc).OfClass(ParameterElement)
	for p in collector:
		if p.Name == pName:
			param = p
			break
	
	if param:
		# Create List of category ID's
		catList = [ElementId(i.Id) for i in cats]
		typedCatList = List[ElementId](catList)
		
		# Create the filter rule
		rule = FilterStringRule(param.Id, ParameterValueProvider(param.Id), pValue, False, 1)
		
		# Create the filter with a name based on pValue
		fName = str(pValue) + " Dynamo 3D View"
		filter = ParameterFilterElement.Create(doc, fName, typedCatList)
		filter.SetElementFilter(rule)
		
		# Create a new 3D View	
		view3D = View3D.CreateIsometric(doc, ElementId.InvalidElementId)
		view3D.Name = fName
		
		# Assign the filter to the view
		view3D.AddFilter(filter.Id)
		
		# Set the filter's visibility to false
		doc.ActiveView.SetFilterVisibilty(filter.Id, False)
		
		OUT = filter

	else:
		OUT = "Parameter not found. Parameter Name: {}".format(pName)
		
except Exception as e:
	OUT = "Error :{}".format(str(e))
	
finally:
	# close the transaction
	TransactionManager.Instance.TransactionTaskDone()

When I run this it outputs “Parameter not found. Parameter Name: Reference Level” which does not make sense because the parameter is very obviously in the project. Any help fixing the code to run properly would be greatly appreciated, Thank you.

P.S. I have been using ChatGPT to write this code, so I am unable to answer any questions about why the code is written the way it is, I really only have a fundamental understanding of how it’s supposed to work.

There may be an issue with the Revit OOTB Select Rule Type node - it sometimes outputs a null value which will stop your graph from completing. Fortunately it is only text so you can substitute a code block. Also watch out for nodes from other packages like archilab that have the same function but may not work with OOTB nodes

Not all parameters can be used to create a filter - check the category and dropdown
image

Here is a working example

You are going to find that Chat-GPT is not your best option, and thank you for mentioning it, however this will most likely lead to more frustration. Even if the code works it will have logic flaws - just your code creates a new view but sets visibility on the ActiveView and you should be able to get a known parameter directly rather than iterating through a FilteredElementCollector

If you haven’t coded before start with the free e-book Think Python and for Dynamo specific information the Dynamo Python Primer is essential reading and then dig into the api at Revit API Docs or apidocs.co where you can search for code examples

The Add View Filter works better than what I had so thanks for pointing that out. However, I have found the main issue to be centered around setting the value of the filter. If the parameter allows you to select the value from a dropdown list, it leaves the value blank, even if you give it an input.
image
I can create the filter a multitude of ways but can never get Dynamo to set the value, except for if I add a new text-based parameter that is simply a copy of the existing drop-down parameter that I actually want to use. Dynamo has no issue working off of a text parameter.

This was why I looked into making a python script hoping I could bypass the issue that way and not have to create a whole new parameter, but you’re right about the coding. I plan on studying it more and the resources you provided will be a great help. Thank you!