VI-Suite v0.3 – Exporting LiVi Data

A user asked me how to export the x, y, z co-ordinates of the LiVi sensor positions with the results to a comma separated file for further plotting or analysis. This capability will be enabled in version 0.4 with the “Results out” socket of the LiVi simulation node, but for now you can copy the python code below into Blender’s text editor and press the “Run Script” button at the bottom of the text editor window. This code will export the currently visualised results for the current frame to files in the project directory. These files are named ‘name_of_results_object.manualres’.

LiVi results data is stored within the mesh’s vertices and faces. To do this LiVi uses Blender’s Bmesh wrapper for the mesh data. So the first part of the code loops through results objects (line 3) creates a bmesh representation of each one (line 5) and transforms the bmesh (line 6) so that vertex and space positions are in global or world co-ordinates.

Line 7 designates either vertices or faces as the results points depending on the object ‘cpoint’ property (which is created on LiVi geometry export). Line 8 then checks that there are results available in the mesh and line 9 defines the cindex layer which stores whether a point in a mesh is a results point. Line 10 defines the resl layer which actually holds the results.

Line 11 generates the vertex or face positions for each results point and stores them in the variable posis. Line 12 and 13 then define the output file and writes the co-ordinates and the data to the file. The final line 14 clears the bmesh data from memory.

import bpy, bmesh, os
scene = bpy.context.scene
for o in [o for o in bpy.data.objects if o.type == 'MESH' and o.get('cpoint') and o.lires]:
    bm = bmesh.new()
    bm.from_mesh(o.data)
    bm.transform(o.matrix_world)
    geom = bm.verts if o['cpoint'] == '1' else bm.faces
    if geom.layers.float.get('res{}'.format(scene.frame_current)) and geom.layers.int.get('cindex'):
        cindex = geom.layers.int['cindex']
        resl = geom.layers.float['res{}'.format(scene.frame_current)]
        posis = [v.co for v in bm.verts if v[cindex] > 0] if o['cpoint'] == '1' else [f.calc_center_bounds() for f in bm.faces if f[cindex] > 0]
        with open(os.path.join(scene['viparams']['newdir'], '{}.manualres'.format(o.name)), 'w') as resfile:
            resfile.write('\n'.join(['{0[0]}, {0[1]}, {0[2]}, {1}'.format(posis[gi], g[resl]) for gi, g in enumerate(geom) if g[cindex] > 0]))
    bm.free()

Indentation in Python is important so make sure the code in the Blender Text editor window looks like the screenshot below.

manualres