Elif statement gets executed even though technically false

Hi everyone,
I’ve been writing a python dynamo script to try and execute a list of pipes by routing order (to be eventually exported to excel). The idea is that the user manually selects the first pipe to start off with, which is “PipeToStartWith”, and that pipe gets added as the first item in the list PipeOrder. The script then runs through all the start and end coordinates of all pipes and pipe fittings to see which one matches the coordinates of the item that’s already in the list. Once that pipe or pipe fitting has been found, it gets added to the PipeOrder list and the while loop then iterates by 1 to point to the next item in the list, which is the newly added pipe or pipe fitting, and it repeats the process.

Two conditions must be satisfied:

  1. One of the pipe/fitting’s start or end coordinates have to exactly match one of the start or end coordinates of the pipe/fitting already in the list that the loop is currently pointing to
  2. That new pipe/fitting should not be in the list yet

Ideally what should end up happening is, when the script executes correctly, it should output something like pipe, pipe fitting, pipe, pipe fitting etc. Below illustrates the direction I want the pipes to be added:

The pipe run that branches off is not to be chosen for the moment, because I want to get the pipes and fittings on the main route first. I chose to manually select the pipe with ID no. 19229164 to start off with, and if all goes to plan, the order in the end should be:
19229164
19229833
19229834
19229210
19229197
19229218
19229212
19229228
19229220

If you look at the below code, you’ll see what I’ve done so far (I didn’t bother pasting the boiler plate stuff here but it is in my original code). It is quite a long code to read through admittedly, so I’ll try my best to explain what I want it to do. You’ll notice in my above image that there is a t-branch fitting, which means it has 3 sets of x, y, z coordinates. In my below code, I have tried to eliminate the set of coordinates that is connected to the pipe that branches off by only including in my list the two other sets of coordinates that have exactly 2 of 3 coordinates matching each other. These two sets of coordinates are on the main route and it’ll ensure that I only choose the pipe that continues along the main route (ID 19229834) and not the pipe that branches off (ID. 19229818). It’s also explained in my comments below where and how I do this, which is the if-statement “if len(coordx14) == 3:” (ie. 3 sets of x, y, z coordinates, indicating that it’s a t-branch fitting)

If I comment out the “elif len(coordx14) == 2:” loop, then the previous if-statement works perfectly fine. However, if I include this elif loop, then it ends up messing things up where, for some reason it adds the branched off pipe to my PipeOrder list even though it shouldn’t even detect it and it shouldn’t even be running because technically it’s false as the t-branch has 3 sets of coordinates, not 2, and this elif- statement should execute only if it’s a normal pipe fitting bend, which would only have two sets of coordinates. This is what gets output in the end:

I don’t understand why this is happening? Am I maybe not using the if statements or for loops correctly? I think my indentations are correct but maybe I’m wrong? I know, I’ve written a war and peace and apologies if I didn’t explain things clearly enough as I’m a beginner when it comes to python. I’ve been looking at this for a few days and it’s doing my head in. I’m hoping that it’s something really simple that I’m just overlooking. Any help would be much appreciated. Also apologies, I accidentally ended up deleting the first post, hence the double up.

while length < 6:
	if PipeOrder[length-1].LookupParameter("Comments").AsString() == "fitting":
		coordx = []
		coordy = []
		coordz = []
		for connector in PipeOrder[length-1].MEPModel.ConnectorManager.Connectors:
			coordx.append(toDynPoint(connector.Origin).X)
			coordy.append(toDynPoint(connector.Origin).Y)
			coordz.append(toDynPoint(connector.Origin).Z)
			coordx14 = []
			coordy14 = []
			coordz14 = []
			for x, y, z in zip(coordx, coordy, coordz):
				coordx14.append(round(x, 14))
				coordy14.append(round(y, 14))
				coordz14.append(round(z, 14))
			if len(coordx14) == 3:
				TBranchEndCoordx = []
				TBranchEndCoordy = []
				TBranchEndCoordz = []
				for x1, y1, z1 in zip(coordx14, coordy14, coordz14):
					for x2, y2, z2 in zip(coordx14, coordy14, coordz14):
						if (x1 == x2 and y1 == y2 and z1 != z2) or (x1 == x2 and y1 != y2 and z1 == z2) or (x1 != x2 and y1 == y2 and z1 == z2):
							TBranchEndCoordx.append(x1)
							TBranchEndCoordy.append(y1)
							TBranchEndCoordz.append(z1)
							for sx, sy, sz, ex, ey, ez, pipe in zip(PipeStartX, PipeStartY, PipeStartZ, PipeEndX, PipeEndY, PipeEndZ, PipeElements):
								for tx, ty, tz in zip(TBranchEndCoordx, TBranchEndCoordy, TBranchEndCoordz):
									if (tx == sx and ty == sy and tz == sz) or (tx == ex and ty == ey and tz == ez):
										if pipe not in PipeOrder:
											PipeOrder.append(pipe)
			elif len(coordx14) == 2:
				for x, y, z in zip(coordx14, coordy14, coordz14):
					for sx, sy, sz, ex, ey, ez, pipe in zip(PipeStartX, PipeStartY, PipeStartZ, PipeEndX, PipeEndY, PipeEndZ, PipeElements):	
						if (x == sx and y == sy and z == sz) or (x == ex and y == ey and z == ez):
							if pipe not in PipeOrder:
								PipeOrder.append(pipe)
	length += 1

OUT = PipeOrder

I think what’s happening is you’re checking your list length as you’re building it.

Your if len(coordx14) == 3 loop is under the for connector in PipeOrder... loop, meaning each connector is adding its coordinates to your list then you’re checking the list and evaluating the rest of your loop. It does this for the first connector, then the second, then the third. So the first connector fails both conditions, then the second connector would run the len(coordx14) == 2 loop, then the third connector would run the len(coordx14) == 3 loop.

You need to loop your connectors and then check your coordinate lists.

1 Like

I moved the len(coordx14) loops back by one indentation and it seemed to do the trick. I thought it might be a simple solution in the end but needed a fresh pair of eyes. As always you’re great help, Nick. Thank you!