Quick Explode: replace BI based material handling (using 'fade'/'blend')
authorPhilipp Oeser <info@graphics-engineer.com>
Mon, 7 Jan 2019 14:42:46 +0000 (15:42 +0100)
committerPhilipp Oeser <info@graphics-engineer.com>
Tue, 8 Jan 2019 14:35:06 +0000 (15:35 +0100)
using a simple shader graph mixing Transparent BSDF now

Fixes T59185

Reviewers: brecht

Maniphest Tasks: T59185

Differential Revision: https://developer.blender.org/D4181

release/scripts/startup/bl_operators/object_quick_effects.py

index a47bdf9ff32daeaf8e7ca78788de6e1b70261082..37c4af593c54462d8627cb32a376cc7e1a24cd32 100644 (file)
@@ -192,17 +192,6 @@ class QuickExplode(Operator):
 
                 return {'CANCELLED'}
 
-        if self.fade:
-            tex = bpy.data.textures.new("Explode fade", 'BLEND')
-            tex.use_color_ramp = True
-
-            if self.style == 'BLEND':
-                tex.color_ramp.elements[0].position = 0.333
-                tex.color_ramp.elements[1].position = 0.666
-
-            tex.color_ramp.elements[0].color[3] = 1.0
-            tex.color_ramp.elements[1].color[3] = 0.0
-
         if self.style == 'BLEND':
             from_obj = mesh_objects[1]
             to_obj = mesh_objects[0]
@@ -229,30 +218,59 @@ class QuickExplode(Operator):
                 explode.particle_uv = uv.name
 
                 mat = object_ensure_material(obj, "Explode Fade")
-
-                mat.use_transparency = True
-                mat.use_transparent_shadows = True
-                mat.alpha = 0.0
-                mat.specular_alpha = 0.0
-
-                tex_slot = mat.texture_slots.add()
-
-                tex_slot.texture = tex
-                tex_slot.texture_coords = 'UV'
-                tex_slot.uv_layer = uv.name
-
-                tex_slot.use_map_alpha = True
+                mat.blend_method = 'BLEND'
+                mat.transparent_shadow_method = 'HASHED'
+                if not mat.use_nodes:
+                    mat.use_nodes = True
+
+                nodes = mat.node_tree.nodes
+                for node in nodes:
+                    if (node.type == 'OUTPUT_MATERIAL'):
+                        node_out_mat = node
+                        break
+
+                node_surface = node_out_mat.inputs['Surface'].links[0].from_node
+
+                node_x = node_surface.location[0]
+                node_y = node_surface.location[1] - 400
+                offset_x = 200
+
+                node_mix = nodes.new('ShaderNodeMixShader')
+                node_mix.location = (node_x - offset_x, node_y)
+                mat.node_tree.links.new(node_surface.outputs["BSDF"], node_mix.inputs[1])
+                mat.node_tree.links.new(node_mix.outputs["Shader"], node_out_mat.inputs['Surface'])
+                offset_x += 200
+
+                node_trans = nodes.new('ShaderNodeBsdfTransparent')
+                node_trans.location = (node_x - offset_x, node_y)
+                mat.node_tree.links.new(node_trans.outputs["BSDF"], node_mix.inputs[2])
+                offset_x += 200
+
+                node_ramp = nodes.new('ShaderNodeValToRGB')
+                node_ramp.location = (node_x - offset_x, node_y)
+                offset_x += 200
+                mat.node_tree.links.new(node_ramp.outputs["Alpha"], node_mix.inputs["Fac"])
+                color_ramp = node_ramp.color_ramp
+                color_ramp.elements[0].color[3] = 0.0
+                color_ramp.elements[1].color[3] = 1.0
 
                 if self.style == 'BLEND':
+                    color_ramp.elements[0].position = 0.333
+                    color_ramp.elements[1].position = 0.666
                     if obj == to_obj:
-                        tex_slot.alpha_factor = -1.0
-                        elem = tex.color_ramp.elements[1]
-                    else:
-                        elem = tex.color_ramp.elements[0]
-                    # Keep already defined alpha!
-                    elem.color[:3] = mat.diffuse_color
-                else:
-                    tex_slot.use_map_color_diffuse = False
+                        # reverse ramp alpha
+                        color_ramp.elements[0].color[3] = 1.0
+                        color_ramp.elements[1].color[3] = 0.0
+
+                node_sep = nodes.new('ShaderNodeSeparateXYZ')
+                node_sep.location = (node_x - offset_x, node_y)
+                offset_x += 200
+                mat.node_tree.links.new(node_sep.outputs["X"], node_ramp.inputs["Fac"])
+
+                node_uv = nodes.new('ShaderNodeUVMap')
+                node_uv.location = (node_x - offset_x, node_y)
+                node_uv.uv_map = uv.name
+                mat.node_tree.links.new(node_uv.outputs["UV"], node_sep.inputs["Vector"])
 
             if self.style == 'BLEND':
                 settings.physics_type = 'KEYED'