Tag Browse Tree - filterTag - stop to a certain list condition

Hello am looking a way to filter my tagBrowse tree to remove certain folder and stop to a certain level when it find the typeId that I want. I would like to remove some FOlder with a list of it and same for the typeId. I got some script to remove all none UDT tag to be in the tagBrowse Tree but I can stop to catch and stop to certain level.
This is the code I use:

def filterTag(self, tag):

	
	from com.inductiveautomation.ignition.common.tags.config.types import TagObjectType

	if tag.objectType == TagObjectType.Folder:
		results = system.tag.browse(tag.fullPath,{'tagType':'UdtInstance','recursive':True}).getResults()
		if results:
			return True
		return False
	
	if tag.objectType == TagObjectType.UdtInstance:
		return True
	
	if tag.objectType == TagObjectType.AtomicTag:
		parentConfig = system.tag.getConfiguration(tag.fullPath.parentPath,False)
		if parentConfig[0]['tagType'] == TagObjectType.UdtInstance:
			return True
			
	return False

Update: I found a way but not sure its very optimized. Let see if anyone have a cleaner way to do it.

	udtList = ["AIN_DDT","ALM_DDT","DIDO_DDT","MOTOR_DDT","PID_DDT","TOT_DDT","TRIP_DDT","VALVE_DDT"]
	from com.inductiveautomation.ignition.common.tags.config.types import TagObjectType
	
	if tag.objectType == TagObjectType.Folder:
		for x in udtList:
			r1 = system.tag.browse(tag.fullPath,{'tagType':'UdtInstance',"typeId":x,'recursive':True}).getResults()
			if r1:
				return True
	
		return False
	if tag.objectType == TagObjectType.UdtInstance:
		parentConfig = system.tag.getConfiguration(tag.fullPath,False)
		if parentConfig[0]['typeId'] in udtList:
			return True
		return False
		
	if tag.objectType == TagObjectType.UdtInstance:
		return True
		if tag.objectType == TagObjectType.AtomicTag:
			parentConfig = system.tag.getConfiguration(tag.fullPath.parentPath,False)
			if parentConfig[0]['tagType'] == TagObjectType.UdtInstance:
				return True
				
		return False
		
		
	if tag.objectType == TagObjectType.AtomicTag:
		parentConfig = system.tag.getConfiguration(tag.fullPath.parentPath,False)
		tagConfig = system.tag.getConfiguration(tag.fullPath,False)
		if tagConfig[0]['tagType'] == TagObjectType.AtomicTag:
			return False
		if parentConfig[0]['tagType'] == TagObjectType.UdtInstance:
			return True
		else:
			return False
	return False

image

Here's a refactor that should do the same thing as the above code:

	udtList = ["AIN_DDT","ALM_DDT","DIDO_DDT","MOTOR_DDT","PID_DDT","TOT_DDT","TRIP_DDT","VALVE_DDT"]
	from com.inductiveautomation.ignition.common.tags.config.types import TagObjectType
	
	if tag.objectType == TagObjectType.Folder:
		return any(system.tag.browse(tag.fullPath, {'tagType':'UdtInstance', "typeId":x, 'recursive':True}).getResults() for x in udtList)
	
	if tag.objectType == TagObjectType.UdtInstance:
		return system.tag.getConfiguration(tag.fullPath, False)[0]['typeId'] in udtList or True
	
	if tag.objectType == TagObjectType.AtomicTag:
		tagConfig = system.tag.getConfiguration(tag.fullPath, False)
		if tagConfig[0]['tagType'] == TagObjectType.AtomicTag:
			return False
	
		parentConfig = system.tag.getConfiguration(tag.fullPath.parentPath, False)
		return parentConfig[0]['tagType'] == TagObjectType.UdtInstance
	
	return False

It actually work great but I still going too deep. I got a UDT call HMIMEMORY and I don't want it... And inside this one I have LBLMULTI that I dont want to show too.
image