Fix: Add poll methods for quick effects operators
[blender.git] / release / scripts / startup / bl_operators / object_quick_effects.py
index 7597b2a04911e8b3bd12c5c342e32d8b673cf0e7..54673b245209cc1718390e120b0c9615ca9009c7 100644 (file)
@@ -46,8 +46,12 @@ def object_ensure_material(obj, mat_name):
             obj.data.materials.append(mat)
     return mat
 
+class ObjectModeOperator:
+    @classmethod
+    def poll(cls, context):
+        return context.mode == 'OBJECT'
 
-class QuickFur(Operator):
+class QuickFur(ObjectModeOperator, Operator):
     bl_idname = "object.quick_fur"
     bl_label = "Quick Fur"
     bl_options = {'REGISTER', 'UNDO'}
@@ -77,7 +81,7 @@ class QuickFur(Operator):
     def execute(self, context):
         fake_context = context.copy()
         mesh_objects = [obj for obj in context.selected_objects
-                        if obj.type == 'MESH' and obj.mode == 'OBJECT']
+                        if obj.type == 'MESH']
 
         if not mesh_objects:
             self.report({'ERROR'}, "Select at least one mesh object")
@@ -112,7 +116,7 @@ class QuickFur(Operator):
         return {'FINISHED'}
 
 
-class QuickExplode(Operator):
+class QuickExplode(ObjectModeOperator, Operator):
     bl_idname = "object.quick_explode"
     bl_label = "Quick Explode"
     bl_options = {'REGISTER', 'UNDO'}
@@ -192,17 +196,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]
@@ -225,34 +218,63 @@ class QuickExplode(Operator):
 
             if self.fade:
                 explode.show_dead = False
-                uv = obj.data.uv_layers.new("Explode fade")
+                uv = obj.data.uv_layers.new(name="Explode fade")
                 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'
@@ -300,7 +322,7 @@ def grid_location(x, y):
     return (x * 200, y * 150)
 
 
-class QuickSmoke(Operator):
+class QuickSmoke(ObjectModeOperator, Operator):
     bl_idname = "object.quick_smoke"
     bl_label = "Quick Smoke"
     bl_options = {'REGISTER', 'UNDO'}
@@ -404,7 +426,7 @@ class QuickSmoke(Operator):
         return {'FINISHED'}
 
 
-class QuickFluid(Operator):
+class QuickFluid(ObjectModeOperator, Operator):
     bl_idname = "object.quick_fluid"
     bl_label = "Quick Fluid"
     bl_options = {'REGISTER', 'UNDO'}