Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
# created to test answer for
# http://blender.stackexchange.com/questions/14814/object-creation-slows-over-time
import bpy
import bpy_extras
import bmesh
import time
arraysizex = 100
arraysizey = 10
def createMeshObjects():
for x in range(arraysizex):
for y in range(arraysizey):
bpy.ops.mesh.primitive_cube_add()
bpy.context.active_object.location = (x,y,1)
def createSingleMesh():
faces = [(0, 1, 2, 3),
(4, 7, 6, 5),
(0, 4, 5, 1),
(1, 5, 6, 2),
(2, 6, 7, 3),
(4, 0, 3, 7),
]
mesh = bpy.data.meshes.new("Boxes")
bm = bmesh.new()
for x in range(arraysizex):
for y in range(arraysizey):
verts_loc = [(x+1.0, y+1.0, -1.0),
(x+1.0, y-1.0, -1.0),
(x-1.0, y-1.0, -1.0),
(x-1.0, y+1.0, -1.0),
(x+1.0, y+1.0, +1.0),
(x+1.0, y-1.0, +1.0),
(x-1.0, y-1.0, +1.0),
(x-1.0, y+1.0, +1.0),
]
f_offfset = len(bm.verts)
for v_co in verts_loc:
bm.verts.new(v_co)
for f_idx in faces:
bm.faces.new([bm.verts[i+f_offfset] for i in f_idx])
bm.to_mesh(mesh)
mesh.update()
bpy_extras.object_utils.object_data_add(bpy.context, mesh)
t_start = time.time()
createMeshObjects()
t_end = time.time()
objects_time_taken = t_end-t_start
t_start = time.time()
createSingleMesh()
t_end = time.time()
mesh_time_taken = t_end-t_start
t_start = time.time()
createSingleMesh()
bpy.ops.mesh.separate(type='LOOSE')
bpy.ops.object.origin_set(type='ORIGIN_GEOMETRY')
t_end = time.time()
mesh_separate_time_taken = t_end-t_start
t_start = time.time()
bpy.ops.mesh.primitive_cube_add()
c_name = bpy.context.active_object.name
bpy.ops.mesh.primitive_grid_add(x_subdivisions=arraysizex, y_subdivisions=arraysizey, radius=arraysizex)
g_name = bpy.context.active_object.name
bpy.ops.object.select_pattern(pattern=c_name, extend=False)
bpy.ops.object.select_pattern(pattern=g_name, extend=True)
bpy.ops.object.parent_set(type='OBJECT')
bpy.context.object.dupli_type = 'VERTS'
bpy.ops.object.duplicates_make_real()
t_end = time.time()
dupliverts_time_taken = t_end-t_start
print("Testing %d objects" % (arraysizex*arraysizey))
print("Creating individual objects takes %f" % objects_time_taken)
print("Creating single mesh takes %f" % mesh_time_taken)
print("Creating single mesh then separate takes %f" % mesh_separate_time_taken)
print("Creating dupliverts takes %f" % dupliverts_time_taken)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.