Execute a node if input is true

Hi all, thanks to the ones that will help me out with this.
I searched through numerous forums and topic but i still get quite confused about this. I have a script in which i want to execute the node Element.SetParameterByName based on a bool value. If the value is true, i want to execute the node and set the parameter; if the value is false i don’t want to execute the node at all. I found this very ugly workaround, indeed when the input bool value is set to false it returns an error (the argument of parameter is null function).
Any idea?

Hi Andrea,

You could use a function apply node for this, it wont put out an error if it doesnt do anything:

Thank you for your reply, this actually fix the error message.
Might be my bad for not getting this, but i have two questions:

  1. why doesn’t the Function.Apply node return the error? (i think it is just a different way of executing the SetParameter node)
  2. Given the previous question, isn’t this only a way to make the script more complicated?

My understanding of that node is limited so i’m not sure why it doesnt output an error.
However, as far as i know there are no ways of stopping a part of your graph from running, except for the function apply node, or the tool.runme node (im not sure how that node works), perhaps that one is of better use for you.

As stated here:

Many thanks!

1 Like

Hey @andrea.dilisa95 I know you have a solution already. If the parameter you are writing to is already blank, then replace null with double quotations " " (no space between them). This actually pushes a blank value to the Set Parameter node. One thing to note, this will overwrite any existing value in the parameter with a blank. But since the node executes properly, it will not throw an error. Otherwise, the solution above is the way to go.

This is what ScopeIf is designed for :slight_smile: Funnily enough it’s one of, if not the most complex node that exists in Dynamo as it controls what part of a graph will actually enter into the VM to execute.

If you look at the screenshot below, I’m using the ScopeIf node to scope what part of the graph I want to execute. This requires a few things:

  1. All branches going into the ports of the ScopeIf node to be 100% separate and not touch each other. If they interact, weird stuff happens which will not be your desired result.
  2. Set a pass condition (True) and a fail condition (False) - in my case I’m sending a message.
  3. The entire branch that isn’t passing the boolean test will not be passed to the VM and not be executed, hence the returning of null for all those nodes in that branch rather than actual results.

When you run this on True, you’ll notice that all those nodes still show a null result, but the end result is passed to the output of the ScopeIf node. You can then work with it from there.

1 Like

Hello @staylor , thank you. I know this but i don’t actually want to override any parameter, that is why i went for null initially. cheers

1 Like

@solamour, brilliant. I will have a deeper look at the node.
Thank you for taking the time to share your knowledge

@andrea.dilisa95 Not done yet…LOL! Using the ScopeIf triggered another thought. Maybe this setup could work for you.

1 Like

You can do it in Python node very easy.

# Import required modules
import clr
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

# Input values
element = IN[0]
bool_value = IN[1]
parameter_name = IN[2]
parameter_value = IN[3]

# Check if bool_value is true before setting parameter
if bool_value:
    # Start a Revit transaction
    TransactionManager.Instance.EnsureInTransaction(doc)
    # Set the parameter value
    element.LookupParameter(parameter_name).Set(parameter_value)
    # Commit the transaction
    TransactionManager.Instance.TransactionTaskDone()
    
# Output the modified element
OUT = element

2 Likes

@ruben.romero
Can’t get any simpler than that. Nice!

I am not into python (yet) but i suspected it was a joke for someone able to manipulate programming languages.
Thank you cheers!

@ruben.romero
I got an error with the LookupParameter part of your code. Had to change it to this. Never posted code before, so apologies if not correct.

# Import required modules
import clr
clr.AddReference("RevitServices")
import RevitServices

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

from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

# Input values
element = UnwrapElement(IN[0])
bool_value = IN[1]
parameter_name = IN[2]
parameter_value = IN[3]

# Check if bool_value is true before setting parameter
if bool_value:
    # Start a Revit transaction
    TransactionManager.Instance.EnsureInTransaction(doc)
    # Set the parameter value
    Autodesk.Revit.DB.Element.LookupParameter(element, parameter_name).Set(parameter_value)
    # Commit the transaction
    TransactionManager.Instance.TransactionTaskDone()
    
# Output the modified element
OUT = element
1 Like

I said the same 2 years ago, now python to work with multiple lists and conditions in few code lines, it doesn’t fail and it’s fast, less garbage of nodes. Those conditional nodes like if, if scope, list map, functions…cannot even be converted as node to code of designscript, I see limitations and headaches in their use, I personally avoid them