Skip to content

Instantly share code, notes, and snippets.

/create_objects.py
Created Jul 28, 2015

Embed
What would you like to do?
import bpy
import bmesh
import mathutils
import numpy as np
import time
scene = bpy.context.scene
mesh = bpy.data.meshes.new('PythonCube')
bm = bmesh.new()
bmesh.ops.create_cube(bm, size=2.0)
bm.to_mesh(mesh)
bm.free()
wxy = 15.0
sx, sy, sz = 0.9, 0.9, 0.2
nxy_list = [10, 15, 20, 25, 30, 35, 40]
listlen = len(nxy_list)
namefs = "obj.%03d.%04d"
for ii, nxy in enumerate(nxy_list):
rcube = 0.5 * wxy / float(nxy)
q = 0.5 * wxy * (1. - 1. / float(nxy))
xyc = np.linspace(-q, q, nxy)
XC, YC = np.meshgrid(xyc, xyc)
ZC = np.zeros_like(XC)
centers = zip(XC.flatten(), YC.flatten(), ZC.flatten())
centerslen = nxy * nxy
start = time.clock()
for ic, center in enumerate(centers):
name = namefs % (listlen - ii, centerslen - ic)
obj = bpy.data.objects.new(name, mesh)
obj.location = center
obj.scale = rcube * mathutils.Vector((sx, sy, sz))
scene.objects.link(obj)
stop = time.clock()
print("time_to_create: %f, nxy %02d" % (stop - start, nxy))
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.