Announcements

Between mid-October and November, the content on AREA will be relocated to the Autodesk Community M&E Hub and the Autodesk Community Gallery. Learn more HERE.

c++ pass max objects as argument

c++ pass max objects as argument

MehdiZangenehBar
Advocate Advocate
982 Views
3 Replies
Message 1 of 4

c++ pass max objects as argument

MehdiZangenehBar
Advocate
Advocate

I want to pass array of objects as argument and work on it's items, but I got some exception and weird errors. This is my code:

#include <maxscript/maxscript.h>
#include <maxscript/macros/define_instantiation_functions.h>

def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
   Array* nodesArray = (Array*)arg_list[0];
   INodeTab* nodes = new INodeTab();
   for (int i = 0; i < nodesArray->size; i++)
   {
      INode* node = (INode*)nodesArray->data[i];
      nodes->AppendNode(node);
   }
   mprintf((*nodes)[0]->GetName());
   return &ok;
}

Where is my mistake?

0 Likes
Accepted solutions (1)
983 Views
3 Replies
Replies (3)
Message 2 of 4

istan
Advisor
Advisor

why you cast this?

Array* nodesArray = (Array*)arg_list[0];

did you check e.g.:

  type_check( arg_list[0], MAXNode, "not a node" );
  MAXNode * Mnode = (MAXNode*) arg_list[0];
0 Likes
Message 3 of 4

MehdiZangenehBar
Advocate
Advocate

Because I want to pass array of nodes, not a single node. 

0 Likes
Message 4 of 4

MehdiZangenehBar
Advocate
Advocate
Accepted solution

And this the edited and finalized version:

#include <maxscript/maxscript.h>
#include <maxscript/maxwrapper/mxsobjects.h>
#include <maxscript/macros/define_instantiation_functions.h>


INode* MaxNodeToINode(Value* value)
{
	INode* node = NULL;
	if (is_node(value))
	{
		MAXNode* maxNode = (MAXNode*)value;
		if (!deletion_check_test(maxNode)) node = maxNode->to_node();
	}
	return node;
}


void MaxArrayToINodeTab(Value* value, INodeTab& nodes)
{
	if (value->is_kind_of(class_tag(Array)))
	{
		Array* maxArray = (Array*)value;
		for (int i = 0; i < maxArray->size; ++i)
		{
			INode* node = MaxNodeToINode(maxArray->data[i]);
			if (node)nodes.AppendNode(node);
		}
	}
}

def_visible_primitive(TestFunction, "TestFunction");
Value* TestFunction_cf(Value **arg_list, int count)
{
	// TestFunction <nodes>

	INodeTab nodeTab;

	MaxArrayToINodeTab(arg_list[0], nodeTab);
	if (nodeTab.Count() == 0)return &false_value;
	mprintf(L"%s\n", nodeTab[0]->GetName());
	return &true_value;
}
0 Likes