Creating Libraries in Cheetah3D
I use libraries with my code. Lots of them. So one of my big questions was how to create one with Cheetah3D. This is my answer (this is from an older post).
Current Library usage is as follows:
1. Create a directory in ~/Library/Application Support/Cheetah3D/Scripts/ named Library. This is where you will keep all your libraries.
2. Put this in a file name MyTest.js in ~/Library/Application Support/Cheetah3D/Scripts/Tool
This should work with any user name.
The geval is a hack that makes the thing work. For some reason using eval doesn’t work with 7.0.18 (or other versions of Cheetah). I leave the “why” as an exercise for those who know Javascript better than I do.
3. Put the following in a file named testLibrary.js in the Library folder you created in step 1.
Code:
4. Restart Cheetah3D
5. Go to Tools/Script/Tools Script and select MyTest. You should get a window pop up.
This can be used to create multiple libraries in a fashion that is SIMILAR to Node (technically it isn't the same, but I'll let Javascript guru's explain that).
No, it is not beautiful and you can find a LOT of reasons why “eval” is considered "bad".
But, for me, libraries are a good thing.
I use libraries with my code. Lots of them. So one of my big questions was how to create one with Cheetah3D. This is my answer (this is from an older post).
Current Library usage is as follows:
1. Create a directory in ~/Library/Application Support/Cheetah3D/Scripts/ named Library. This is where you will keep all your libraries.
2. Put this in a file name MyTest.js in ~/Library/Application Support/Cheetah3D/Scripts/Tool
Code:
function buildUI(doc)
{
loadLibrary(doc, "testLibrary.js");
doc.addParameterSeparator("The title of our Tool");
doc.addParameterButton("Press the button!", "The Button", "buttonpressed");
}
function buttonpressed(doc)
{
testLibrary.doit(doc, "This is just a test");
}
function loadLibrary(doc, libraryName)
{
var geval = eval;
var prepath = doc.scriptPath().match(/(\/Users\/[-\w^&'@{}[\],$=!#().%+~ ]*\/)/g);
var file = new File(prepath + "Library/Application Support/Cheetah3D/Scripts/Library/" + libraryName);
file.open(READ_MODE);
var funcstring = file.read(file.size());
file.close();
geval(funcstring);
}
This should work with any user name.
The geval is a hack that makes the thing work. For some reason using eval doesn’t work with 7.0.18 (or other versions of Cheetah). I leave the “why” as an exercise for those who know Javascript better than I do.
3. Put the following in a file named testLibrary.js in the Library folder you created in step 1.
Code:
Code:
var testLibrary = new function()
{
this.doit = function (tool, name)
{
print("it worked");
OS.messageBox("And the answer is:", name);
return 0;
}
this.anotherfunction = function (tool, name)
{
var object = 0;
return 0;
}
}
5. Go to Tools/Script/Tools Script and select MyTest. You should get a window pop up.
This can be used to create multiple libraries in a fashion that is SIMILAR to Node (technically it isn't the same, but I'll let Javascript guru's explain that).
No, it is not beautiful and you can find a LOT of reasons why “eval” is considered "bad".
But, for me, libraries are a good thing.