Rename Family Types adding a prefix excluding already named

Hello everyone.
I’ve managed to create a script with a pyhton node inspired in some Clockwork Package code to pickup all the Type Names of a certain category and rename all of them adding a prefix. It works pretty well, the only problem is that I can’t find a way to exclude the types that already have the prefix in order to not have elements with a Type Name with infinite repeated prefixes every time I run the script.
Attached is the script image and this is the code inside the Python Node:

import sys
import clr
clr.AddReference('RevitAPI')
import Autodesk
from Autodesk.Revit.DB import *
![RenameTypes|690x186](upload://qw4fONHvLRKIbAITzRc2vRzK9j5.png)

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

doc = DocumentManager.Instance.CurrentDBDocument
items = UnwrapElement(IN[0])
names = IN[1]

def SetElementName(item, name):
	if item.GetType().ToString() == "Autodesk.Revit.DB.FamilyParameter":
		try:
			doc.FamilyManager.RenameParameter(item, name)
			return True
		except: return False	
	else:
		try:
			item.Name = name
			return True
		except: return False
			

TransactionManager.Instance.EnsureInTransaction(doc)
if isinstance(IN[0], list):
	if isinstance(names, list): OUT = [SetElementName(x, y) for x, y in zip(items, names)]
	else: OUT = [SetElementName(x, names) for x in items]
else:
	if isinstance(names, list): OUT = SetElementName(items, names[0])
	else: OUT = SetElementName(items, names)
TransactionManager.Instance.TransactionTaskDone()
![RenameTypes|690x186](upload://qw4fONHvLRKIbAITzRc2vRzK9j5.png)

Just use String.StartsWith. If the element begins with the prefix use the current name, otherwise add the prefix.

1 Like

@Nick_Boyts could you please help me with the Python function?:

def SetElementName(item, name):
	if item.GetType().ToString() == "Autodesk.Revit.DB.FamilyParameter":
		if not item.GetType().ToString().StartsWith(IN[2]):
			try:
				doc.FamilyManager.RenameParameter(item, name)
				return True
			except: return False		
	else:
		try:
			item.Name = name
			return True
		except: return False

Not sure what I’m doing wrong because it is still adding more and more prefixes :sweat_smile:

You’re not getting the name of the parameter. You’re comparing the type again.
image

Thank you @Nick_Boyts but it is now returning only false values :confused:

def SetElementName(item, name):
	if item.GetType().ToString() == "Autodesk.Revit.DB.FamilyParameter":
		if not item.Name.StartsWith(IN[2]):
			try:
				doc.FamilyManager.RenameParameter(item, name)
				return True
			except: return False		
	else:
		try:
			if not item.Name.StartsWith(IN[2]) == name:
				item.Name = name
			return True
		except: return False	

I know this is not a Python learning forum but if you have any clue that could help I’ll be thankful

Output all of your values before checking them. That way we can compare and be sure that the values your code is using are actually what you’re expecting.

This is how I finally solved it:

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

clr.AddReference('RevitServices')
import RevitServices
from RevitServices.Persistence import DocumentManager
from RevitServices.Transactions import TransactionManager
doc = DocumentManager.Instance.CurrentDBDocument

def tolist(obj1):
	if hasattr(obj1,"__iter__"): return obj1
	else: return [obj1]

elems = tolist(UnwrapElement(IN[0]))

outList = []
TransactionManager.Instance.EnsureInTransaction(doc)
for e in elems:
	id = e.GetTypeId()
	elemess = doc.GetElement(id)
	nombre = elemess.get_Parameter(BuiltInParameter.ALL_MODEL_TYPE_NAME).AsString()
	if not nombre.StartsWith(IN[1]):
		elemess.Name = IN[1] + nombre
TransactionManager.Instance.TransactionTaskDone()

OUT = outList

RenameTypes_01

Thank you @Nick_Boyts for your help.