The cube is composed of 1 million voxels (100x100x100) having the same size (1 unit). Voxel information is saved in this .txt file having the format: (4 columns x 1 million lines).
The first 3 columns represent x, y, z of each voxel, while the 4th column has the values: {0, 1, 2, 7, 8, 9, 10, 12, 13, 16, 17}
. Each number represents a color in the model (eg: {black, yellow, green, blue, cyan, lawngreen, pink, magenta, white, gray, orange}
)
Because of the high number of voxels, I tried to pass by creating instances by adapting a code that I found here. the script worked for representing all voxels as instances. However, I'm unable to find a solution to color each voxel based on data in the 4th column within the voxels file.
I tried to use instancing and arrays by geometry nodes but it is still challenging for me as it's just my 2nd application using Blender.
Here is the script representing the voxels:
import bpy
import numpy as np
import mathutils as mt
bpy.ops.object.select_all(action="SELECT")
bpy.ops.object.delete()
C = bpy.context
#open text file
rawfile = open("/Users/test/Documents/voxels.txt","r")
data=np.loadtxt( rawfile, delimiter=' ' )
rawfile.close()
#Create and arrange mesh data
verts = [ mt.Vector( data[i,:3] ) for i in range(data.shape[0]) ]
m = bpy.data.meshes.new('pc')
m.from_pydata(verts, [], [])
# Create mesh object and link to scene collection
o = bpy.data.objects.new('pc', m)
C.scene.collection.objects.link( o )
#Add primitive cube
bpy.ops.mesh.primitive_cube_add( size = 1)
isobj = bpy.data.objects[C.object.name]
# Set instancing props
for ob in [ isobj, o]:
ob.instance_type = 'VERTS'
ob.show_instancer_for_viewport = True
ob.show_instancer_for_render = True
# Set instance parenting (parent cube to verts)
o.select_set(True)
C.view_layer.objects.active = o
bpy.ops.object.parent_set( type = 'VERTEX', keep_transform = True )