Login  Register

Scripting Brainstorming

Posted by xnakos on Oct 19, 2012; 1:40pm
URL: https://forum.librecad.org/Scripting-Brainstorming-tp5707168.html

I chose to start a new thread than to revive one from the dead. I find the scripting element very important and missing from LibreCAD.

First of all, I took a look at the current scripting code (whose state I am not sure of, I didn't really try to use it). It is using Boost Python. I was not familiar with Boost Python, though it seemed really good. I played around just to make the sample plugin interact with some Python code to see that it could be able to provide a solution. It needs time, though, to get around and a lot of stuff must be done manually, even to get the slightest thing working. Plus, it is just Python that works with that and could extend LibreCAD (although Python is the bomb, at least for me). I took some time to look into some more stuff and came across SWIG. SWIG provides wrappers with great ease to facilitate extending an application with scripting. Python is just one language that can be used as a scripting language. Lisp, Lua, even Java and shit can be used. SWIG seems very actively developed (GSOC had five slots for SWIG during last summer) plus the license is permissive (application and generated code). The important thing to do is to design a concrete (and complete) interface to be exported. This interface must be implemented in C++, just having in mind that it will be used externally (alternatively, the desired external usage drives the implementation). Then exporting a selected set of API functions is done automatically using SWIG, for the target scripting language.

I also looked into a scenario that SWIG was used in. The Ogre graphics engine had some first initial bindings using Boost Python. But after a couple of developers could not continue maintaining the code, the new developers had a terrible time supporting this feature as it was. They gave up and provided a new solution using SWIG. They were very pleased with the result. In the end, SWIG was dropped from the project, though, because performance-wise it was inferior compared to a well-tuned implementation using Boost Python (the new Boost implementation was developed along the lifetime of the SWIG implementation). But this is understandable for a graphics engine, where performance is a feature of great importance. SWIG provides an automated mapping, correct but not greatly efficient, compared to a manual mapping. For extending an application like LibreCAD, it seems amazingly suitable (and I really don't believe that a few milliseconds drops in performance are an issue, someone should remember that it was once used for a graphics engine, not that it was dropped in favour of something with better performance but that also took a shitload of more time to implement). Plus the possibility of being able to use multiple scripting languages (with most of the code base being the same) seems very attractive.

Any views on the above (since my hands-on experience was extremely limited)? Rallaz, if you can at some point, could you provide some info on the current plugin API? Could you all provide some scenarios that you would want a hypothetical script of yours to be able to perform?