ty.
ty.

Reputation: 231

How to use a numpy array to generate array of meshes in blender?

I would like to use blender to visualize a scatter plot animation with data from a large 2D array, such as

a = np.array([[0, 0, 0],
              [1, 2, 1],
              [4, 0, 1]]).

Here a[i] describes the position of the i-th mesh/object. I wish to create these objects in the blender scene. The following code does that with cubes, but is too slow when a contains thousands of vectors.

import bpy
import numpy as np

a = np.array([[0, 0, 0],
              [1, 2, 1],
              [4, 0, 1]])
for pos_vec in a:
    bpy.ops.mesh.primitive_cube_add(location=pos_vec)

How can I do this without looping in python or make it as fast as possible?

Upvotes: 0

Views: 922

Answers (1)

vicky Lin
vicky Lin

Reputation: 159

It looks like your "3D" array is a more like point Cloud data.

And Blender can import .ply point cloud data

bpy.ops.import_mesh.ply(filepath="PATH_TO_PLY.ply")

And Before that you just need to convert your 3-D array to .ply, there are plenty of ways. Each below will work.

  • Supposed you have your 3-D array in numpy.array. You can try python2plyfile

  • matlab

array3D =[1,0,0
          2,0,0
          3,0,0]
pc = pointCloud(array3D) 
pcwirte(pc, "PLY_FILE.ply")

Upvotes: 1

Related Questions