OpenSceneGraph Forum Forum Index OpenSceneGraph Forum
Official forum which mirrors the existent OSG mailing lists. Messages posted here are forwarded to the mailing list and vice versa.
 
   FAQFAQ    SearchSearch    MemberlistMemberlist    RulesRules    UsergroupsUsergroups    RegisterRegister 
 Mail2Forum SettingsMail2Forum Settings  ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
   AlbumAlbum  OpenSceneGraph IRC ChatOpenSceneGraph IRC Chat   SmartFeedSmartFeed 

Lua scripting


 
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> Plugins [osgPlugins]
View previous topic :: View next topic  
Author Message
rarum
Newbie


Joined: 22 Sep 2012
Posts: 10

PostPosted: Wed Feb 18, 2015 6:30 pm    Post subject:
Lua scripting
Reply with quote

Hi,

Long time I am trying to use lua scripting plugin without success.
This is all my lua code :

"
function test (string, double,matrix )
print(string)
print(double)
print(matrix)
return osg.Matrixd
end

"

It's very simple, but output is still "Lua error : attempt to call a string value". I think that in script is missing some require or somethig that tell Lua about OSG . I add as input parameter
inputParameters.push_back(new osg::MatrixfValueObject("matrix",osg::Matrixf()));

I thik lua doesn't know something about OSG, and I got errors in console.
Can you tell me what is wrong ?
I have idea about generating some glue with Swig generator , and build it with OSG (as simple DLL file), and use with require in lua script. But it doesn't work. Application crashed. Can you public simple lua test script with one funcion, which is used with [Examples osgprezentation] project ? On web there are no examples about lua scripting in OSG. Or can you tell me how to do some magic with osg and lua. I am using OSG for 3 years but scripting is very difficult for me, I need som explanation how it works together .

Thank you!

Cheers,
Martin
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 11152

PostPosted: Wed Feb 18, 2015 7:11 pm    Post subject:
Lua scripting
Reply with quote

Hi Martin,


Which version of the OSG are you using?  In svn/trunk and the recent OSG-3.3.x dev series there is a lua plugin which provides the Lua source code directly within the plugin as well as the glue to integrate the OSG with Lua.  There won't be any need to use tools like Swig as the OSG's serialization support is used to expose the OSG class properties and methods for you.


As a test could you build OSG svn/trunk and OpenSceneGraph-Data svn/trunk, put the OpenSceneGraph-Data directory on an OSG_FILE_PATH env var and then run:

osgviewer ui/TabWidget.lua.90,0,0.rot cow.osgt


What you should get is a cow in the low left of widget with a tab panel in it.  You can drag the panel around via the title area, and resize it using the mouse scroll wheel in the title area.  Clicking on the tab will jump you between the different elements.  Note, the 90,0,0.rot is simply there to rotate the osgUI widget from being in the XY plane to the XZ plane.  The cow.osgt isn't lua of course but should give a little 3d reference to it.


Now this example TabWidget.lua script is just what I've used for testing.  A more sophisticated usage of lua can be found with VolumeSettings.lua script. This is designed to be used as part of Present3D along with volume rendering.  These scripts are all examples of using .lua as a pseudo plugin where the lua script creates scene graph objects, populates the scene graph with lua callbacks and passes back the scene graph.

One can also create Lua scripts and assign these as callbacks to the scene graph.  To do this you'd create your script/read it from a file (via osgDB::readScriptFile("myscript.lua"); and then call this via creating script engine and then passing in the script to be run via ScriptEngine::run(..), or add as a callback to the scene graph by attaching the osg::Script to an osg::ScriptNodeCallback.  Have a look at the include/osg/ScriptEngine for all these classes.


The osgtransferfunction and osgpresentation examples are really just test beds that I've used when developing this functionality so aren't written for the purpose of education.  You might be able to glean a little from these.  I'm afraid there aren't any examples written specifically for teaching users how to use the scripting yet.  While this will be a bit of hurdle it'll should be much easier to get to grips with than jumping through all the hoops that a tool like Swig would introduce.


It's been quite a few months since last worked specifically on the lua code so I'll be a little rusty but hopefully will be able to point you in the right direction.  If you do have problems then if you can put together an example that can be tested then it'll be easier for me to spot what might be going wrong.

Good luck,
Robert.

------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
rarum
Newbie


Joined: 22 Sep 2012
Posts: 10

PostPosted: Thu Feb 19, 2015 9:16 am    Post subject:
Reply with quote

Hi, Robert

I have success with small example on OSG version 3.3.3. I call lua function.

function test (s)
k = s.Name;
print(k);
s.Name="InScript";
print(s.Name);
no = new("osg::Group");
no.Name = "Node2";
s:addChild(no);
return no;
end

as input parameters in code is
osg::ref_ptr<osg::Group>inNode = new osg::Group;
inNode->setName("Start");
inputParameters.push_back(inNode);

outuput parameters
osg::ref_ptr<osg::Group>outNode = new osg::Group;
outputParameters.push_back(outNode);

and output :
Start
InScript
input node name = InScript
num children 1
Child node name : Node2
output node name =

As you can see, output parameter node has not set new name from csript. Can you tell me where is the problem, or how to use output parameters ?

And second example.
lua code

function test2(a)
--print(a)
return 0;
end

input parameter
inputParameters.push_back(new osg::Vec3fValueObject("vector",osg::Vec3f()));

output parameter
osg::ref_ptr<osg::Vec3fValueObject> svo = new osg::Vec3fValueObject("return", osg::Vec3f());
outputParameters.push_back(svo);
output
Lua error : attempt to call a table value
script run failed

This simple example doesn't work. It work with base value objects as string or double but not with osg value objects as Matrixd. Can you please tell me where is the problem? Whatever I try, it still doesn't work.

This is very basic examples and I think it can help all people starting with scripting in OSG.

Thank you!

Cheers,
Martin
Back to top
View user's profile Send private message
rarum
Newbie


Joined: 22 Sep 2012
Posts: 10

PostPosted: Thu Feb 19, 2015 9:17 am    Post subject:
Reply with quote

Hi, Robert

I have success with small example on OSG version 3.3.3. I call lua function.

function test (s)
k = s.Name;
print(k);
s.Name="InScript";
print(s.Name);
no = new("osg::Group");
no.Name = "Node2";
s:addChild(no);
return no;
end

as input parameters in code is
osg::ref_ptr<osg::Group>inNode = new osg::Group;
inNode->setName("Start");
inputParameters.push_back(inNode);

outuput parameters
osg::ref_ptr<osg::Group>outNode = new osg::Group;
outputParameters.push_back(outNode);

and output :
Start
InScript
input node name = InScript
num children 1
Child node name : Node2
output node name =

As you can see, output parameter node has not set new name from csript. Can you tell me where is the problem, or how to use output parameters ?

And second example.
lua code

function test2(a)
--print(a)
return 0;
end

input parameter
inputParameters.push_back(new osg::Vec3fValueObject("vector",osg::Vec3f()));

output parameter
osg::ref_ptr<osg::Vec3fValueObject> svo = new osg::Vec3fValueObject("return", osg::Vec3f());
outputParameters.push_back(svo);
output
Lua error : attempt to call a table value
script run failed

This simple example doesn't work. It work with base value objects as string or double but not with osg value objects as Matrixd. Can you please tell me where is the problem? Whatever I try, it still doesn't work.

This is very basic examples and I think it can help all people starting with scripting in OSG.

Thank you!

Cheers,
Martin
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 11152

PostPosted: Thu Feb 19, 2015 9:27 am    Post subject:
Lua scripting
Reply with quote

Hi Martin,

From what you've written it's difficult to know exactly what you've done wrong or are mis-understand.  A complete c++ example would be easier to know exactly what you've done and to recreate the problem.


The role of the outputParameters in the ScriptEngine::run(.., Parameters& outputParameters); method is to serve as a list of parameters that the script itself populates.  The way you use it is to pass in an empty Parameters vector, this then will have zero of more osg::Object's assigned to on completion of the run(..) call.


Does this help?

Robert.




On 19 February 2015 at 09:17, Martin Ferry < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi, Robert

I have success with small example on OSG version 3.3.3. I call lua function.

function test (s)
        k = s.Name;
        print(k);
        s.Name="InScript";
        print(s.Name);
        no = new("osg::Group");
        no.Name = "Node2";
        s:addChild(no);
    return no;
end

as input parameters in code is
osg::ref_ptr<osg::Group>inNode = new osg::Group;
                inNode->setName("Start");
                inputParameters.push_back(inNode);

outuput parameters
osg::ref_ptr<osg::Group>outNode = new osg::Group;
                outputParameters.push_back(outNode);

and output :
Start
InScript
input node name = InScript
num children 1
 Child node name : Node2
output node name =

As you can see, output parameter node has not set new name from csript. Can you tell me where is the problem, or how to use output parameters ?

And second example.
lua code

function test2(a)
        --print(a)
        return 0;
        end

input parameter
inputParameters.push_back(new osg::Vec3fValueObject("vector",osg::Vec3f()));

output parameter
osg::ref_ptr<osg::Vec3fValueObject> svo = new osg::Vec3fValueObject("return", osg::Vec3f());
                outputParameters.push_back(svo);
output
Lua error : attempt to call a table value
script run failed

This simple example doesn't work. It work with base value objects as string or double but not with osg value objects as Matrixd. Can you please tell me where is the problem? Whatever I try, it still doesn't work.

This is very basic examples and I think it can help all people starting with scripting in OSG.

Thank you!

Cheers,
Martin

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=62725#62725





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
rarum
Newbie


Joined: 22 Sep 2012
Posts: 10

PostPosted: Thu Feb 19, 2015 8:00 pm    Post subject:
Reply with quote

Hi, Rober

I have not added any object to outputParameters, and after script run I have object in output parameters. So I understand it outputParameters.

But I still have problem with value object as Vec3f or similar.
here is C++ code .

int main( int argc, char **argv )
{

osg::ArgumentParser arguments(&argc,argv);
osgDB::getDataFilePathList().push_back( "D:/zbcmy/osg33/data/" );

osg::ref_ptr<osg::Script> script = osgDB::readScriptFile("D:/zbcmy/vosg2/Debug/script/test.lua");
if (script.valid())
{
osg::Parameters inputParameters;
osg::Parameters outputParameters;
inputParameters.push_back(new osg::MatrixTransform);
inputParameters.push_back(new osg::Vec3fValueObject("Vec3f",osg::Vec3f()));
osg::ref_ptr<osg::ScriptEngine> luaScriptEngine = osgDB::readFile<osg::ScriptEngine>("ScriptEngine.lua");

if (luaScriptEngine->run(script.get(),"test", inputParameters, outputParameters))
OSG_NOTICE<<"Successfully ran script : return value = ";
else
OSG_NOTICE<<"script run failed"<<std::endl;
}
_getch();
}

and lua code

function test(a,k)
a.Name = "Pito";
a.Matrix = new ("osg::Matrixf()");
e = new ("osg::Vec3f");
--a.Matrix.Trans()
return a;
end

output
Lua error : attempt to call a table value
Function doesn't accept osg value object ? Something is wrong but I don't know what is wrong.

When I comment out line with Vec3fValueObject I get output

ClassInterface::createObject(osg::Matrixf()), No object wrapper avaiable.
Failed to create object osg::Matrixf()
LuaScriptEngine::setPropertyFromStack(0060FF80, Matrix) property of type = MATRIX not implemented
ClassInterface::createObject(osg::Vec3f), No object wrapper avaiable.
Failed to create object osg::Vec3f

I think that creation Matrix or Vec3f in Lua code is wrong , but I don't know how to do this.

Many thanks that you spent your expensive time with my not clever posts but I don't see any way for learn lua scripting in OSG.

Cheers,
Martin
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 11152

PostPosted: Fri Feb 20, 2015 9:06 am    Post subject:
Lua scripting
Reply with quote

 Hi Martin,


I can't spot what might be wrong from your a simple review of your code, I think in principle it's roughly what you should be doing.


The only thing I can think of that might produce the warnings you are reporting would be if the serializer plugins aren't being loaded by the OSG for some reason - perhaps the library paths aren't set up on your system correctly.


Try setting the env var OSG_NOTIFY_LEVEL to DEBUG and have a look at the output - this should contain the lists of plugins and paths that it's attempting to load.


If this doesn't work and I have some time I'll see if I can build your code. 



Robert.







On 19 February 2015 at 20:01, Martin Ferry < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi, Rober

I have not added any object to outputParameters, and after script run I have object in output parameters. So I understand it outputParameters.

But I still have problem with value object as Vec3f or similar.
here is C++ code .

int main( int argc, char **argv )
{

        osg::ArgumentParser arguments(&argc,argv);
        osgDB::getDataFilePathList().push_back( "D:/zbcmy/osg33/data/" );

        osg::ref_ptr<osg::Script> script = osgDB::readScriptFile("D:/zbcmy/vosg2/Debug/script/test.lua");
        if (script.valid())
        {
                osg::Parameters inputParameters;
                osg::Parameters outputParameters;
                inputParameters.push_back(new osg::MatrixTransform);
                inputParameters.push_back(new osg::Vec3fValueObject("Vec3f",osg::Vec3f()));
                osg::ref_ptr<osg::ScriptEngine> luaScriptEngine = osgDB::readFile<osg::ScriptEngine>("ScriptEngine.lua");

                if (luaScriptEngine->run(script.get(),"test", inputParameters, outputParameters))
                        OSG_NOTICE<<"Successfully ran script : return value = ";
                else
                        OSG_NOTICE<<"script run failed"<<std::endl;
        }
        _getch();
}

and lua code

function test(a,k)
        a.Name = "Pito";
        a.Matrix = new ("osg::Matrixf()");
        e = new ("osg::Vec3f");
        --a.Matrix.Trans()
        return a;
end

output
Lua error : attempt to call a table value
Function doesn't accept osg value object ? Something is wrong but I don't know what is wrong.

When I comment out line with Vec3fValueObject I get output

ClassInterface::createObject(osg::Matrixf()), No object wrapper avaiable.
Failed to create object osg::Matrixf()
LuaScriptEngine::setPropertyFromStack(0060FF80, Matrix) property of type = MATRIX not implemented
ClassInterface::createObject(osg::Vec3f), No object wrapper avaiable.
Failed to create object osg::Vec3f

I think that creation Matrix or Vec3f in Lua code is wrong , but I don't know how to do this.

Many thanks that you spent your expensive time with my not clever posts but I don't see any way for learn lua scripting in OSG.

Cheers,
Martin

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=62743#62743





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
rarum
Newbie


Joined: 22 Sep 2012
Posts: 10

PostPosted: Sun Feb 22, 2015 8:45 pm    Post subject:
Reply with quote

Hi, Robert

After long debuging I found issue in your code. In file LuaScriptEngine.cpp, method void LuaScriptEngine::pushValue(const osg::Vec2f& value) const on line 3777 is double call lua_newtable(_lua); so if I remove one call , all works OK without error in lua. Can you look at it, and to similar overloaded call of method pushValue ? I think that call lua_newtable is redudant , and breaks ValueObject as input parameter to Lua table error.

Second question. Can you write me a simple example how to create new Matrixf Object in Lua script ?
I am using
mobject = new ("osg::MatrixdValueObject");
matrix = mo.Value

but I think it' so ugly .

And how to call methods of Matrix object, for example method makeRotate ?
Please write simple example in which is called update method which rotate matrix of MatrixTransform object. It is possible ?

I have problem with methods of value objects . Lua still say that called method is nill. Please write simple lua example with using these value object (Matrixd, Vec3, transforming Matrix ....)


Many thanks!

Cheers,
Martin
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 11152

PostPosted: Mon Feb 23, 2015 10:51 am    Post subject:
Lua scripting
Reply with quote

Hi Martin,


For basic types like Vec3, BoundingBox you can simply create a table with the named elements and the LuaScriptEngine will adapt these to the appropriate osg::Vec3 i.e.


For a vec3 :


  {x=10,y=2,z=20}


For a colour vec4:


  {r=0.4,g=0.2,b=0.1,a=1.0}


For a bounding box:

{xMin=0, yMin=0, zMin=0, xMax=10, yMax=10, zMax=10}


For a Matrix it'll match against 16 numbers in a table. Which I can't remember off the top of my head the most convenient form for.


Robert.












On 22 February 2015 at 20:45, Martin Ferry < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi, Robert

After long debuging I found issue in your code. In file LuaScriptEngine.cpp, method void LuaScriptEngine::pushValue(const osg::Vec2f& value) const on line 3777 is double call lua_newtable(_lua); so if I remove one call , all works OK without error in lua. Can you look at it, and to similar overloaded call of method pushValue ? I think that call lua_newtable is redudant , and breaks ValueObject as input parameter to Lua  table error.

Second question. Can you write me a simple example how to create new Matrixf Object in Lua script ?
I am using
mobject = new ("osg::MatrixdValueObject");
matrix = mo.Value

but I think it' so ugly .

And how to call methods of Matrix object, for example method  makeRotate ?
Please write simple example in which is called update method which rotate matrix of MatrixTransform object. It is possible ?

I have problem with methods of value objects . Lua still say that called method is nill. Please write simple lua example with using these value object (Matrixd, Vec3, transforming Matrix  ....)


Many thanks!

Cheers,
Martin

------------------
Read this topic online here:
http://forum.openscenegraph.org/viewtopic.php?p=62758#62758





_______________________________________________
osg-users mailing list
(
Only registered users can see emails on this board!
Get registred or enter the forums!
)
http://lists.openscenegraph.org/listinfo.cgi/osg-users-openscenegraph.org




------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
rarum
Newbie


Joined: 22 Sep 2012
Posts: 10

PostPosted: Tue Feb 24, 2015 8:21 am    Post subject:
Reply with quote

Hi, Robert

I found another issue in code of LuaScriptEngine.
Method LuaScriptEngine::pushValue(const osg::Matrixd& value) const has switched lua_pushnumber with lua_pushinteger. So if I sent matrix as input to method in lua script by input parameters, luascriptengine cast every value to int. Than I have in script numbers only 1 or 0. It's bad.

I try your example of creating value objects. It works when I return objects like Vec3f to C++ code. In output parameter is mentioned vector.
But how to do operation with objects as Vec3f, Matrixd in lua csript ?
Could you please write a small lua example how to create and add together 2 vectors?
Or example with using simple matrix transformation (e.g osg::Matrix::rotate(osg::Vec3f) ) in Lua script?

When I try this lua still claims , that called method (rotate) is nill.

Thank you!

Cheers,
Martin
Back to top
View user's profile Send private message
robertosfield
OSG Project Lead


Joined: 18 Mar 2009
Posts: 11152

PostPosted: Tue Feb 24, 2015 9:56 am    Post subject:
Lua scripting
Reply with quote

Hi Matrin,

On 24 February 2015 at 08:22, Martin Ferry < (
Only registered users can see emails on this board!
Get registred or enter the forums!
)> wrote:
Quote:
Hi, Robert

I found another issue in code of LuaScriptEngine.
Method LuaScriptEngine::pushValue(const osg::Matrixd& value) const has switched lua_pushnumber with lua_pushinteger. So if I sent matrix as input to method in lua script by input parameters, luascriptengine cast every value to int. Than I have in script numbers only 1 or 0. It's bad.


Well spotted.  Looks like I got the index and value assignment the wrong way around.  I have fixed this and check it into svn/trunk.  The changes I made are:

@@ -3869,7 +3869,7 @@
     {
         for(unsigned int c=0; c<4; ++c)
         {
-            lua_pushnumber(_lua, r*4+c); lua_pushinteger(_lua, value(r,c)); lua_settable(_lua, -3);
+            lua_pushinteger(_lua, r*4+c); lua_pushnumber(_lua, value(r,c)); lua_settable(_lua, -3);
         }
     }
 }



 
Quote:
I try your example of creating value objects. It works when I return objects like Vec3f to C++ code. In output parameter is mentioned vector.
But how to do operation with objects as Vec3f, Matrixd in lua csript ?


The maths methods in Vec* and Matrix* don't have serializes for them so they are accessible to the Lua script.  These classes aren't subclassed from osg::Object so there isn't a any serialization mechanism that is built for them so the Lua script integration has to provide special handling of them.  To implement these methods we'd need to implement each of these methods in the LuaScriptEngine.cpp.


In the project that help fund the development of lua plugin there was no need for low level maths functions to be exposed so I haven't spent time implementing stuff that isn't required.



Given going to/from C++ to lua isn't entirely free it may well be most efficient to simply implement maths methods in Lua itself and use these as a library.




Quote:
Could you please write a small lua example how to create and add together 2 vectors?

Quote:
Or example with using simple matrix transformation (e.g osg::Matrix::rotate(osg::Vec3f) ) in Lua script?



No. I have other work to get on with.  While I can sympthize the desire have everything work first time and everything documented in way that helps you write your program I'm not on your payroll and I have plenty of other important project to get on with.

Quote:

When I try this lua still claims , that called method (rotate) is nill.


Lua will report nil for methods that don't exist.


Robert.



 

------------------
Post generated by Mail2Forum
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    OpenSceneGraph Forum Forum Index -> Plugins [osgPlugins] All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum

Similar Topics
Topic Author Forum Replies Posted
No new posts OpenSceneGraph scripting interface SMesserschmidt General 2 Tue Sep 29, 2015 12:21 pm View latest post
No new posts TexGen Python Scripting Samnite General 1 Tue Feb 22, 2011 7:06 pm View latest post


Board Security Anti Bot Question MOD - phpBB MOD against Spam Bots
Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP