4

I can't figure out if there's something wrong with blender or my script. So far I have this little code, which does its job if the file which is parsing is not SO big:

import bpy
fh = open('coordinates.txt')

for line in fh:
    coo = line.split()
    x = float(coo[0])
    y = float(coo[1])
    z = float(coo[2])
    bpy.ops.mesh.primitive_cube_add(location=(x, y, z))
    bpy.ops.transform.resize(value=(1, 1, 1))

When I try coordinates.txt with few lines, it works and it creates the cubes, but when I do it with its original size of around 215000 lines, Blender stops responding and it stays like that forever.

Has anyone any idea of what I can do to avoid it? (Reducing the file's lines is not an option )

Btw, coordinates.txt has this format

335.0091 2433.163 -126.9688
351.7849 2352.089 -126.9688
-493 -808 128.5834
-1076 -843 132.8153
160.123 2369.68 -126.9688
-760.663 -836.174 132.07
182.9523 2438.389 -126.9688
-332 -754 88.59317
259.0002 2480.136 -126.9688
-857.507 -738.361 130.3733
338.3209 2430.226 -126.9688
353.1722 2349.083 -126.9688
-493 -808 128.5834
-1076 -843 132.8153
David
  • 49,291
  • 38
  • 159
  • 317

1 Answers1

4

Your srcipt is fine, and blender is not timing out, but rather you have found a limitation of blender. Blender is not good at displaying many objects, 250,000 different cubes will crash blender.
Try adding 3 array modifiers to a cube, to generate 250,00 cubes, then go into edit mode and separate by Loose Parts (blender will crash.)

Instead of adding a cube at each coordinate, create a mesh with all the coordinates as vertices. So each coordinate is represented as a vertex.

import bpy
fh = open('coordinates.txt')

# A list of the locations of the cubes
vertices = []

for line in fh:
    coo = line.split()
    x = float(coo[0])
    y = float(coo[1])
    z = float(coo[2])

    # Instead of adding a cube, append the location of the cube to vertices
    vertices.append((x, y, z))

# Make a new mesh and set its vertices
mesh = bpy.data.meshes.new('verts')
mesh.from_pydata(vertices, [], [])

# Make an object for the mesh
object = bpy.data.objects.new('verts', mesh)
bpy.context.scene.objects.link(object)

Then parent a default cube to the newly generated mesh, and use dupliverts to add a cube at each vertex.
Dupliverts

From the excerpt of your coordinates, in top view, it produces this:
Test file

David
  • 49,291
  • 38
  • 159
  • 317
  • Woah, thank you for your answer, it is really helpful, but while trying your script, I get in this line mesh = bpy.data.meshes.new('verts') an error: Invalid syntax – Rubén Rincón Jan 10 '15 at 17:49
  • @RubénRincónBlanco There was a missing closing parenthesis on line 14. It will work now. – David Jan 10 '15 at 20:40