簡単なトコロから、ボタン付きロータリーエンコーダでタイムスライダをスクラブ、ボタンで再生、停止出来るようにしてみました。

間にProcessingで作成したアプリを入れていますが、MayaへpyserialモジュールをインストールしてArduinoとMayaを直接シリアル通信でつなげる事もできるかも。

ロータリーエンコーダが返すグレイコードの扱いがあってるか分かりませんが、以下がArduino IDEのコード。

const int ENCA_PIN PROGMEM = 10;
const int ENCB_PIN PROGMEM =  9;
const int BTN_PIN  PROGMEM =  8;

int lastBtn = HIGH;
int lastu = -1;
int lastv = -1;

int count = 0;

void setup() {
  Serial.begin(115200);
  pinMode(ENCA_PIN, INPUT);
  pinMode(ENCB_PIN, INPUT);
  pinMode(BTN_PIN, INPUT_PULLUP);
}

void loop() {
  int u = digitalRead(ENCA_PIN);
  int v = digitalRead(ENCB_PIN);

  if((lastu != u) || (lastv != v)){
    if((u == 1) && (v == 1)){
      if(lastu == 1){
        count ++;
        sendData("r",count);
        //Serial.println("plus");
      }
  
      if(lastu == 0){
        count --;
        sendData("r",count);
      }
    }
  }

  int btn = digitalRead(BTN_PIN);
  if((btn == LOW) && (btn != lastBtn)){
    sendData("c",0);
    delay(100);
  }
  lastBtn = btn;
  lastu = u;
  lastv = v;
}

void sendData(char *id,int value){
  Serial.print(id);
  Serial.print(",");
  Serial.println(value);
}

ロータリーエンコーダとArduino Uno R3を以下のように結線。

  • CLKとDigital10
  • DTとDigital9
  • SWとDigital8
  • +と5V
  • GNDとGND

次にProcessing、Mayaへ直接melコマンドを送っています、Processing上にハードコードするより、Maya側でコマンドを持っておく方が良いとは思いますが。

import processing.net.*;
import processing.serial.*;

Serial myport;
Client myclient;
int value = 0;
int lastValue = 0;

void setup(){
  size(512,128);
  printArray(Serial.list());
  myport = new Serial(this, Serial.list()[1], 115200);
  myport.bufferUntil('\n');
  myclient = new Client(this,"127.0.0.1", 7001);
  
  noLoop();
}

void draw() {
  background(0);
  textSize(32);
  text("Maya w/Arduino", 10, 30);
}

void stop(){
  myport.stop();
  myclient.stop();
}

void serialEvent(Serial myport){
  String mystr = myport.readStringUntil('\n');
  String data[] = split(trim(mystr), ',');
  //printArray(data);

  if(data[0].equals("r")){
    value = int(data[1]);
    if(lastValue - value < 0){
      myclient.write("currentTime (`currentTime -q`+1);");
    }else{
      myclient.write("currentTime (`currentTime -q`-1);");
    }
    lastValue = value;
  }
  
  if(data[0].equals("c")){
    myclient.write("if(`play -q -st`){play -st false;}else{play -st true;}");
  }
}

後はMayaを起動して以下を実行しmelコマンド用に7001番ポートを開きます。

import maya.cmds as cmds
try:
    cmds.commandPort(name=":7001", close=True)
except:
    cmds.warning('Could not close port 7001 (maybe it is not opened yet...)')

cmds.commandPort(name=":7001", sourceType="mel")

Arduino IDEもProcessingも接続が出来ない場合の処理を省いています。Arduino、Maya(ポートを開ける)、Processingの順に起動してひとまず出来上がり~。

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です