full lua API access!

Moho allows users to write new tools and plugins. Discuss scripting ideas and problems here.

Moderators: Víctor Paredes, Belgarath, slowtiger

Post Reply
janimatic
Posts: 136
Joined: Sat Mar 26, 2005 12:22 am
Contact:

full lua API access!

Post by janimatic »

i am impressed !

moho gives access to the WHOLE lua api!
i just tested it :
print( "PATH:", os.getenv ("PATH") )
it works!!

so the doc is here :

http://www.lua.org/manual/5.0/manual.html

Will add a script to run autotrace on bitmap files and import resulting eps.

thank you LM!
Toontoonz
Posts: 763
Joined: Fri Mar 25, 2005 9:28 pm

Post by Toontoonz »

I look forward to trying all the new scripts you create and post here at Moho.
You have lots of very interesting ideas on how one could use scripting in Moho.
janimatic
Posts: 136
Joined: Sat Mar 26, 2005 12:22 am
Contact:

Post by janimatic »

thank you tooonz,

that's very encouraging! :)
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Uh oh, here we go.

Originally, the access to the OS lib wasn't enabled in Moho, so I asked for it and then got caught in Work Hell for a long time and never had a chance to play with it. I've been meaning to. Know of any good (for boneheads) references for the Win API?
janimatic
Posts: 136
Joined: Sat Mar 26, 2005 12:22 am
Contact:

Post by janimatic »

Hey 7f!

well i am just using the usual manual and tutorial

http://lua-users.org/wiki/
http://lua-users.org/wiki/TutorialDirectory
http://www.lua.org/manual/5.0/manual.html

Are you looking for something specific?
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Sorry, I meant for the win32 (OS) API. I had made a tweak of the freehand script, one of the differences was that the tool would produce fewer points the faster a line was being drawn. The only only timer I could get at was the os.clock() one in lua, which seemed to be counting clock cycles for the current thread, which made it not too consistent. Theres a Lua binding for the w32 API, apparently, over ay LuaForge, but I coudn't find any info on what calls it supports. Or for that matter, what calls are available (I know it's huge). Which Lib is in which DLL, stuff like that. Thought you might have an idea, saw some of those bit's of c++ code in comments in one of your scripts.

The other problem I'm having a problem with is how to set up variable to communicate back and forth with Lua. As as example, I found a couple of pretty nifty color picker DLL's that I thought would be handy. The both wanted to return single bytes for the RGB data (and A in one case, that one returned as type boolean - don't really know c++ so I don't know what to make of that). The first, and simpler one I liked for this purpose worked fine until it wanted to return. Wanted to send back the data by reference, which seemed to make Moho very, very unhappy. The second was a nonstarter as I guess I just wasn't passing it anything it liked. I'm not sure how to make Lua's "whatever" data typing work outside of Lua.

Hell, there's plenty of stuff I'd like to do, but I'm just getting comfortable with Lua. Ah well, I'll figure it out eventually.
janimatic
Posts: 136
Joined: Sat Mar 26, 2005 12:22 am
Contact:

Post by janimatic »

hey 7feet!

i wasn't even aware that we could use our own dll with moho embeded lua interpreter! Can you tell me more about this?
I am new to lua though i know some c++/interpreted language bindings (such as tcl). Actually I do all i need to do with the lua api as it is though..
I agree there is not much lua documentation online, compared to tcl and others...
PS :
did you look in lua source code for your requests? that's the best place to know about lua!
janimatic
Posts: 136
Joined: Sat Mar 26, 2005 12:22 am
Contact:

Post by janimatic »

LuaThread may be interesting for you
http://www.cs.princeton.edu/~diego/prof ... luathread/
but i am afraid it would implie recompiling with moho source...
(i guess the whole programe would have to be multithreaded then)
I found it on this usefull page :
his page http://lua-users.org/wiki/LibrariesAndBindings
janimatic
Posts: 136
Joined: Sat Mar 26, 2005 12:22 am
Contact:

Post by janimatic »

apparently and according to
http://lua-users.org/wiki/CppLuaArgumentPassingTutorial
you should use C functions (lua_pushnumber(), etc) to convert your arguments to lua types..

About color though they are often stored in 4 byte and it can be tricky to convert them to 0-255 int as you need to use shift operators << >>, something like :

int RGBAval = ((r << 24) | (g << 16) | (b << 8) | a)

int r = (RGBAval >>24)
int g = ((RGBAval >>16) & 0xF)
int b = ((RGBAval >>8) & 0xF)
int a = (RGBAval & 0xF))

(hum not sure...)

Can you post some example?
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

What I had used was

Code: Select all

function SF_ColorPick:Run()
	dllPath = os.getenv ("WINDIR").."\\system32\\colorpicker.dll"
	local ColorPicker = assert(loadlib(dllPath),"pickcolor")
	self.red,self.green,self.blue = colorPicker()
end
Loadlib to pick the library, and the name of the function you want to call (in quotes). I can't recall where I got the DLL, my computer had a meltdown last night (while trying to do some data recovery for someone else - Ack!) so I can't check the history. Anyway, simple DLL with one function in it. Called correctly, looked fine, but crashed Moho with a "cannot read memory" error when I tried to send the colors back. That was the one that wanted the RGB vals by reference. On that part I'm clueless.

I was wondering if defining the variables as 1 character strings would work, or maybe a 3 of 4 character string and then just pull out the individual bytecodes. That tutorial is meant for a C++ program with Lua embedded (like Moho). The problem here is going right from Lua to external code. Since every number inside Lua is a dword float, I figure that something that's expecting a strictly typed variabe won't work well. Thats why I thought the string bit might be a useable workaround. Dunno.

Also, if you haven't checked it out yet, the online copy of the Programming in Lua that they have over at Lua.org has been very helpful for me.
User avatar
Fazek
Posts: 246
Joined: Thu Apr 13, 2006 1:37 pm
Location: Hungary
Contact:

Post by Fazek »

Sorry to answer this old topic, but now I am playing with Lua to extend it with some C functions in a library, so I know what is the problem. Lua can receive return values from a C function only if it puts the values to the special stack of Lua. So it won't work with normal functions of normal dlls, only with those designed specially for Lua. So you have to write a dll function in C if you want to call the ColorPicker(), make something like this:

Code: Select all

// From http://www.linxexplorer.com/colorpickerSDK.html
////////////////////////////////////////////////////////////////////////////////////////////
// C++ sample on how to open Visual Color Picker dialog
// 

class CColorRGBA
{
public:
	CColorRGBA()
	{
		ASSERT(sizeof(CColorRGBA) == 4);
		b = g = r = a = 0;
	}
	BYTE b, g, r, a;
};

typedef UINT (*PickColor)(CWnd *pParent, CColorRGBA & colText, CColorRGBA & colBk);

static int PickRGBColor(lua_State *L)
{
        CColorRGBA colText;
        CColorRGBA colBk;

	// Load Visual Color Picker DLL
	HINSTANCE hModule = ::LoadLibrary(_T("ColorPicker.dll"));
	if (!hModule)
		return (UINT)-1;
	// Get Visual Color Picker function
	PickColor func = (PickColor)::GetProcAddress(hModule, "PickColor");
	if (!func)
	{
		::FreeLibrary(hModule);
		return 0;
	}
	// call Visual Color Picker dialog function
	UINT ret = func(NULL, &colText, &colBk); // returns IDCANCEL, IDOK
	::FreeLibrary(hModule);
        lua_pushnumber(L, ret >> 16);
        lua_pushnumber(L, (ret >> 8) & 0xFF);
        lua_pushnumber(L, ret & 0xFF);
	return 3;
}

//
// end of sample code.
////////////////////////////////////////////////////////////////////////////////////////////
By the way, it seems the loadlib() support is not working in Moho 5.4, at least under Linux...
- - - Fazek
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Well, that's mighty handy to know. As I can't program in C to save my life, but can perhaps puzzle things out if I have a good chunk in front of me, that's great.

I'm not sure if it was you, Fazek, but I had noticed someone mentioning being able to use some process that was specific to Lua 5.1. I don't know if that was correct and Moho 5.4 upgraded the Lua as well, but that could be the problem. As I (vaugely) understand it Lua 5.1 handles a number of things quite differently than 5.0, including library loading. There are supposed to be some flags you can set to ensure compatibility with 5.0 you can put in there when compiling 5.1, but in this is the case perhaps that didn't happen. That might explain a few strange things that have been mentioned.
User avatar
Fazek
Posts: 246
Joined: Thu Apr 13, 2006 1:37 pm
Location: Hungary
Contact:

Post by Fazek »

Hello,

Moho still using Lua 5.0.2, but I hope it will upgrade to 5.1 because it has better compatibility between the different operating systems. You can read the _VERSION global variable to ask the actual version of Lua.
- - - Fazek
User avatar
7feet
Posts: 840
Joined: Wed Aug 04, 2004 5:45 am
Location: L.I., New Yawk.
Contact:

Post by 7feet »

Ah, okay, I was wondering about that. Haven't really had a chance to check. That said, are you talking specifically about Linux, or is that something that Lua itself throws in there. The other day, I ran out the envirionment variables on my box that I could find what I had to play with, and didn't see that.

--Okay, just checked, and I get a nil for that from os.getenv in the standalone interpreter on my XP machine, haven't tried it in Moho, but I don't think it's set in windows itself by default. So I couldn't check it. Ah, well, it's always something.
User avatar
Fazek
Posts: 246
Joined: Thu Apr 13, 2006 1:37 pm
Location: Hungary
Contact:

Post by Fazek »

Maybe I was not clear enough.. _VERSION is not an environment variable, but a simple Lua global variable.
- - - Fazek
Post Reply