Developer forum
Showing results for 
Search instead for 
Did you mean: 

Python Error with

Does anyone know why when I run the script with the Python plug-in I get this error:
Bad Name: A1 Landscape AttributeError: 'NoneType' object has no attribute 'db'

Below is the Python script:
import os
import sys
import re
import xlrd

excelFileName = "LayoutBook.xls"
excelFilePath = os.path.join (os.path.dirname (sys.argv[0]), excelFileName)

if os.path.isfile (excelFilePath) is False:
	sys.exit ("File does not exists: {}".format (excelFilePath))

xl_workbook = xlrd.open_workbook(excelFilePath)
xl_sheet = xl_workbook.sheet_by_index(0)

layoutTree = GetNavigatorTree (API_LayoutMap)
subSetTree = [(list(layoutTree)[0], 0)]

def GetItemsFromTree (tree, typeSet = None):
	l = []
	for k in tree.keys():
		if typeSet is None or k.itemType in typeSet:
			l.append (k)
		l.extend (GetItemsFromTree (tree, typeSet))
	return l

masters = GetItemsFromTree (layoutTree, {API_MasterLayoutNavItem})

def GetChildrenFromTree (tree, subSetTree):
	if not subSetTree:
		return list(tree.keys())
	for k in tree.keys():
		if == subSetTree[0][0].name and k.uiId == subSetTree[0][0].uiId:
			return GetChildrenFromTree (tree, subSetTree[1:])
	return []

def FindItemByName (items, name):
	for item in items:
		if == name:
			return item
	PrintError('Bad Name: {}'.format(name))
	return None

def FindItemByIDName (items, ID, name):
	for item in items:
		if == name and item.uiId == ID:
			return item
	return None

def CreateSubSetItem (ID, name, parent):
	newSubSet = APIObject ()
	newSubSet.uiId = ID
	newSubSet.customUiId = True = name
	newSubSet.customName = True
	newSubSet.itemType = API_SubSetNavItem
	newSubSet.mapId = API_LayoutMap
	CreateNavigatorItem (newSubSet, parent)
	return newSubSet

def CreateOrChangeLayoutItem (ID, name, masterName, parent, oldItem = None):
	newLayout = APIObject () if oldItem is None else oldItem
	newLayout.uiId = ID
	newLayout.customUiId = True = name
	newLayout.customName = True
	newLayout.itemType = API_LayoutNavItem
	newLayout.mapId = API_LayoutMap
	newLayout.db = APIObject ()
	newLayout.db.typeID = APIWind_LayoutID
	masterLayoutItem = FindItemByName (masters, masterName)
	newLayout.db.masterLayoutUnId = masterLayoutItem.db.databaseUnId
	if oldItem is None:
		parentChildren = GetChildrenFromTree (layoutTree, subSetTree)
		if not parentChildren:
			CreateNavigatorItem (newLayout, parent)
			CreateNavigatorItem (newLayout, parent, parentChildren[-1])
		ChangeNavigatorItem (newLayout)
	return newLayout

def GetParent (actIndex):
	global subSetTree
	for i in reversed(range(1,len(subSetTree))):
		if subSetTree[1] >= actIndex:
			del subSetTree
	return subSetTree[-1][0]

newSubSetNumber, newLayoutNumber = 0, 0
for row_idx in range(1, xl_sheet.nrows):
	for column_idx in range(1, xl_sheet.ncols):
		idCell = xl_sheet.cell(row_idx, column_idx)
		if idCell.value != '':
			ID = idCell.value.strip()
			NAME = xl_sheet.cell(row_idx, column_idx + 1).value.strip()
			MASTER_NAME = xl_sheet.cell(row_idx, column_idx + 3).value.strip()

			PARENT = GetParent(column_idx)
			oldItem = FindItemByIDName (GetChildrenFromTree (layoutTree, subSetTree), ID, NAME)
			if MASTER_NAME == '':
				newSubSetNumber += 1
				if oldItem is None:
					subSetTree.append((CreateSubSetItem (ID, NAME, PARENT), column_idx))
					subSetTree.append((oldItem, column_idx))
				newLayoutNumber += 1
				CreateOrChangeLayoutItem (ID, NAME, MASTER_NAME, PARENT, oldItem)

			layoutTree = GetNavigatorTree (API_LayoutMap)
print("Created {} subsets and {} layouts based on {}".format(newSubSetNumber, newLayoutNumber, excelFileName))
Archicad 23 (3003), Windows 10, AMD RYZEN 7 3900 (64 GB RAM)

Figured it out.

Just had to have a Master Layout named correctly "A1 Landscape".
Archicad 23 (3003), Windows 10, AMD RYZEN 7 3900 (64 GB RAM)

Still looking?

Browse more topics

Back to forum

See latest solutions

Accepted solutions

Start a new discussion!