LoginSignup
0
1

More than 5 years have passed since last update.

PythonやBlenderでmrubyを動かす

Last updated at Posted at 2015-02-02

はじめに

mrubyをcloneしてビルドしておきます。
pythonはblenderが使っているのが、3.4.2なので、
これに近いやつだと以下のコードで動きます。

この記事の応用例のご紹介

C

#include <stdio.h>
#include <assert.h>
#include "mruby.h"
#include "mruby/proc.h"
#include "mruby/compile.h"

// "(1..10).each { |x| p x }"

int spam_hello(char *name)
{
  assert(name != NULL);
  mrb_state* mrb = mrb_open();
  mrb_load_string(mrb, name);
  mrb_close(mrb);

  return printf("hello %s\n", name);
}
gcc -I$HOME/local/src/mruby/include -shared -fPIC sapm.c -o sapm.dylib $HOME/local/src/mruby/build/host/lib/libmruby.a

Python

from ctypes import *

class MrubyPyError(Exception):
    pass

class MrubyPy:
    c_spam_module = CDLL('./sapm.dylib')
    c_spam_hello_func_type = CFUNCTYPE(c_int, c_char_p)
    c_spam_hello_func = cast(c_spam_module.spam_hello, c_spam_hello_func_type)

    @classmethod
    def hello(cls, name):
        if name == None or not isinstance(name, str):
            raise MrubyPyError('invalid argument')
        ret = cls.c_spam_hello_func(create_string_buffer(name.encode('UTF-8')))
        #ret = cls.c_spam_hello_func(create_string_buffer(name))
        print ('c_spam_hello_func returned:', ret)

if __name__ == '__main__':
    #MrubyPy.hello('(1..10).each { |x| p x }')
    f = open("hoge.rb","r",encoding="UTF-8")
    ruby=f.read()
    print(ruby)
    MrubyPy.hello(ruby)

OSXのblenderの場合

/Applications/blender.app/Contents/MacOS/blender --background --python hoge.py
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1