Version 3.3.5 of G’MIC introduces an interesting change that concerns the scope of variables in commands.
Until now, In G’MIC, you had two different kinds of variables :
- Local variables, like
var=1976
, which were only accessible in the command they are defined in. - Global variables, like
_var=1976
, which were accessible everywhere.
G’MIC 3.3.5 will introduce the new concept of subcommands : A subcommand of a command foo
is a command whose name starts with _foo
(e.g. _foo_sub
), and which is called from command foo
. A subcommand has these interesting properties (which are not true in a non-subcommand of course):
- In a subcommand, you have access to local variables defined in the calling command. So,
foo :
var=1976
_foo_sub
_foo_sub :
echo "var = "$var
Calling foo
will print var = 1976
.
- In a subcommand, the variables you define can be accessed from the calling command too:
foo :
_foo_sub
echo "var = "$var
_foo_sub :
var=1976
So, here again, calling foo
will print var = 1976
.
I realized that this concept of subcommand is useful, while I was reimplementing the parsing of primitive materials to read .obj
files (3D meshes). My parser of the material file was actually passing materials properties to the input_obj
command by defining global variables for each primitive. Meaning, a lot of global variables were defined, when the 3D mesh has a large number of primitives (it often has more than 100k primitives…).
And having more than 100k global variables defined is not really cool Global variables cannot be “unset” (their values can be emptied, but not the variable name definition itself). And that was slowing down the access to other global variables in the rest of the G’MIC pipeline.
So now, I’m able to define my material parsing procedure as a subcommand of input_obj
, which defines only ‘local’ variables, and once the 3D mesh is read, those local variables are freed.
Subcommands are then useful when you have a lot of variables to pass from one command to another one (so much variables than passing them as command arguments would be painful).
That’s all the G’MIC progress news for the weekend Have a good Sunday!