Create elements disappear / no transaction

I’m trying to do a quick test to check if I’m getting the write point of an element. I’ve decided to check this by placing conduit in the model. When I run the script I see the conduits pop up for half a second and then they vanish. After the script is ran there is no transaction to undo. Am I doing something wrong with this section of code?

t = Transaction(doc, "pyRevit - Wall Coordination: conduit test")
t.Start()
for flp in fLocation:
    newPoint = XYZ(float(flp.X) + 1.0, float(flp.Y) + 1.0, float(flp.Z) + 1.0)
    conduit = Conduit.Create(doc, conduitType, flp, newPoint, lvl)
    # curve = Line.CreateBound(flp, newPoint)
    # detailLine = doc.Create.NewDetailCurve(cView,curve)
t.Commit()

Ok, I had a script.exit() after this test because I didn’t want everything to run. That appears to have done something weird with the transaction. Does anyone know why?

I find script exit will often seem to prevent commiting of transactions as well. I often indent the remainder of a script into a never true condition temporarily vs call an exit when testing if i run into this.

1 Like

Hi @Crapai,
The script.exit just calls the sys.exit function, so I suspect the ironpython interpreter or the c# part of pyrevit handle the rollback. I’ll check as soon as I have time to see if it’s something we can fix.

In the meantime, to add to @GavinCrump suggestion:

The simplest way is to just comment everything out; if you use an IDE such as Visual Studio Code, select all the lines after the one you want to run and press CTRL+/. This shortcut toggles the comment on and off.

You can also enclose everything in a main function, and call it at the end of the script

def main():
     # your code here

main()

So that you can return instead of exit to have the same effect.

I usually tend to use the more pythonic way

if __name__ == "__main__":
    main()

This is usually needed in regular python packages in order to not execute the script when it is imported from another python module (as opposed to directly called by the user). It is not the case for pyRevit, but it doesn’t hurt either.

Another thing that helps, not only for your user case but also for writing cleaner code, is to arrange it into smaller, simpler functions that will be called by the main one, so that you can just comment or place a return between the few lines in the main function.

3 Likes