Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > SDK Help
  New Posts New Posts
  FAQ FAQ  Forum Search   Register Register  Login Login

This piece of code crash. Can i know why?

 Post Reply Post Reply
Author
Message
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Topic: This piece of code crash. Can i know why?
    Posted: 2006 Jun 19 at 8:33am

User Information:

Cinema 4D Version:   9.1+ 
Platform:   Windows  ; Mac  ;  Mac OSX  ; 
Language(s):     C++  ;  

---------

Hi all,

i hope that someone can help me to fix this problem.

All work like a charm except when i render on viewport on a picture viewer.

BaseObject *Fast_Fur::GetVirtualObjects(PluginObject *op, HierarchyHelp *hh)
{
     BaseDocument *doc=op->GetDocument();
     BaseObject *ret = NULL;
     BaseContainer *data=op->GetDataInstance();
     BaseObject *geo=data->GetObjectLink(FAST_FUR_GEOMETRY,doc);
    if     (!geo) return NULL;

     BaseTime tempo          = doc->GetTime();
     Real fps               = doc->GetFps();
     Real frame               = tempo.Get()*fps;

     //PolygonObject* cloned = (PolygonObject*) (geo->GetClone(NULL,NULL));
     //PolygonObject* cloned = ToPoly(geo->GetClone(COPY_NO_HIERARCHY,NULL));

    PolygonObject* cloned = static_cast<PolygonObject*>( geo->GetClone(COPY_NO_HIERARCHY,NULL) );
     if (!cloned) return NULL;

     Vector* vettore=NULL;
     
     // Get undeformed object
     ModelingCommandData cd;
     cd.doc     = doc;
     cd.op     = cloned;
     if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, cd)) return NULL;
     //PolygonObject* geopoly = static_cast<PolygonObject*>(cd.result->GetIndex(0));
     AutoAlloc<PolygonObject> geopoly(static_cast<PolygonObject*>(cd.result->GetIndex(0)));
     if (!geopoly) return NULL;

     // Getting *Deformed* object at frame - 1
     doc->AnimateObject(geo,BaseTime( (frame-1)/fps ),NULL);
     BaseContainer bc;
     cd.doc = doc;
     cd.op = geo;
     cd.bc=&bc;
     bc.SetBool(MDATA_CURRENTSTATETOOBJECT_KEEPANIMATION,TRUE);
     if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, cd)) return NULL;

     AutoAlloc<PolygonObject> Deformed_Object(static_cast<PolygonObject*>(cd.result->GetIndex(0) ));
     if (!Deformed_Object) return FALSE;
     Matrix Mres=Deformed_Object->GetMg();
     Vector *deformed_padr=Deformed_Object->GetPoint();

     LONG countpoint=Deformed_Object->GetPointCount();
     vettore = (Vector*) GeAlloc ( sizeof(Vector) * countpoint );

     // store vertex coordinate
     for (LONG i=0; i<countpoint; i++ ) {
          vettore=deformed_padr*Mres;
     }

     // Get deformed vertex at current frame
     doc->AnimateObject(geo,BaseTime( frame/fps ),NULL);
     cd.doc = op->GetDocument();
     cd.op = geo;
     cd.bc=&bc;
     bc.SetBool(MDATA_CURRENTSTATETOOBJECT_KEEPANIMATION,TRUE);
     if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, cd)) return NULL;
     
     AutoAlloc<PolygonObject> Deformed_Object2(static_cast<PolygonObject*>(cd.result->GetIndex(0) ));
     if (!Deformed_Object2) return FALSE;
     Matrix Mres2=Deformed_Object2->GetMg();

     Vector *deformed2_padr=Deformed_Object2->GetPoint();

     for (LONG i=0; i<countpoint; i++ ) {
          vettore=vettore - deformed2_padr*Mres2 ;
     }
     
     Real     lod;
     Real     fur_display     = data->GetReal(FAST_FUR_DISPLAY);

     lod=doc->GetLOD() * fur_display;

     if(hh->GetVFlags()==20 ) lod=hh->GetLOD();

     ret = GenerateFast_Fur( op, hh->GetThread(), lod, geopoly, Deformed_Object2, vettore, Mres);
     if (!ret) goto Error;

     ret->SetName(op->GetName());

     PolygonObject::Free(cloned);
     if (vettore) GeFree(vettore);
     return ret;

Error:
     if (ret) blDelete(ret);
     if (vettore) GeFree(vettore);
     return NULL;
}
Back to Top
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 19 at 8:39am
I forgot to tell you that i tried to comment:

     ret = GenerateFast_Fur( op, hh->GetThread(), lod, geopoly, Deformed_Object2, vettore, Mres);
     if (!ret) goto Error;
     ret->SetName(op->GetName());

so c4d crash outside the plugin also without the GenerateFast_Fur call.

Cheers
Renato T.
Back to Top
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 19 at 6:38pm
Sometime when i render it crash here:



Thanks all :)
Renato T.
Back to Top
David O Reilly View Drop Down
Member
Member


Joined: 2002 Nov 07
Location: United Kingdom
Online Status: Offline
Posts: 244
Post Options Post Options   Quote David O Reilly Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 19 at 10:47pm
It is very illegal to call CURRENTSTATETOOBJECT within the cache build (GetVirtualObjects) this will give crashes, if you need to use this you must clone the object (and hierarchy) and put it into its own document, you can not call it in the same document because it affects the cache building. Try removing this first and see if it stops the crashing, this might be your problem and even if it is not it should be removed as it will give crashes.
Back to Top
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 20 at 12:02am
Argh!... so where is legal to use CurrentStateToObject?
This mean that Hair work in this way? I'm just curious :)

Anyway thanks David, i'll try to make what you suggest.

Cheers
Renato T.
Back to Top
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 20 at 11:20am
Hello Dear David,

can you help me to understand how clone an object in a new document?
I don't find any SDK example about it and some try that i made are not working.

Thanks in advance
Renato T.

Back to Top
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 20 at 11:29am
David,
if the 3 object are cloned.. i can call currentstatetoobject without probs and without insert in a new document?

Cheers
Renato T.
Back to Top
David O Reilly View Drop Down
Member
Member


Joined: 2002 Nov 07
Location: United Kingdom
Online Status: Offline
Posts: 244
Post Options Post Options   Quote David O Reilly Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 21 at 9:52am
CURRENTSTATETOOBJECT is fine when its not used within the cache building (i.e. where GetVirtualObjects is called), calling that function isn't allow there because it uses the caches itself, so can only be used before or afterwards. Hair doesn't work like this and the system I had to use to make it work (without using CURRENTSTATETOOBJECT, see below why) is rather involved.

As for using that function, it means you must clone your objects into another document and call it on the clones in the new document so that it isn't affecting the currently building cache. This does mean that its slow because not only are you cloning, you are then rebuilding any caches in a new document each time. Unfortunately there is no fast and easy way to handle any of this.

To clone into a new document just allocate a temporary document (i.e. just for the scope you need it) then clone your objects and insert them into the temp document, then you can safely uses this. You might be able to optimise a little by reusing these if the originals haven't changed, something else that also gets very involved (checking an object and hierarchy match exactly).
Back to Top
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 21 at 9:55am
Thanks david, i was testing this and seem to work with no memory leak and crash :)

Seem to be a bit slow.. but i'm glad for this :)

Thanks
Renato T.

BaseDocument *copiadoc=doc->Alloc();
     copiadoc->InsertObject(cloned,NULL,NULL,FALSE);

     BaseObject* copiato=copiadoc->GetFirstObject();
     //GePrint(copiato->GetName());

     Vector* vettore=NULL;
     
     // Get undeformed object
     ModelingCommandData cd,cd2,cd3;
     cd.doc     = copiadoc;
     cd.op     = cloned;
     if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, cd)) return NULL;
     //PolygonObject* geopoly = static_cast<PolygonObject*>(cd.result->GetIndex(0));
     AutoAlloc<PolygonObject> geopoly(static_cast<PolygonObject*>(cd.result->GetIndex(0)));
     if (!geopoly) return NULL;

     // Getting *Deformed* object at frame - 1
     doc->AnimateObject(geo,BaseTime( (frame-1)/fps ),NULL);
     PolygonObject* cloned2 = static_cast<PolygonObject*>( geo->GetClone(NULL,NULL) );
     BaseContainer bc;
     cd2.doc = copiadoc;
     cd2.op = cloned2;
     cd2.bc=&bc;
     bc.SetBool(MDATA_CURRENTSTATETOOBJECT_KEEPANIMATION,TRUE);
     if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, cd2)) return NULL;
     //PolygonObject* Deformed_Object = static_cast<PolygonObject*>(cd2.result->GetIndex(0));
     AutoAlloc<PolygonObject> Deformed_Object(static_cast<PolygonObject*>(cd2.result->GetIndex(0) ));
     //GePrint("atom array counter #2 :" + LongToString(cd2.result->GetCount()));


     if (!Deformed_Object) return FALSE;
     Matrix Mres=Deformed_Object->GetMg();
     Vector *deformed_padr=Deformed_Object->GetPoint();

     LONG countpoint=Deformed_Object->GetPointCount();
     vettore = (Vector*) GeAlloc ( sizeof(Vector) * countpoint );

     // store vertex coordinate
     for (LONG i=0; i<countpoint; i++ ) {
          vettore=deformed_padr*Mres;
     }

     // Get deformed vertex at current frame
     doc->AnimateObject(geo,BaseTime( frame/fps ),NULL);
     PolygonObject* cloned3 = static_cast<PolygonObject*>( geo->GetClone(NULL,NULL) );
     cd3.doc = copiadoc;
     cd3.op = cloned3;
     cd3.bc=&bc;
     bc.SetBool(MDATA_CURRENTSTATETOOBJECT_KEEPANIMATION,TRUE);
     if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, cd3)) return NULL;
     //PolygonObject* Deformed_Object2 = static_cast<PolygonObject*>(cd3.result->GetIndex(0));
     AutoAlloc<PolygonObject> Deformed_Object2(static_cast<PolygonObject*>(cd3.result->GetIndex(0) ));

     

     if (!Deformed_Object2) return FALSE;
     Matrix Mres2=Deformed_Object2->GetMg();

     Vector *deformed2_padr=Deformed_Object2->GetPoint();

     for (LONG i=0; i<countpoint; i++ ) {
          vettore=vettore - deformed2_padr*Mres2 ;
     }
     
     Real     lod;
     Real     fur_display     = data->GetReal(FAST_FUR_DISPLAY);

     lod=doc->GetLOD() * fur_display;

     if(hh->GetVFlags()==20 ) lod=hh->GetLOD();

     ret = GenerateFast_Fur( op, hh->GetThread(), lod, geopoly, Deformed_Object2, vettore, Mres);
     if (!ret) goto Error;

     ret->SetName(op->GetName());

     PolygonObject::Free(cloned);
     PolygonObject::Free(cloned2);
     PolygonObject::Free(cloned3);
//     PolygonObject::Free(geopoly);
//     PolygonObject::Free(Deformed_Object);
//     PolygonObject::Free(Deformed_Object2);
Back to Top
RenatoT View Drop Down
Member
Member


Joined: 2004 Nov 03
Location: Italy
Online Status: Offline
Posts: 452
Post Options Post Options   Quote RenatoT Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 23 at 1:12pm
Hi David,

i see that now my plugin is slower than before.. what's about GetCache() and GetDeformCache() on GetVirtualObjects?

Thanks
Renato T.
Back to Top
Mikael Sterner View Drop Down
Member
Member


Joined: 2002 Oct 29
Location: Sweden
Online Status: Offline
Posts: 1512
Post Options Post Options   Quote Mikael Sterner Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 27 at 12:40am

GetCache() and GetDeformCache() should be fine if they are built I believe. That will however depend on where in the pipeline you are, so you will always need to support the case where the caches are not available.

Mikael Sterner
Developer Support
Back to Top
Michael Welter View Drop Down
Member
Member


Joined: 2002 Nov 09
Location: Germany
Online Status: Offline
Posts: 217
Post Options Post Options   Quote Michael Welter Quote  Post ReplyReply Direct Link To This Post Posted: 2006 Jun 27 at 1:41am
Yes, it can easily happen if for exmaple you link a generator that comes after your object in the hierarchy, and was previously turned off. Now it's on, but Cinema hasn't updated it yet, because it goes from top to buttom.

It can also happen if all viewports show only isoparams, not the real polygon geometry.

Make sure to check the BIT_CONTROLOBJECT, to find out what objects are hidden because of usage by generators.
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.109 seconds.