hi,
a simple script to array a set of selected objects with random placement & random spacing.maybe useful for filling up bookshelves,row of bottles,buildings,etc.this is my first script.if you can check the code and give comments it will be very useful for me.thanks
a try:
a sample file,script can be downloaded from
http://uploader.polorix.net//files/1329/Randobjarray.zip
http://www.mediafire.com/?umifxt5dlmx
code:
#!BPY
"""
Name: 'Randobjarray'
Blender: 245
Group: 'Object'
Tooltip: 'random object select and array'
"""
__author__ = "cibikone"
__url__ = ("",)
__version__ = ""
__bpydoc__ = """Description: selects objects randomly and arranges along x-axis
Usage:
1.select objects to be arrayed
2.set total number of objects needed
3.set gap(in blender units)
4.if random gap is needed set maxgap and mingap(then mingap must be zero or less than maxgap)
5.set alignbottom/alignfront if needed
6.press execute
7.you can select next objects and run execute again
Notes: objects will be arrayed along x-axis starting at origin
apply size&rotation (ctrl+A) before arraying
first version feb28,2008
"""
# --------------------------------------------------------------------------
# ***** 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# ***** END GPL LICENCE BLOCK *****
# --------------------------------------------------------------------------
import Blender
from Blender import Scene,Object,Draw,BGL
from Blender.Mathutils import Rand
from Blender.Draw import *
## User input values ##
number = 10
gap = 0.0
maxgap = 0.0
mingap = 0.0
alignfront = 1
alignbottom = 1
## GUI ##
bt_number = Blender.Draw.Create(number)
bt_gap = Blender.Draw.Create(gap)
bt_maxgap = Blender.Draw.Create(maxgap)
bt_mingap = Blender.Draw.Create(mingap)
bt_alignfront = Blender.Draw.Create(alignfront)
bt_alignbottom = Blender.Draw.Create(alignbottom)
bt_execute = Blender.Draw.Create(1)
bt_exit = Blender.Draw.Create(1)
def gui():
global number,gap,maxgap,mingap,alignbottom,alignfront
global bt_number,bt_gap,bt_maxgap,bt_mingap,bt_alignfront,bt_alignbottom
x = 10
y = 10
width = 120
height = 20
hgap = 10
vgap = 20
Blender.BGL.glClear(Blender.BGL.GL_COLOR_BUFFER_BIT)
BGL.glColor3f(0.0,0.0,0.0)
BGL.glRasterPos2i(x, 5*y+4*vgap)
Blender.Draw.Text("RandObjArray:","large")
bt_number = Number("number:", 101, x, 4*y+3*vgap, width, height, bt_number.val, 0, 10000, "total array objects")
bt_gap = Number("gap:", 102, x+width+hgap,4*y+3*vgap, width, height, bt_gap.val, 0.0, 100.0, "gap bet. objects")
bt_maxgap = Number("maxgap:", 103, x, 3*y+2*vgap, width, height, bt_maxgap.val, 0.0, 1000.0, "maximum gap")
bt_mingap = Number("mingap:", 104, x+width+hgap,3*y+2*vgap, width, height, bt_mingap.val, 0.0, 100.0, "minimum gap")
bt_alignbottom = Blender.Draw.Toggle("alignbottom", 105, x, 2*y+vgap, width, height, bt_alignfront.val,"Align object bottom")
bt_alignfront = Blender.Draw.Toggle("alignfront", 106, x+width+hgap, 2*y+vgap, width, height, bt_alignbottom.val,"Align object front")
bt_exit = PushButton("Exit",107,x+width+hgap,y,width,height, "ExitScript")
bt_execute = PushButton("Execute",108,x,y,width,height, "MakeArray")
def bevent(evt):
global number,gap,maxgap,mingap,alignbottom,alignfront
if evt == 107:
Blender.Draw.Exit()
if evt == 108:
print 'calling main()'
number = bt_number.val
gap = bt_gap.val
maxgap = bt_maxgap.val
mingap = bt_mingap.val
alignfront = bt_alignfront.val
alignbottom = bt_alignbottom.val
main()
def event(evt, val):
if evt == Blender.Draw.ESCKEY:
Blender.Draw.Exit()
Blender.Draw.Register(gui,event,bevent)
## Variables and Function definitions ##
scn = Scene.GetCurrent()
objlist = []
newobjlist = []
objlist = Object.GetSelected()
selobjs = len(objlist)
## Objects randomly select and duplicate ##
def objduplicate():
#global number
for i in xrange(number):
scn.objects.selected = []
index = int(Rand(0,selobjs))
objlist[index].sel = 1
Object.Duplicate()
newobj = scn.objects.active
newobjlist.append(newobj)
## Object locate and align ##
def arrange(obj1,obj2):
global gap,maxgap,mingap,alignbottom,alignfront
obj1_bbox = obj1.getBoundBox()
obj2_bbox = obj2.getBoundBox()
obj2x = obj2_bbox[0][0]
if ( maxgap == 0 ):
newx = obj1_bbox[4][0]+abs(obj2x)+obj1.LocX+gap
if ( maxgap != 0 ):
gap = Rand(mingap,maxgap)
newx = obj1_bbox[4][0]+abs(obj2x)+obj1.LocX+gap
if (alignfront == 1):
obj2y = obj2_bbox[0][1]
newy = abs(obj2y)
else:
newy = 0
if (alignbottom == 1):
obj2z = obj2_bbox[0][2]
newz = abs(obj2z)
else:
newz = 0
obj2.setLocation(newx,newy,newz)
## Arrange the duplicate objects ##
## Locate and align first object at origin ##
def arrange_first():
obj0 = newobjlist[0]
obj0_bbox = obj0.getBoundBox()
if (alignfront == 1):
obj0y = obj0_bbox[0][1]
obj0_newy = abs(obj0y)
else:
obj0_newy = 0
if(alignbottom == 1):
obj0z = obj0_bbox[0][2]
obj0_newz = abs(obj0z)
else:
obj0_newz = 0
obj0.setLocation(0,obj0_newy,obj0_newz)
## Locate and align other objects ##
def arrange_all():
for i in xrange(0,len(newobjlist)-1):
newobj1 = newobjlist[i]
newobj2 = newobjlist[i+1]
arrange(newobj1,newobj2)
#Main function#
def main():
global scn,selobjs,newobjlist ,objlist
if( Object.GetSelected() == [] ):
print 'error:select objects to be arrayed'
return
elif( number == 0 ):
print 'error:number should be more than zero '
return
elif( maxgap < mingap ):
print 'maxgap should be more than mingap'
return
else:
objlist = Object.GetSelected()
selobjs = len(objlist)
objduplicate()
arrange_first()
arrange_all()
newobjlist = []
objlist = []
scn.objects.selected = [] #clear selections for next array with diff. selection
Blender.Redraw()