Python Script - Function

Hello everyone,

I am trying to create a Dynamo graph that overrides the Color of some Revision Clouds in all views, including dependents.

I first created a list of the Revision Clouds I want to override, now I would like to create a list (of lists) that gives me the owner view and (if existent) it’s correspondent dependent views. As a beginner, I imagine I am missing something in the function “depen”, as the Output only Shows me one dependent view per owner (one of them should have 3 dependents). Could someone help me with that please? Cheers!


Ok. Have you solved the override of rev clouds?

that would come after, i am trying to do it step by step…

‘return’ will end your function and for loop. You’ll want to collect your dependent views on your temp list first, then return the view and temp list after the for loop.

1 Like

Not sure whats in the views.py (not so good in python)

Clockwork packages has view.DependentViews.
You need to give the main view.
If all index [0] got your main views, it should work. :slightly_smiling_face:

2 Likes

If I may speculate, because I already provided solution with nodes and packages in other topic, I think what @bialmeida wan’t is to reach a working results with Python and learn a bit. Something what we all encourage here.

@bialmeida for somebody to test your script, provide the text version as well, not just screenshot because nobody is going to retype so much lines to help others. We are all working people after all :wink:.

I’m not at my pc, so I’ll check this one later out and see if I can help. Meanwhile if somebody has time te help now, feel free to do so.

1 Like

That’s what I want indeed, thanks!

I managed to create the list of lists I wanted (thanks @Kibar), and thought everything would be fine once I create a new list of Revision Clouds that replicates the revisions according to the amount of dependent views, so I can pair them and use the zip you recommended yesterday - for example:

Revision Cloud 3601952 appears only one time (no dependent views), Revision 3602394 appears 4 times (one owner and 3 dependents), so I create a new list that goes:

Revision 3601952
Revision 3602394
Revision 3602394
Revision 3602394
Revision 3602394
and so on

Thanks for the hint, I added the .dyn file here and hope it helps?
I am also open to other recomendations of course
Thank you very much!!

Revisions.dyn (22.7 KB)

2 Likes

Congrats on making it work!
I went through your scripts and understand how you´ve build it up. This should always be your aim.

There are a few other ways to get to your goal, but if your way works: it works. You´ll get back to it at some point and can make changes then.

The only thing you should consider, before putting the dynamo graph aside as “done”, is to make your python script more readable :

  • It´s great to see you´re commenting your script! Keep that up (!)
  • Readable variable names: make them descriptive like number, index, dependentView, ... instead of x, y, a, b, i, ...
  • Have a look at pythons enumerate() function. It basically creates a zip(index, element)

Without enumerate:

for x in range(len(ownerviews)):
	dependents[x].append(ownerviews[x])

With enumerate:

for index, ownerview in enumerate(ownerviews):
	dependents[index].append(ownerview)
  • In your last for loop, you´re counting the elements of the sublists. len() counts the length of lists.

Try this instead:

countlist = list()

for x in dependents:
	#count = 0
	#for y in x:
	#	count = count + 1

	count = len(x)
	countlist.append(count)
1 Like

I’ve added the flatten function from @Kulkul

The last line takes every item of your list of views and counts it. Then it takes every item of your revision list and multiplies it by that count.

Tip! Never ever ever use drop down menus in your Script. So instead of node Categories use Category.ByName. You can read here why exactly it’s not a good idea and my simple solution to resolve it.

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

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

clr.AddReference("RevitServices")
from RevitServices.Persistence import DocumentManager
doc = DocumentManager.Instance.CurrentDBDocument

# credits Kulkul https://forum.dynamobim.com/t/use-the-flatten-node-in-python-script/10363/3
def flatten(a, r=None):
	if r is None:
		r = []
	for x in a:
		 if isinstance(x, list):
		 	flatten(x, r)
		 else:
		 	r.append(x)
	return r

##################################################
# Erstellung einer Liste, die die Owner-Views enthält (ownerviews)

ownerviews = list()

def GetOwnerView(item):
	return ownerviews.append(item.Document.GetElement(item.OwnerViewId))

revisions = UnwrapElement(IN[0])

for revision in revisions:
	GetOwnerView(revision)
	
##################################################
# Erstellung einer Liste, die die entsprechenden Dependent-Views enthält
dependents = list()

def depen(view):
	temp = list()
	dependentIds = view.GetDependentViewIds()
	for Id in dependentIds:
		if Id is not None:
			temp.append(doc.GetElement(Id))
		else:
			return list()
	return temp

for x in range(len(ownerviews)):
	owner1 = ownerviews[x]
	dependents.append(depen(owner1))
	
##################################################
# Erstellung einer Liste, die die Owner-Views und Dependent-Views zusammenbringt

for x in range(len(ownerviews)):
	dependents[x].append(ownerviews[x])

##################################################
# Erstellung einer Liste, die die Anzahl von Revisionswolken pro View enthält

countlist = list()

for x in dependents:
	count = 0
	for y in x:
		count = count + 1
	countlist.append(count)

revision = [[i]*len(k) for i, k in zip(revisions,dependents)]

OUT = flatten(dependents), flatten(revision)

3 Likes

Thank you SO much guys, that was incredibly helpful!
It’s a pitty it’s not possible to mark two answers as a solution, why is that so?
Thanks again both of you!! :fist_right::fist_left:

2 Likes

Can somebody kindly post the phyton code for views.py?
I am using a lower version of Dynamo and cannot open the script.

@Revit_Noob it’s in the post one above. The other one is in here.

1 Like