# Generating a 3D grid

I'm trying to generate a 3D grid that can be rendered visibly in the Cycles renderer. Preferably using a script where I can customize the size and dimensions.

I'm picturing something like this:

I've tried the answers from this question: Create gridlines in Python But the script from the first answer only generates a floor grid which isn't very useful. For the 2nd script, I couldn't figure out how it works.

So any help will be appreciated!

• First and Second answer will depend on how answers are ordered. (Active, Oldest, Votes) Surely being shown how to create one grid is somewhat useful. Both scripts would need updating to work in 2.8x. Which is the primary question here, how to render edges in cycles or how to create them? – batFINGER Apr 3 '20 at 20:00
• @batFINGER I managed to update both of them just fine. The script by CodeManX is the one that generates one grid, but you can already do that simply with Add->Grid. The primary question is creating them. – what the Apr 3 '20 at 20:07

I came up with two scripts, one which "brutally" instantiates verts and edges, then converts it to a curve in order to add thickness.

import bpy

mesh = bpy.data.meshes.new('Mesh_Grid')
obj = bpy.data.objects.new('Obj_Grid', mesh)

rows, columns, levels = 10, 10, 10
wire_thickness = 0.02

verts, edges = [], []

i = 0
for l in range(levels):
for r in range(rows):
for c in range(columns):
verts.append((c, r, l))
if c < columns - 1:
edges.append((i, i + 1))
if r < rows - 1:
edges.append((i, i + columns))
if l < levels - 1:
edges.append((i, i + (rows * columns)))
i += 1

mesh.from_pydata(verts, edges , [])

obj.select_set(True)
bpy.context.view_layer.objects.active = obj

bpy.ops.object.convert(target='CURVE')

obj.data.bevel_depth = wire_thickness


Result :

And the other one which is the same as @Vega_KH's answer, but as a script. I just added a weld modifier in order to avoid duplicate geometry. (Need Blender 2.82+) :

import bpy

dimensions = (10,10,10)
wf_thickness = 0.1

cube = bpy.context.selected_objects[0]

for i in range(3):
mod = cube.modifiers.new('Array_' + str(i), 'ARRAY')
mod.relative_offset_displace[0] = 0
mod.relative_offset_displace[i] = 1
mod.count = dimensions[i]

cube.modifiers.new('WD', 'WELD')
wf = cube.modifiers.new('WF', 'WIREFRAME')
wf.thickness = wf_thickness


Result :

1. Start with a cube. The base cube that comes with a new scene is fine.
3. Change count to 8. Leave the "relative offset" default (1,0,0)
5. Change the count to 8. Change the "relative offset" to (0,1,0)
7. Change the count to 8. Change the "relative offset" to (0,0,1)
8. Add a wireframe modifier. Change the thickness to 0.1m.

This isn't a script but you can just adjust the count on each modifier up and down to easily resize the grid. And obviously change the thickness on the wireframe modifier to make the lines thicker.

ANother one

Sheesh, doesn't pay to get sidetracked.

Similarly to https://blender.stackexchange.com/a/61254/15543

import bpy
import bmesh
from bpy import context

obj = context.object
me = obj.data

arrays = ((10, (1, 0, 0)),
(10, (0, 1, 0)),
(10, (0, 0, 1)),
)

for count, displace in arrays:
print(count, displace)
mod = obj.modifiers.new("Array", 'ARRAY')
mod.count = count
mod.relative_offset_displace = displace
bpy.ops.object.modifier_apply(modifier=mod.name)

bpy.ops.object.origin_set()

bm = bmesh.new()
bm.from_mesh(me)

# remove faces and doubles

bmesh.ops.delete(bm,
geom=bm.faces,
context='FACES_ONLY',
)

bmesh.ops.remove_doubles(bm,
verts=bm.verts,
dist=1e-05,
)

bm.to_mesh(me)
me.update()