ianwaters
Hi all,

Quick question...

How do I embed an AETemplate (quotes and all) into the initializePlugin command of a C++ plug-in?

Or is that not the way to embed an AETemplate? Not sure how SOuP does it... All I know is that I don't won't separate AETemplate files :)

Any advice?

Thaaaanks!
Ian
Reply 0 0
ntashev
Usually I do a static procedure and use MGlobal::executeCommand() before registering the node, something like:


class Node : public MPxNode
{
// ...
public:
        static MString nodeName;
        static MString AETemplate(MString);
};

MString Node::nodeName("myNode");

MString Node::AETemplate(MString nodeName)
{
    MString AEStr = "";
    AEStr += "global proc AE"+nodeName+"Template(string $nodeName)\n";
    AEStr += "{\n";
    AEStr += "editorTemplate -beginScrollLayout;\n";
    AEStr += "    editorTemplate -beginLayout \"Attributes\" -collapse 0;\n";   
    AEStr += "        editorTemplate -addControl \"attribute\";\n";
    AEStr += "  editorTemplate -endLayout;\n";
    AEStr += "editorTemplate -addExtraControls;";
    AEStr += "editorTemplate -endScrollLayout;\n";
    AEStr += "}\n";
    return AEStr;
}

MStatus initializePlugin( MObject obj )
{
    MFnPlugin plugin( ..... );
    MGlobal::executeCommand( Node::AETemplate(Node::nodeName) );
    plugin.registerNode( nodeName, nodeId, Node::creator, Node::initialize, MPxNode::kDependNode);
    return MS::kSuccess;
}
<< aut inveniam viam aut faciam >>
Reply 0 0
pshipkov
While this is a valid way of doing it, it makes things very hard to manage. There are better ways of handling embedded resources, that don't hold you back on the management side of them.
MS Visual Studio has some nice built-in ways for that, you can look them up online, but i believe they are not platform independent.
*nux is my primary platform of choice, so that naturally decided how i do things.
Recently i do something like that:

All the scripts i use in SOuP are located in one directory (with couple of subdirs inside, etc).
As a pre-compilation process i convert everything to C char arrays and save them in few fat ascii files that i include in my build. Then in the ::initializePlugin method i cast these char arrays to MStrings and pass them to MGlobal::executeCommand() and ::executePythonCommand().
That's for the simple python and mel scripts.

For more complex scripts instead of directly calling them during plugin initialization i write their char arrays into python script files and then import them back in (using the regular python import mechanisms). This way i conform to the standard python workflow of importing modules, instantiating classes, etc.

I find this very handy because it simplifies user workflow and makes things less error prone in environments that are outside my reach (100% of whoever is using SOuP outside DD).
Basically - we just load the plug-in and the entire environment gets established "for free".
Reply 0 0
ntashev
Neat and way more elegant :)
Any insights on where to begin looking for those pre-compilation procedures. I'm primary compiling under linux, so a make file example or something would be of great help.
<< aut inveniam viam aut faciam >>
Reply 0 0
ianwaters
Ooo, neat tip. I had considered including the mel files (or a plist of mel files) as resources but I wasn't sure how platform independent the loading and reading of the files would be. 

I love the python tip, I hadn't thought of that, I'll give it a go this week and see how quickly my brain melts (Mel into Python into C++)!

Thanks for the replies!
Ian
Reply 0 0
pshipkov
Sorry for the late reply.

I like things simple and found the method below to fit my needs best.


here is a bash script that does the conversion:
#!/bin/bash

if [[ $# -ne 1 ]]; then
    echo "Usage: $0 FILENAME"
    exit 1
fi
file=$1

if [[ ! -f "$file" ]]; then
    echo "File not found: $file"
    exit 1
fi

cname=$file
cname=${cname//-/_}
cname=${cname//./_}

echo "static char $cname[] = {"
hexdump -v -e '" " 16/1 "  0x%02x, " "\n"' $file | sed -e '$s/0x  ,//g'
echo "};"


call it like that from shell:
bash convert.sh myscript.py > myscript.c (or .h, etc)

then include the produced file in your build scripts

inside ::initializePlugin() do this:
MGlobal::executePythonCommand(MString(myscript), false, false);
And that's it.

Here is how you write out data into file from within your C++ code, if you need to:
std::string sFile = "/temp/dir/to/myscript.py";
char *cFile = (char*)(sFile).c_str();
std::ofstream outFile(cFile);
outFile.write((char *)myscript, sizeof(myscript)); // myscript is the converted code i included in the build
outFile.close();





Forgot to mention in my previous posts that you can go through SWIG too. Search online - there are couple of examples how do do the bindings.
I found it too much for what i need.
Reply 0 0
pshipkov
Ian, can i dare hope you are redoing MASH in C++ ?
Reply 0 0
ianwaters
Peter, that post will save me at least a day of messing about, thank-you so much!

And yes :) I have so far converted 14 of the 17 MASH nodes to C++. Generally everything is about 5x faster, but I haven't done ANY optimisation yet; some nodes took less then an hour to convert, others took nearly a day because I'm learning C++ as I do it...

Anyway, it's all fun, and I have a big list of features to add, not least some kind of PolyFX so you can use MASH as a deformer chain :)

Thanks again!
Ian
Reply 0 0
SGIFreak
Ian, you are a king 
Reply 0 0
Reply