How can I check if a component exists or not?

Hi there,
I'm new to Ignition and I'm trying to make an event script on Startup of a Popup that loop over a range of 16 and check first whether a component FlexContainer exists before applying some modifications. Here is my code :

def runAction(self):
# Calling my logger
logger = system.util.getLogger("myLogger")

bin_CI = [int(x) for x in list('{0:016b}'.format(self.view.params.Integer_CI))]
for i in range(16):
	FlexCont = "FlexContainer_" + str(15 - i )
	
	#path_to_component = FlexCont + "/Value"
	
	window = system.gui.getWindow("POP_UP_Cycle0")  # Replace with the actual path to your window
	if window is not None:
	    component = window.getRootContainer().getComponent(FlexCont)
	            
	    if component is not None:
	        if bin_CI[i] == 0:
	            component.getChild("Value").props.style.classes = "Condition_NOK"
	        elif bin_CI[i] == 1:
	            component.getChild("Value").props.style.classes = "Condition_OK"

I get errors because I don't know what functions to use. Can you help me ?

Take a step back and explain what the goal is, instead of HOW you're trying to do it

2 Likes

One thing is it seems like you are mixing up Vision and Perspective here. You are using system.gui.getWindow() which is a Vision only function but then you do component.getChild("Value").props.style.classes which is a perspective scripting style for assigning a value to a prop.

It looks like you just want the styles of a component to be one thing if bin_CI[i] == 0 and another bin_CI[i] == 1. In that case, I would suggest using an expression binding on the style prop and you can use a case or switch or even just an if statement if it really is just between those two styles.

This script seems like overkill to just toggle between two different style classes and scripting is less performant than bindings so it will be slower as well.

5 Likes

Thank you. I didn't realise I was mixing up modules here.
pascal.fragnoud yes, you're right. I actually have multiples pop ups and each of them have multiples FlexContainers that have labels. I want to apply style to the labels. The pop up have same structure but different number of labels. And the styles are goind to be based on the bin_CI value.

bkarabinchak.psi I still need to use a script in order to apply the modifications in one shot instead of configuring the binding for each label.

I just need to know how to check if a label exists before doing anything on it . Just like this :

def runAction(self):

  # Calling my logger for debug purposes
  logger = system.util.getLogger("myLogger")
  
  # Convert integer to array of binary 
  bin_CI = [int(x) for x in list('{0:016b}'.format(self.view.params.Integer_CI))]

  for i in range(16) :
        FlexCont = "FlexContainer_" + str(15 - i )
        
        # Check if FlexContainer_i exists first before applying modif
        if :        
              if bin_CI[i] == 0 :                 
                    self.getChild("FlexListeCI").getChild(FlexCont).getChild("Value").props.style.classes = "Condition_NOK"
                    
              elif bin_CI[i] == 1 :
                    self.getChild("FlexListeCI").getChild(FlexCont).getChild("Value").props.style.classes = "Condition_OK"

Can I ask why? If all these flex containers are identical (or identical sans some information from a parameter), you should have them as a view, and then put them on the page as an embedded view. Then you would only need to setup the binding once and if you ever needed to change it, you would only change it in one place.

If you're familiar with templates in Vision that is essentially the behavior an embedded views give you. I would recommend that over scripting.

1 Like

Don't.
If you really don't want to replicate a binding on several labels, just create a custom property, bind it to your logic, then bind your labels to this custom property.
Don't use a script to set up styles. Really.

1 Like

Indirect bindings are your friend.

1 Like