[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [1173] trunk/py/scripts/addons/ object_cloud_gen.py: new features see https://projects.blender.org/tracker/ ?func=detail&atid=469&aid=22015&group_id=153 for updates.

Brendon Murphy meta.androcto1 at gmail.com
Sun Nov 21 05:58:50 CET 2010


Revision: 1173
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=1173
Author:   meta-androcto
Date:     2010-11-21 05:58:45 +0100 (Sun, 21 Nov 2010)

Log Message:
-----------
new features see https://projects.blender.org/tracker/?func=detail&atid=469&aid=22015&group_id=153 for updates.

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-11-21 02:17:01 UTC (rev 1172)
+++ trunk/py/scripts/addons/object_cloud_gen.py	2010-11-21 04:58:45 UTC (rev 1173)
@@ -19,8 +19,8 @@
 bl_addon_info = {
     "name": "Cloud Generator",
     "author": "Nick Keeline(nrk)",
-    "version": (0,7),
-    "blender": (2, 5, 3),
+    "version": (1,0),
+    "blender": (2, 5, 5),
     "api": 31965,
     "location": "Tool Shelf ",
     "description": "Creates Volumetric Clouds",
@@ -45,6 +45,9 @@
 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
 Rev 0.7 added particles option and Type of Cloud wanted selector
+Rev 0.8 fixed particles by commenting out add cloud texture force field
+Rev 0.9 Added smoothing and explosion material
+Rev 1.0 Added ability to convert object with particle system to cloud and auto resizing of bound box
 """
 
 import bpy
@@ -56,17 +59,27 @@
 # 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.
-def makeObjectIntoBoundBox(object, sizeDifference):
+
+def getMeshandPutinEditMode(scene, object):
+    
+     # Go into Object Mode
+    bpy.ops.object.mode_set(mode='OBJECT')   
+    
     # Deselect All
     bpy.ops.object.select_all(action='DESELECT')
 
     # Select the object
     object.select = True
-
+    scene.objects.active = object
+    
     # Go into Edit Mode
     bpy.ops.object.mode_set(mode='EDIT')
 
-    mesh = object.data
+    return object.data
+   
+def maxAndMinVerts(scene, object):
+    
+    mesh = getMeshandPutinEditMode(scene, object)
     verts = mesh.vertices
 
     #Set the max and min verts to the first vertex on the list
@@ -90,7 +103,17 @@
             minVert[1] = vert.co[1]
         if vert.co[2] < minVert[2]:
             minVert[2] = vert.co[2]
+            
+    return [maxVert, minVert]
+    
+def makeObjectIntoBoundBox(scene, object, sizeDifference, takeFromObject):
+    
+    #Let's find the max and min of the reference object, it can be the same as the destination object
+    [maxVert, minVert] = maxAndMinVerts(scene, takeFromObject)
 
+    #get objects mesh
+    mesh = getMeshandPutinEditMode(scene, object)
+
     #Add the size difference to the max size of the box
     maxVert[0] = maxVert[0] + sizeDifference
     maxVert[1] = maxVert[1] + sizeDifference
@@ -142,7 +165,6 @@
     # Update the mesh
     mesh.update()
 
-
 def applyScaleRotLoc(scene, obj):
     # Deselect All
     bpy.ops.object.select_all(action='DESELECT')
@@ -151,11 +173,10 @@
     obj.select = True
     scene.objects.active = obj
 
-    #bpy.ops.object.rotation_apply()
+    bpy.ops.object.rotation_apply()
     bpy.ops.object.location_apply()
     bpy.ops.object.scale_apply()
-
-
+   
 def totallyDeleteObject(scene, obj):
     scene.objects.unlink(obj)
     bpy.data.objects.remove(obj)
@@ -164,18 +185,12 @@
 def makeParent(parentobj, childobj, scene):
 
     applyScaleRotLoc(scene, parentobj)
-
     applyScaleRotLoc(scene, childobj)
-
     childobj.parent = parentobj
 
-    #childobj.location = childobj.location - parentobj.location
 
+def addNewObject(scene, name, copyobj):
 
-def addNewObject(scene, name, copyobj):
-    '''
-    Add an object and do other silly stuff.
-    '''
     # Create new mesh
     mesh = bpy.data.meshes.new(name)
 
@@ -192,7 +207,62 @@
 
     return ob_new
 
+def getpdensitytexture(object):
+    
+    for mslot in object.material_slots:
+        mat = mslot.material
+        for tslot in mat.texture_slots:
+            if tslot!= 'NoneType':
+                tex = tslot.texture
+                if tex.type == 'POINT_DENSITY':
+                    if tex.point_density.point_source == 'PARTICLE_SYSTEM':
+                        return tex
+    
+def removeParticleSystemFromObj(scene, object):
 
+    # Deselect All
+    bpy.ops.object.select_all(action='DESELECT')
+
+    # Select the object.
+    object.select = True
+    scene.objects.active = object
+
+    bpy.ops.object.particle_system_remove()
+
+    # Deselect All
+    bpy.ops.object.select_all(action='DESELECT')
+    
+def convertParticlesToMesh(scene, particlesobj, destobj, replacemesh):
+    
+    # Select the Destination object.
+    destobj.select = True
+    scene.objects.active = destobj
+    
+    #Go to Edit Mode
+    bpy.ops.object.mode_set(mode='EDIT',toggle=False)
+    
+    #Delete everything in mesh if replace true
+    if replacemesh:
+        bpy.ops.mesh.select_all(action='SELECT')
+        bpy.ops.mesh.delete(type='ALL')
+
+    meshPnts = destobj.data
+
+    listCloudParticles = particlesobj.particles
+
+    listMeshPnts = []
+    for pTicle in listCloudParticles:
+        listMeshPnts.append(pTicle.location)
+
+    # Must be in object mode for from_pydata to work.
+    bpy.ops.object.mode_set(mode='OBJECT')
+
+    # Add in the mesh data.
+    meshPnts.from_pydata(listMeshPnts, [], [])
+
+    # Update the mesh.
+    meshPnts.update()
+
 def combineObjects(scene, combined, listobjs):
     # scene is the current scene
     # combined is the object we want to combine everything into
@@ -219,25 +289,26 @@
                 # Apply modifier
                 bpy.ops.object.modifier_apply(apply_as='DATA', modifier=union[0].name)
 
-
-# Returns True if we want to degenerate
-# and False if we want to generate a new cloud.
-def degenerateCloud(obj):
-    if not obj:
-        return False
-
-    if "CloudMember" in obj:
+# Returns the action we want to take
+def getActionToDo(obj):
+    
+    if not obj or obj.type != 'MESH':
+        return 'NOT_OBJ_DO_NOTHING'
+    elif obj is None:
+        return 'NO_SELECTION_DO_NOTHING'
+    elif "CloudMember" in obj:
         if obj["CloudMember"] != None:
-            if obj.parent:
-               if "CloudMember" not in obj.parent:
-                return False
-
+            if obj["CloudMember"] == "MainObj":
+                return 'DEGENERATE'            
+            elif obj["CloudMember"] == "CreatedObj" and len(obj.particle_systems) > 0:
+                return 'CLOUD_CONVERT_TO_MESH'
             else:
-                return True
+                return 'CLOUD_DO_NOTHING'
+    elif obj.type == 'MESH':
+        return 'GENERATE'
+    else:
+        return 'DO_NOTHING'
 
-    return False
-
-
 class VIEW3D_PT_tools_cloud(bpy.types.Panel):
     bl_space_type = 'VIEW_3D'
     bl_region_type = 'TOOLS'
@@ -250,39 +321,44 @@
         layout = self.layout
         col = layout.column(align=True)
 
-        degenerate = degenerateCloud(active_obj)
+        WhatToDo = getActionToDo(active_obj)
 
-        if active_obj and degenerate:
+        if WhatToDo == 'DEGENERATE':
 
             col.operator("cloud.generate_cloud", text="DeGenerate")
 
-        elif active_obj is None:
+        elif WhatToDo == 'CLOUD_CONVERT_TO_MESH':
 
+            col.operator("cloud.generate_cloud", text="Convert to Mesh")
+
+        elif WhatToDo == 'NO_SELECTION_DO_NOTHING':
+
             col.label(text="Select one or more")
             col.label(text="objects to generate")
             col.label(text="a cloud.")
 
-        elif "CloudMember" in  active_obj:
+        elif WhatToDo == 'CLOUD_DO_NOTHING':
 
             col.label(text="Must select")
             col.label(text="bound box")
            
-        elif active_obj and active_obj.type == 'MESH':
+        elif WhatToDo == 'GENERATE':
 
             col.operator("cloud.generate_cloud", text="Generate Cloud")
 
+            col.prop(context.scene, "cloud_type")
             col.prop(context.scene, "cloudparticles")
-            col.prop(context.scene, "cloud_type")
+            col.prop(context.scene, "cloudsmoothing")
         else:
             col.label(text="Select one or more")
             col.label(text="objects to generate")
             col.label(text="a cloud.")
 
-
+                                
 class GenerateCloud(bpy.types.Operator):
     bl_idname = "cloud.generate_cloud"
     bl_label = "Generate Cloud"
-    bl_description = "Create a Cloud."
+    bl_description = "Create a Cloud,Undo Cloud, or convert to Mesh Cloud depending on selection"
     bl_register = True
     bl_undo = True
 
@@ -308,14 +384,15 @@
         # the number of points the scripts will put in the volume.
         numOfPoints = 1.0
         maxNumOfPoints = 100000
+        maxPointDensityRadius = 1.5
         scattering = 2.5
         pointDensityRadiusFactor = 1.0
         densityScale = 1.5
 
-        # Should we degnerate?
-        degenerate = degenerateCloud(active_object)
+        # What should we do?
+        WhatToDo = getActionToDo(active_object)
 
-        if degenerate:
+        if WhatToDo == 'DEGENERATE':
            # Degenerate Cloud
            mainObj = active_object
 
@@ -347,6 +424,34 @@
                eachMember.draw_type = 'SOLID'
                eachMember.select = True
                eachMember.hide_render = False
+               
+        elif WhatToDo == 'CLOUD_CONVERT_TO_MESH':
+            
+           cloudParticles = active_object.particle_systems.active
+           
+           bounds = active_object.parent
+           
+           ###############Create CloudPnts for putting points in#########
+           # Create a new object cloudPnts
+           cloudPnts = addNewObject(scene, "CloudPoints", bounds)
+           cloudPnts["CloudMember"] = "CreatedObj"
+           cloudPnts.draw_type = 'WIRE'
+           cloudPnts.hide_render = True
+
+           makeParent(bounds, cloudPnts, scene) 
+            
+           convertParticlesToMesh(scene, cloudParticles, cloudPnts, True)
+          
+           removeParticleSystemFromObj(scene, active_object)
+           
+           pDensity = getpdensitytexture(bounds)
+           pDensity.point_density.point_source = 'OBJECT'
+           pDensity.point_density.object = cloudPnts
+           
+           #Let's resize the bound box to be more accurate.
+           how_much_bigger =  pDensity.point_density.radius 
+           makeObjectIntoBoundBox(scene, bounds, how_much_bigger, cloudPnts)
+           
         else:
             # Generate Cloud
 
@@ -407,16 +512,17 @@
 
             bpy.ops.object.editmode_toggle()
             bpy.ops.mesh.select_all(action='SELECT')
-            bpy.ops.mesh.subdivide(number_cuts=2, fractal=0, smoothness=1)

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list