# Python Scrips Newbie - "expected an indented block"

**URL:** <https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911>\
**Category:** DesignScript\
**Tags:** python, dynamo, color\
**Created:** [February 9, 2019, 8:45pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911 "2019-02-09T20:45:26Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jacko](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jacko](https://forum.dynamobim.com/u/Jacko)\
**Post date:** [February 9, 2019, 8:45pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/1 "2019-02-09T20:45:26Z")

</div>

Hello,

I’m trying to create new materials with Python script. I have used @Scott_Crichton script from this [topic](https://forum.dynamobim.com/t/create-new-material-thermal-physical-properties-using-dynamo-how/4365) but Im getting an error “expected an indented block”. I know that @mimp had the same problem, but this has not been resolved.

Have you any ideas what might be wrong with that script?  
I’m just a total beginner 😕

```
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

def ToRevitColor(dynamoColor):
return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)

def ToDynamoObject(revitObject, isRevitOwned=False): # isRevitOwned should be False if Dynamo script created the object.
return revitObject.ToDSType(isRevitOwned)

doc = DocumentManager.Instance.CurrentDBDocument

names = IN[0]
transparencies = IN[1]
colors = IN[2]

newMaterials = []

for mat_name, transparency, color in zip(names, transparencies, colors):
TransactionManager.Instance.EnsureInTransaction(doc)
new_mat_id = Material.Create(doc, mat_name)
new_mat = doc.GetElement(new_mat_id)
new_mat.Color = ToRevitColor(color)
new_mat.Transparency = transparency
TransactionManager.Instance.TransactionTaskDone()
newMaterials.append(ToDynamoObject(new_mat))

OUT = newMaterials

```

Thanks in advance  
Jack

Here is attached my .dyn file  
[New\_Materials\_Script.dyn](https://forum.dynamobim.com/uploads/short-url/fSt4EDcXa5T0oeFn2RVcY1JwPxD.dyn) (39.5 KB)

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [February 9, 2019, 8:50pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/2 "2019-02-09T20:50:40Z")

</div>

As a start, for this section:

TransactionManager.Instance.EnsureInTransaction(doc)  
new\_mat\_id = Material.Create(doc, mat\_name)  
new\_mat = doc.GetElement(new\_mat\_id)  
new\_mat.Color = ToRevitColor(color)  
new\_mat.Transparency = transparency  
TransactionManager.Instance.TransactionTaskDone()  
newMaterials.append(ToDynamoObject(new\_mat))

OUT = newMaterials

Indent all lines with one Tab spacing, as this lets the **for** function know what it is trying to apply.

---

<div class="post-metadata">

**Author:** ![Jacko](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jacko](https://forum.dynamobim.com/u/Jacko)\
**Post date:** [February 9, 2019, 9:09pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/3 "2019-02-09T21:09:22Z")

</div>

So the script has to look like this?

```
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

def ToRevitColor(dynamoColor):
return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)

def ToDynamoObject(revitObject, isRevitOwned=False): # isRevitOwned should be False if Dynamo script created the object.
return revitObject.ToDSType(isRevitOwned)

doc = DocumentManager.Instance.CurrentDBDocument

names = IN[0]
transparencies = IN[1]
colors = IN[2]

newMaterials = []

for mat_name, transparency, color in zip(names, transparencies, colors):
	TransactionManager.Instance.EnsureInTransaction(doc)
	new_mat_id = Material.Create(doc, mat_name)
	new_mat = doc.GetElement(new_mat_id)
	new_mat.Color = ToRevitColor(color)
	new_mat.Transparency = transparency
	TransactionManager.Instance.TransactionTaskDone()
	newMaterials.append(ToDynamoObject(new_mat))

	OUT = newMaterials

```

If so, then it is still not working. And error is same.  
[New\_Materials\_Script.dyn](https://forum.dynamobim.com/uploads/short-url/fsP4cNkDnIDz08Q5xpRn7VNLq4v.dyn) (39.5 KB)

---

<div class="post-metadata">

**Author:** ![Ewan\_Opie](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/ewan_opie/32/77070_2.png) [@Ewan\_Opie](https://forum.dynamobim.com/u/Ewan_Opie)\
**Post date:** [February 9, 2019, 9:40pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/4 "2019-02-09T21:40:18Z")

</div>

Not at the PC right now, so can’t plug the code into Dynamo.

Take the indent off the OUT line and see if that helps.

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [February 9, 2019, 9:45pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/5 "2019-02-09T21:45:40Z")

</div>

You need to indent the definitions too. Add a tab before “return” for both definitions.

Also, remove indent on OUT. It should sit out of the for loop.

Also also, transaction should really sit out of the loop too, but it will still work as is

---

<div class="post-metadata">

**Author:** ![Jacko](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jacko](https://forum.dynamobim.com/u/Jacko)\
**Post date:** [February 9, 2019, 10:08pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/6 "2019-02-09T22:08:14Z")

</div>

Ok, so now it shows me another error.

> Traceback (most recent call last):  
> File “”, line 29, in   
> Exception: The given value for name is already in use as a material element name.  
> Parameter name: name

At this moment my script looks like this:

```
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

def ToRevitColor(dynamoColor):
	return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)

def ToDynamoObject(revitObject, isRevitOwned=False): # isRevitOwned should be False if Dynamo script created the object.
	return revitObject.ToDSType(isRevitOwned)

doc = DocumentManager.Instance.CurrentDBDocument

names = IN[0]
transparencies = IN[1]
colors = IN[2]

newMaterials = []

for mat_name, transparency, color in zip(names, transparencies, colors):
	TransactionManager.Instance.EnsureInTransaction(doc)
	new_mat_id = Material.Create(doc, mat_name)
	new_mat = doc.GetElement(new_mat_id)
	new_mat.Color = ToRevitColor(color)
	new_mat.Transparency = transparency
TransactionManager.Instance.TransactionTaskDone()
newMaterials.append(ToDynamoObject(new_mat))

OUT = newMaterials

```

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [February 9, 2019, 10:12pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/7 "2019-02-09T22:12:39Z")

</div>

try this…

```
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

def ToRevitColor(dynamoColor):
	return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)

def ToDynamoObject(revitObject, isRevitOwned=False): # isRevitOwned should be False if Dynamo script created the object.
	return revitObject.ToDSType(isRevitOwned)

doc = DocumentManager.Instance.CurrentDBDocument

names = IN[0]
transparencies = IN[1]
colors = IN[2]

newMaterials = []

for mat_name, transparency, color in zip(names, transparencies, colors):
    try:
	    TransactionManager.Instance.EnsureInTransaction(doc)
	    new_mat_id = Material.Create(doc, mat_name)
	    new_mat = doc.GetElement(new_mat_id)
	    new_mat.Color = ToRevitColor(color)
	    new_mat.Transparency = transparency
        TransactionManager.Instance.TransactionTaskDone()
        newMaterials.append(ToDynamoObject(new_mat))
    except:
        newMaterials.append(None)

OUT = newMaterials

```

I have added Try/Except. It should pass now, but it will return a null if there was an error (for instance if you have a material with that name already in the project).

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [February 9, 2019, 10:32pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/8 "2019-02-09T22:32:31Z")

</div>

Hang on, I edited this in the forum and Python gets a little funny with mixed tabs and spaces and failed when i ran it. I have also pulled the transaction out of the loop…

```
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

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

def ToRevitColor(dynamoColor):
	return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)

def ToDynamoObject(revitObject, isRevitOwned=False): # isRevitOwned should be False if Dynamo script created the object.
	return revitObject.ToDSType(isRevitOwned)

doc = DocumentManager.Instance.CurrentDBDocument

names = IN[0]
transparencies = IN[1]
colors = IN[2]

newMaterials = []

TransactionManager.Instance.EnsureInTransaction(doc)
for mat_name, transparency, color in zip(names, transparencies, colors):
	try:
		new_mat_id = Material.Create(doc, mat_name)
		new_mat = doc.GetElement(new_mat_id)
		new_mat.Color = ToRevitColor(color)
		new_mat.Transparency = transparency
		newMaterials.append(ToDynamoObject(new_mat))
	except:
		newMaterials.append(None)
TransactionManager.Instance.TransactionTaskDone()

OUT = newMaterials
```

---

<div class="post-metadata">

**Author:** ![Jacko](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jacko](https://forum.dynamobim.com/u/Jacko)\
**Post date:** [February 9, 2019, 10:55pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/9 "2019-02-09T22:55:53Z")

</div>

Wow! It’s working! Thank you.  
Pitty that it is not overriding materials ☹

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [February 9, 2019, 11:43pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/10 "2019-02-09T23:43:32Z")

</div>

Nah, this creates new materials, i can do a condition to check first if material exists and use that instead of creating a new one.

I will sort in the morning as it’s late where i am.

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [February 10, 2019, 9:38am UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/11 "2019-02-10T09:38:28Z")

</div>

Hi @Jacko

This should now override any materials with the same name.

I have also added an option to disable whether the Material is Owned by Dynamo (this feature can be annoying sometimes as Dynamo keeps track of which Elements it made and always remembers these for modification, now you have a choice of whether you want to do this. I prefer to disable this personally, particularly on a generic script that I will use many times in different situations on the same document).

I have also made sure that even if you are passing in one one item (not in a list), that this will still work and I have added a new OUT variable for errors that will give you a description of the error it encountered.

Additionally, I have commented on what’s going on to make it forum friendly so people new to python can understand step by step.

```
import clr
clr.AddReference("RevitAPI")
from Autodesk.Revit.DB import *
clr.AddReference("RevitServices")
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager

doc = DocumentManager.Instance.CurrentDBDocument

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

################ Definitions ################

# Convert DSCore Colour to Revit Colour...
def ToRevitColor(dynamoColor):
	return Color(dynamoColor.Red, dynamoColor.Green, dynamoColor.Blue)

# Add Dynamo Ownership to Element...
def ToDynamoObject(revitObject, isRevitOwned=False): # isRevitOwned should be False if Dynamo script created the object.
	return revitObject.ToDSType(isRevitOwned)

# Gets the Material Id by name from a list of Materials ...
def GetMaterialIdByName(mat_Name, mats):
	return next((m.Id for m in mats if m.Name == mat_Name), None)

# Used to Ensure that object is in a list...
def tolist(obj1):
	if hasattr(obj1," __iter__"): return obj1
	else: return [obj1]

################ IN Variables ################

names = tolist(IN[0])
transparencies = tolist(IN[1])
colors = tolist(IN[2])
toDynObj = tolist(IN[3])[0]

################ OUT Variables ################

newMaterials = []
err = []

################ Main Body ################

# Get all the Materials that are currently created in the Revit doc...
mats = FilteredElementCollector(doc).OfClass(Material).ToElements()

TransactionManager.Instance.EnsureInTransaction(doc)
for mat_name, transparency, color in zip(names, transparencies, colors):
	try:
		# Check if material exists already...
		new_mat_id = GetMaterialIdByName(mat_name, mats)
		# Create one if it doesn't...
		if not new_mat_id:
			new_mat_id = Material.Create(doc, mat_name)
		# Get Material from Revit doc...
		new_mat = doc.GetElement(new_mat_id)
		# Set Material colour...
		new_mat.Color = ToRevitColor(color)
		# Set Material Transparency...
		new_mat.Transparency = transparency
		
		# If toDynObj is true, Dynamo will own the Elements it creates and overwrite those...
		if toDynObj:
			newMaterials.append(ToDynamoObject(new_mat))
		# if toDynObj is false, Dynamo will not own this Element...
		else:
			newMaterials.append(new_mat)
		err.append(None)
	except Exception, ex:
		newMaterials.append(None)
		err.append(ex.message)
TransactionManager.Instance.TransactionTaskDone()

################ OUT ################

OUT = newMaterials, err  

```

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/0/0/00dea923b87567adc10204842166911d4ce8fdc2.png)

Cheers,  
Dan

---

<div class="post-metadata">

**Author:** ![mimp](https://avatars.discourse-cdn.com/v4/letter/m/47e85d/32.png) [@mimp](https://forum.dynamobim.com/u/mimp)\
**Post date:** [February 11, 2019, 8:15am UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/12 "2019-02-11T08:15:09Z")

</div>

Hi jacek -

Try this code if you can make any sense of it

---

<div class="post-metadata">

**Author:** ![Jacko](https://avatars.discourse-cdn.com/v4/letter/j/4af34b/32.png) [@Jacko](https://forum.dynamobim.com/u/Jacko)\
**Post date:** [February 11, 2019, 9:05am UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/13 "2019-02-11T09:05:41Z")

</div>

@Daniel_Woodcock1 Sir, you are great!  
Well it is a real black magic for me 😃  
I see I have a lot to learn.

---

<div class="post-metadata">

**Author:** ![Daniel\_Woodcock1](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@Daniel\_Woodcock1](https://forum.dynamobim.com/u/Daniel_Woodcock1)\
**Post date:** [February 11, 2019, 2:24pm UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/14 "2019-02-11T14:24:21Z")

</div>

You’re welcome @Jacko! Glad this is now working for you!

Ah, it might seem like black magic now, but I’m sure soon enough, you’ll be up and running with python and this will look simple! It’s scarier than it looks! 😁

---

<div class="post-metadata">

**Author:** ![leneborma](https://avatars.discourse-cdn.com/v4/letter/l/53a042/32.png) [@leneborma](https://forum.dynamobim.com/u/leneborma)\
**Post date:** [August 26, 2021, 6:34am UTC](https://forum.dynamobim.com/t/python-scrips-newbie-expected-an-indented-block/31911/15 "2021-08-26T06:34:21Z")

</div>

Indentation means the space from margin to the begin of characters in a line. Python language is a very sensitive language for indentation, it has caused confusion for many beginners. Putting in an extra space or leaving one out where it is needed will surely generate an error message . Some common causes of this error include:

- Forgetting to indent the statements within a compound statement.
- Forgetting to indent the statements of a user-defined function.

The error message IndentationError: [expected an indented block](http://net-informations.com/python/err/indentation.htm) would seem to indicate that you have an indentation error. It is probably caused by a mix of tabs and spaces .
