python, openFrameworks (c ++)

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

python, openFrameworks (c ++)
python C ++ notes
Python C / C ++ Extension Pattern-Pointer
Next Python in C
C API in Python 3
ABC147 C --HonestOrUnkind2 [Python]
Extend python in C ++ (Boost.NumPy)
ABC163 C problem with python3
Python, Java, C ++ speed comparison
PyTorch C ++ VS Python (2019 Edition)
Python
Binary search in Python / C ++
Multi-stage selection (C # / Python) (old)
I tried Python C extension
Python started by C programmers
ABC188 C problem with python3
ABC187 C problem with python
ABC memorandum [ABC159 C --Maximum Volume] (Python)
Solve ABC163 A ~ C with Python
Call C from Python with DragonFFI
Create Awaitable with Python / C API
Multi-stage selection (Go / C # / Ruby / Python)
Call popcount from Ruby / Python / C #
Introduction to Protobuf-c (C language ⇔ Python)
Python cheat sheet (for C ++ experienced)
Solve ABC168 A ~ C with Python
ABC memorandum [ABC161 C --Replacing Integer] (Python)
Solve ABC036 A ~ C in Python
Tips for calling Python from C
Execute Python code from C # GUI
How to wrap C in Python
ABC memorandum [ABC158 C --Tax Increase] (Python)
Solved AtCoder ABC 114 C-755 with Python3
C / C ++ programmer challenges Python (first step)
Solve ABC162 A ~ C with Python
Run Python scripts synchronously from C #
Solve ABC167 A ~ C with Python
ABC128 A, B, C commentary (python)
Solve ABC158 A ~ C with Python
ABC126 A, B, C Explanation (python)
Solve ABC037 A ~ C in Python
Write C unit tests in Python
AtCoder Beginner Contest 174 C Problem (Python)
Call C / C ++ from Python on Mac
Call c language from python (python.h)
msgpack deserialization performance comparison (C ++ / Python / Ruby)
kafka python
Python basics ⑤
python + lottery 6
Python Summary
Built-in python
Python comprehension
Python technique
Studying python
Python 2.7 Countdown
Python memorandum
Python FlowFishMaster
Python service
python tips
python function ①
Python basics