[Bf-extensions-cvs] SVN commit: /data/svn/bf-extensions [594] trunk/py/scripts/addons/ object_cloud_gen.py: addons/object_cloud_gen.py accepted.

Brendon Murphy meta.androcto1 at gmail.com
Sat Apr 17 13:51:46 CEST 2010


Revision: 594
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-extensions&revision=594
Author:   meta-androcto
Date:     2010-04-17 13:51:46 +0200 (Sat, 17 Apr 2010)

Log Message:
-----------
addons/object_cloud_gen.py accepted.
moved directly to trunk/py/scripts/addons/object_cloud_gen.py

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

Added: trunk/py/scripts/addons/object_cloud_gen.py
===================================================================
--- trunk/py/scripts/addons/object_cloud_gen.py	                        (rev 0)
+++ trunk/py/scripts/addons/object_cloud_gen.py	2010-04-17 11:51:46 UTC (rev 594)
@@ -0,0 +1,624 @@
+# ##### 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
+#  as published by the Free Software Foundation; either version 2
+#  of the License, or (at your option) any later version.
+#
+#  This program is distributed in the hope that it will be useful,
+#  but WITHOUT ANY WARRANTY; without even the implied warranty of
+#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+#  GNU General Public License for more details.
+#
+#  You should have received a copy of the GNU General Public License
+#  along with this program; if not, write to the Free Software Foundation,
+#  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# ##### END GPL LICENSE BLOCK #####
+
+
+bl_addon_info = {
+    'name': 'Cloud generator',
+    'author': 'Nick Keeline(nrk)',
+    'version': '0.1',
+    'blender': (2, 5, 3),
+    'location': 'Tool Shelf ',
+    'description': 'Creates Volumetric Clouds',
+    'url': 'http://wiki.blender.org/index.php/Extensions:2.5/Py/' \
+        'Scripts/Object/Cloud_Gen',
+    'category': 'Object'}
+
+
+"""
+Place this file in the .blender/scripts/addons dir
+You have to activated the script in the "Add-Ons" tab (user preferences).
+The functionality can then be accessed via the Tool shelf when objects
+are selected
+
+Rev 0 initial release
+Rev 0.1 added scene to create_mesh per python api change.
+"""
+
+import bpy
+import mathutils
+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.
+def makeObjectIntoBoundBox(object, sizeDifference):
+    # Deselect All
+    bpy.ops.object.select_all(action='DESELECT')
+
+    # Select the object
+    object.selected = True
+
+    # Go into Edit Mode
+    bpy.ops.object.mode_set(mode='EDIT')
+
+    mesh = object.data
+    verts = mesh.verts
+
+    #Set the max and min verts to the first vertex on the list
+    maxVert = [verts[0].co[0], verts[0].co[1], verts[0].co[2]]
+    minVert = [verts[0].co[0], verts[0].co[1], verts[0].co[2]]
+
+    #Create Max and Min Vertex array for the outer corners of the box
+    for vert in verts:
+        #Max vertex
+        if vert.co[0] > maxVert[0]:
+            maxVert[0] = vert.co[0]
+        if vert.co[1] > maxVert[1]:
+            maxVert[1] = vert.co[1]
+        if vert.co[2] > maxVert[2]:
+            maxVert[2] = vert.co[2]
+
+        #Min Vertex
+        if vert.co[0] < minVert[0]:
+            minVert[0] = vert.co[0]
+        if vert.co[1] < minVert[1]:
+            minVert[1] = vert.co[1]
+        if vert.co[2] < minVert[2]:
+            minVert[2] = vert.co[2]
+
+    #Add the size difference to the max size of the box
+    maxVert[0] = maxVert[0] + sizeDifference
+    maxVert[1] = maxVert[1] + sizeDifference
+    maxVert[2] = maxVert[2] + sizeDifference
+
+    #subtract the size difference to the min size of the box
+    minVert[0] = minVert[0] - sizeDifference
+    minVert[1] = minVert[1] - sizeDifference
+    minVert[2] = minVert[2] - sizeDifference
+
+    #Create arrays of verts and faces to be added to the mesh
+    addVerts = []
+
+    #X high loop
+    addVerts.append([maxVert[0], maxVert[1], maxVert[2]])
+    addVerts.append([maxVert[0], maxVert[1], minVert[2]])
+    addVerts.append([maxVert[0], minVert[1], minVert[2]])
+    addVerts.append([maxVert[0], minVert[1], maxVert[2]])
+
+    #x low loop
+    addVerts.append([minVert[0], maxVert[1], maxVert[2]])
+    addVerts.append([minVert[0], maxVert[1], minVert[2]])
+    addVerts.append([minVert[0], minVert[1], minVert[2]])
+    addVerts.append([minVert[0], minVert[1], maxVert[2]])
+
+    # Make the faces of the bounding box.
+    addFaces = []
+
+    # Draw a box on paper and number the vertices.
+    # Use right hand rule to come up with number orders for faces on
+    # the box (with normals pointing out).
+    addFaces.append([0, 3, 2, 1])
+    addFaces.append([4, 5, 6, 7])
+    addFaces.append([0, 1, 5, 4])
+    addFaces.append([1, 2, 6, 5])
+    addFaces.append([2, 3, 7, 6])
+    addFaces.append([0, 4, 7, 3])
+
+    # Delete all geometry from the object.
+    bpy.ops.mesh.select_all(action='SELECT')
+    bpy.ops.mesh.delete(type='ALL')
+
+    # Must be in object mode for from_pydata to work
+    bpy.ops.object.mode_set(mode='OBJECT')
+
+    # Add the mesh data.
+    mesh.from_pydata(addVerts, [], addFaces)
+
+    # Update the mesh
+    mesh.update()
+
+
+def applyScaleRotLoc(scene, obj):
+    # Deselect All
+    bpy.ops.object.select_all(action='DESELECT')
+
+    # Select the object
+    obj.selected = True
+    scene.objects.active = obj
+
+    #bpy.ops.object.rotation_apply()
+    bpy.ops.object.location_apply()
+    bpy.ops.object.scale_apply()
+
+
+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):
+    '''
+    Add an object and do other silly stuff.
+    '''
+    # Create new mesh
+    mesh = bpy.data.meshes.new(name)
+
+    # Create a new object.
+    ob_new = bpy.data.objects.new(name, mesh)
+    tempme = copyobj.data
+    ob_new.data = tempme.copy()
+    ob_new.scale = copyobj.scale
+    ob_new.location = copyobj.location
+
+    # Link new object to the given scene and select it.
+    scene.objects.link(ob_new)
+    ob_new.selected = True
+
+    return ob_new
+
+
+def combineObjects(scene, combined, listobjs):
+    # scene is the current scene
+    # combined is the object we want to combine everything into
+    # listobjs is the list of objects to stick into combined
+
+    # Deselect All
+    bpy.ops.object.select_all(action='DESELECT')
+
+    # Select the new object.
+    combined.selected = True
+    scene.objects.active = combined
+
+    # Add data
+    if (len(listobjs) > 0):
+            for i in listobjs:
+                # Add a modifier
+                bpy.ops.object.modifier_add(type='BOOLEAN')
+
+                union = combined.modifiers
+                union[0].name = "AddEmUp"
+                union[0].object = i
+                union[0].operation = 'UNION'
+
+                # Apply modifier
+                # Can't use bpy.ops.object.modifier_apply because poll fails.
+                combined.data = combined.create_mesh(scene,
+                    apply_modifiers=True,
+                    settings='PREVIEW')
+                combined.modifiers.remove(union[0])
+
+
+# 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:
+        if obj["CloudMember"] != None:
+            if obj.parent:
+                return True
+
+            else:
+                del(obj["CloudMember"])
+
+    return False
+
+
+class View3DPanel(bpy.types.Panel):
+    bl_space_type = 'VIEW_3D'
+    bl_region_type = 'TOOLS'
+
+
+class VIEW3D_PT_tools_cloud(View3DPanel):
+    bl_label = "Cloud Generator"
+    bl_context = "objectmode"
+
+    def draw(self, context):
+        active_obj = context.active_object
+
+        degenerate = degenerateCloud(active_obj)
+
+        if active_obj and degenerate:
+            layout = self.layout
+
+            col = layout.column(align=True)
+            col.operator("cloud.generate_cloud", text="DeGenerate")
+
+        elif active_obj and active_obj.type == 'MESH':
+            layout = self.layout
+
+            col = layout.column(align=True)
+            col.operator("cloud.generate_cloud", text="Generate Cloud")
+
+        else:
+            layout = self.layout
+
+            col = layout.column(align=True)
+            col.label(text="Select one or more")
+            col.label(text="objects to generate")
+            col.label(text="a cloud.")
+
+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"
+    bl_description = "Create a Cloud."
+    bl_register = True
+    bl_undo = True
+
+    def execute(self, context):
+        # Make variable that is the current .blend file main data blocks
+        main = context.main
+
+        # Make variable that is the active object selected by user
+        active_object = context.active_object
+
+        # 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 = 35
+            maxNumOfPoints = 100000
+            scattering = 3
+            pointDensityRadius = 0.4
+
+            # Should we degnerate?
+            degenerate = degenerateCloud(active_object)
+
+            if degenerate:
+                # Degenerate Cloud
+                if active_object["CloudMember"] == "MainObj":
+                    mainObj = active_object
+
+                else:
+                    mainObj = active_object.parent
+
+                cloudMembers = active_object.children
+
+                # Find the created objects children of main and delete.
+                createdObjFound = False
+
+                createdObjects = []
+                i = 0
+                for member in cloudMembers:
+                    applyScaleRotLoc(scene, member)
+
+                    if (member["CloudMember"] == "CreatedObj"):
+                        createdObjects.append(member)
+                        del cloudMembers[i]
+

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-extensions-cvs mailing list