daily pastebin goal
85%
SHARE
TWEET

Untitled

a guest Aug 17th, 2010 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #! /usr/bin/env python
  2.  
  3. '''
  4. Stairs and railing creator script for blender 2.49
  5. Author: Nick Trefiak
  6. Date: 2010 08 09
  7.  
  8. Creates a straight-run staircase with railings and stringer
  9. All components are optional and can be turned on and off by setting e.g. makeTreads=True or makeTreads=False
  10. No GUI for the script, all parameters must be defined below
  11. Current values assume 1 blender unit = 1 metre
  12.  
  13. Stringer will rest on lower landing and hang from upper landing
  14. Railings start on the lowest step and end on the upper landing
  15.  
  16. NOTE: You must have numpy installed for this script to work!
  17.      numpy is used to easily perform the necessary algebra
  18.      numpy can be found at http://www.scipy.org/Download
  19.  
  20. Note: I'm not sure how to use recalcNormals so not all normals points ouwards.
  21.       Perhaps someone else can contribute this.
  22. '''
  23.  
  24. #---------------------------------------------------------------------
  25. '''define all parameters here'''
  26.  
  27. global rise, run
  28. rise=0.20 #single tread rise, including the tread height
  29. run=0.30 #single tread run, not including the nosing
  30.  
  31. #for treads
  32. makeTreads=True
  33. wT=1.2 #tread width
  34. hT=0.04 #tread height
  35. tT=0.03 #tread toe, a.k.a. nosing
  36. nT=10 #number of treads
  37.  
  38. #for posts
  39. makePosts=True
  40. dP=0.04 #post depth
  41. wP=0.04 #post width
  42. nP=5 #number of posts
  43.  
  44. #for railings
  45. makeRailings=True
  46. wR=0.12 #rail width
  47. tR=0.03 #rail thickness
  48. hR=0.90 #rail height
  49.  
  50. #for retainers
  51. makeRetainers=True
  52. wRR=0.01 #retainer width
  53. hRR=0.01 #retainer height
  54. nRR=3 #number of retainers
  55.  
  56. #for stringer
  57. makeStringer=True
  58. wS=0.15 #stringer width, set equal to tread width for fully enclosed riser
  59.  
  60.  
  61. #---------------------------------------------------------------------
  62. '''import modules'''
  63. from Blender import *
  64. import bpy
  65. import math
  66. from numpy import *
  67.  
  68. #---------------------------------------------------------------------
  69. '''classes for mesh creation'''
  70.  
  71. class General:
  72.    
  73.    '''common data, methods needed for other objects'''
  74.  
  75.    def __init__(self,rise,run,N):
  76.        self.start=zeros((3))
  77.        self.stop=float(N)*array([run,0,rise])
  78.        self.slope=rise/run
  79.        self.angle=arctan(self.slope)
  80.        #identical quads for all objects except stringer
  81.        self.faces=[[0,1,3,2],[0,1,5,4],[0,2,6,4],[4,5,7,6],[2,3,7,6],[1,3,7,5]]
  82.  
  83.    def Make_mesh(self,coords,faces,meshname,objname):
  84.        '''make a mesh given the vertex coordinates, faces, and names for mesh and object'''
  85.        me = bpy.data.meshes.new(meshname)
  86.        me.verts.extend(coords.tolist())
  87.        me.faces.extend(faces)
  88.        scn = bpy.data.scenes.active
  89.        ob = scn.objects.new(me, objname)
  90.  
  91. class Treads:
  92.  
  93.    '''class for creating treads'''
  94.  
  95.    def __init__(self,w,h,d,r,toe,N):
  96.        self.w=w #tread width
  97.        self.h=h #tread height
  98.        self.d=d #tread run
  99.        self.r=r #tread rise
  100.        self.t=toe #tread nosing
  101.        self.N=N #number of treads
  102.        self.Create()
  103.  
  104.    def Create(self):
  105.        for i in range(self.N):
  106.            coords=zeros((8,3))
  107.            coords[0,:]=[-self.t,0,0]
  108.            coords[1,:]=[self.d,0,0]
  109.            coords[2,:]=[-self.t,self.w,0]
  110.            coords[3,:]=[self.d,self.w,0]
  111.            coords[4:,:]=coords[0:4,:]+array([0,0,-self.h])
  112.            coords=coords+array([i*self.d,0,i*self.r])
  113.            G.Make_mesh(coords,G.faces,'tMesh','tObj')
  114.  
  115. class Posts:
  116.  
  117.    '''class to create posts'''
  118.    
  119.    def __init__(self,d,w,wT,nP,hR,tR):
  120.        self.x1=G.start+array([0,0,hR-tR]) #rail start
  121.        self.x2=G.stop+array([0,0,hR-tR]) #rail stop
  122.        self.d=d #post depth
  123.        self.w=w #post width
  124.        self.wT=wT #tread width
  125.        self.nP=nP #number of posts
  126.        self.sp=array([(self.x2[0]-self.x1[0])/float(nP+1),0,0]) #spacing between posts
  127.        self.Create()
  128.  
  129.    def Intersect(self,i,d):
  130.        '''find intersection point, x, for rail and post'''
  131.        x3=self.x1+i*self.sp+d
  132.        x4=x3+array([0,0,self.x2[-1]])
  133.        a=self.x2-self.x1
  134.        b=x4-x3
  135.        c=x3-self.x1
  136.        cr_ab=cross(a,b)
  137.        mag_cr_ab=(cr_ab**2).sum()
  138.        return self.x1+a*(dot(cross(c,b),cr_ab)/mag_cr_ab)
  139.  
  140.    def Create(self):
  141.        for i in range(0,self.nP+2,1):
  142.            coords=zeros((8,3))
  143.            #intersections with rail
  144.            coords[0]=self.Intersect(i,0.0)
  145.            coords[1]=self.Intersect(i,self.d)
  146.            #intersections with tread
  147.            coords[2]=array([self.x1[0]+i*self.sp[0],0,int(coords[0,0]/run)*rise])
  148.            coords[3]=coords[2]+array([self.d,0,0])
  149.            #inner face
  150.            coords[4:,:]=coords[0:4,:]+array([0,self.w,0])
  151.            G.Make_mesh(coords,G.faces,'pMesh','pObj')
  152.            #make post on other side of steps as well
  153.            coords=coords+array([0,self.wT-self.w,0])
  154.            G.Make_mesh(coords,G.faces,'pMesh','pObj')
  155.                        
  156. class Rails:
  157.  
  158.    '''class for creating railings'''
  159.  
  160.    def __init__(self,w,t,h,tT,wP,dP,wT):
  161.        self.w=w #rail width
  162.        self.t=t #rail thickness
  163.        self.h=h #rail height
  164.        self.start=G.start+array([0,0,self.h-self.t]) #rail start
  165.        self.stop=G.stop+array([0,0,self.h-self.t]) #rail stop
  166.        self.tT=tT #tread toe
  167.        self.wP=wP #post width
  168.        self.dP=dP #post depth
  169.        self.wT=wT #tread width
  170.        self.Create()
  171.  
  172.    def Create(self):
  173.        #determine offset to include railing toe
  174.        offset=array([self.tT,0,self.tT*tan(G.angle)])
  175.        coords=zeros((8,3))
  176.        coords[0]=self.start-offset
  177.        coords[1]=self.stop+offset+array([self.dP,0,self.dP*tan(G.angle)])
  178.        coords[2]=self.start-offset+array([0,self.w,0])
  179.        coords[3]=self.stop+offset+array([self.dP,self.w,self.dP*tan(G.angle)])
  180.        coords[4:,:]=coords[0:4,:]+array([0,0,self.t])
  181.        #centre over posts
  182.        coords=coords+array([0,0.5*(-self.w+self.wP),0])
  183.        G.Make_mesh(coords,G.faces,'rMesh','rObj')
  184.        #make rail on other side
  185.        coords=coords+array([0,self.wT-self.wP,0])
  186.        G.Make_mesh(coords,G.faces,'rMesh','rObj')
  187.        
  188. class Retainers:
  189.  
  190.    '''class for creating retainers'''
  191.  
  192.    def __init__(self,w,h,wP,wT,nR):
  193.        self.w=w #retainer width
  194.        self.h=h #retainer height
  195.        self.wP=wP #post width
  196.        self.wT=wT #tread width
  197.        self.nR=nR #number of retainers
  198.        self.sp=hR/float(nR+1) #retainer spacing
  199.        self.Create()
  200.  
  201.    def Create(self):
  202.        for i in range(self.nR):
  203.            coords=zeros((8,3))
  204.            offset=(i+1)*array([0,0,self.sp])
  205.            coords[0]=G.start+offset
  206.            coords[1]=G.stop+offset
  207.            coords[2]=G.start+offset+array([0,self.w,0])
  208.            coords[3]=G.stop+offset+array([0,self.w,0])
  209.            coords[4:,:]=coords[0:4,:]+array([0,0,self.h])
  210.            #centre in posts
  211.            coords=coords+array([0,0.5*(self.wP-self.w),0])
  212.            G.Make_mesh(coords,G.faces,'rrMesh','rrObj')
  213.            #make retainer on other side
  214.            coords=coords+array([0,self.wT-self.wP,0])
  215.            G.Make_mesh(coords,G.faces,'rrMesh','rrObj')
  216.  
  217. class Stringer:
  218.    
  219.    '''class for creating stringer'''
  220.  
  221.    def  __init__(self,w,nT,hT,wT):
  222.        self.w=w #stringer width
  223.        self.nT=nT #number of treads
  224.        self.hT=hT #tread height
  225.        self.wT=wT #tread width
  226.        self.faces=[[0,1,3,2],[1,5,3],[3,5,4],[6,7,9,8],[7,11,9],[9,11,10],[0,2,8,6],[0,1,7,6],[1,5,11,7]]
  227.        self.Create()
  228.        
  229.    def Create(self):
  230.        for i in range(self.nT):
  231.            coords=zeros((12,3))
  232.            coords[0]=G.start+array([0,0,-rise])
  233.            coords[1]=G.start+array([run,0,-rise])
  234.            coords[2]=G.start+array([run*0,0,-self.hT])
  235.            coords[3]=G.start+array([run*1,0,-self.hT])
  236.            coords[4]=G.start+array([run*1,0,0])
  237.            coords[5]=G.start+array([run*2,0,0])
  238.            coords[6:]=coords[0:6]+array([0,self.w,0])
  239.            coords=coords+i*array([run,0,rise])
  240.            #centre below tread
  241.            coords=coords+array([0,0.5*(self.wT-self.w),0])
  242.            G.Make_mesh(coords,self.faces,'sMesh','sObj')
  243.        
  244. #-----------------------------------------------------------
  245. '''create meshes'''
  246.  
  247. editmode = Window.EditMode()    # are we in edit mode?  If so ...
  248. if editmode: Window.EditMode(0) # leave edit mode before getting the mesh
  249.  
  250. global G
  251. G=General(rise,run,nT)
  252. if makeTreads: Treads(wT,hT,run,rise,tT,nT)
  253. if makePosts: Posts(dP,wP,wT,nP,hR,tR)
  254. if makeRailings: Rails(wR,tR,hR,tT,wP,dP,wT)
  255. if makeRetainers: Retainers(wRR,hRR,wP,wT,nRR)
  256. if makeStringer: Stringer(wS,nT,hT,wT)
  257.  
  258. if editmode: Window.EditMode(1)  # optional, just being nice
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top