Stop a branch if list is empty

Hi all,

I’ve got a script for numbering doors automatically. It basically takes the Parametric Monkey custom node that automatically numbers all doors but removes the sequences numbering at the end of a room only has one door to it. It all works great, except it returns an error if either the interior or exterior doors list is empty. The script accomplishes the task, but I would really like to get rid of that error.

I’ve read about trying to end a branch if a list is empty, using “if” nodes or the Clockwork “Passthrough” node, but those nodes at the end still return an error if an empty list comes into them. Is there a way to avoid this or is this just the nature of Dynamo?

Thanks in advance for any help.

Please try to search for similar topics before posting a new one. This question has been asked many times before.

Yep, I have read these and a dozen or so other threads.

Perhaps the whole stopping a node isn’t even the solution I need. It boils down to wanting to get rid of the error on the Room.Number node. Everything works how it is intended to, but I’d rather Dynamo not show essentially a false error warning that will confuse other people using the script.

It’s still the same situation.

You can’t just tell Dynamo not return a warning (yet). Even if you ignored the room number warning you still can’t write to an element that doesn’t exist. The way your graph is written you’re going to have warnings when you don’t have elements.

I’m guessing one branch is for interior doors and one is for exterior. If that’s the case, you’re way better off just writing a conditional statement for the two conditions and doing everything in a single branch. That way you’re pretty much guaranteed you’ll have at least one door.

So that’s my question. Is there a way to write it to avoid the warnings? I’m not looking for someone to write it for me, but at least lay out the general logic if there is. Or if there is some node or group of nodes I’m not utilizing, let me know. I only started using Dynamo a few weeks ago.

I have it as two branches because the interior doors need to pull from the “toRoom” output and the exterior doors need to pull from the “fromRoom” output. Even if I combined them somehow, I would really rather not avoid errors “most of the time”.

I’m going to go ahead and show the entire script here. Again, the intent is to run the Parametricmonkey renumber node and remove the last decimal and sequence number from the doors that go to rooms with only one door (see numbering in second image).

This is exactly what’s described in the other topics. Typically you have 3 options:

  1. Write the graph in a way that prevents nulls or empty lists if possible or at least runs without them causing warnings. This isn’t always possible. In your case, it could be done by combining the two branches.
  2. Wrap the portion of the graph causing warnings inside a custom node. As long as you handle those warnings inside the custom node, they won’t cause warnings with the custom node itself.
  3. Write this portion of the code in python and handle the warnings there.

As I mentioned before, you can handle this with a conditional statement. Get both values for all elements. If the door is interior, use the toRoom value. If the door is exterior, use the fromRoom value. Now you can do everything in one branch.

It’s only “most of the time” because you can’t completely remove the chance of an empty list. In this case, the only time that would happen is if there were no doors at all.

1 Like

There is a fourth option: pass a function and have that iterate over the list.

However this will be slower and will suppress all warnings, so it’s a double edged sword.

2 Likes

A fifth option - try pyRevit.

It can stop a script partway through using a simple command. This is what actually got me into the program recently and it’s been a game changer for how I design my scripts now.

Here’s an example that can do this (line 46): pyRoovit/script.py at main · aussieBIMguru/pyRoovit · GitHub

1 Like

Thanks Jacob and Gavin! I have not gotten to learning how to use functions or pyRevit yet, so I’ll have to dig into those and get them figured out!

scope if node can also do this

1 Like

I was trying to work with this one, but what is an output that wouldn’t cause the error?

I was able to improve the script following some of Nick’s suggestions, but I’m still curious how to use the scope if node in a similar application for future reference.

it would not be an output, the code you want to run if the condition is true will be an input branch to scope if node. Only one of the two input branches will be executed.

it’s a different paradigm from the rest of Dynamo, you’re not sending data into the scope if node, but code.

1 Like