From ab9b659c14d049caf558dc27bf8a69d377fd3ba9 Mon Sep 17 00:00:00 2001
From: "Olivier J.N. Bertrand" <olivier.bertrand@uni-bielefeld.de>
Date: Sat, 3 Mar 2018 23:03:42 +0100
Subject: [PATCH] Update blendnavipy to support non virtualenvironment

---
 navipy/sensors/blendnavipy.py | 49 +++++++++++++++++++++++++++--------
 setup.py                      |  5 ++--
 2 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/navipy/sensors/blendnavipy.py b/navipy/sensors/blendnavipy.py
index c19bf8e..baee99d 100644
--- a/navipy/sensors/blendnavipy.py
+++ b/navipy/sensors/blendnavipy.py
@@ -16,11 +16,18 @@ def activate_virtualenv(venv_path):
     tell blender to use our virtualenv where the navigation toolbox \
     is installed.
     """
-    if venv_path is None:
-        raise NameError('Python is not running within a virtualenv')
-    filepath = os.path.join(venv_path, 'bin', 'activate_this.py')
-    with open(filepath, 'r') as f:
-        exec(f.read(), dict(__file__=filepath))
+    with open(venv_path, 'r') as f:
+        exec(f.read(), dict(__file__=venv_path))
+
+
+def append_path(path_navipy):
+    """ insert path
+
+    Blender comes with its own python installation. Thus, we need to \
+    tell blender where to look for navipy
+    """
+    for cpath in path_navipy:
+        sys.path.insert(0, cpath)
 
 
 def blender_version(pyversion):
@@ -74,6 +81,14 @@ def main():
     # Find the name of the virtualenv, so that we can activate
     # it in blender
     venv_path = sys.base_prefix
+    venv_path = os.path.join(venv_path, 'bin', 'activate_this.py')
+    if os.path.exists(venv_path):
+        # Runnin in virtualenv
+        syspath = None
+    else:
+        # Find path packages
+        syspath = sys.path
+        venv_path = None
 
     # Find python version to be checked agains blender python version
     pyver = sys.version_info[:3]
@@ -94,16 +109,28 @@ def main():
         tfile.write(header.encode(encoding))
         tfile.write('# check blender version\n'.encode(encoding))
         tfile.write('import sys \n'.encode(encoding))
+        tfile.write('import os \n'.encode(encoding))
         for line in inspect.getsourcelines(blender_version)[0]:
             tfile.write(line.encode(encoding))
         line = 'blender_version({})\n'.format(pyver)
         tfile.write(line.encode(encoding))
-        tfile.write('# activate virtualenv within blender\n'.encode(encoding))
-        tfile.write('import os \n'.encode(encoding))
-        for line in inspect.getsourcelines(activate_virtualenv)[0]:
-            tfile.write(line.encode(encoding))
-        line = 'activate_virtualenv(\"{}\")\n'.format(venv_path)
-        tfile.write(line.encode(encoding))
+        if venv_path is not None:
+            tfile.write(
+                '# activate virtualenv within blender\n'.encode(encoding))
+            for line in inspect.getsourcelines(activate_virtualenv)[0]:
+                tfile.write(line.encode(encoding))
+                line = 'activate_virtualenv(\"{}\")\n'.format(venv_path)
+                tfile.write(line.encode(encoding))
+        elif syspath is not None:
+            tfile.write(
+                '# add navipy path within blender\n'.encode(encoding))
+            for line in inspect.getsourcelines(append_path)[0]:
+                tfile.write(line.encode(encoding))
+                line = 'activate_virtualenv({})\n'.format(str(syspath))
+                tfile.write(line.encode(encoding))
+        else:
+            raise NameError('The path to navipy could not be found')
+
         tfile.write('# run simulation\n'.encode(encoding))
         with open(python_script) as infile:
             for line in infile:
diff --git a/setup.py b/setup.py
index 2179d11..5026698 100644
--- a/setup.py
+++ b/setup.py
@@ -43,7 +43,8 @@ setup_dict = {'name': 'navipy',
               'package_data': {'navipy': ['resources/database.db',
                                           'resources/forest_world.blend']},
               'include_package_data': True,
-              'console_scripts': [
-                  'blendnavipy = navipy.sensors.blendnavipy:main_func'], }
+              'entry_points': {
+                  'console_scripts': [
+                      'blendnavipy=navipy.sensors.blendnavipy:main']}, }
 
 setup(**setup_dict)
-- 
GitLab