Pymel / Python Coding

Processing to Maya

Set up command ports in Maya

First thing is to open a port in maya this enables a processing sketch to communicate in real time back to maya.
The port numbers need to match up with those used in the sketch.

// mel script open port
commandPort -name "127.0.0.1:6004"
// mel script close port
commandPort -name "127.0.0.1:6004" -cl

Example of sending mouse position from processing sketch to sphere in maya and keyframing.
This isn’t anything fancy – in fact this can be done directly in maya, but understanding about using command port and communicating with processing opens up more possibilities.

Processing to Maya

    import processing.net.*;
    float x;
    float y;
    float z;

    Client client;
    
    void setup(){
      // mel code: commandPort -n "localhost:12000";
      client = new Client(this, "127.0.0.1", 6004);
      client.write("sphere -n \"new_sphere\";\n");
      frameRate(25);
      
    }
    
    void draw(){
      if (keyPressed == true) {
      float x = (mouseX-width/2) *0.2;
      float y = (mouseY-height/2)*0.2;
      int s = frameCount;
     
      // mel code to keyframe sphere
      client.write("currentTime "+s+";\nsetKeyframe -v "+x+" \"new_sphere.translateX\";");
      client.write("currentTime "+s+";\nsetKeyframe -v "+y+" \"new_sphere.translateY\";");
      }
    }

Leave a Reply

Your email address will not be published. Required fields are marked *