Learning Python---uidoc question


I’m in the early processes of learning Python and am getting hung up on something which appears simple. I’m trying to recreate the code in this post:

The snippet below shows my current code. I am getting an error stating that uidoc is not defined. Not sure how it is working in the example code. Am I not importing something correctly?

Well so the idea is that in order for python to do something, or interact with something. It needs to know about that thing.

In the example you provide for example the code is expecting to do something with an object called ‘uidoc’, but it doesn’t understand what this uidoc thing is. Because (as the error says) it has not been defined.

Now if you’re running this as a pyrevit script you are in luck. Because all you have to add to your script is

from pyrevit import uidoc
and that should do it.
This line tells python to look for the ‘uidoc’ in pyrevit’s code.

I do hope that you’re doing this in a pyrevit script, otherwise getting the uidoc is a bit more cumbersome ( but perfectly doable)

MMM…I add that at the start and get this error:

ImportError: cannot import uidoc from pyrevit

I feel stupid…but it is my first attempt so have patience with me!

from Autodesk.Revit.UI.Selection import ObjectType, ISelectionFilter
from pyrevit import uidoc

class WallSelectionFilter(ISelectionFilter):
def AllowElement(self, element):
if type(element) == Wall:
print(“Is Wall”)

ref_wall=uidoc.Selection.PickObject(ObjectType.Element,WallSelectionFilter(), “Select a wall”)


Try this

from pyrevit import HOST_APP

doc = HOST_APP.doc
uidoc = HOST_APP.uidoc

And I think this works too (not at my computer right now)

from pyrevit import revit

doc = revit.doc # this is how I do it
uidoc = revit.uidoc

OK—that worked. As I look back on the Pyrevit documents, I see mention of ‘HOST_APP’ in one of the examples on this page,


But I honestly don’t know how I would have known to use that just by reading this page. It goes more indepth into ‘class pyrevit._HostApplication’…but I don’t see mention of ‘HOST_APP’ anymore outside of the example at the top.

I guess what I’m getting at is I’m having a hard time understanding when to load what module based off of the documentation. Am I missing something or does it just come with time and referencing other scripts?

You are right Aaron, you will get it with time and experience. But here are a few pointers:

When searching in the code base you will get the definition of HOST_APP here

all the attached properties here in the _HostApplication Class object

The search and the symbol navigation have greatly improved in github in the past year or so. Use it.

Using the new and not yet publicized documentation, you will get there a bit quicker as well
The search is much more performant and the auto documentation itself is grabbing every variable, class, property and definition of the pyrevit libraries, not only some of them randomly like the previous readthedocs one.

and obviously, reading the code of others script does help.

credits to @sanzoghenzo for the refactoring of the documentation process and the quick start forum post

There is plenty of samples, sources for learning. But my best advice is to start small and incrementally grow your scripts and skills. Don’t start with a pet project that is too big.