function pairsByKeys (t, f) local a = {} for n in pairs(t) do table.insert(a, n) end table.sort(a, f) local i = 0 -- iterator variable local iter = function () -- iterator function i = i + 1 if a[i] == nil then return nil else return a[i], t[a[i]] end end return iter end function readLines(sPath) local file = io.open(sPath, "r") local n1,n2,n3,n4,n5,n6,n7,n8,n9 local section_name,section_start,section_end local xfa_read=0 local file_read=0 if file then local tLines = {} local tline = {} for sline in file:lines() do file_name=sline:match('file :(.-[^\\]-([^%.]+))$') if file_name then print(file_name) file_read=file_read+1 table.insert(FILES,{file_name,xfa_read+1,0}) if file_read-1>0 then FILES[file_read-1][3]=xfa_read end else n1,n2,n3,n4,n5,n6,n7,n8,n9,n10,n11,n12,n13,n14,n15,n16 = sline:match("([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+),([^,]+)") if n1 and n2 and n3 and n4 and n5 and n6 and n7 and n8 and n9 and n10 and n11 and n12 and n13 and n14 and n15 and n16 then xfa_read= xfa_read+1 table.insert(tLines,{{n1,n2,n3,n4},{n5,n6,n7,n8},{n9,n10,n11,n12}} ) end end end file.close() if file_read>0 then FILES[file_read][3]=xfa_read end XFA = tLines end return nil end function lengthTable(T) local count = 0 for _ in pairs(T) do count = count + 1 end return count end function importfiles() -- create the group node local transforms={} local MESHES={} local MATERIALS={} local items={} local parent local group local meshout local refresh local cursel = octane.project.getSelection()[1] collectgarbage() if cursel == nil then parent = octane.nodegraph.create{ type = octane.GT_STANDARD, name = FILES[#FILES][1] } group = octane.node.create{ type=octane.NT_GEO_GROUP, name="GROUP" ,graphOwner=parent} meshout=octane.node.create{type=octane.NT_OUT_GEOMETRY, name="GEOM_OUT",graphOwner=parent} refresh=false else refresh=true parent=cursel group= parent:findItemsByName("GROUP")[1] print(group) meshout=parent:findItemsByName("GEOM_OUT")[1] print(meshout) end group:setAttribute(octane.A_PIN_COUNT, #FILES, true) MM = parent:findNodes(octane.NT_GEO_MESH) SS = parent:findNodes(octane.NT_GEO_SCATTER) for i=1, #MM do print(MM[i]) MM[i]:destroy() end for i=1, #SS do print(SS[i]) SS[i]:destroy() end -- create a node for each obj file and connect it to the group for i=1, #FILES do collectgarbage() local mesh local scatter local mesh_exist=false mesh=parent:findItemsByName(FILES[i][1])[1] if mesh== nil then mesh=octane.node.create{type=octane.NT_GEO_MESH , name=FILES[i][1] ,graphOwner=parent} mesh:setAttribute(octane.A_FILENAME, FILES[i][1], true) scatter= octane.node.create{type=octane.NT_GEO_SCATTER ,graphOwner=parent} mesh_exist=false else scatter=mesh:getDestinationNodes()[1]['node'] mesh_exist=true print(scatter) end transforms={} for j=FILES[i][2], FILES[i][3] do table.insert(transforms,XFA[j]) end scatter:setAttribute(octane.A_TRANSFORMS, transforms) if mesh_exist==false then scatter:connectToIx(1,mesh) end group:connectToIx(i, scatter) table.insert(MESHES,mesh) end local pin collectgarbage() for i=1, #MESHES do for pinIx=1,MESHES[i]:getPinCount() do pin=MESHES[i]:getPinInfoIx(pinIx) if MATERIALS[pin.label]==nil then MATERIALS[pin.label]={} end table.insert(MATERIALS[pin.label],{i,pinIx}) end end local mat collectgarbage() for mat_name,MESHES_PIN in pairsByKeys(MATERIALS) do mat=nil print(MESHES_PIN) mat=parent:findItemsByName(mat_name)[1] print("for name= ",mat_name," Material= ",mat) if mat==nil then mat=octane.node.create{type=octane.NT_IN_MATERIAL, name=mat_name,graphOwner=parent} end for j=1,#MESHES_PIN do octane.node.connectToIx(MESHES[MESHES_PIN[j][1]], MESHES_PIN[j][2], mat) end end if refresh==false then meshout:connectToIx(1, group) end end -- Global vars XFA={} FILES={} local ret1 = octane.gui.showDialog { type = octane.gui.dialogType.FILE_DIALOG, title = "Choose instance export script", wildcards = "*.oscr", save = false, path = IN_PATH, } -- if a file is chosen if ret1.result ~= "" then IN_PATH = ret1.result readLines(IN_PATH ) importfiles() else IN_PATH = nil end