Jump to content

Simple Py Node Example


Recommended Posts

Hi all,

here you have a simple example of Py node with comments to help you understanding how to create your own Nodes:

First, lets see the whole example:

from Mistika.classes import Cconnector
            
def init(self):
    self.addConnector("myInput",Cconnector.CONNECTOR_TYPE_INPUT,Cconnector.MODE_REQUIRED)
    self.addConnector("myOutput",Cconnector.CONNECTOR_TYPE_OUTPUT,Cconnector.MODE_OPTIONAL)    
    self.addProperty("myProperty","default value")
    return True
    
def isReady(self):
    return True

def process(self):
    i=self.firstConnectorByName("myInput")
    o=self.firstConnectorByName("myOutput")
    print "input: "+i.url()
    print i.universalPath().files()
    print "myProperty: "+self.myProperty
    o.setUrl(i.url())
    return True

 

now lets examine de sections:

 

from Mistika.classes import Cconnector

included to avoid writing the whole path to access the Cconnector constants in the code.

The initialization function: init(self)

def init(self):
    self.addConnector("myInput",Cconnector.CONNECTOR_TYPE_INPUT,Cconnector.MODE_REQUIRED)
    self.addConnector("myOutput",Cconnector.CONNECTOR_TYPE_OUTPUT,Cconnector.MODE_OPTIONAL)    
    self.addProperty("myProperty","default value")
    return True

this function initializates the node. It can be used to create the connectors and properties to be used in the node.

the parameter received is the node itself.

The function to create the connectors is:

addConnector (name, type, mode)

the parameters are:

  1. The Connector Name.
  2. The connector Type. it can be either Cconnector.CONNECTOR_TYPE_INPUT or Cconnector.CONNECTOR_TYPE_OUTPUT for input or output connectors.
  3. The connector mode. It can be Cconnector.MODE_REQUIRED or Cconnector.MODE_OPTIONAL defining if the connector must be connected to enable to workflow, of if it is an optional input to the node.

Properties can also be added with :

addProperty (name, defaultValue)

the parameters are:

  1. The parameter Name
  2. The default Value. This parameter is optional. If it is not defined, the attribute is initialized to null.

Finally, the init function returns True. meaining the initialization has been done successfully. The function can return False on error.

The ready function: isReady(self)

def isReady(self):
    return True

This function is used to define if the node is ready for processing (returning True) or not ready (returning False). It can be used to validate the integrity of the inputs and properties, to be sure the node is not processed if relevant information is missing.

The Process function: process(self)

def process(self):
    i=self.firstConnectorByName("myInput")
    o=self.firstConnectorByName("myOutput")
    print "input: "+i.url()
    print i.universalPath().files()
    print "myProperty: "+self.myProperty
    o.setUrl(i.url())
    return True

Finally, the process function executes the task

In this case, we just print in the console the contents of the property and the input files

in order to access the input connector, the function firstConnectorByName is used.

This function returns the first input connector found with the given Name.

the prints show the url defined in the input connection with

print "input:"+i.url()

then print the list of all the files in the input sequence with 

print i.universalPath().files()

Note: Dealing with Universal Paths is something we will cover in a different post.

then prints the property value with 

print "myProperty: "+self.myProperty

and finally, it sets the output connector with the same sequence found in the input connector with

o.setUrl(i.url())

That will send the unaltered sequence to the next node.

Returning True to indicate success

  • Like 1
Link to comment
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.