From 79f241abe001bd8c43b94043c83d236c0a32309f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Santeri=20Salmij=C3=A4rvi?= <sndels>
Date: Wed, 12 Feb 2020 10:29:17 +0100
Subject: [PATCH] Fix T63635: Node Wrangler does not set relative paths for
 images using Add Texture Setup (Ctrl+Shift+T)

Using absolute paths for texture images break materials if the project is moved to another location or computer. Relative paths are also the default option in Image Texture Node's file view.

This adds a toggle to use relative paths when adding a texture setup for Principled BSDF. The option is selected by default to match Image Texture Node's behavior.

Tested on Blender v2.81.16 (Windows)

Reviewed By: lichtwerk, mont29

Maniphest Tasks: T63635

Differential Revision: https://developer.blender.org/D6756
---
 node_wrangler.py | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/node_wrangler.py b/node_wrangler.py
index 519868d04..f1aa1e0c0 100644
--- a/node_wrangler.py
+++ b/node_wrangler.py
@@ -2661,11 +2661,23 @@ class NWAddPrincipledSetup(Operator, NWBase, ImportHelper):
         options={'HIDDEN', 'SKIP_SAVE'}
     )
 
+    relative_path: BoolProperty(
+        name='Relative Path',
+        description='Select the file relative to the blend file',
+        default=True
+    )
+
     order = [
         "filepath",
         "files",
     ]
 
+    def draw(self, context):
+        layout = self.layout
+        layout.alignment = 'LEFT'
+
+        layout.prop(self, 'relative_path')
+
     @classmethod
     def poll(cls, context):
         valid = False
@@ -2747,6 +2759,15 @@ class NWAddPrincipledSetup(Operator, NWBase, ImportHelper):
             print('No matching images found')
             return {'CANCELLED'}
 
+        # Don't override path earlier as os.path is used to check the absolute path
+        import_path = self.directory
+        if self.relative_path:
+            if bpy.data.filepath:
+                import_path = bpy.path.relpath(self.directory)
+            else:
+                self.report({'WARNING'}, 'Relative paths cannot be used with unsaved scenes!')
+                print('Relative paths cannot be used with unsaved scenes!')
+
         # Add found images
         print('\nMatched Textures:')
         texture_nodes = []
@@ -2759,7 +2780,7 @@ class NWAddPrincipledSetup(Operator, NWBase, ImportHelper):
             # DISPLACEMENT NODES
             if sname[0] == 'Displacement':
                 disp_texture = nodes.new(type='ShaderNodeTexImage')
-                img = bpy.data.images.load(self.directory+sname[2])
+                img = bpy.data.images.load(path.join(import_path, sname[2]))
                 disp_texture.image = img
                 disp_texture.label = 'Displacement'
                 if disp_texture.image:
@@ -2784,7 +2805,7 @@ class NWAddPrincipledSetup(Operator, NWBase, ImportHelper):
             if not active_node.inputs[sname[0]].is_linked:
                 # No texture node connected -> add texture node with new image
                 texture_node = nodes.new(type='ShaderNodeTexImage')
-                img = bpy.data.images.load(self.directory+sname[2])
+                img = bpy.data.images.load(path.join(import_path, sname[2]))
                 texture_node.image = img
 
                 # NORMAL NODES
-- 
GitLab