Export to STL format

Visionary Render does not include exporters out of the box, however using LUA scripts a Visionary Render Platform developer can write their own exporter. 

This example code translates a Visionary Render mesh node to an STL file. For more information on the STL format, see https://en.wikipedia.org/wiki/STL_%28file_format%29.

function stlWriteMesh(mesh)
  -- all the vertex attribute related arrays have thier data in a property called Content
  local function aget(array, idx)
    return vrNodeArrayGetElement(array, "Content", idx)
  end
 
  -- find special child nodes of the mesh
  local function findchild(name)
    local arr = mesh:find(name);
    if not arr then
      print(mesh:path() .. " : " .. name .. " array not found")
      return nil
    end
    return arr
  end
 
  -- find the positions
  local pos = findchild("pos")
  if not pos then return end
 
  -- find the trianle indices
  local trilist = findchild("trilist")
  if not trilist then return end
 
  -- gets vector position from an index
  local function vget(tri)
    local idx = aget(trilist, tri)
    return vrVec3(aget(pos, (idx * 3) + 0), aget(pos, (idx * 3) + 1), aget(pos, (idx * 3) + 2))
  end
 
  -- process all the triangles
  local idxcount = vrNodeArrayGetSize(trilist, "Content")
  for i = 0, idxcount-1, 3 do
    local v1 = vget(i)
    local v2 = vget(i+1)
    local v3 = vget(i+2)
    local nrm = vrNormalise(vrCross(v1, v2))
 
   io.write("facet normal " .. tostring(nrm) .. "\n")
   io.write("\touter loop\n")
   io.write("\t\tvertex " .. tostring(v1) .. "\n")
   io.write("\t\tvertex " .. tostring(v2) .. "\n")
   io.write("\t\tvertex " .. tostring(v3) .. "\n")
   io.write("\tendloop\n")
  end
end
 
function stlWrite(mesh, filename)
  local fd = io.open(filename, "w")
  if not fd then
    print("Failed to open file")
    return
  end
 
  io.output(fd)
  io.write("solid test\n")
  stlWriteMesh(mesh)
  io.write("endsolid\n")
  io.close(fd)
end
 
local mesh = vrTreeRoot():find("root/Libraries/Default/Models/TorusKnot/GeoGroup/Mesh")
stlWrite(mesh, "c:\\temp\\test.stl")



E
Ericka is the author of this solution article.

Did you find it helpful? Yes No

Send feedback
Sorry we couldn't be helpful. Help us improve this article with your feedback.