On Sep 12, 3:30 pm, "Edward K. Ream" <[email protected]> wrote:

> So this is a fun puzzle...solvable, I hope, using ctypes...

This is a ton of fun.  Here is the latest script.

**Warning**: This script must be run with Python 3.x: it will crash
Python 2.x.

    import ctypes
    m = ctypes.PyDLL(r'C:\apps\Blender\python32.dll')
    m.Py_Initialize()

    # Import imp.
    s = ctypes.c_char_p(b'imp')
    m.PyImport_ImportModule.restype = ctypes.py_object
    o = m.PyImport_ImportModule(s)
    print(o

And here is the output::

    <module 'imp' (built-in)>

o is **Blender's** imp module, as can be seen by comparing id(o) with
id(imp), where imp is Python's "regular" (not-embeded in Blender) imp
module.

The key to the entire puzzle is the assignment::

    m.PyImport_ImportModule.restype = ctypes.py_object

which is effectively a cast from int to PyObject*.  This is buried
deep in the docs:

    http://docs.python.org/library/ctypes.html#ctypes-fundamental-data-types-2

With this cast in place, we can treat o *exactly* as if we had
imported imp from Blender's internal Python compiler.

Now the door is unlocked.  We can import sys in the same way, add
paths to sys.path, and use the *embedded* imp module to load the 'bpy'
module.  If all goes as expected, the bpy should be able to import the
'_bpy' module.  Stay tuned...

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/leo-editor?hl=en.

Reply via email to