Run python inside openFrameworks (c ++).
This article is just a personal memorandum that I tried the article of the great pioneer.
This article is from @ Hzikajr's article just posted. http://qiita.com/Hzikajr/items/afe73cb287af5ab90265 is. You should definitely read it rather than my article like this. Or rather read it. There are no important points in this article.
pyenv referred to here: http://www.python-izm.com/contents/basis/pyenv.shtml
Personally, I was struggling to link python and oF, so it was very helpful. The test is in exactly the same environment as @Hzikajr. satoruhiga's ofxPy https://github.com/satoruhiga/ofxPy I also tried some of the examples in.
bin/data/python/test_script.py
print('hello from test_script.py')
def my_fn():
print ('hello from python function')
def my_fn2(theta):
import math
a = math.sin(theta * math.pi)
return a
def get_random():
import random
return (random.random(), random.random())
def size_expression(t):
import math
return abs(math.sin(t * math.pi) + math.sin(t * math.pi * 1.5))
ofApp.h
#pragma once
#include "ofMain.h"
#include "ofxPy.h"
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxPy::Context python;
};
ofApp.cpp
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
putenv((char *)"PYTHONHOME=/Users/ksumiya/.pyenv/versions/anaconda3-4.3.1");
python.setup();
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(0);
// append data/python to PYTHONPATH
python.appendPath(ofToDataPath("python"));
// import and call python script function
python.exec("import test_script; test_script.my_fn()");
}
//--------------------------------------------------------------
void ofApp::update(){
ofSetWindowTitle(ofToString(ofGetFrameRate()));
}
//--------------------------------------------------------------
void ofApp::draw(){
// get tuple return value
auto v = python.eval<ofxPy::tuple>("test_script.get_random()");
// unpack and cast array-like object
float x = ofxPy::get<float>(v, 0) * ofGetWidth();
float y = ofxPy::get<float>(v, 1) * ofGetHeight();
ofDrawRectangle(x, y, 10, 10);
// get function and call with argument
float s = python.eval("test_script.size_expression").call(ofGetElapsedTimef()).cast<float>();
ofDrawCircle(ofGetMouseX(), ofGetMouseY(), s * 50);
float a = python.eval("test_script.my_fn2").call(0.25).cast<float>();
string msg;
msg += ofToString(1/a) + "\n";
ofSetColor(255);
ofDrawBitmapString(msg, 100, 100);
}
I only added the `my_fn2``` part. As a result, since it is 1 / sin (pi / 4), it becomes ``` sqrt (2)`
, and ``` 1.414 ...
is output.
Pioneer, great ...
Recommended Posts