Array Modifier Grid

This is a practical common scenario where we need to create objects in a Grid, particularly in motion graphic projects.

In this Tutorial, We’ll see how we can write scripts and add functionality step by step. I will try to make each addition only around 5 lines.

Note

This is first tutorial so it contains somewhat detailed instruction, later tutorials aren’t as details to avoid repetition.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import bpy

# create cube
bpy.ops.mesh.primitive_cube_add(size=2, enter_editmode=False, align='WORLD',
                                location=(0, 0, 0), scale=(1, 1, 1))

# create array
bpy.ops.object.modifier_add(type='ARRAY')

# create count = 5
bpy.context.object.modifiers["Array"].count = 5

# create step = 2
bpy.context.object.modifiers["Array"].relative_offset_displace[0] = 2

# apply modifier
bpy.ops.object.modifier_apply(modifier="Array")

# enter editmode
bpy.ops.object.editmode_toggle()

bpy.ops.mesh.separate(type='LOOSE')

# exit editmode
bpy.ops.object.editmode_toggle()

# set pivot
bpy.ops.object.origin_set(type='ORIGIN_CENTER_OF_VOLUME', center='MEDIAN')
There are 4 sub-steps:
  1. create cube.

  2. add array modifier

  3. change array modifier’s properties.

  4. apply and reset origin(centering pivot)

There are several things to note in above code, lets see.
  1. sub-step 4 will be same however many copies we create.

  2. sub-step 3, relative_offset_displace and count should be floatField and intField respectively.

  3. sub-step 2, more modifiers can be added for all 3 axis

  4. sub-step 1 can be an option, where we create any any mesh type. (eg. Sphere,Cylinder)