void WriteBoneInfo(LWItemID objectid) 
{
  LWItemID childID,parentID,parentparentID;
  double dvPos[3],dvRot[3];
  double dvParentPos[3] = {0,0,0};
  double dvParentRot[3] = {0,0,0};
  float  fvPos[3],fvRot[3];
  Matrix12 bi_mRot;
  const char *strName, *strParentName;
  float fLength = 0.5;


  childID = _iti->firstChild(objectid);
  if(_iti->type(objectid) == LWI_OBJECT)
  {
    
    _pmesh = _obi->meshInfo(objectid, 0);
    if((_pmesh == NULL) && (childID == LWITEM_NULL))
    {
      return;
    }


    _iti->param(objectid,LWIP_POSITION,0,dvPos);
    _iti->param(objectid,LWIP_ROTATION,0,dvRot);

    strName = _iti->name(objectid);
    fprintf(_f, "  NAME \"%s\";\n", strName);
    if (_ctNumBones > 0) {

      parentID = _iti->parent(objectid);
  
      _pmesh = _obi->meshInfo(parentID, 0);
      if (_pmesh != NULL) {
        parentparentID = _iti->parent(parentID);

        _pmesh = _obi->meshInfo(parentparentID, 0);
        if (_pmesh == NULL) {
          _iti->param(parentparentID,LWIP_POSITION,0,dvParentPos);
          //_iti->param(parentparentID,LWIP_ROTATION,0,dvParentRot);
          
        } else {
          _iti->param(parentID,LWIP_POSITION,0,dvParentPos);
          //_iti->param(parentID,LWIP_ROTATION,0,dvParentRot);
        }
      } else {
        parentparentID = _iti->parent(parentID);

        _pmesh = _obi->meshInfo(parentparentID, 0);
        if (_pmesh == NULL) {
          _iti->param(parentID,LWIP_POSITION,0,dvParentPos);
         // _iti->param(parentID,LWIP_ROTATION,0,dvParentRot);
        
        }
      }


      strParentName =  _iti->name(parentID);      
    } else {
      strParentName = strdup("");
    }

      
    fprintf(_f, "  PARENT \"%s\";\n", strParentName);
    fprintf(_f, "  LENGTH %g;\n", fLength);
    fprintf(_f, "  {\n");

    for (int i=0;i<3;i++) {
      fvPos[i] = (float) (dvPos[i] - dvParentPos[i]);
      fvRot[i] = 0;//(float) (dvRot[i] - dvParentRot[i]);
    }
    fvPos[2] = -fvPos[2];

    MakeRotationAndPosMatrix(bi_mRot,fvPos,fvRot);
    PrintMatrix(_f,bi_mRot,4);
    fprintf(_f,"\n  }\n");

    _ctNumBones++;

     while (childID != LWITEM_NULL) {
       WriteBoneInfo(childID);
       childID = _iti->nextChild(objectid,childID);
     }

  }

};
int ExportBones() 
{
  LWItemID idMasterObjID;
  char strMessage[256];

  if(!_evaluate)
  {
    // lightwave error
    _msg->error("Lightwave process error !\nClose plugins window and try again.\n", NULL);
    return AFUNC_BADAPP;
  }

  // !!!! make it work with a selected object, not the first one in scene
  ReloadGlobalObjects();

  bool bExportOnlySelected = false;
  int ctSkeletonBones=0;

  // find selected object - to be replaced with the master bone
  int ctSelected = 0;
  int ctMeshes=0;
  _objid = _iti->first(LWI_OBJECT,0);
  idMasterObjID = LWITEM_NULL;
  while(_objid != LWITEM_NULL)
  {
    if(_iti->type(_objid) == LWI_OBJECT)
    {
        if(_ifi->itemFlags(_objid) & LWITEMF_SELECTED)
        {
          ctSelected++;
          idMasterObjID = _objid;
        }
      ctMeshes++;
    }
    _objid = _iti->next(_objid);
  }

  if (idMasterObjID == LWITEM_NULL) 
  {
    // lightwave error
    _msg->error("ERROR: Object for top level bone not selected.\n", NULL);
    return AFUNC_BADAPP;
  }

  if (ctSelected > 1) 
  {
    // lightwave error
    _msg->error("ERROR: More than one object selected.\n", NULL);
    return AFUNC_BADAPP;
  }
    
  _ctNumBones = 0;
  AddBoneToCount(idMasterObjID);

  sprintf(strMessage,"Number of bones: %d",_ctNumBones);


  // get scene name
  _strFileName = strdup(_sci->filename);

  // open the file to print into
  char fnmOut[256];
  strcpy(fnmOut, _strFileName);
  char *pchDot = strrchr(fnmOut, '.');
  if (pchDot!=NULL) {
    strcpy(pchDot, ".as");
  }

  _msg->info(strMessage,fnmOut);

  if ((_f = fopen(fnmOut,"w")) == NULL) {
    _msg->error("ERROR: File open.\n", NULL);
    return AFUNC_BADAPP;
  }

  fprintf(_f, "SE_SKELETON %s;\n\n",SE_ANIM_VER);
  fprintf(_f, "BONES %d\n{\n",_ctNumBones);

  _ctNumBones = 0;
  WriteBoneInfo(idMasterObjID);

  fprintf(_f, "}\n\nSE_SKELETON_END;\n");
  fclose(_f);


  return AFUNC_OK;
};
Beispiel #3
0
void WriteFile (void)
{
	FILE		*modelouthandle;
	int			total = 0;
	int			i;

	pStart = kalloc( 1, FILEBUFFER );

	StripExtension (outname);

	for (i = 1; i < numseqgroups; i++)
	{
		// write the non-default sequence group data to separate files
		char groupname[128], localname[128];

		sprintf( groupname, "%s%02d.mdl", outname, i );

		printf ("writing %s:\n", groupname);
		modelouthandle = SafeOpenWrite (groupname);

		pseqhdr = (studioseqhdr_t *)pStart;
		pseqhdr->id = IDSTUDIOSEQHEADER;
		pseqhdr->version = STUDIO_VERSION;

		pData = pStart + sizeof( studioseqhdr_t ); 

		pData = WriteAnimations( pData, pStart, i );

		ExtractFileBase( groupname, localname );
		sprintf( sequencegroup[i].name, "models\\%s.mdl", localname );
		strcpy( pseqhdr->name, sequencegroup[i].name );
		pseqhdr->length = pData - pStart;

		printf("total     %6d\n", pseqhdr->length );

		SafeWrite( modelouthandle, pStart, pseqhdr->length );

		fclose (modelouthandle);
		memset( pStart, 0, pseqhdr->length );
	}

	if (split_textures)
	{
		// write textures out to a separate file
		char texname[128];

		sprintf( texname, "%sT.mdl", outname );

		printf ("writing %s:\n", texname);
		modelouthandle = SafeOpenWrite (texname);

		phdr = (studiohdr_t *)pStart;
		phdr->id = IDSTUDIOHEADER;
		phdr->version = STUDIO_VERSION;

		pData = (byte *)phdr + sizeof( studiohdr_t );

		WriteTextures( );

		phdr->length = pData - pStart;
		printf("textures  %6d bytes\n", phdr->length );

		SafeWrite( modelouthandle, pStart, phdr->length );

		fclose (modelouthandle);
		memset( pStart, 0, phdr->length );
		pData = pStart;
	}

//
// write the model output file
//
	strcat (outname, ".mdl");
	
	printf ("---------------------\n");
	printf ("writing %s:\n", outname);
	modelouthandle = SafeOpenWrite (outname);

	phdr = (studiohdr_t *)pStart;

	phdr->id = IDSTUDIOHEADER;
	phdr->version = STUDIO_VERSION;
	strcpy( phdr->name, outname );
	VectorCopy( eyeposition, phdr->eyeposition );
	VectorCopy( bbox[0], phdr->min ); 
	VectorCopy( bbox[1], phdr->max ); 
	VectorCopy( cbox[0], phdr->bbmin ); 
	VectorCopy( cbox[1], phdr->bbmax ); 

	phdr->flags = gflags;

	pData = (byte *)phdr + sizeof( studiohdr_t );

	WriteBoneInfo( );
	printf("bones     %6d bytes (%d)\n", pData - pStart - total, numbones );
	total = pData - pStart;

	pData = WriteAnimations( pData, pStart, 0 );

	WriteSequenceInfo( );
	printf("sequences %6d bytes (%d frames) [%d:%02d]\n", pData - pStart - total, totalframes, (int)totalseconds / 60, (int)totalseconds % 60 );
	total  = pData - pStart;

	WriteModel( );
	printf("models    %6d bytes\n", pData - pStart - total );
	total  = pData - pStart;

	if (!split_textures)
	{
		WriteTextures( );
		printf("textures  %6d bytes\n", pData - pStart - total );
	}

	phdr->length = pData - pStart;

	printf("total     %6d\n", phdr->length );

	SafeWrite( modelouthandle, pStart, phdr->length );

	fclose (modelouthandle);
}