error when including an external library
-
On 10/06/2018 at 05:29, xxxxxxxx wrote:
Hi,
I have problems using an external lib within python. The path where the required DLL is located is added to the PATH variable and afterwards the PyDLL call is run:
Lets say I create a dummy external package "foo" in the C4D root dir (R19) :
<c4d root dir>\resource\modules\python\Python.win64.framework\Lib\site-packages\foo
Then I create a subdir named "DLLs" and copy the required foo.dll to it:
<c4d root dir>\resource\modules\python\Python.win64.framework\Lib\site-packages\foo\DLLs\foo.dll
Now I create ...
<c4d root dir>\resource\modules\python\Python.win64.framework\Lib\site-packages\foo\__init__.py
and enter this code:
import os import ctypes try: dll_name = "foo.dll" dll_path = os.path.abspath(__file__ + "/../DLLs") original_path = os.environ['PATH'] os.environ['PATH'] = "%s;%s" % (dll_path, original_path) rt = ctypes.PyDLL(dll_name) except (ImportError, WindowsError) : raise
This method of adding the dllpath to the PATH environment variable works like a charm without errors when running this code in a command box:
cd <c4d root dir>\resource\modules\python\Python.win64.framework python.exe \>>> import foo
But inside C4D (using a python tag or inside a plugin) it's not working:
WindowsError: [Error 126] Das angegebene Modul wurde nicht gefunden
I've also tried to specify a absolute path in the ctypes.PyDLL() call. Same result: it works in a windows command box but not inside C4D
Any help is appreciated.
Thanks,Jens
-
On 10/06/2018 at 14:36, xxxxxxxx wrote:
To be a little more specific: __"Rtree" is a ctypes Python wrapper that uses the library "spatialindex". Finding and loading the spatialindex dll is the problem.
cd <c4d root dir>\resource\modules\python\Python.win64.framework scripts\pip install https://download.lfd.uci.edu/pythonlibs/g1ylh5la/Rtree-0.8.3-cp27-cp27m-win_amd64.whl
package "rtree" now gets installed in Python.win64.framework\lib\site-packages\rtree
Here is also the needed dll located: "spatialindex_c.dll"Using rtree and loading the dll is no problem when using a command box:
cd <c4d root dir>\resource\modules\python\Python.win64.framework python.exe \>>> from rtree import index
no error
BUT ... inside C4D it doesn't work:
Start C4D
create null object
create python tag
insert "from rtree import index"
run code and see script console:File "<c4d root dir>\Python.win64.framework\lib\site-packages\rtree\core.py", line 116, in <module> raise OSError("could not find or load spatialindex_c.dll") OSError: could not find or load spatialindex_c.dll
Where is the difference between running C4D's python.exe in a command box and using python inside C4D when loading any DLL ?
Thanks,
Jens -
On 12/06/2018 at 09:20, xxxxxxxx wrote:
Hi Jens,
unfortunately we can not really provide support for installation and usage of 3rd party modules.
I think, you will have to build Rtree specifically for the use in Cinema 4D. Currently the best help I can provide is a link to our community member Niklas Rosenstein's page: Building NumPy for Cinema 4D on Windows
Another thing may be, the Python documentation (FAQ) says, 3rd party modules need to be installed here: {USER_FOLDER}/library/python/packages/{osx/win32/win64}
The delivered python.exe is a pure Python console with no access to Cinema 4D (i.e. you can't import module c4d). It's not related to the python environment inside Cinema 4D.
-
On 19/06/2018 at 07:34, xxxxxxxx wrote:
Hi Andreas,
thanks for clarifying this. I already built Rtree especially for the current R19 python version ("MSC v. 1700]) following Niklas' hints on his help pages. And for all other extensions that I built this works perfectly in the past. Meanwhile it seems to me that there is something wrong with the way Rtree tries to find and bind the spatialindex dll. This Rtree wrapper is apparently not coded "rocksolid" so it may work in a python console but not in an embedded python environment.
Thanks again for your help and best regards,
Jens