class SynchronizePorts(PyScriptObject): """ Description ----------- Using the I{Synchronize Ports} module, you can synchronize ports of two modules of the same type. For example, it is particularly useful to control and compare two probes on two different datasets. In order to use the module, select the first module to be synchronized, then choose I{Animation/Demos/SynchronizePorts} from the popup menu. The new I{Synchronize Ports} module automatically connects InputA to the selected module. Finally, connect the InputB port to the second module to be synchronized. Alternatively, you can also connect the InputA and InputB ports of the module by hand. Connections ----------- Input a [required] ~~~~~~~ First module to synchronize Input b [required] ~~~~~~~ Second module to synchronize. NOTE: the connected module should be of the same type as the first connected module. """ def __init__(self): self.__connections = [] #name of ports linked between two modules self.__data_a = None #Handle of object connected to input A self.__data_b = None #Handle of object connected to input B self.ports.data.visible = False self.port_input_a = HxConnection(self, "port_input_a", "Input A") self.port_input_a.valid_types = ['HxModule'] self.port_input_b = HxConnection(self, "port_input_b", "Input B") self.port_input_b.valid_types = ['HxModule'] def __del__(self): self.__disconnect_all() self.ports.data.visible = True def update(self): if ((self.port_input_a.source() == None) or (self.port_input_b.source() == None) or ( (self.__data_a != None) and (not self.__data_a.is_same_object(self.port_input_a.source())) ) or ( (self.__data_b != None) and (not self.__data_b.is_same_object(self.port_input_b.source())) ) ): self.__disconnect_all() PyScriptObject.update(self) def compute(self): self.__data_a = self.port_input_a.source() self.__data_b = self.port_input_b.source() if ( ( (self.port_input_a.source() != None) ) or ( (self.port_input_b.source() != None) ) or ( (self.__data_a != None) and (not self.__data_a.is_same_object(self.port_input_a.source())) ) or ( (self.__data_b != None) and (not self.__data_b.is_same_object(self.port_input_b.source())) ) ): self.__disconnect_all() self.__connect_all() PyScriptObject.compute(self) def __connect_all(self): #Links common ports in the modules connected to two inputs A and B self.__connections = [] if (self.__data_a == None) or (self.__data_b == None): return for portname in self.__data_a.portnames: if not hasattr(self.__data_a.ports,portname): continue portA = getattr(self.__data_a.ports,portname) if isinstance(portA, HxConnection): continue portB = getattr(self.__data_b.ports,portname) try: portA.link(portB) self.__data_a.fire() self.__connections.append(portname) except TypeError: continue def __disconnect_all(self): #Unlinks common ports in the two modules if (self.__data_a != None) and (self.__data_b != None): for portname in self.__connections: portA = getattr(self.__data_a.ports,portname) portB = getattr(self.__data_b.ports,portname) portA.unlink(portB) self.__connections=[]