Run mruby with Python or Blender

Introduction

Clone mruby and build it. Since python is used by blender in 3.4.2, If it is close to this, it will work with the following code.

Introduction of application examples of this article

-Blender trusterd.py? (I ran the http2 server trusterd in mruby in Python)

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)

For OSX blender

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

Recommended Posts

Run mruby with Python or Blender
Run Blender with python
Run Python with VBA
Run prepDE.py with python3
Operate Blender with Python
Run iperf with python
Run python with PyCharm (Windows)
Run Python with CloudFlash (arm926ej-s)
Let's run Excel with Python
Run Label with tkinter [Python]
Run DHT22 with RasPi + Python
Run Rotrics DexArm with python API
Run XGBoost with Cloud Dataflow (Python)
Run Aprili from Python with Orange
Run python3 Django1.9 with mod_wsgi (deploy)
Until you run python with apache
Easy modeling with Blender and Python
Get started with Python in Blender
LD_LIBRARY_PATH and Python and Paiza.io problem or run mruby on paiza.io and communicate http2
Blender 2.82 or later + python development environment notes
Run a Python web application with Docker
Try assigning or switching with Python: lambda
[Blender x Python] Let's get started with Blender Python !!
FizzBuzz with Python3
Scraping with Python
Statistics with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Blender 2.9 Python Extrude extrude
Play with 2016-Python
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Bingo with python
Zundokokiyoshi with python
Excel with Python
Microcomputer with Python
Cast with python
Python> Run with run-time arguments> Use import argparse
Run pip install on MacOS Python 3.7 or later
Run VMware vSphere 6 vSphere API with Python script (pyvmomi)
Run Flask on CentOS with python3.4, Gunicorn + Nginx.
Little-known Python language specification (compatible with Python 3.4 or later)
Memo for editing scenes with Blender python (W.I.P.)
[Blender x Python] Think of code with symbols
Serial communication with Python
Zip, unzip with python
Django 1.11 started with Python3.6
Primality test with Python
Python with eclipse + PyDev.
Socket communication with Python
Data analysis with python 2
Python> list> extend () or + =
Scraping with Python (preparation)
Learning Python with ChemTHEATER 03
Sequential search with Python
Run Python from Excel VBA with xlwings & tutorial supplement
Handling yaml with python