Klone und baue mruby. Da Blender 3.4.2 für Python verwendet, Wenn es nahe daran liegt, funktioniert es mit dem folgenden Code.
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)
/Applications/blender.app/Contents/MacOS/blender --background --python hoge.py
Recommended Posts