obj loader script

Forums: obj loader script
Forum for OctaneRender Lua scripting examples, discussion and support.

obj loader script

Postby Davve » Fri Jan 03, 2014 4:52 am

Davve Fri Jan 03, 2014 4:52 am
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
Davve
Licensed Customer
Licensed Customer
 
Posts: 25
Joined: Fri Aug 09, 2013 6:59 pm

Re: obj loader script

Postby bepeg4d » Sat Jan 04, 2014 5:04 pm

bepeg4d Sat Jan 04, 2014 5:04 pm
hi davve, thanks for sharing ;)
unfortunately it doesn't work, at least with 1.24, here is what i get:
Capture-st124-02.JPG

ciao beppe
User avatar
bepeg4d
Octane Guru
Octane Guru
 
Posts: 6514
Joined: Wed Jun 02, 2010 6:02 am
Location: Italy

Re: obj loader script

Postby stratified » Sat Jan 04, 2014 10:48 pm

stratified Sat Jan 04, 2014 10:48 pm
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/luafilesystem/manual.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_mesh.png
group created from all the OBJ files in a folder


cheers,
Thomas
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: obj loader script

Postby Tugpsx » Sun Jan 05, 2014 3:35 pm

Tugpsx Sun Jan 05, 2014 3:35 pm
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.
Dell Win Vista 32 | NVIDIA Quadro NVS135M | Core2 Duo T7500 2.20GHz | 4GB
CyberPowerPC Win 7 64| NVIDIA GTX660Ti (3G) GTX480(1.5G) | 16GB
Tugpsx
Licensed Customer
Licensed Customer
 
Posts: 1058
Joined: Thu Feb 04, 2010 8:04 pm
Location: Chicago, IL

Re: obj loader script

Postby stratified » Sun Jan 05, 2014 7:22 pm

stratified Sun Jan 05, 2014 7:22 pm
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
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: obj loader script

Postby Tugpsx » Sun Jan 05, 2014 8:27 pm

Tugpsx Sun Jan 05, 2014 8:27 pm
Thanks, then its best to leave well alone until next release
Dell Win Vista 32 | NVIDIA Quadro NVS135M | Core2 Duo T7500 2.20GHz | 4GB
CyberPowerPC Win 7 64| NVIDIA GTX660Ti (3G) GTX480(1.5G) | 16GB
Tugpsx
Licensed Customer
Licensed Customer
 
Posts: 1058
Joined: Thu Feb 04, 2010 8:04 pm
Location: Chicago, IL

Re: obj loader script

Postby Davve » Sun Jan 05, 2014 11:08 pm

Davve Sun Jan 05, 2014 11:08 pm
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()
Davve
Licensed Customer
Licensed Customer
 
Posts: 25
Joined: Fri Aug 09, 2013 6:59 pm

Re: obj loader script

Postby Tugpsx » Mon Jan 06, 2014 12:19 am

Tugpsx Mon Jan 06, 2014 12:19 am
Nice update, maybe you could also add an option to store last path used octane.project.getCurrentProject or something like that.
Thanks for sharing.
Dell Win Vista 32 | NVIDIA Quadro NVS135M | Core2 Duo T7500 2.20GHz | 4GB
CyberPowerPC Win 7 64| NVIDIA GTX660Ti (3G) GTX480(1.5G) | 16GB
Tugpsx
Licensed Customer
Licensed Customer
 
Posts: 1058
Joined: Thu Feb 04, 2010 8:04 pm
Location: Chicago, IL

Re: obj loader script

Postby stratified » Mon Jan 06, 2014 2:03 am

stratified Mon Jan 06, 2014 2:03 am
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
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand

Re: obj loader script

Postby stratified » Mon Jan 06, 2014 2:05 am

stratified Mon Jan 06, 2014 2:05 am
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
User avatar
stratified
OctaneRender Team
OctaneRender Team
 
Posts: 945
Joined: Wed Aug 15, 2012 6:32 am
Location: Auckland, New Zealand
Next

Return to Lua Scripting


Who is online

Users browsing this forum: No registered users and 1 guest

Wed Jul 24, 2019 2:56 am [ UTC ]