Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search   Register Register  Login Login

error when including an external library

 Post Reply Post Reply
Author
Message
ruckzuck View Drop Down
Member
Member


Joined: 2015 Mar 19
Online Status: Offline
Posts: 26
Post Options Post Options   Quote ruckzuck Quote  Post ReplyReply Direct Link To This Post Topic: error when including an external library
    Posted: 2018 Jun 10 at 5:29am
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 Disapprove

Any help is appreciated.
Thanks,
Jens





Back to Top
ruckzuck View Drop Down
Member
Member


Joined: 2015 Mar 19
Online Status: Offline
Posts: 26
Post Options Post Options   Quote ruckzuck Quote  Post ReplyReply Direct Link To This Post Posted: 2018 Jun 10 at 2:36pm
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

Back to Top
Andreas Block View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2014 Oct 01
Location: Hannover
Online Status: Offline
Posts: 1848
Post Options Post Options   Quote Andreas Block Quote  Post ReplyReply Direct Link To This Post Posted: 2018 Jun 12 at 9:20am
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.

Cheers,
Andreas
SDK Support Engineer
Back to Top
ruckzuck View Drop Down
Member
Member


Joined: 2015 Mar 19
Online Status: Offline
Posts: 26
Post Options Post Options   Quote ruckzuck Quote  Post ReplyReply Direct Link To This Post Posted: 2018 Jun 19 at 7:34am
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
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.093 seconds.