Open enzyme69 opened 6 years ago
So with script above it will import the Voxel, but it does not really consider the Color Index Palette of each voxel box.
I further think a bit and decided maybe we can PRINT OUT the list of data as Binary Text, something like below [ x y z index ]
8 13 3 15
4 3 1 188
3 3 1 188
16 5 3 15
17 4 3 15
7 15 3 15
8 14 3 15
.....
Above I can read and parse using Sverchok nodes to become Matrix placement of Box object.
Text In node will do the job.
Once it become a data inside Blender that NODES can operate and process, we can simply generate voxel with random color, with color based on Hue Spectrum, or maybe even supplying our own PNG Color Palette ala Magica Voxel.
But I went further and actually modify the Python script executable above into a node. Maybe below setup can improve. I am using Script Node Lite SNL node to turn the script into node.
By doing it this way, I can read and update multiple VOX files on the fly and generate the result in Blender. Thanks to SV nodes.
## HERE IS THE BLEND ZIP vox_reader_study_020_2017_12_30_01_19.zip
### SNL READ FILES OF TYPE VOX
"""
in dummy s
out filepaths s
out filenames s
"""
import os
import bpy
# put the location to the folder where the objs are located here in this fashion
# this line will only work on windows ie C:\objects
#path_to_obj_dir = os.path.join('C:\\', 'Some\\Folder\\Collada\\')
path_to_obj_dir = os.path.join('/Users/jimmygunawan/', 'Documents', '_VOX_FILES')
# get list of all files in directory
file_list = sorted(os.listdir(path_to_obj_dir))
# get a list of files ending in 'obj'
obj_list = [item for item in file_list if item.endswith('.vox')]
# loop through the strings in obj_list and add the files to the scene
for item in obj_list:
path_to_file = os.path.join(path_to_obj_dir, item)
filepaths.append(path_to_file)
filenames.append(item)
#print(path_to_file)
### SNL VOX READER AS NODE
"""
in mypath s
out obpos v
out colorindex s
"""
import struct
import bpy
# Import MagicaVoxel .VOX files and MagicaVoxel .PNG Palette Files
# by @matesteinforth
# modified by Blender Sushi Guy 20171229 to work for Blender via Sverchok Node
#mypath = "/Users/jimmygunawan/facemonster_002.vox"
#mypath = mypath[0][0]
mypath = mypath[0]
# Parse file
with open(mypath, 'rb') as f:
# Check filetype
bytes = f.read(4)
file_id = struct.unpack(">4s", bytes)
if file_id[0] == b'VOX ':
# Init material list
matlist = []
#skip header
f.seek(56)
# Read number of voxels, stuct.unpack parses binary data to variables
bytes = f.read(4)
numvoxels = struct.unpack('<I', bytes)
# Iterate through voxels
for x in range(0, numvoxels[0]):
# Read voxels, ( each voxel : 1 byte x 4 : x, y, z, colorIndex ) x numVoxels
bytes = f.read(4)
voxel = struct.unpack('<bbbB', bytes)
#update material list, generate new material only if it isn't in the list yet
matid = voxel[3]
#print(voxel[0], voxel[1], voxel[2], matid)
obpos.append([voxel[0], voxel[1], voxel[2]])
colorindex.append(matid)
So you have 3 methods up there to import VOX using Blender Python and SV nodes: 1) Once off Python script to grab the Voxel, no material or color 2) Get list of data and assign color procedurally using Sverchok 3) Actually use NODES to read VOX files in the directory and read and update every frame as needed to generate VOX on the fly.
KEEP IN MIND:
Magica Voxel version 0.98 and 0.99 should work with this importer.
VOXEL is definitely a great study, especially in case of Magica Voxel app, it has a lot of treasures.
I am studying IO input output interoperability between Magica Voxel app and Blender. I am using a bit of Python knowledge and Sverchok add-on to assist me in the process.
Basically I started by reading the Python script by Mate Steinforth below for Cinema4D and making it work for Blender Python bpy. https://github.com/MateSteinforth/ImportVOX
The script is 2 years old and still works fine. I modified and simplified the script so that the script will read the VOX bytes and parse it into LIST OF NUMBERS that we can use to regenerate voxel box in Blender. Surprisingly it is not as complicated after all I can extract the X Y Z and Palette Index Number of VOX.