Setting Revision Visibility with Python - RevisionVisibility Enumeration Members

Due to this thread How to change Revision Visibility? I got curious as to how to set the visibility of Revisions with Python.

If I input Revision elements into this first script, it changes all of the Revision’s visibility to “Hidden” which is what I assume would be 0 in the RevisionVisibility enumeration (see here in the API docs http://www.revitapidocs.com/2016/486d8b1e-811d-fd43-d2a4-a4eef4e53538.htm )

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

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

revisions = UnwrapElement(IN[0])

doc = DocumentManager.Instance.CurrentDBDocument

TransactionManager.Instance.EnsureInTransaction(doc)

newvisibility = []
for revision in revisions:
	newvis = revision.Visibility = RevisionVisibility()
	newvisibility.append(newvis)

TransactionManager.Instance.TransactionTaskDone()

OUT = revisions, newvisibility

Because of this I know that the Revision visibility can be modified with Python, but I cannot figure out how to get the members of RevisionVisibility. I tried to get an output with this code, but I get the Error “TypeError: RevisionVisibility is not iterable”:

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

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

vislist = []
visibilityoptions = enumerate(RevisionVisibility())
vislist.append(visibilityoptions)

OUT = vislist

Am I missing some Python knowledge on how to list enumeration members, or does the error “TypeError: RevisionVisibility is not iterable” mean that it is just not possible for the RevisionVisibility enumeration?

If I could get the members, inputting Revision Elements, and a “Select Revision Visibility” node into the following code should in theory, work (I think :thinking: - I might be trying to grab the members by index incorrectly in the lines that say visibility = vislist[0] etc - someone please correct me if I am wrong :sweat_smile: )

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

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

revisions = UnwrapElement(IN[0])
selectvisibility = UnwrapElement(IN[1])

doc = DocumentManager.Instance.CurrentDBDocument

visibilityoptions = enumerate(RevisionVisibility())
vislist.append(RevisionVisibility())

if selectvisibility == "Hidden":
	visibility = vislist[0]
elif selectvisibility == "CloudAndTagVisible":
	visibility = vislist[1]
else:
	visibility = vislist[2]

TransactionManager.Instance.EnsureInTransaction(doc)

newvisibility = []
for revision in revisions:
	newvis = revision.Visibility = visibility
	newvisibility.append(newvis)

TransactionManager.Instance.TransactionTaskDone()

OUT = revisions, newvisibility, vislist

Anyone have any ideas/thoughts/suggestions? Maybe I am just trying to get the enumeration members incorrectly? Thank you in advance :slight_smile:

EDIT: I had a thought (of course after leaving my office/computer with Dynamo) on something I didn’t think to try before posting because I was so thinking in my head the way the WallFunction enumeration works. I’m thinking maybe the code would work if I just omitted the enumeration (still not sure if I was trying to use that improperly) and changed:

if selectvisibility == "Hidden":
	visibility = vislist[0]
elif selectvisibility == "CloudAndTagVisible":
	visibility = vislist[1]
else:
	visibility = vislist[2]

to:

if selectvisibility == "Hidden":
	visibility = RevisionVisibility(Hidden)
elif selectvisibility == "CloudAndTagVisible":
	visibility = RevisionVisibility(CloudAndTagVisible)
else:
	visibility = RevisionVisibility(TagVisible)

I will have to try tomorrow unless someone else decides to test it before I get back to my computer with Dynamo before then :grin:

1 Like

Amy,

This is good stuff. You were really close. Here’s how to make this work:

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

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

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

#The inputs to this node will be stored as a list in the IN variable.
dataEnteringNode = IN

revisions = UnwrapElement(IN[0])

# enumeration class can be parsed by it's member's name
vis = System.Enum.Parse(Autodesk.Revit.DB.RevisionVisibility, IN[1])

TransactionManager.Instance.EnsureInTransaction(doc)

if isinstance(revisions, list):
	# we have a list here let's iterate over it in a loop
	for r in revisions:
		r.Visibility = vis

TransactionManager.Instance.TransactionTaskDone()

OUT = [v.Visibility for v in revisions]

Now a few things that explain what’s going on. First and foremost the Select Revision Visibility node outputs a string representation of the RevisionVisibility class from Revit API. You can see that on my image where I put Object.Type and it says System.String. Why does that matter? It does, because what we have here is a string, and not an actual enum object.

Now, that we are talking about enums. To briefly explain what they are. Think of them as immutable lists. That means they hold some items in an ordered collection, but you cannot neither remove nor add more to that list. They are pretty good for things that you know you only have certain amount of, and you never want to add/remove any but you need a really fast access to it. Since they are ordered, you can access items in an enum via it’s index etc. Here’s some more on Enums. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/enum

Now, knowing we are dealing with an enum and we had it’s string representation we can use System.Enum.Parse() to convert from string to Enum. You see that in my code.

Once we sorted that out, the rest was like you had it.

Cheers!

6 Likes

Thank you @Konrad_K_Sobon !! I did notice that the output was a string, which is why I was trying to do the

if selectvisibility == "Hidden":
	visibility = vislist[0]
elif selectvisibility == "CloudAndTagVisible":
	visibility = vislist[1]
else:
	visibility = vislist[2]

part - because i wanted my code to allow that node to be the second input after the revisions!

THIS is the Python/API speak I wasn’t aware of. Thank you very much for for the link. I certainly was lacking knowledge on System.Enum.Parse() This was mostly an experiment out of curiosity and an exercise in gaining proficiency with utilizing Python within Dynamo; I haven’t much use for applying this in my own workflows but I couldn’t help but to try to figure it out. I’m sure at some point in the future I’ll be glad to have the custom node I will make of it at hand. Thank you again! I will try in the morning when I am back on a computer with Dynamo :smile: Thanks again for your knowledge and input!

4 Likes

@Konrad_K_Sobon Finally just had some time during lunch to revise my code to work :grin: thanks again for the assist! I noticed in your code you included if isinstance(revisions, list): before iterating over the revisions, do you mind explaining to me the reason for this is? Thanks again :slightly_smiling_face:

1 Like

This checks if the input is indeed a list and not a singleton.

I thought so @Andreas_Dieckmann, I already put mine into a custom node with a TurnIntoList node beforehand. But in any case, it appears that the code doesn’t work if that line is included (I just tested it out because of this post/thread: How to change Revision Visibility?

Now I’m wondering why that line is stopping the code from working, it doesn’t seem like it should :thinking:

@awilliams,

Andreas is correct, I wanted to make sure it’s a list before attempting to iterate over it otherwise it would have failed on a loop. Now, the UnwrapElement(IN[0]) line is possibly doing things of weird nature. I almost never try to unwrap the whole list in one call, because it used to cause issues, but I was told that it was fixed so i tried it here. It seemed to have work fine on mine. I guess it’s still weird.

Cheers!

Ps. Thanks for fixing this and posting back for others to see.

1 Like

I have noticed that this before too, and it looks like UnwrapElement() is converting the Pyton List into a Collections.Generic.List. That’s why isintance() returns false.

2 Likes

Ah good discovery @Einar_Raknes ! Just tested and the isinstance() part of @Konrad_K_Sobon’s code works when I revised it to to unwrap the elements after determining if they are an instance. Good to know :slight_smile:

1 Like

@awilliams If you had paid more attention to the code we helped James_Washbourne with the other day, you would have known :wink: Just kidding, you seem to be learning fast. Keep up the good work!

1 Like

@Einar_Raknes Oh my gosh! :see_no_evil: I definitely had read the resolution to that post!! My attention couldn’t have been fully focused when I read it, because I had zero recollection of System.Enum.Parse last night :laughing: Thats too funny! Thank you!

2 Likes

Ha! Great find. I knew it was messing with it but i just avoided that thing. It’s great to know. Cheers!