Page 1 of 2
obj loader script
Posted: Fri Jan 03, 2014 4:52 am
by Davve
A script that loads obj files from a directory and hook them up to an geo group node.
/David
Code: Select all
path = "C:\\your path\\"
cmd = "dir " .. path .. " /b /a"
objFile = {}
nObj = 0;
for dir in io.popen(cmd):lines() do
s = string.lower(string.sub(dir, -4))
if(s == ".obj") then
nObj = nObj + 1
objFile[nObj] = dir
print(objFile[nObj])
end
end
root = octane.nodegraph.getRootGraph()
group = octane.node.create{type=octane.NT_GEO_GROUP, name="Group", groupOwner=root, position={400,300}}
group:setAttribute(octane.A_PIN_COUNT, nObj, true)
for i=1,nObj do
mesh = octane.node.create{type=octane.NT_GEO_MESH , name=objFile[i], groupOwner=group , position={300 + i*50,230}}
mesh:setAttribute(octane.A_FILENAME, path .. objFile[i], true)
group:connectToIx(i, mesh)
end
Re: obj loader script
Posted: Sat Jan 04, 2014 5:04 pm
by bepeg4d
hi davve, thanks for sharing

unfortunately it doesn't work, at least with 1.24, here is what i get:
ciao beppe
Re: obj loader script
Posted: Sat Jan 04, 2014 10:48 pm
by stratified
Hi Davve,
Thanks for your script! I modified it a bit to make it work for us Linux users. The only catch is that you'll need to install the lua file system library (
http://keplerproject.github.io/luafiles ... anual.html) because we need a portable way to list the content of a directory (We'll probably include this in one of the next versions of Octane because it's really useful). I described LuaRocks in a different post:
http://render.otoy.com/forum/viewtopic.php?f=73&t=37632.
Just some remarks, the field
groupOwner
doesn't exist in
PROPS_NODE_ITEM
so it's ignored by Octane. When you don't want to position your nodes manually, you can call
unfold()
on your graph which will layout it nicely for you. Thanks again! here's the code:
Code: Select all
-- Generates a group node from the obj files loaded from a directory
--
-- @description Loads OBJ files from a folder and hooks them up with a group node
-- @version 0.2
-- @author Davve
-- use the lua file system module
require("lfs")
-- let the user choose a directory
local result = octane.gui.showDialog
{
type = octane.gui.dialogType.FILE_DIALOG,
title = "Choose OBJ file directory",
browseDirectory = true,
save = false,
}
local path = result.result
assert(path ~= "", "invalid OBJ directory")
-- fetch all the obj files in the directory
local objFiles = {}
for child in lfs.dir(path) do
if (string.lower(string.sub(child, -4)) == ".obj") then
table.insert(objFiles, child)
end
end
local root = octane.nodegraph.getRootGraph()
-- create the group node
group = octane.node.create{ type=octane.NT_GEO_GROUP, name="Group" }
group:setAttribute(octane.A_PIN_COUNT, #objFiles, true)
-- create a node for each obj file and connect it to the group
for i, name in ipairs(objFiles) do
local mesh = octane.node.create{type=octane.NT_GEO_MESH , name=name }
mesh:setAttribute(octane.A_FILENAME, string.format("%s//%s", path, name), true)
group:connectToIx(i, mesh)
end
-- call unfold on the root graph to arrange everything neatly
root:unfold()
You should see something like this in the end:

- group created from all the OBJ files in a folder
cheers,
Thomas
Re: obj loader script
Posted: Sun Jan 05, 2014 3:35 pm
by Tugpsx
It there an OS detection option, that would allow this to work on PC, Mac and Linux.
Maybe even adding a octane version under the octane.osversion similar to the octane.aversion=123 that would allow a coder to have a script which detects the OS and run the script based on detection.
BTW I had issues getting LFS working on win7 pc, but after a search so did a lot of others.
Re: obj loader script
Posted: Sun Jan 05, 2014 7:22 pm
by stratified
Hi,
You can get the current Octane version and the operating system via getSystemInfo()
, is this what you're after?
The only reason we're using lfs is to have a portable way of iterating directories. But in the next version of Octane we'll add that functionality.
If we can we'll try to avoid external libraries in our scripts. But for users who aren't distributing their scripts it isn't really an issue.
cheers,
Thomas
Re: obj loader script
Posted: Sun Jan 05, 2014 8:27 pm
by Tugpsx
Thanks, then its best to leave well alone until next release
Re: obj loader script
Posted: Sun Jan 05, 2014 11:08 pm
by Davve
Thanks Thomas for your modification.
I must admit that I'm a scripting noob and that it is mainly my brother that created the script, but I'm learning..
I also tried to install the lfs lib without success.
So this version will have to do for us win users for now.
root:unfold() is cool, is there a way to add it as a button in the octane graph?
Code: Select all
-- Generates a group node from the obj files loaded from a directory
--
-- @description Loads OBJ files from a folder and hooks them up with a group node
-- @version 0.3 for windows
-- @author Davve
-- let the user choose a directory
local result = octane.gui.showDialog
{
type = octane.gui.dialogType.FILE_DIALOG,
title = "Choose OBJ file directory",
browseDirectory = true,
save = false,
}
local path = result.result
assert(path ~= "", "invalid OBJ directory")
-- fetch all obj files in the directory
cmd = "dir " .. path .. " /b /a"
objFile = {}
nObj = 0;
for dir in io.popen(cmd):lines() do
s = string.lower(string.sub(dir, -4))
if(s == ".obj") then
nObj = nObj + 1
objFile[nObj] = dir
print(objFile[nObj])
end
end
root = octane.nodegraph.getRootGraph()
-- create the group node
group = octane.node.create{type=octane.NT_GEO_GROUP, name="Group"}
group:setAttribute(octane.A_PIN_COUNT, nObj, true)
-- create a node for each obj file and connect it to the group
for i=1,nObj do
name=objFile[i]
mesh = octane.node.create{type=octane.NT_GEO_MESH , name=name }
mesh:setAttribute(octane.A_FILENAME, string.format("%s//%s", path, name), true)
group:connectToIx(i, mesh)
end
root:unfold()
Re: obj loader script
Posted: Mon Jan 06, 2014 12:19 am
by Tugpsx
Nice update, maybe you could also add an option to store last path used octane.project.getCurrentProject or something like that.
Thanks for sharing.
Re: obj loader script
Posted: Mon Jan 06, 2014 2:03 am
by stratified
Hi all,
In 1.25 there will be a function
octane.file.listDirectory
to get the contents of a directory in a portable way. So LuaFileSystem isn't mandatory anymore. This should work fine in release candidate 1.25.
Code: Select all
-- Generates a group node from the obj files loaded from a directory
--
-- @description Loads OBJ files from a folder and hooks them up with a group node
-- @version 0.3
-- @author Davve
-- let the user choose a directory
local result = octane.gui.showDialog
{
type = octane.gui.dialogType.FILE_DIALOG,
title = "Choose OBJ file directory",
browseDirectory = true,
save = false,
}
local path = result.result
assert(path ~= "", "invalid OBJ directory")
-- fetch all the obj files in the directory
local objFiles = octane.file.listDirectory(path, true, false, true, false, "*.obj")
local root = octane.nodegraph.getRootGraph()
-- create the group node
group = octane.node.create{ type=octane.NT_GEO_GROUP, name="Group" }
group:setAttribute(octane.A_PIN_COUNT, #objFiles, true)
-- create a node for each obj file and connect it to the group
for i, name in ipairs(objFiles) do
local mesh = octane.node.create{type=octane.NT_GEO_MESH , name=name }
mesh:setAttribute(octane.A_FILENAME, name, true)
group:connectToIx(i, mesh)
end
-- call unfold on the root graph to arrange everything neatly
root:unfold()
Unfolding can be done in the Standalone version by pressing
F2
.
cheers,
Thomas
Re: obj loader script
Posted: Mon Jan 06, 2014 2:05 am
by stratified
Tugpsx wrote:Nice update, maybe you could also add an option to store last path used octane.project.getCurrentProject or something like that.
Thanks for sharing.
not sure what you mean,
octane.project.getCurrentProject()
already exists in the API.
cheers,
Thomas