Template Canvas customizer through scripting

Hi there,

I have a Template canvas object in my window and instead of using the template canvas customizer to add instances of my template, setting its name, position, width, height, parameter (...) . I want to edit those fields through script. Is it possible?

Yes, the property that drives the entire template canvas is just a dataset, and is well-documented. Simply write a script that generates the dataset dynamically with the necessary content.

1 Like

For a dropdown template how do we pass the dataset in to canvas dataset?

I assume this means the dropdown is being rendered by the canvas. If that is the case, you will have to navigate the parent hierarchy to manipulate the dataset. Here is an example of how to do this using the dropdown's propertyChange event handler:

# Written for a dropdown component's propertyChange event handler
# Only execute the dataset manipulation if it is the selected index property that has changed
if event.propertyName == 'selectedIndex' and event.newValue > -1:
	# Import swing utilites and a the template canvas class to retrieve the parent canvas without a recursive function
	from javax.swing import SwingUtilities
	from com.inductiveautomation.factorypmi.application.components import TemplateCanvas
	
	# Get the canvas from the parent hierarchy using swing utilities
	canvas = SwingUtilities.getAncestorOfClass(TemplateCanvas, event.source)
	
	# Check to see if template is being rendered by a canvas
	if canvas is not None:	
		# Get the templates dataset from the canvas
		dataset = canvas.templates
		
		# ***********************
		# newDataset = manipulate dataset in some way here
		# ***********************
		
		# Write manipulated dataset back to the canvas's templates property
		canvas.templates = newDataset