Select objects by python code

Is it possible to retrieve selected objects from screen by python code? if yes, may i know how

Look into the Tool.GetRevitSelection node in Steamnodes package.

You can also try writing code on your own. This is where you should start:

i wrote the code as below. its not working perfectly. don’t know where is the mistake.

import clr
from Autodesk.Revit.DB import *
from Autodesk.Revit.DB.Mechanical import *
#from Autodesk.Revit.UI.Selection import *
from Autodesk.Revit.UI import *

from System.Collections.Generic import List

import Revit

import RevitServices
from RevitServices.Persistence import *
#from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import *
#from RevitServices.Transactions import TransactionManager
#The inputs to this node will be stored as a list in the IN variables.

doc1 = DocumentManager.Instance.CurrentDBDocument
uiapp = DocumentManager.Instance.CurrentUIApplication
app = uiapp.Application
uidoc = uiapp.ActiveUIDocument
doc = uidoc.Document
sel = uidoc.Selection
selid = sel.GetElementIds()
OUT = selid

Try this:

import clr

import RevitServices
from RevitServices.Persistence import DocumentManager

OUT = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument.Selection.GetElementIds()

this is same as the which i posted. both are working at one time. second time it is not working !

It’s perfectly normal. You’re not changing the inputs/wire/etc., so fore Dynamo perspective, there is no need to reevaluate this script.

Try connecting a “pseudo-input” to the script and change its value everytime you want to re-run the script (e.g. a true/false button, that you’d switch every time).

Add second input Refresh bool true/false.

now its working.

that means we can’t use Dynamo as a plugin, right?

Glad it worked!

We can use. It depends how you build your script.

Please mark the post as solved.

It means that scripts that use selection are better run from Dynamo Player rather than from opened Dynamo.