Advertisement
Guest User

cotejrp1

a guest
Jan 25th, 2010
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.25 KB | None | 0 0
  1. """
  2. Name: 'step_pyramid'
  3. Blender: 250
  4. Group: 'AddMesh'
  5. Tip: 'Add Step Pyramid Object...'
  6. """
  7. __author__= [ "Phil Cote", "BlenderPythonTutorials.com" ]
  8. __version__ = '.01'
  9.  
  10. import bpy
  11. from bpy.props import IntProperty, FloatProperty
  12.  
  13.  
  14.  
  15.  
  16. def makePyramid( initialSize, stepHeight, stepWidth, numberSteps ):
  17.    
  18.     extrude = bpy.ops.mesh.extrude
  19.     translate = bpy.ops.tfm.translate
  20.     resize = bpy.ops.tfm.resize
  21.    
  22.     # establish the initial base.
  23.     bpy.ops.mesh.primitive_plane_add( enter_editmode=True )
  24.     ob = bpy.context.object
  25.     ob.name = 'pyramid'
  26.     resize( value=( initialSize, initialSize, 0 ) )
  27.     extrude()
  28.     translate( value=(0,0, stepHeight ) )
  29.    
  30.    
  31.     # iterate to create the steps to extrude out from the base
  32.     currentSize = initialSize
  33.    
  34.     while numberSteps > 0:
  35.         currentSize = currentSize - stepWidth
  36.         extrude()
  37.         resize(value=(currentSize, currentSize, 0 ) )
  38.         extrude()
  39.         translate( value=(0, 0, -stepHeight ) )
  40.         numberSteps = numberSteps - 1
  41.    
  42.  
  43.     mesh = ob.data
  44.     bpy.ops.object.editmode_toggle()
  45.     bpy.context.scene.objects.unlink( ob )
  46.     return mesh
  47.  
  48.  
  49.  
  50.  
  51. class OBJECT_OT_PyramidOp( bpy.types.Operator ):
  52.    
  53.     initialSize = FloatProperty( name="initialSize", default=1.0, min=0.0, max=5.0 )
  54.     stepHeight= FloatProperty( name="stepHeight", default=0.3, min=0.0, max=5.0 )
  55.     stepWidth= FloatProperty( name="stepWidth", default=0.1, min=0.0, max=5.0 )
  56.     numberSteps= IntProperty( name="numberSteps", default=5, min=1, max=10 )
  57.    
  58.     bl_undo = True
  59.     bl_register = True
  60.     bl_idname="pyramid_op"
  61.  
  62.  
  63.     def execute( self, context ):
  64.    
  65.         initSize = self.properties.initialSize
  66.         sHeight = self.properties.stepHeight
  67.         sWidth = self.properties.stepWidth
  68.         nSteps = self.properties.numberSteps
  69.         mesh = makePyramid( initSize, sHeight, sWidth, nSteps )
  70.        
  71.         scene = context.scene
  72.  
  73.         for ob in scene.objects:
  74.             ob.selected = False
  75.  
  76.         mesh.update()
  77.         ob_new = bpy.data.add_object( 'MESH', "Pyramid" )
  78.         ob_new.data = mesh
  79.         scene.objects.link( ob_new )
  80.         scene.objects.active = ob_new
  81.         ob_new.selected = True
  82.        
  83.         return( 'FINISHED', )
  84.  
  85.  
  86.  
  87. bpy.ops.add( OBJECT_OT_PyramidOp )
  88.  
  89. import dynamic_menu
  90. menuFunc = ( lambda self, context: self.layout.operator( OBJECT_OT_PyramidOp.bl_idname, text="Pyramid" ) )
  91. menu_item = dynamic_menu.add( bpy.types.INFO_MT_mesh_add, menuFunc )
  92.  
  93.  
  94.  
  95. # DEBUGGING SECION...
  96. def deleteMeshes():
  97.    
  98.     obs = bpy.context.scene.objects
  99.  
  100.     for ob in obs:
  101.         if ob.type == 'MESH':
  102.             obs.unlink( ob )
  103.  
  104. # test method to verify that the function that generates the pyramid actually works.
  105. def testPyramidGeneration():
  106.      mesh = makePyramid( initialSize=1.0, stepHeight=0.3, stepWidth=0.1, numberSteps=5 )
  107.        
  108.      scene = bpy.context.scene
  109.  
  110.      for ob in scene.objects:
  111.          ob.selected = False
  112.  
  113.      mesh.update()
  114.      ob_new = bpy.data.add_object( 'MESH', "Pyramid" )
  115.      ob_new.data = mesh
  116.      scene.objects.link( ob_new )
  117.  
  118.  
  119. # leave these 2 lines commented out when not validating the makePyramid method.
  120. deleteMeshes()
  121. testPyramidGeneration()
  122.  
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement