Notifications
Clear all

[Closed] C++ – Returning Array of Nodes

 MZ1

I just want to write a function to get bonelist of skin modifier. But I’m not sure how I can return array of nodes.

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

def_visible_primitive(Skin_GetBoneList, "Skin_GetBoneList");
Value* Skin_GetBoneList_cf(Value **arg_list, int count)
{
	check_arg_count(Skin_GetBoneList, 1, count);
	BonesDefMod* bmod = (BonesDefMod*)(arg_list[0]);
	Array * boneList = new Array(bmod->BoneData.Count());
	for (size_t i = 0; i < bmod->BoneData.Count(); i++)
	{
		boneList->append((Value*)bmod->BoneData[i].Node);
	}
	one_typed_value_local(Array* result);
	vl.result = boneList;
	return_value(vl.result);
}
6 Replies
boneList->append(MAXNode::intern(bmod->BoneData[i].Node));

more correct:

	one_typed_value_local(Array* boneList);
	vl.boneList = new Array(bmod->BoneData.Count());
	for (size_t i = 0; i < bmod->BoneData.Count(); i++)
	{
		INode* bone = bmod->BoneData[i].Node;
		if (bone) vl.boneList->append(MAXNode::intern(bone));
	}
	return_value(vl.boneList);
 MZ1

Thank You! But now I got another error:

-- Known system exception
-- ########################################################################
-- Address: 0x36af1809; nCode: 0x00000000C0000005
-- Desc: EXCEPTION_ACCESS_VIOLATION The thread tried to read from or write to a virtual address for which it does not have the appropriate access.
--       Read of Address: 0x0000000000000001
-- ########################################################################

This is updated version:
#include “bonesdef.h”
#include <maxscript/maxscript.h>
#include <maxscript/maxwrapper/mxsobjects.h>
#include <maxscript/macros/define_instantiation_functions.h>

def_visible_primitive(Skin_GetBoneList, "Skin_GetBoneList");
Value* Skin_GetBoneList_cf(Value **arg_list, int count)
{
	check_arg_count(Skin_GetBoneList, 1, count);
	Modifier* modifier = arg_list[0]->to_modifier();
	if (!modifier) return &undefined;
	ISkin* skin = (ISkin*) modifier;
	if (!skin) return &undefined;
	BonesDefMod* bonesDef = (BonesDefMod*)skin;
	if (!bonesDef) return &undefined;
	int boneCount = bonesDef->BoneData.Count();
	one_typed_value_local(Array* boneList);
	vl.boneList = new Array(boneCount);
	for (size_t i = 0; i < boneCount; i++)
	{
		INode* bone = bonesDef->BoneData[i].Node;
		if (bone) vl.boneList->append(MAXNode::intern(bone));
	}
	return_value(vl.boneList);
}

this is wrong… you have to cast

You’ve got it arse over tit … you have a modifier that implements the ISkin interface use that to access the bones.

Modifier* mod = arg_list[0]->to_modifier();
    if(!mod) return &undefined;

    ISkin* skin = (ISkin*)mod->GetInterface(I_SKIN);
    if(!skin) return &undefined;

    for(int i = 0; i < skin->GetNumBones(); ++i)
    {
          INode* node = skin->GetBone(i);
    .....

this is how i do it…

 MZ1

Thank You! So the complete code would be:

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

def_visible_primitive(Skin_GetBoneList, "Skin_GetBoneList");
Value* Skin_GetBoneList_cf(Value **arg_list, int count)
{
	check_arg_count(Skin_GetBoneList, 1, count);
	Modifier* modifier = arg_list[0]->to_modifier();
	if (!modifier) return &undefined;
	ISkin* skin = (ISkin*)modifier->GetInterface(I_SKIN);
	if (!skin) return &undefined;
	int boneCount = skin->GetNumBones();
	one_typed_value_local(Array* boneList);
	vl.boneList = new Array(boneCount);
	for (int i = 0; i < skin->GetNumBones(); ++i)
	{
		INode* bone = skin->GetBone(i);
		if (bone) vl.boneList->append(MAXNode::intern(bone));
	}
	return_value(vl.boneList);
}