Passing parameter from project script

I have a function in the project script where I am trying to change the value of a parameter on a popup view. Its related to visibility for a label. I have a button which runs the function and its supposed to change the visibility on the label. Using the system.util.logger I have confirmed the correct value is being determined based on the script, however it is not actually changing the visibility of the label.

For the view I have a parameter called LblNoGoVis and have it bidirectional. I am defining that parameter as lblNoGoVis in the call to the script and including that as a parameter that is passed into the function.

image

Here is the script I am calling.

def download_to_cell(cell_id,new_recipe,label_vis):
	"""Check if its a new recipe.  If so check for 9999 in memory cells
	
	Param:
		cell_id (str): Id designation of cell
		new_recipe (bool): Is this new recipe
		label_vis: visibility of message telling operator to edit recipe
	"""
	myBad = 0
	logger = system.util.getLogger("myLogger")
	
	if new_recipe:
		logger.info("New Recipe")
		counter = 1
		while counter < 36:
			#counterToString = counter.toString()
			memTag = '[{}]/Cell/Recipe/Memory/{}'.format(cell_id,str(counter))
			q1 = system.tag.read(memTag)
			memVal = q1.value
			if memVal == 9999:
				logger.info("Value is 9999")
				myBad = 1
				label_vis = "true"
				logger.info(label_vis)
				break
			else:
				counter = counter + 1
	
	if myBad == 0:
		value = 1
		label_vis = "false"
		logger.info(label_vis)
		myTag = '[{}]/Cell/Control/Recipe DownLoad/Request'.format(cell_id)
		system.tag.write(myTag, value)
		system.perspective.navigate(page = '/Machine')

The logger.info(label_vis) is correctly giving me either true or false based on the function but it is not changing the actual parameter on the view. I do have the visibility of the label binded to the view parameter correctly. What am I doing wrong?

You aren’t actually doing anything to change the visibility, you’re just modifying the download_to_cell function’s local label_vis variable.

That parameter is passed by value to the function, meaning it’s essentially just a copy of the boolean value at the time you called the function.

Is there a way to have it change the parameter on the view? Is it possible to change values of things (other than writing to tags) from project scripts outside of putting this script directly on the button instead of in the project scripting?

Make the parameter the label component itself and then modify the property by doing label.meta.visible = False or whatever.

Could you elaborate? I have taken the binding off of the view parameter. Still not working. What would I have to change? Thanks

@michael.black You need to pass the Label object to your function as the parameter rather than passing in the label’s visibility property as the parameter. an example function might look like this:

def func(label):
    label.meta.visible = False

and assuming that you are calling this function from a component script of a component that is a sibling of your label in the component hierarchy you could then call it like this:

myLabel = self.getSibling('theNameOfMyLabel')
func(myLabel)

The visibility property was not being changed because when you pass a boolean to a function in python, your parameter is a reference to the immutable True object or the immutable False object. When you change the parameter, the object itself isn’t changed, the parameter is just changed to refer to a different object. When you pass the label in directly, the label object is mutable, so changing the visibility property via the reference to the label object does actually change the object, so the changes made to the object are retained outside the function.

1 Like