### Navigation

- index
- next |
- previous |
- NDK Dev Guide »
- Shared Op Functionality »

# Executable Ops

The vast majority of the time the tree is evaluated in response to either a viewer or a write node causing a render. The write node relies on what is known as ‘executable’ functionality to enable this to happen without the viewer being connected. A node can use this executable mix in class to allow any arbitrary calculation to occur outside of the standard viewer processing tree, in response to a user interaction event (generally implemented as a button knob). The most common example is where you might want to generate some data which relies on processing the entire image sequence. For example, if you’re writing a motion vector generator you might want to know the maximum amount of inter-frame motion to be seen throughout the full range of frames. You might want to have an “Analyse” button that looks at every pair of consecutive frames and keeps a running guess of how much motion is observed throughout the sequence, and then updates some motion vector generator parameters accordingly. In addition, setting a Node up as a Render Node includes the Node in NUKE’s “Render” menu, allowing the user to call the Node from “Render All” or “Render Selected” options.

The NormaliseExecute.cpp example in the NDK creates an op which extends the Normalise example to search for the highest value over a series of frames. That is, we’re going to have a button that first causes NUKE to scan through every frame of the sequence, find the overall max color value, and store it as an Float_knob. The value in that knob will then be multiplied by every pixel to ‘normalise’ the image to 1.0. The first step is to instruct your plug-in to inherit from the mix in Executable class, this tells NUKE to treat it as a Render Node when required. When inheriting from this class, three key functions need to be overloaded; beginExecuting(), execute(), and endExecuting(). The beginExecuting() function is called once the Node has been told to render, and can be used to set up any data structures you’d like to store running data in. The execute() function is called on each frame, grabbing the OutputContext::defaultContext() in this function will give you back all your frame and view information, and calling input(n) will give you back the input Op for the corresponding context. Once all the frames and views you specified when invoking the Render have been processed, the endExecuting() function will be called.This can be used to perform any analysis or clean-up of the data you collected over the render. For example, closing output files, performing cluster analysis etc .The Executable interesting parts of the code are as follows:

    class NormaliseExecute : public Iop, Executable
    {

      // snip....
      /// executable methods

      void beginExecuting();
      void endExecuting();
      void execute();
      virtual Executable* executable() { return this; }

    void NormaliseExecute::beginExecuting()
    {
      std::cerr << "Begin Executing." << std::endl;
      _maxValue = _calcMaxValue  = 0;
    }

    void NormaliseExecute::endExecuting()
    {
      std::cerr <<"End Executing." << std::endl;
      knob("maxValue")->set_value( _calcMaxValue );
    }

    void NormaliseExecute::execute()
    {
      // do anaylsis for current frame
      Format format = input0().format();

      // these useful format variables are used later
      const int fx = format.x();
      const int fy = format.y();
      const int fr = format.r();
      const int ft = format.t();

      ChannelSet readChannels = input0().info().channels();

      Interest interest( input0(), fx, fy, fr, ft, readChannels, true );
      interest.unlock();

      // fetch each row and find the highest number pixel
      _maxValue = 0;
      for ( int ry = fy; ry < ft; ry++) {
        progressFraction( ry, ft - fy );
        Row row( fx, fr );
        row.get( input0(), ry, fx, fr, readChannels );
        if ( aborted() )
          return;

        foreach( z, readChannels ) {
          const float *CUR = row[z] + fx;
          const float *END = row[z] + fr;
          while ( CUR < END ) {
            _calcMaxValue = std::max( (float)*CUR, (float)_calcMaxValue );
            CUR++;
          }
        }
      }
    }

In the above snippet, you can see how our global (over the sequence) variables are initialised in the beginExecuting() function. The execute() function is then used to read in an image, and update the running global counter. Finally, only the entire sequence batch is finished, the endExecuting() function is called to calculate the average colour of all pixels in the sequence, and update our _maxValue knob. Note there is one last function, executable(), that must be overridden for your plug-in to be treated as a Render style Node.

The following snippet shows the operation of the engine() function. Also shown is how to use a Python script knob, a PyScript_knob, to create a button for invoking the batch operation of the plug-in.

    //...continued

    void NormaliseExecute::engine ( int y, int x, int r,
                                  ChannelMask channels, Row& row )
    {
      Row in( x,r);
      in.get( input0(), y, x, r, channels );
      if ( aborted() )
        return;

      foreach( z, channels ) {
        float *CUR = row.writable(z) + x;
        const float* inptr = in[z] + x;
        const float *END = row[z] + r;
        while ( CUR < END ) {
            *CUR++ = *inptr++ * ( 1. / _maxValue );
        }
      }
    }

    void NormaliseExecute::knobs( Knob_Callback f ) {
       Float_knob(f,  &_maxValue, IRange(0,5), "maxValue", "Max");
       Divider(f);
       const char* renderScript = "currentNode = nuke.toNode(\"this\")\n"
       "nodeList = [currentNode]\n"
       "nukescripts.render_panel(nodeList, False)\n";
       PyScript_knob(f, renderScript, "Get Max Value");
    }

In both these circumstances the python nukescripts.render_panel and TCL execute_panel provide a wrapper around the underlying python nuke.execute and TCL execute respectively. These wrappers provide a user interface panel allowing selection of frame ranges, views and so on. In some circumstances you may not wish to allow this selection. For example you may instead want the user to position the playhead on a particular frame and then analyse at that particular frame, without adding an extra user interface panel. To do this you’d instead directly use the relevant execute method:

    PyScript_knob(f, "nuke.execute(nuke.thisNode(), nuke.frame(), nuke.frame(), 1)", "calcMaxThisFrame", "Calculate Max Value This Frame")

### Navigation

- index
- next |
- previous |
- NDK Dev Guide »
- Shared Op Functionality »