Python IF statment with Boolean list input


#1

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?

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)

#2

Check this:


This Python course here should help you grasp basics of Python:

I really suggest just starting with the free version.

What do you want to do with those grids exactly?


#3

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


#4

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


#5

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

Grid bubble on/off
#6

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

#7

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.


#8

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