List project templates

Hello.

Currently the function system.gui.getWindowNames() returns a list of the paths of all windows in the current project, sorted alphabetically. That is good! :smiley:
But does exist a function to get a list of all templates in the current project? :astonished:

Thanks in advance.

unfortunately there isn’t a built in method for getting a list of templates within scripting.

A little late on this one. In case anyone is looking for an answer.

def getTemplateResourceType():
	"""Resource type string."""
	return 'component-template'


def getTemplateFilter():
	"""Filter throw away for template types."""
	return lambda x: bool(x.getResourceType() == getTemplateResourceType())


def getContext(event):
	"""Context."""
	return event.source.parent.getAppContext()


def getProjectReference(event):
	"""Project reference."""
	return getContext(event).getProject()


def getTemplateFolderMembers(event, folderName):
	"""
	Returns a list of resource objects.
	# Requires an event object and a template folder path. 
	# i.e. 'tank_components'
	"""
	if event and folderName:
		context = getContext(event)
		project_obj = context.getProject()
		# Get the folders resource id from the Template Manager.
		folder_resource_id = context.getTemplateManager().getId(folderName).getLeft()
		# Get the folder resource_object by the id.
		resource = project_obj.getResource(folder_resource_id)
		# get the folder uuid.
		folder_uuid = resource.getDataAsUUID()
		# returns an array of template objects under the specified folder.
		return project_obj.browseFolder('fpmi', [getTemplateResourceType()], folder_uuid)
	return []


def getAllTemplates(event):
	"""
	Returns a list of all template resource objects.
	# Requires an event object. 
	"""
	if event:
		# Get all resources.
		resources = getProjectReference(event).getResources()
		# returns an array of template objects.
		return filter(getTemplateFilter(), resources)
	return []


#example
for resource in getAllTemplates(event):
	print resource.getName()

#example
folder_name = 'tank_components'
for resource in getTemplateFolderMembers(event, folder_name):
	print resource.getName()
4 Likes