Using maxscript structs in sdk c++ code

Using maxscript structs in sdk c++ code

Anonymous
Not applicable
1,372 Views
6 Replies
Message 1 of 7

Using maxscript structs in sdk c++ code

Anonymous
Not applicable
Does anybody have any examples of working with Maxscript structs in an SDK c++ exposed function?

I need to be able to take a struct as a parameter to my c++ function, look at the parameter names (I'd rather not have to pre-assume they'll be defined names), process the data, and return a different struct with the results.

this now seems to be working, but I'd still welcome feedback in case I'm doing anything in a silly way

The key is pre-defining the parameter names using the def_name macros, rather than using strings





def_visible_primitive(SetStruct, "SetStruct");

// Initialise the parameter names we're using
#include <maxscrpt\lclinsfn.h>
def_name(param1)
def_name(param2)

// This is called from the Gup's Start() function
void InitParams()
{
// This creates the actual key vars
#include <maxscrpt\lclimpfn.h>
def_name(param1)
def_name(param2)
}

Value* TestStruct_cf(Value** arg_list, int count)
{
check_arg_count(TestStruct, 1, count);

Value* val = arg_list;
if( is_struct(val) )
{
Struct* myStruct = (Struct*)val;

// Assumes the passed in struct contains a key called "param1"
std::string strBlah = val->_get_property( n_param1 )->to_string();
}
else
{
throw UserThrownError("Parameter is not a maxscript struct!", TRUE);
}
return &true_value;
}


This is called from maxscript like so:
struct wibble ( param1, param2 )
doodads = wibble param1:"hello" param2:42

TestStruct doodads
0 Likes
1,373 Views
6 Replies
Replies (6)
Message 2 of 7

Anonymous
Not applicable
This also seems to work now, using the declared parameters mentioned above

Value* ReturnStruct_cf(Value** arg_list, int count)
{
check_arg_count(TestStruct, 0, count);

const int nNumParams = 2;

Value** myParams = new Value*;

myParams = n_param1;
myParams = n_param2;

HashTable* myValues = new HashTable();
myValues->put_new( myParams, new Integer(0) );
myValues->put_new( myParams, new Integer(1) );

StructDef* myDef = new StructDef( new String("myStruct"), nNumParams, myParams, myValues );

Struct* myStruct = new Struct( myDef, 2 );
myStruct->_set_property( myParams, new Integer(32) );
myStruct->_set_property( myParams, new String("hello") );

return myStruct;
}


This seems to return a struct ok, but Maxscript seems to get confused about the parameters

a = ReturnStruct()
(myStruct param1:32 param2:"hello")

a.param1
32
"a.param2"
hello
0 Likes
Message 3 of 7

Anonymous
Not applicable
Is it possible to do the other way : define a struct in c++/SDK and expose it to MAXScript, and then use it in MAXScript ?

I did find a def_struct_primitive macro but no real sample of how to use it
0 Likes
Message 4 of 7

Anonymous
Not applicable
Is it possible to do the other way : define a struct in c++/SDK and expose it to MAXScript, and then use it in MAXScript ?


That's exactly what I'm doing in the ReturnStruct snippet above.

(Note I wrote that 3 years ago, and now work for a different company, so can't remember exactly how it works!)
0 Likes
Message 5 of 7

spacefrog_
Advisor
Advisor
Is it possible to do the other way : define a struct in c++/SDK and expose it to MAXScript, and then use it in MAXScript ?

I did find a def_struct_primitive macro but no real sample of how to use it


just stumbled over this post...
I know it's quite old, but i thought i give an example of how to use the def_struct_primitive macro.
Since i'm currently learning this stuff by myself the hard way i know how valuable such sparse knowledge about the Max SDK is...

To show the usage of the above macro, here's an example:

def_struct_primitive( sillyFunction, SillyStruct, "someCall");


This exposes a struct called "SillyStruct" to maxscript, which offers one member function called "someCall". Internally "someCall" is connected to your C++ defined function sillyFunction().
The signature of sillyfunction looks like:

Value* sillyFunction_cf( Value **args, int argCount )


Note that the macro adds the "_cf" part to the function name, as you might probably already know by now
So if you type the following in the listener ( with the plugin loaded of course 😞

sillystruct.somecall() 


sillyFunction gets called, and you can deal with the **args and argcount pointer as you like, can check the argument types via the other known macros ( check_arg_count() etc.. )

Josef Wienerroither
Software Developer & 3d Artist Hybrid
0 Likes
Message 6 of 7

spacefrog_
Advisor
Advisor
I'm not quite sure , but i think there is a potential cause of trouble in your second example, were you return "myStruct". You have to "save" your locally allocated struct from the maxscript garbage collector by "protecting" it using the "one_value_local" macro.

More details in the SDK docs here...
http://download.autodesk.com/global/docs/3dsmaxsdk2012/en_us/index.html?url=files/GUID-5230478B-A4AB...

Josef Wienerroither
Software Developer & 3d Artist Hybrid
0 Likes
Message 7 of 7

Anonymous
Not applicable
Does anyone know how to add a member variable to a struct, I cant seem to find anything in the docs to even hint at how it is done. Thanks.
0 Likes