[Bf-blender-cvs] [dd6bf3f84a9] blender2.8: node_shader_utils: several fixes, improvements and cleanups.

Bastien Montagne noreply at git.blender.org
Sat Oct 13 19:40:09 CEST 2018


Commit: dd6bf3f84a9137affbcd8ba0fc957c32b8c0aff7
Author: Bastien Montagne
Date:   Sat Oct 13 19:27:12 2018 +0200
Branches: blender2.8
https://developer.blender.org/rBdd6bf3f84a9137affbcd8ba0fc957c32b8c0aff7

node_shader_utils: several fixes, improvements and cleanups.

Fix broken behavior in case of texcoords mapping (we do need texcoords
node in all cases, then, even for UV coords...).

Use nodes by default when generating new write-allowed wrapper around a
material.

Do not try to find nodes in existing tree all the time, do it only once,
even for lazy-initialized nodes (through accessors).

Fix ugly spacing in property accessors (since it looks like some people
do not like a single 'block' with both getters, setters and prop
definition, at least use one sep line everywhere (and two sep lines to
separate properties)...

===================================================================

M	release/scripts/modules/bpy_extras/node_shader_utils.py

===================================================================

diff --git a/release/scripts/modules/bpy_extras/node_shader_utils.py b/release/scripts/modules/bpy_extras/node_shader_utils.py
index f8f0efdef8a..e97eac0a9e6 100644
--- a/release/scripts/modules/bpy_extras/node_shader_utils.py
+++ b/release/scripts/modules/bpy_extras/node_shader_utils.py
@@ -43,6 +43,7 @@ def rgb_to_rgba(rgb):
 def rgba_to_rgb(rgba):
     return Color((rgba[0], rgba[1], rgba[2]))
 
+
 class ShaderWrapper():
     """
     Base class with minimal common ground for all types of shader interfaces we may want/need to implement.
@@ -90,9 +91,10 @@ class ShaderWrapper():
             dst_node.width = min(dst_node.width, self._col_size - 20)
         return loc
 
-    def __init__(self, material, is_readonly=True):
+    def __init__(self, material, is_readonly=True, use_nodes=True):
         self.is_readonly = is_readonly
         self.material = material
+        self.use_nodes = use_nodes
         self.update()
 
     def update(self):  # Should be re-implemented by children classes...
@@ -101,6 +103,7 @@ class ShaderWrapper():
         self._textures = {}
         self._grid_locations = set()
 
+
     def use_nodes_get(self):
         return self.material.use_nodes
 
@@ -108,17 +111,22 @@ class ShaderWrapper():
     def use_nodes_set(self, val):
         self.material.use_nodes = val
         self.update()
+
     use_nodes = property(use_nodes_get, use_nodes_set)
 
+
     def node_texcoords_get(self):
         if not self.use_nodes:
             return None
-        if self._node_texcoords is None:
+        if self._node_texcoords is ...:
+            # Running only once, trying to find a valid texcoords node.
             for n in self.material.node_tree.nodes:
                 if n.bl_idname == 'ShaderNodeTexCoord':
                     self._node_texcoords = n
                     self._grid_to_location(0, 0, ref_node=n)
                     break
+            if self._node_texcoords is ...:
+                self._node_texcoords = None
         if self._node_texcoords is None and not self.is_readonly:
             tree = self.material.node_tree
             nodes = tree.nodes
@@ -129,6 +137,7 @@ class ShaderWrapper():
             self._grid_to_location(-5, 1, dst_node=node_texcoords)
             self._node_texcoords = node_texcoords
         return self._node_texcoords
+
     node_texcoords = property(node_texcoords_get)
 
 
@@ -154,8 +163,9 @@ class PrincipledBSDFWrapper(ShaderWrapper):
 
     NODES_LIST = ShaderWrapper.NODES_LIST + NODES_LIST
 
-    def __init__(self, material, is_readonly=True):
-        super(PrincipledBSDFWrapper, self).__init__(material, is_readonly)
+    def __init__(self, material, is_readonly=True, use_nodes=True):
+        super(PrincipledBSDFWrapper, self).__init__(material, is_readonly, use_nodes)
+
 
     def update(self):
         super(PrincipledBSDFWrapper, self).update()
@@ -211,31 +221,42 @@ class PrincipledBSDFWrapper(ShaderWrapper):
 
         # --------------------------------------------------------------------
         # Normal Map, lazy initialization...
-        self._node_normalmap = None
+        self._node_normalmap = ...
 
         # --------------------------------------------------------------------
         # Tex Coords, lazy initialization...
-        self._node_texcoords = None
+        self._node_texcoords = ...
+
 
     def node_normalmap_get(self):
         if not self.use_nodes:
             return None
-        if self._node_normalmap is None and self.node_principled_bsdf is not None:
+        if self.node_principled_bsdf is not None:
             node_principled = self.node_principled_bsdf
-            if node_principled.inputs["Normal"].is_linked:
-                node_normalmap = node_principled.inputs["Normal"].links[0].from_node
-                if node_normalmap.bl_idname == 'ShaderNodeNormalMap':
-                    self._node_normalmap = node_normalmap
-                    self._grid_to_location(0, 0, ref_node=node_normalmap)
+            if self._node_normalmap is ...:
+                # Running only once, trying to find a valid normalmap node.
+                if node_principled.inputs["Normal"].is_linked:
+                    node_normalmap = node_principled.inputs["Normal"].links[0].from_node
+                    if node_normalmap.bl_idname == 'ShaderNodeNormalMap':
+                        self._node_normalmap = node_normalmap
+                        self._grid_to_location(0, 0, ref_node=node_normalmap)
+                if self._node_normalmap is ...:
+                    self._node_normalmap = None
             if self._node_normalmap is None and not self.is_readonly:
+                tree = self.material.node_tree
+                nodes = tree.nodes
+                links = tree.links
+
                 node_normalmap = nodes.new(type='ShaderNodeNormalMap')
                 node_normalmap.label = "Normal/Map"
                 self._grid_to_location(-1, -2, dst_node=node_normalmap, ref_node=node_principled)
                 # Link
                 links.new(node_normalmap.outputs["Normal"], node_principled.inputs["Normal"])
         return self._node_normalmap
+
     node_normalmap = property(node_normalmap_get)
 
+
     # --------------------------------------------------------------------
     # Base Color.
 
@@ -249,8 +270,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
         self.material.diffuse_color = color
         if self.use_nodes and self.node_principled_bsdf is not None:
             self.node_principled_bsdf.inputs["Base Color"].default_value = rgb_to_rgba(color)
+
     base_color = property(base_color_get, base_color_set)
 
+
     def base_color_texture_get(self):
         if not self.use_nodes or self.node_principled_bsdf is None:
             return None
@@ -259,8 +282,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
             self.node_principled_bsdf.inputs["Base Color"],
             grid_row_diff=1,
         )
+
     base_color_texture = property(base_color_texture_get)
 
+
     # --------------------------------------------------------------------
     # Specular.
 
@@ -274,8 +299,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
         self.material.specular_intensity = value
         if self.use_nodes and self.node_principled_bsdf is not None:
             self.node_principled_bsdf.inputs["Specular"].default_value = value
+
     specular = property(specular_get, specular_set)
 
+
     def specular_tint_get(self):
         if not self.use_nodes or self.node_principled_bsdf is None:
             return 0.0
@@ -285,19 +312,24 @@ class PrincipledBSDFWrapper(ShaderWrapper):
     def specular_tint_set(self, value):
         if self.use_nodes and self.node_principled_bsdf is not None:
             self.node_principled_bsdf.inputs["Specular Tint"].default_value = rgb_to_rgba(value)
+
     specular_tint = property(specular_tint_get, specular_tint_set)
 
+
     # Will only be used as gray-scale one...
     def specular_texture_get(self):
         if not self.use_nodes or self.node_principled_bsdf is None:
+            print("NO NODES!")
             return None
         return ShaderImageTextureWrapper(
             self, self.node_principled_bsdf,
             self.node_principled_bsdf.inputs["Specular"],
             grid_row_diff=0,
         )
+
     specular_texture = property(specular_texture_get)
 
+
     # --------------------------------------------------------------------
     # Roughness (also sort of inverse of specular hardness...).
 
@@ -311,8 +343,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
         self.material.roughness = value
         if self.use_nodes and self.node_principled_bsdf is not None:
             self.node_principled_bsdf.inputs["Roughness"].default_value = value
+
     roughness = property(roughness_get, roughness_set)
 
+
     # Will only be used as gray-scale one...
     def roughness_texture_get(self):
         if not self.use_nodes or self.node_principled_bsdf is None:
@@ -322,8 +356,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
             self.node_principled_bsdf.inputs["Roughness"],
             grid_row_diff=0,
         )
+
     roughness_texture = property(roughness_texture_get)
 
+
     # --------------------------------------------------------------------
     # Metallic (a.k.a reflection, mirror).
 
@@ -337,8 +373,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
         self.material.metallic = value
         if self.use_nodes and self.node_principled_bsdf is not None:
             self.node_principled_bsdf.inputs["Metallic"].default_value = value
+
     metallic = property(metallic_get, metallic_set)
 
+
     # Will only be used as gray-scale one...
     def metallic_texture_get(self):
         if not self.use_nodes or self.node_principled_bsdf is None:
@@ -348,8 +386,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
             self.node_principled_bsdf.inputs["Metallic"],
             grid_row_diff=0,
         )
+
     metallic_texture = property(metallic_texture_get)
 
+
     # --------------------------------------------------------------------
     # Transparency settings.
 
@@ -362,8 +402,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
     def ior_set(self, value):
         if self.use_nodes and self.node_principled_bsdf is not None:
             self.node_principled_bsdf.inputs["IOR"].default_value = value
+
     ior = property(ior_get, ior_set)
 
+
     # Will only be used as gray-scale one...
     def ior_texture_get(self):
         if not self.use_nodes or self.node_principled_bsdf is None:
@@ -373,8 +415,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
             self.node_principled_bsdf.inputs["IOR"],
             grid_row_diff=-1,
         )
+
     ior_texture = property(ior_texture_get)
 
+
     def transmission_get(self):
         if not self.use_nodes or self.node_principled_bsdf is None:
             return 0.0
@@ -384,8 +428,10 @@ class PrincipledBSDFWrapper(ShaderWrapper):
     def transmission_set(self, value):
         if self.use_nodes and self.node_principled_bsdf is not None:
             self.node_principled_bsdf.inputs["Transmission"].default_value = value
+
     transmission = property(transmission_get, transmission_set)
 
+
     # Will only 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list