Notifications
Clear all

[Closed] MaxScript structs back and forth

Hi everybody,

I would like to create a struct in maxscript, send it to a SDK plugin, modify its values and send it back to maxscript. I have found several references but the code did not work. I am posting my tries in case you find some problems


Value* exposeStruct_cf(Value** arg_list, int count)
{
	CHECK_ARG_COUNT(exposeStruct, 1, count);
	four_typed_value_locals(String* p1, Integer* p2, Struct* s, Value* v);
	Value* val = arg_list[0];
	if (is_struct(val))
	{
		vl.s = (Struct*)val;
		try
		{
			vl.v = vl.s->_get_property(n_param1); 
		}
		catch (exception e)
		{
			UserThrownError(L"Excepcion capturada", TRUE);
		}

		//val->_get_property(n_param1)->to_string();
		//vl.p1 = new String(val->_get_property(n_param1)->to_string());
	}
	return vl.p1;
}

six_typed_value_locals(Name* struct_name, StructDef* sd, Struct* s, Value** myParams, HashTable* members, Integer* i);
	vl.members = new HashTable();
	int member_count = 2;
	vl.myParams = new Value*[member_count];
    vl.myParams[0] = n_param1;
    vl.myParams[1] = n_param2;
	vl.members->put_new(n_param1, new Integer(0));
	vl.members->put_new(n_param2, new Integer(1));
	vl.struct_name = (Name*)Name::intern(L"myStruct");
	vl.sd = new StructDef (vl.struct_name, member_count, vl.myParams, vl.members, NULL, NULL);
	vl.s = new Struct(vl.sd, 200);
	vl.s->_set_property( n_param1, new Integer(0) );
	vl.s->_set_property( n_param2, new Integer(1) );

Thank you in advance

2 Replies

getting the values is easy enough, setting is a completely different ball game though.

def_visible_primitive(getStruct, "getStruct");
    
    Value* getStruct_cf(Value **arg_list, int count)
    {
    	check_arg_count(getStruct, 1, count);
    	Value* val = arg_list[0];
    	Value* structfield = Name::intern("testfield");
    
    	if(is_struct(val))
    	{
    		Struct* mxs_struct = (Struct*)val;
    		Value* temp;
    		if((temp = mxs_struct->definition->get_member_value(structfield)) != NULL)
    		{
    			int index = temp->to_int();
    			int ival = mxs_struct->member_data[index]->to_int();
    			return Integer::intern(ival);
    		}
    	}
    	return &undefined;
    }

not as bad as I thought this will do it

def_visible_primitive(getStruct, "getStruct");
 
 Value* getStruct_cf(Value **arg_list, int count)
 {
 	check_arg_count(getStruct, 1, count);
 	Value* val = arg_list[0];
 	Value* structfield = Name::intern("anotherfield");
 
 	if(is_struct(val))
 	{
 		Struct* mxs_struct = (Struct*)val;
 
 		int ival = mxs_struct->_get_property(structfield)->to_int();
 		mxs_struct->_set_property(structfield, Integer::intern(ival + 1));
 	}
 	return &undefined;
 }

then from mxs it would be

struct test (testfield, anotherfield)
obj = (test 10 10)
getStruct obj
obj

Thank you very much Klunk. Really nice help!!