[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [31377] branches/soc-2010-aligorith-2/ release/scripts: Bullet SoC - Simulation Component Templates

Joshua Leung aligorith at gmail.com
Mon Aug 16 15:24:50 CEST 2010


Revision: 31377
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=31377
Author:   aligorith
Date:     2010-08-16 15:24:49 +0200 (Mon, 16 Aug 2010)

Log Message:
-----------
Bullet SoC - Simulation Component Templates

This (last minute) commit adds one such template, which sets up a frequently used rigidbody element: a brick wall, which would otherwise take a lot more setup.

Admittedly, it is not the most efficient or even well-tuned template yet, but it acts as a base from which further templates can be created by users as required.

Modified Paths:
--------------
    branches/soc-2010-aligorith-2/release/scripts/ui/space_info.py

Added Paths:
-----------
    branches/soc-2010-aligorith-2/release/scripts/op/add_rigidbody_wall.py

Added: branches/soc-2010-aligorith-2/release/scripts/op/add_rigidbody_wall.py
===================================================================
--- branches/soc-2010-aligorith-2/release/scripts/op/add_rigidbody_wall.py	                        (rev 0)
+++ branches/soc-2010-aligorith-2/release/scripts/op/add_rigidbody_wall.py	2010-08-16 13:24:49 UTC (rev 31377)
@@ -0,0 +1,124 @@
+# ##### 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 #####
+
+import bpy
+
+# add 'brick' mesh
+def add_brick(context, major_radius, minor_radius, mass):
+    # add new brick as a cube
+    bpy.ops.mesh.primitive_cube_add()
+    brick = context.active_object
+    
+    # set name
+    brick.name = "RbBrick"
+    
+    # set the sizes by scaling + applying that scaling
+    brick.scale = (major_radius, minor_radius, minor_radius)
+    bpy.ops.object.scale_apply()
+    
+    # make it an active rigidbody, with the appropriate settings
+    bpy.ops.rigidbody.objects_add(type = 'ACTIVE')
+    
+    rbo = brick.rigid_body
+    
+    rbo.collision_shape = 'BOX'     # use the simplest shape possible...
+    rbo.mass = mass
+    # XXX: triggering settings?
+    
+    return brick; 
+
+from bpy.props import *
+
+class AddRbWall(bpy.types.Operator):
+    '''Add a wall of bricks for Rigid Body Simulation'''
+    bl_idname = "rigidbody.add_prefab_wall"
+    bl_label = "Add Wall"
+    bl_options = {'REGISTER', 'UNDO'}
+    
+    rows = IntProperty(name="Rows",
+            description="Number of bricks stacked on top of each other",
+            default=10, min=1, max=256)
+    columns = IntProperty(name="Columns",
+            description="Number of bricks per row",
+            default=5, min=1, max=256)
+    
+    major_length = FloatProperty(name="Major Length",
+            description="Distance from center to edge along widest axis",
+            default=1.5, min=0.01, max=100.0)
+    minor_length = FloatProperty(name="Minor Length",
+            description="Distance from center to edge along all other axes",
+            default=0.5, min=0.01, max=100.0)
+            
+    mass = FloatProperty(name="Mass",
+            description="How much each brick weighs (same as for Rigid Bodies)",
+            default=1, min=0.001, max=100.0)
+    
+    def poll(self, context):
+        return (not context.object) or (context.object.mode == 'OBJECT')
+    
+    def execute(self, context):
+        props = self.properties
+        
+        # add single brick 
+        # - this will end up being the source brick from which 
+        #   all other bricks are derived
+        # - it will also be the lower-left brick in the wall...
+        # - assume that this is added at (0,0,0)
+        brick = add_brick(context, props.major_length, props.minor_length, props.mass)
+        
+        # duplicate this brick as many times as needed
+        for row in range(props.rows):
+            for col in range(props.columns):
+                # make a copy of the active brick
+                bpy.ops.object.duplicate()
+                curBrick = context.active_object
+                
+                # set parent to original brick so they can all be moved at once
+                curBrick.parent = brick
+                
+                # calculate the positions based on the row and column
+                rowLoc = (2 * props.minor_length) * row
+                colLoc = (2 * props.major_length) * col
+                
+                curBrick.location = (colLoc, 0.0, rowLoc)
+                
+        # select only the starting brick 
+        bpy.ops.object.select_all(action='DESELECT')
+        
+        brick.selected = True
+        context.scene.objects.active = brick
+        
+        return {'FINISHED'}
+
+
+# Add to a menu
+menu_func = (lambda self, context: self.layout.operator(AddRbWall.bl_idname,
+                    icon='MESH_GRID', text="Wall"))
+
+
+def register():
+    bpy.types.register(AddRbWall)
+    bpy.types.INFO_MT_rigidbody_add.append(menu_func)
+
+
+def unregister():
+    bpy.types.unregister(AddRbWall)
+    bpy.types.INFO_MT_rigidbody_add.remove(menu_func)
+
+if __name__ == "__main__":
+    register()

Modified: branches/soc-2010-aligorith-2/release/scripts/ui/space_info.py
===================================================================
--- branches/soc-2010-aligorith-2/release/scripts/ui/space_info.py	2010-08-16 13:13:05 UTC (rev 31376)
+++ branches/soc-2010-aligorith-2/release/scripts/ui/space_info.py	2010-08-16 13:24:49 UTC (rev 31377)
@@ -235,7 +235,15 @@
         layout.operator_context = 'INVOKE_REGION_WIN'
         layout.operator("object.armature_add", text="Single Bone", icon='BONE_DATA')
 
+class INFO_MT_rigidbody_add(bpy.types.Menu):
+    bl_idname = "INFO_MT_rigidbody_add"
+    bl_label = "Rigid Body Template"
 
+    def draw(self, context):
+        layout = self.layout
+        layout.operator_context = 'INVOKE_REGION_WIN'
+        # XXX: add anything here?
+
 class INFO_MT_add(bpy.types.Menu):
     bl_label = "Add"
 
@@ -268,6 +276,9 @@
 
         layout.operator_menu_enum("object.effector_add", "type", text="Force Field", icon='OUTLINER_OB_EMPTY')
         layout.separator()
+        
+        layout.menu("INFO_MT_rigidbody_add", icon='OUTLINER_OB_EMPTY')
+        layout.separator()
 
         if(len(bpy.data.groups) > 10):
             layout.operator_context = 'INVOKE_DEFAULT'
@@ -383,6 +394,7 @@
     INFO_MT_curve_add,
     INFO_MT_surface_add,
     INFO_MT_armature_add,
+    INFO_MT_rigidbody_add,
     INFO_MT_game,
     INFO_MT_render,
     INFO_MT_help,





More information about the Bf-blender-cvs mailing list