Check to see if template path exists?

I’m trying to dynamically load templates via button press; I would like the button to show only if a template exists before loading it into a template holder

I’ve figured out that I can “load” a non-existent template into a hidden template holder, then run .getLoadedTemplate(), which will return None if it doesn’t exist… but that seems like an awfully hacky way around this problem

Any ideas?

Thanks!

bump? Project due Monday

This should get you started:

p = event.source.getAppContext().getProject() #Get project reference from a vision component. 
_res = p.getResourcesOfType("fpmi","component-template") #Get a list of all templates
for res in _res:  #Loop through resource list
	print p.getFolderPath(res.resourceId)  #Print the path of all templates.  You could match here or something.  Maybe make a list when the window first opens, and compare "return "templatename" in listoftemplates
3 Likes

Yes! That is exactly what I need

I kind of forgot about getResourcesOfType and that. I made a post where a few of the Ignition gurus helped me iterate through templates, that was back in October last year, and when I was still brand new to Ignition. Could have gotten it from there but I didn’t even think about that.

Thank you!

Note that you’ll have to change that code completely once you upgrade to 8.0+ - the entire project interface changed to accommodate resources on the filesystem.

1 Like

Thanks for the heads-up. Fortunately we have a staging environment and even if this functionality can’t be duplicated, it would be easy enough to create a fallback

But definitely something that needs to be considered

In my case I was using global templates, updated first line to

p = event.source.getAppContext().getGlobalProject().getProject()

big up

Here’s the updated version for 8.0:

from com.inductiveautomation.ignition.common.project.resource import ResourceType
from com.inductiveautomation.vision.api.client import VisionClientInterface

project = event.source.getAppContext().getProject() #Get project reference from a vision component. 
resource_type = ResourceType(VisionClientInterface.VISION_MODULE_ID, 'templates')
resources = project.getResourcesOfType(resource_type) #Get a list of all templates
paths = [str(resource.getResourcePath()) for resource in resources]

if 'com.inductiveautomation.vision/templates/foo' in paths:
    print 'template exists'
2 Likes

com.inductiveautomation.factorypmi.application.model.TemplateManager#RESOURCE_TYPE is the constant for the template resource type, if you want to use that instead of building your own.

1 Like

Yeah that cleans it up a bit, thanks

def template_exists(template_name):
	"""
	Syntax: template_exists(template_name)

	Parameter:
	template_name: A string of the template's name. If in a directory prefix with the directory name: path/to/template

	Return Type: This method returns a Boolean value of class bool. This method returns True if the template exists otherwise returns False. 
	"""
	from com.inductiveautomation.factorypmi.application.model import TemplateManager
	
	context = event.source.getAppContext()
	project = context.getProject()
	resources = project.getResourcesOfType(TemplateManager(context).RESOURCE_TYPE)
	paths = [str(resource.getResourcePath()) for resource in resources]
	
	if 'com.inductiveautomation.vision/templates/' + template_name in paths:
		return True
	return False

print template_exists('foo')

As long as we’re golfing, this should also work and be marginally more efficient (short circuits, plus doesn’t build a list in memory):

def template_exists(template_path):
	"""
	Syntax: template_exists(template_path)

	Parameter:
	template_path: A string of the template's name. If in a directory prefix with the directory name: path/to/template

	Return Type: This method returns a Boolean value of class bool. This method returns True if the template exists otherwise returns False. 
	"""
	from com.inductiveautomation.factorypmi.application.model import TemplateManager
	
	context = event.source.getAppContext()
	project = context.getProject()
	resources = project.getResourcesOfType(TemplateManager.RESOURCE_TYPE)
	return any(template_path in resource.folderPath for resource in resources)

print template_exists('foo')
1 Like

You win; I’m handing over the solution point. Kudos on the use of any, every time I see that guy I remind myself I should be using it more

Nice improvements in readability and performance

1 Like