[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [649] trunk/py/scripts/addons/ object_cloud_gen.py: Added poll() function to fix crash with no selected objects.

Bart Crouch bartius.crouch at gmail.com
Fri Apr 30 11:58:42 CEST 2010


Revision: 649
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=649
Author:   crouch
Date:     2010-04-30 11:58:42 +0200 (Fri, 30 Apr 2010)

Log Message:
-----------
Added poll() function to fix crash with no selected objects. Grouped registration functions.

Modified Paths:
--------------
    trunk/py/scripts/addons/object_cloud_gen.py

Modified: trunk/py/scripts/addons/object_cloud_gen.py
===================================================================
--- trunk/py/scripts/addons/object_cloud_gen.py	2010-04-30 09:05:46 UTC (rev 648)
+++ trunk/py/scripts/addons/object_cloud_gen.py	2010-04-30 09:58:42 UTC (rev 649)
@@ -1,4 +1,4 @@
- # ##### BEGIN GPL LICENSE BLOCK #####
+# ##### BEGIN GPL LICENSE BLOCK #####
 #
 #  This program is free software; you can redistribute it and/or
 #  modify it under the terms of the GNU General Public License
@@ -20,7 +20,7 @@
 bl_addon_info = {
     'name': 'Object: Cloud Generator',
     'author': 'Nick Keeline(nrk)',
-    'version': '0.5',
+    'version': '0.6',
     'blender': (2, 5, 3),
     'location': 'Tool Shelf ',
     'description': 'Creates Volumetric Clouds',
@@ -41,6 +41,7 @@
 Rev 0.3 Fixed bug in degenerate
 Rev 0.4 updated for api change/changed to new apply modifier technique
 Rev 0.5 made particle count equation with radius so radius increases with cloud volume
+Rev 0.6 added poll function to operator, fixing crash with no selected objects
 """
 
 import bpy
@@ -48,10 +49,7 @@
 from math import *
 from bpy.props import *
 
-# Deselect All
-bpy.ops.object.select_all(action='DESELECT')
 
-
 # This routine takes an object and deletes all of the geometry in it
 # and adds a bounding box to it.
 # It will add or subtract the bound box size by the variable sizeDifference.
@@ -281,24 +279,7 @@
             col.label(text="a cloud.")
         # col.label(active_obj["CloudMember"])
 
-classes = [VIEW3D_PT_tools_cloud]
 
-
-def register():
-    register = bpy.types.register
-    for cls in classes:
-        register(cls)
-
-
-def unregister():
-    unregister = bpy.types.unregister
-    for cls in classes:
-        unregister(cls)
-
-if __name__ == "__main__":
-    register()
-
-
 class GenerateCloud(bpy.types.Operator):
     bl_idname = "cloud.generate_cloud"
     bl_label = "Generate Cloud"
@@ -306,6 +287,12 @@
     bl_register = True
     bl_undo = True
 
+    def poll(self, context):
+        if not context.active_object:
+            return False
+        else:
+            return (context.active_object.type=='MESH')
+
     def execute(self, context):
         # Make variable that is the current .blend file main data blocks
         main = context.main
@@ -316,323 +303,331 @@
         # Make variable scene that is current scene
         scene = context.scene
 
-        if active_object and active_object.type == 'MESH':
-            # Parameters the user may want to change:
-            # Number of points this number is multiplied by the volume to get
-            # the number of points the scripts will put in the volume.
-            numOfPoints = 1.0
-            maxNumOfPoints = 100000
-            scattering = 2.5
-            pointDensityRadiusFactor = 1.0
-            densityScale = 1.5
+        # Parameters the user may want to change:
+        # Number of points this number is multiplied by the volume to get
+        # the number of points the scripts will put in the volume.
+        numOfPoints = 1.0
+        maxNumOfPoints = 100000
+        scattering = 2.5
+        pointDensityRadiusFactor = 1.0
+        densityScale = 1.5
 
-            # Should we degnerate?
-            degenerate = degenerateCloud(active_object)
+        # Should we degnerate?
+        degenerate = degenerateCloud(active_object)
 
-            if degenerate:
-                if active_object is not None:
-                   # Degenerate Cloud
-                   mainObj = active_object
+        if degenerate:
+           # Degenerate Cloud
+           mainObj = active_object
 
-                   cloudMembers = active_object.children
+           cloudMembers = active_object.children
 
-                   createdObjects = []
-                   definitionObjects = []
-                   for member in cloudMembers:
-                       applyScaleRotLoc(scene, member)
-                       if (member["CloudMember"] == "CreatedObj"):
-                          createdObjects.append(member)
-                       else:
-                          definitionObjects.append(member)
+           createdObjects = []
+           definitionObjects = []
+           for member in cloudMembers:
+               applyScaleRotLoc(scene, member)
+               if (member["CloudMember"] == "CreatedObj"):
+                  createdObjects.append(member)
+               else:
+                  definitionObjects.append(member)
 
-                   for defObj in definitionObjects:
-                       # @todo check if it wouldn't be better to remove this
-                       # in the first place (see del() in degenerateCloud)
-                       #totally agree didn't know how before now...thanks! done.
-                       if "CloudMember" in defObj:
-                          del(defObj["CloudMember"])
+           for defObj in definitionObjects:
+               # @todo check if it wouldn't be better to remove this
+               # in the first place (see del() in degenerateCloud)
+               #totally agree didn't know how before now...thanks! done.
+               if "CloudMember" in defObj:
+                  del(defObj["CloudMember"])
 
-                   for createdObj in createdObjects:
-                       # Deselect All
-                       bpy.ops.object.select_all(action='DESELECT')
+           for createdObj in createdObjects:
+               # Deselect All
+               bpy.ops.object.select_all(action='DESELECT')
    
-                       # Select the object and delete it.
-                       createdObj.selected = True
-                       scene.objects.active = createdObj
-                       bpy.ops.object.delete()
+               # Select the object and delete it.
+               createdObj.selected = True
+               scene.objects.active = createdObj
+               bpy.ops.object.delete()
 
-                   # Delete the main object
-                   # Deselect All
-                   bpy.ops.object.select_all(action='DESELECT')
+           # Delete the main object
+           # Deselect All
+           bpy.ops.object.select_all(action='DESELECT')
    
-                   # Select the object and delete it.
-                   mainObj.selected = True
-                   scene.objects.active = mainObj
+           # Select the object and delete it.
+           mainObj.selected = True
+           scene.objects.active = mainObj
    
-                   # Delete all material slots in mainObj object
-                   for i in range(len(mainObj.material_slots)):
-                       mainObj.active_material_index = i - 1
-                       bpy.ops.object.material_slot_remove()
+           # Delete all material slots in mainObj object
+           for i in range(len(mainObj.material_slots)):
+               mainObj.active_material_index = i - 1
+               bpy.ops.object.material_slot_remove()
   
-                   # Delete the Main Object
-                   bpy.ops.object.delete()
+           # Delete the Main Object
+           bpy.ops.object.delete()
   
-                   # Select all of the left over boxes so people can immediately
-                   # press generate again if they want.
-                   for eachMember in definitionObjects:
-                       eachMember.max_draw_type = 'SOLID'
-                       eachMember.selected = True
-                       scene.objects.active = eachMember
+           # Select all of the left over boxes so people can immediately
+           # press generate again if they want.
+           for eachMember in definitionObjects:
+               eachMember.max_draw_type = 'SOLID'
+               eachMember.selected = True
+               scene.objects.active = eachMember
 
-            else:
-                # Generate Cloud
+        else:
+            # Generate Cloud
 
-                ###############Create Combined Object bounds##################
-                # Make a list of all Selected objects.
-                selectedObjects = bpy.context.selected_objects
+            ###############Create Combined Object bounds##################
+            # Make a list of all Selected objects.
+            selectedObjects = bpy.context.selected_objects
+            if not selectedObjects:
+                selectedObjects = [bpy.context.active_object]
 
-                # Create a new object bounds
-                if selectedObjects is None:
-                    bounds = addNewObject(scene,
-                        "CloudBounds",
-                        [])
+            # Create a new object bounds
+            bounds = addNewObject(scene,
+                    "CloudBounds",
+                    selectedObjects[0])
 
-                else:
-                    bounds = addNewObject(scene,
-                        "CloudBounds",
-                        selectedObjects[0])
+            bounds.max_draw_type = 'BOUNDS'
+            bounds.restrict_render = False
 
-                bounds.max_draw_type = 'BOUNDS'
-                bounds.restrict_render = False
+            # Just add a Definition Property designating this
+            # as the main object.
+            bounds["CloudMember"] = "MainObj"
 
-                # Just add a Definition Property designating this
-                # as the main object.
-                bounds["CloudMember"] = "MainObj"
+            # Since we used iteration 0 to copy with object we
+            # delete it off the list.
+            firstObject = selectedObjects[0]
+            del selectedObjects[0]
 
-                # Since we used iteration 0 to copy with object we
-                # delete it off the list.
-                firstObject = selectedObjects[0]
-                del selectedObjects[0]
+            # Apply location Rotation and Scale to all objects involved.
+            applyScaleRotLoc(scene, bounds)
+            for each in selectedObjects:
+                applyScaleRotLoc(scene, each)
 
-                # Apply location Rotation and Scale to all objects involved.
-                applyScaleRotLoc(scene, bounds)
-                for each in selectedObjects:
-                    applyScaleRotLoc(scene, each)
+            # Let's combine all of them together.
+            combineObjects(scene, bounds, selectedObjects)
 
-                # Let's combine all of them together.
-                combineObjects(scene, bounds, selectedObjects)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list