From dbd2c915ff5be55e567d14813dbec7dc87e99f52 Mon Sep 17 00:00:00 2001
From: Campbell Barton <ideasman42@gmail.com>
Date: Fri, 5 Apr 2013 00:37:20 +0000
Subject: [PATCH] fix [#34864] Save&Load - Relative Paths ticked, Obj Import
 and Textures tab uses absolute paths use preference for relative paths.

---
 io_scene_obj/__init__.py   |  4 ++++
 io_scene_obj/import_obj.py | 15 +++++++++------
 2 files changed, 13 insertions(+), 6 deletions(-)

diff --git a/io_scene_obj/__init__.py b/io_scene_obj/__init__.py
index a2b97bff9..eef29f97c 100644
--- a/io_scene_obj/__init__.py
+++ b/io_scene_obj/__init__.py
@@ -164,6 +164,10 @@ class ImportOBJ(bpy.types.Operator, ImportHelper):
                                         ).to_4x4()
         keywords["global_matrix"] = global_matrix
 
+        if bpy.data.is_saved and context.user_preferences.filepaths.use_relative_paths:
+            import os
+            keywords["relpath"] = os.path.dirname((bpy.data.path_resolve("filepath", False).as_bytes()))
+
         return import_obj.load(self, context, **keywords)
 
     def draw(self, context):
diff --git a/io_scene_obj/import_obj.py b/io_scene_obj/import_obj.py
index 72c17bfa4..68f24a998 100644
--- a/io_scene_obj/import_obj.py
+++ b/io_scene_obj/import_obj.py
@@ -74,20 +74,22 @@ def line_value(line_split):
         return b' '.join(line_split[1:])
 
 
-def obj_image_load(imagepath, DIR, recursive):
+def obj_image_load(imagepath, DIR, recursive, relpath):
     """
     Mainly uses comprehensiveImageLoad
     but tries to replace '_' with ' ' for Max's exporter replaces spaces with underscores.
     """
     if b'_' in imagepath:
-        image = load_image(imagepath.replace(b'_', b' '), DIR, recursive=recursive)
+        image = load_image(imagepath.replace(b'_', b' '), DIR, recursive=recursive, relpath=relpath)
         if image:
             return image
 
-    return load_image(imagepath, DIR, recursive=recursive, place_holder=True)
+    return load_image(imagepath, DIR, recursive=recursive, place_holder=True, relpath=relpath)
 
 
-def create_materials(filepath, material_libs, unique_materials, unique_material_images, use_image_search, float_func):
+def create_materials(filepath, relpath,
+                     material_libs, unique_materials, unique_material_images,
+                     use_image_search, float_func):
     """
     Create all the used materials in this obj,
     assign colors and images to the materials from all referenced material libs
@@ -103,7 +105,7 @@ def create_materials(filepath, material_libs, unique_materials, unique_material_
         texture = bpy.data.textures.new(name=type, type='IMAGE')
 
         # Absolute path - c:\.. etc would work here
-        image = obj_image_load(imagepath, DIR, use_image_search)
+        image = obj_image_load(imagepath, DIR, use_image_search, relpath)
         has_data = False
         image_depth = 0
 
@@ -838,6 +840,7 @@ def load(operator, context, filepath,
          use_split_groups=True,
          use_image_search=True,
          use_groups_as_vgroups=False,
+         relpath=None,
          global_matrix=None,
          ):
     """
@@ -1101,7 +1104,7 @@ def load(operator, context, filepath,
     time_sub = time_new
 
     print('\tloading materials and images...')
-    create_materials(filepath, material_libs, unique_materials, unique_material_images, use_image_search, float_func)
+    create_materials(filepath, relpath, material_libs, unique_materials, unique_material_images, use_image_search, float_func)
 
     time_new = time.time()
     print("%.4f sec" % (time_new - time_sub))
-- 
GitLab