Python IF statment with Boolean list input

Hello all,

I am new to python and trying to wrap my head around writing an IF statement with a Boolean list input. Can I just call if bool: or do I need to call the items in the index if i in bool:? is this even close?

Capture

grid = UnwrapElement(IN[0])
bool = UnwrapElement(IN[1])
v = UnwrapElement(IN[2])
TransactionManager.Instance.EnsureInTransaction(doc)

for i in grid:
	if bool:
		i.HideBubbleInView(DatumEnds.End1,v)
		i.ShowBubbleInView(DatumEnds.End0,v)
	else:
		i.HideBubbleInView(DatumEnds.End0,v)
		i.ShowBubbleInView(DatumEnds.End1,v)

or this

grid = UnwrapElement(IN[0])
bool = UnwrapElement(IN[1])
v = UnwrapElement(IN[2])
TransactionManager.Instance.EnsureInTransaction(doc)

for i in grid:
	if i in bool:
		i.HideBubbleInView(DatumEnds.End1,v)
		i.ShowBubbleInView(DatumEnds.End0,v)
	else:
		i.HideBubbleInView(DatumEnds.End0,v)
		i.ShowBubbleInView(DatumEnds.End1,v)

Check this:
Capture
This Python course here should help you grasp basics of Python:
https://www.codecademy.com/learn/learn-python
I really suggest just starting with the free version.

What do you want to do with those grids exactly?

I am trying to turn the grid bubbles on at the top and right sides and off for the bottom left. The true and false are checking if the End0 is on the top right (true) or bottom left (false).

So for grids that have End0 in the top right there is a true Boolean in the bool list. It should turn on the grid bubble at End0 and turn off the bubble at End1. If the grid was drawn in the other direction it will turn on the bubble at End1 and off at End0.

I will try and re-wright the IF statement based on what you sent in a little bit. Got tasked with some other stuff.

Thanks

Try this:

c = 0    
for i in grid:
    if bool[c] == True:
        i.HideBubbleInView(DatumEnds.End1,v)
	    i.ShowBubbleInView(DatumEnds.End0,v)
    else:
	    i.HideBubbleInView(DatumEnds.End0,v)
	    i.ShowBubbleInView(DatumEnds.End1,v)
    c = c+1

Change bool[c] ==True to False if necessary. I cannot understand all the context.
Hope it helps

Xavier

1 Like

When two (or more) inputs are directly connected you can iterate through each list simultaneously by zipping them together.

grid = IN[0]
bool = IN[1]

for g, b in zip(grid, bool):

You also don’t need to check a boolean with a test. You can simply use

if b: 
    this
else:
    that
3 Likes

Thanks Nick,

That did the trick. See full code below. One last thing the out only has the last grid item. What did I do wrong / change with the zip code to affect this? (If you don’t have time thats cool I am just wondering its not crucial to what I am doing).

@xavier.loayza I got yours to work as well. I marked Nick’s as solved because his seams more efficient by simultaneously looking at the data. Thank you for your input and helping me out.

Thanks a million,

import clr
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *

clr.AddReference("RevitNodes")
import Revit
clr.ImportExtensions(Revit.Elements)

clr.ImportExtensions(Revit.GeometryConversion)

clr.AddReference("RevitServices")
import RevitServices

from RevitServices.Persistence import *
from RevitServices.Transactions import TransactionManager
from System.Collections.Generic import *

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

doc = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument

g = UnwrapElement(IN[0])
b = UnwrapElement(IN[1])
v = UnwrapElement(IN[2])

TransactionManager.Instance.EnsureInTransaction(doc)

for g, b in zip(g, b):
	if b:
		g.HideBubbleInView(DatumEnds.End1,v)
		g.ShowBubbleInView(DatumEnds.End0,v)
	else:
		g.HideBubbleInView(DatumEnds.End0,v)
		g.ShowBubbleInView(DatumEnds.End1,v)
	
TransactionManager.Instance.TransactionTaskDone()

doc.Regenerate()

OUT = g
4 Likes

Your grid g gets overridden each time, so when you finally return g at the end of your code it’s only returning the current item (the last grid). Normally you would create an output list and after each function you would append the current item g to your output list, so by the end of all the iterations you’d have a list of each item as it was processed. In your case however you can just return the original input list grid. It’s the same list of items so you don’t need to create a new list.

1 Like

Thanks Nick thats a big help. I really appreciate the time you took to help me out.

Can somebody share the final graph? i also need this.

What do you need? The python is there so you can copy it and use it as a python node. You just feed a list of true/false and the grid lines into it. perhaps you should take an attempt at what you want and then you can start a new topic if you need help with the graph. My final graph will likely not help you as it was built for my firm and our specific templet/needs.

Also please read this (#7 in particular)

1 Like

Hi, getting an understanding of this. Will it use grid bubbles on or off as a template from one view and copy it to another?

The script above will turn the grids bubbles on or off depending on a true or false. The opposite side will “NOT” be the other side (only one side turned on). What you are asking should work just fine as long as you only want one side on.

This is off-topic for this thread though. If you want to give it a shot and then start a new thread referencing this one we can help you out.

1 Like

Hi Steven,

Thanks alot for this code/script it has helped out on a project im working on. Is there away to amend the code to do all views rather than just active view?