bool CSkeletonCandidate::AddNode(CBaseNode *pNode, int parentId)
{
	// check if the node is valid
	if(pNode == 0)
	{
		theExporter.SetLastError("Invalid handle!", __FILE__, __LINE__);
		return false;
	}

        // Don't add any nodes that have already been added.
        for (size_t i = 0; i < m_vectorBoneCandidate.size(); ++i)
        {
            if (*m_vectorBoneCandidate[i]->GetNode() == *pNode)
            {
                delete pNode;
                return true;
            }
        }

	// if the node does not get used, we have to delete it!
	bool bDeleteNode = true;

	// Check if the node is a candidate
	//We want to be able to export all type of nodes as bones...
	//if(theExporter.GetInterface()->IsBone(pNode) || theExporter.GetInterface()->IsDummy(pNode))
	{
		// allocate a new bone candidate
		CBoneCandidate *pBoneCandidate = new CBoneCandidate;
		if(pBoneCandidate == 0)
		{
			delete pNode;
			theExporter.SetLastError("Memory allocation failed!", __FILE__, __LINE__);
			return false;
		}

		// create the bone candidate
		if(!pBoneCandidate->Create(m_vectorBoneCandidate.size(), parentId, pNode))
		{
			delete pBoneCandidate;
			return false;
		}

		//when the node is a dummy, it's not selected, so select it anyway...
		pBoneCandidate->SetSelected(true);

		// insert node element into hierarchy
		m_vectorBoneCandidate.push_back(pBoneCandidate);

		// insert node element id into parent node element
		if(parentId == -1)
		{
			// no parent -> this is a root node
			m_listRootBoneCandidateId.push_back(pBoneCandidate->GetId());
		}
		else
		{
			// valid parent -> this is a child node
			m_vectorBoneCandidate[parentId]->AddChildId(pBoneCandidate->GetId());
		}

		// set parent id for the children
		parentId = pBoneCandidate->GetId();

		bDeleteNode = false;
	}

	// handle all children of the node
	for(int childId = 0; childId < pNode->GetChildCount(); childId++)
	{
		if(!AddNode(pNode->GetChild(childId), parentId))
		{
			// free the node of needed
			if(bDeleteNode)
			{
				delete pNode;
			}

			return false;
		}
	}

	// free the node of needed
	if(bDeleteNode)
	{
		delete pNode;
	}

	return true;
}
bool CSkeletonCandidate::AddNode(CalCoreSkeleton *pCoreSkeleton, CalCoreBone *pCoreBone, int parentId)
{
	// check if the core skeleton and core bone are valid
	if((pCoreSkeleton == 0) || (pCoreBone == 0))
	{
		theExporter.SetLastError("Invalid handle!", __FILE__, __LINE__);
		return false;
	}

	// find the node with the name of the core bone
	CBaseNode *pNode = theExporter.GetInterface()->GetNode(pCoreBone->getName());
	if(pNode == 0)
	{
		theExporter.SetLastError("Skeleton assignment failed!", __FILE__, __LINE__);
		return false;
	}
 
	// check if  the node is a candidate
	//we want to export all types of nodes as bones
	/*if(!theExporter.GetInterface()->IsBone(pNode) && !theExporter.GetInterface()->IsDummy(pNode))
	{
		delete pNode;
		theExporter.SetLastError("Invalid node types in skeleton!", __FILE__, __LINE__);
		return false;
	}
	*/

	// allocate a new bone candidate
	CBoneCandidate *pBoneCandidate = new CBoneCandidate;
	if(pBoneCandidate == 0)
	{
		delete pNode;
		theExporter.SetLastError("Memory allocation failed!", __FILE__, __LINE__);
		return false;
	}

	// create the bone candidate
	if(!pBoneCandidate->Create(m_vectorBoneCandidate.size(), parentId, pNode))
	{
		delete pBoneCandidate;
		return false;
	}

	// insert node element into hierarchy
	m_vectorBoneCandidate.push_back(pBoneCandidate);

	// insert node element id into parent node element
	if(parentId == -1)
	{
		// no parent -> this is a root node
		m_listRootBoneCandidateId.push_back(pBoneCandidate->GetId());
	}
	else
	{
		// valid parent -> this is a child node
		m_vectorBoneCandidate[parentId]->AddChildId(pBoneCandidate->GetId());
	}

	// get core bone vector
	std::vector<CalCoreBone *>& vectorCoreBone = pCoreSkeleton->getVectorCoreBone();

	// handle all children of the core bone
	std::list<int>::iterator iteratorChildId;
	for(iteratorChildId = pCoreBone->getListChildId().begin(); iteratorChildId != pCoreBone->getListChildId().end(); ++iteratorChildId)
	{
		if(!AddNode(pCoreSkeleton, vectorCoreBone[*iteratorChildId], pBoneCandidate->GetId())) return false;
	}

	return true;
}