# Python code, GetElementByID

**URL:** <https://forum.dynamobim.com/t/python-code-getelementbyid/92884>\
**Category:** Developers\
**Tags:** python, dynamo\
**Created:** [August 15, 2023, 12:41pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884 "2023-08-15T12:41:09Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![BIM\_Mrbrango](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BIM\_Mrbrango](https://forum.dynamobim.com/u/BIM_Mrbrango)\
**Post date:** [August 15, 2023, 12:41pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/1 "2023-08-15T12:41:09Z")

</div>

Hi,  
Would need your help.

I don’t usually write python code and I’m not that comfortable with it, at some point you have to challenge yourself.

When I run the script in the python script module in dynamo, I just get PythonEvaluator.Evaluate: invalid syntax

Could someone who has the habit help me?

Would like to retrieve elements that hold an ID.

Thanks in advance

```auto
import clr

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

clr.AddReference('RevitNodes')
import Revit

clr.ImportExtensions(Revit.GeometryConversion)
clr.ImportExtensions(Revit.Elements)

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
ElementsMatch = []
ElementIDn = IN[0]

if isinstance(ElementIDn, str):
    ElementsMatch = doc.GetElement(ElementIDn)
    elif isinstance(ElementIDn, float):
    integerValue = int(ElementIDn)
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(integerValue))
    elif isinstance(ElementIDn, int):
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(int(ElementIDn)))
else:
ElementsMatch = doc.GetElement(ElementIDn)
OUT = ElementsMatch

```

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [August 15, 2023, 2:00pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/2 "2023-08-15T14:00:25Z")

</div>

You’re missing a lot of indents. Anything under a loop (if / elif / else) needs to be indented.

---

<div class="post-metadata">

**Author:** ![christian.stan](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/christian.stan/32/166296_2.png) [@christian.stan](https://forum.dynamobim.com/u/christian.stan)\
**Post date:** [August 15, 2023, 2:41pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/3 "2023-08-15T14:41:55Z")

</div>

Hello,  
here is a possibility  
with the indents and if you have a list

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/2/d2f7e6cc8e79461075257430dfe34cb5e6f9d350.png)

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

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

doc = DocumentManager.Instance.CurrentDBDocument
#Inputs
ElementsMatch = []
Elem = IN[0]
for i in Elem:
    if isinstance(i,int) or isinstance(i,str):
        a=doc.GetElement(ElementId(int(i)))
        ElementsMatch.append(a)
    else:
        a=doc.GetElement(i)
        ElementsMatch.append(a)
OUT =ElementsMatch

```

cordially  
christian.stan

---

<div class="post-metadata">

**Author:** ![BIM\_Mrbrango](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BIM\_Mrbrango](https://forum.dynamobim.com/u/BIM_Mrbrango)\
**Post date:** [August 15, 2023, 2:45pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/4 "2023-08-15T14:45:16Z")

</div>

Hi @Nick_Boyts

I’ve tried different options for indents, if I have too many it says that there are too many and if I have too few it says.

This setup is where it doesn’t say anything, but just gives an error message

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [August 15, 2023, 2:52pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/5 "2023-08-15T14:52:28Z")

</div>

You have to show us what it is you’re doing, otherwise we have no context. @christian.stan showed you the correct indenting. What is that getting you?

---

<div class="post-metadata">

**Author:** ![BIM\_Mrbrango](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BIM\_Mrbrango](https://forum.dynamobim.com/u/BIM_Mrbrango)\
**Post date:** [August 15, 2023, 2:54pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/6 "2023-08-15T14:54:30Z")

</div>

Hi @christian.stan,

Thanks, I’ll givit a try tomorrow when back at the Office, I’ll get back to you.

@Nick_Boyts and @christian.stan

What program do you use when you write code, I’ve tried various online tools but it still gets wrong with formatting.

Can you recommend any free software

---

<div class="post-metadata">

**Author:** ![Nick\_Boyts](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/nick_boyts/32/76313_2.png) [@Nick\_Boyts](https://forum.dynamobim.com/u/Nick_Boyts)\
**Post date:** [August 15, 2023, 2:57pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/7 "2023-08-15T14:57:45Z")

</div>

If I’m writing for Dynamo I just use the Dynamo Python editor. If it’s a lot of code and I’m dealing with something I’m unfamiliar with I might use Visual Studio Code for some additional context.

However, none of these are a replacement for knowing the formatting and syntax. That’s up to you.

---

<div class="post-metadata">

**Author:** ![christian.stan](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/christian.stan/32/166296_2.png) [@christian.stan](https://forum.dynamobim.com/u/christian.stan)\
**Post date:** [August 15, 2023, 3:07pm UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/8 "2023-08-15T15:07:41Z")

</div>

given my level, I’m doing with dynamo’s python editor, I can’t wait to go under 2024 to be able to zoom in the editor  
Sincerely  
christian.stan

---

<div class="post-metadata">

**Author:** ![GavinNicholls](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/gavinnicholls/32/190442_2.png) [@GavinNicholls](https://forum.dynamobim.com/u/GavinNicholls)\
**Post date:** [August 16, 2023, 12:28am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/9 "2023-08-16T00:28:16Z")

</div>

I recommend looking into pyCharm, it’s a free and solid IDE. Generally I switch to it when writing Python even if in Dynamo these days, although you lose some context that Dynamo provides when querying methods, properties etc.

---

<div class="post-metadata">

**Author:** ![BIM\_Mrbrango](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BIM\_Mrbrango](https://forum.dynamobim.com/u/BIM_Mrbrango)\
**Post date:** [August 16, 2023, 8:34am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/10 "2023-08-16T08:34:10Z")

</div>

Hi @Gavin, @christian.stan and @Nick_Boyts

Thanks for the information about software.

Unfortunately, I have big problems with IT restrictions that set group policies to not be able to run scripts on the local computer.

But that’s my problem…

I’ll look into PyCharm and Visual Studio Code.

Which version of Ironpython, for example, works with Revit intelligence, is it 2.7 or should you always run on the latest version.

If I remember correctly, it is something that is not supported if you install the wrong version.

---

<div class="post-metadata">

**Author:** ![Alien](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/alien/32/40701_2.png) [@Alien](https://forum.dynamobim.com/u/Alien)\
**Post date:** [August 16, 2023, 11:58am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/11 "2023-08-16T11:58:48Z")

</div>

> [@BIM\_Mrbrango](#):
>
> Visual Studio Code.

I’d recommend that one too.  
Or regular Visual Studio.

---

<div class="post-metadata">

**Author:** ![negigulshan903](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/negigulshan903/32/155295_2.png) [@negigulshan903](https://forum.dynamobim.com/u/negigulshan903)\
**Post date:** [August 23, 2023, 11:00am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/12 "2023-08-23T11:00:33Z")

</div>

Well, there are some logical error with your code, you can try below code it may resolve your error.

```auto
import clr

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

clr.AddReference('RevitAPIUI')
from Autodesk.Revit.UI import *

clr.AddReference('System')
from System.Collections.Generic import List

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

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

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
ElementsMatch = []
ElementIDn = IN[0]

if isinstance(ElementIDn, str):
    ElementsMatch = doc.GetElement(ElementIDn)
elif isinstance(ElementIDn, float):
    integerValue = int(ElementIDn)
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(integerValue))
elif isinstance(ElementIDn, int):
    ElementsMatch = doc.GetElement(Autodesk.Revit.DB.ElementId(int(ElementIDn)))
else:
    ElementsMatch = doc.GetElement(ElementIDn)

OUT = ElementsMatch

```

Thanks

---

<div class="post-metadata">

**Author:** ![thanhdatp297GRTHP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thanhdatp297grthp/32/167370_2.png) [@thanhdatp297GRTHP](https://forum.dynamobim.com/u/thanhdatp297GRTHP)\
**Post date:** [August 25, 2023, 2:59am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/13 "2023-08-25T02:59:54Z")

</div>

there are quite a few problems with your script.  
first is about indentation, you should learn about if else structure to solve this problem  
The next thing I’m understanding is that you want to retrieve elementID from a list of input data to ElementIDn.  
There are 2 cases  
Case 1, IN[0] is a list, then your loop is invalid, because you only check it with data types string, float and int so it won’t return any value, you need to use a loop to iterate through the item in the list  
Case 2, IN[0] has only 1 element then maybe it will match 1 of the datatypes you gave but the methods you gave don’t exist in Revit API document, Autodesk.Revit.DB.ElementId(int32) is an constructor, not a method to retrieve element from ID

---

<div class="post-metadata">

**Author:** ![BIM\_Mrbrango](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BIM\_Mrbrango](https://forum.dynamobim.com/u/BIM_Mrbrango)\
**Post date:** [August 28, 2023, 10:57am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/14 "2023-08-28T10:57:13Z")

</div>

> [@thanhdatp297GRTHP](#):
>
> method to retrieve element from ID

@negigulshan903

Thanks, still have som trouble and that @thanhdatp297GRTHP mension.

---

<div class="post-metadata">

**Author:** ![thanhdatp297GRTHP](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/thanhdatp297grthp/32/167370_2.png) [@thanhdatp297GRTHP](https://forum.dynamobim.com/u/thanhdatp297GRTHP)\
**Post date:** [August 29, 2023, 3:39am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/15 "2023-08-29T03:39:52Z")

</div>

Your program has complications that I don’t think are necessary, According to your purpose, I created a program that can do that with simple logic,

1. You just need to check if the input is a list, if true: contunute with this one, if false: throw it in a list
2. Used Try Except Syntax to remove exceptions such as float or string and get results  
You can try this.  
[GetElementById.dyn](https://forum.dynamobim.com/uploads/short-url/wgIBts6hsIZudbIG9s7eilGxLof.dyn) (8.7 KB)  
 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/4/5/45c758d0f12fce433a32c88332ace219f6a6c10d.png)

```auto
import clr
clr.AddReference("RevitAPI")
import Autodesk
from Autodesk.Revit.DB import ElementId
from Autodesk.Revit.DB import Document
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
clr.AddReference("System")
from System.Collections.Generic import List

doc = DocumentManager.Instance.CurrentDBDocument
uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
ids = IN[0]

# if it's not a list, make it a list
if not isinstance(ids, list):
	ids = [ids]

# convert to element ids

elements = []
for id in ids:
    try:
        eleid = ElementId(id)
        element = doc.GetElement(eleid)
        elements.append(element)
    except:
        pass

OUT = elements

```

---

<div class="post-metadata">

**Author:** ![Mike.Buttery](https://avatars.discourse-cdn.com/v4/letter/m/b19c9b/32.png) [@Mike.Buttery](https://forum.dynamobim.com/u/Mike.Buttery)\
**Post date:** [August 29, 2023, 3:46am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/16 "2023-08-29T03:46:13Z")

</div>

Friendly reminder that for the Dynamo python interpreter indents have to be _ **tabs** _ or _ **4 spaces** _ you can’t have both in the one script

---

<div class="post-metadata">

**Author:** ![BIM\_Mrbrango](https://avatars.discourse-cdn.com/v4/letter/b/a183cd/32.png) [@BIM\_Mrbrango](https://forum.dynamobim.com/u/BIM_Mrbrango)\
**Post date:** [August 29, 2023, 5:43am UTC](https://forum.dynamobim.com/t/python-code-getelementbyid/92884/17 "2023-08-29T05:43:49Z")

</div>

> [@thanhdatp297GRTHP](#):
>
> ```auto
> import clr
> clr.AddReference("RevitAPI")
> import Autodesk
> from Autodesk.Revit.DB import ElementId
> from Autodesk.Revit.DB import Document
> clr.AddReference("RevitServices")
> import RevitServices
> from RevitServices.Persistence import DocumentManager
> clr.AddReference("System")
> from System.Collections.Generic import List
> 
> doc = DocumentManager.Instance.CurrentDBDocument
> uidoc = DocumentManager.Instance.CurrentUIApplication.ActiveUIDocument
> ids = IN[0]
> 
> # if it's not a list, make it a list
> if not isinstance(ids, list):
> ids = [ids]
> 
> # convert to element ids
> 
> elements = []
> for id in ids:
> try:
> eleid = ElementId(id)
> element = doc.GetElement(eleid)
> elements.append(element)
> except:
> pass
> 
> OUT = elements
> 
> ```

Thank you so much, you are a rock, it works perfectly.

Really appreciate this, now I can compare my code against yours and check the API at the same time.
