openscad icon indicating copy to clipboard operation
openscad copied to clipboard

New variable: $name = current file

Open kintel opened this issue 11 years ago • 5 comments

When developing libraries, it's common to include some test code in the global scope of the library. When doing this, opening the file as the main OpenSCAD file will execute the tests. As will include <mylibrary.scad>, while use <mylibrary.scad> will ignore the global instances of the file.

The include vs. use seems to be a bit confusing to some. Also, the Thingiverse Customizer application being pretty popular, forces people into instantiating modules in the global scope.

To remedy this, and to make it possible for libraries to co-exist with Customizer objects, I'm considering adding a new special variable, e.g. $name, which will contain the current library filename of the file being evaluated. If you're evaluating the main file, $name == "main", otherwise, $name == filename.scad (without path).

Doing this, it would be possible to create libraries in somewhat the same way as Python modules:

module mylibrary(param) { ... }

// Customizer parameters

// choose parameter
MyParam = 1; // [0:Shape1, 1:Shape2, 2:Shape3]
mylibrary(MyParam);


if ($name == "main") {
  <library test code here>
}

The whole customizer stuff really should be standardized better, but it somehow exists and is used by numerous people, so we can only hope to standardize it next time around.

Any thoughts?

kintel avatar Apr 27 '13 05:04 kintel

Discussion on the mailing list suggests that $name should evaluate to the current module name, not the filename. In case of code in the global scope, $name could either evaluate to an empty string or undefined. I guess both would evaluate to false though.

kintel avatar Apr 27 '13 16:04 kintel

On second thoughts, the whole $name thing won't really solve anything since Customizer would still execute the code in the global scope. A better way of having customizable libraries would be to have a separate scad file use'ing the library and instantiating an object accordingly. Alternatively, packaging the customizable object in a specially named module, which Customizer can then instantiate manually.

Needs more thinking.

kintel avatar Apr 27 '13 16:04 kintel

note #380

MichaelAtOz avatar Jun 30 '13 00:06 MichaelAtOz

Note #457

MichaelAtOz avatar Aug 15 '13 23:08 MichaelAtOz

Note #460 rather

steelman avatar Aug 18 '13 15:08 steelman