On 05/05/2013 at 07:10, xxxxxxxx wrote:

User Information:
Cinema 4D Version:   R14 
Platform:   Windows  ;   
Language(s) :     C++  ;

Hi Folks,

I'm trying to add points and polygons to an object's Tpoint and Tpolygon tag. But I can't seem to be able to write anything to them. This is what I've managed so far for trying to get points to the point tag:

BaseDocument *doc = GetActiveDocument();   
BaseObject *obj= doc->GetActiveObject();   
BaseTag *TPnt_Tag = obj->GetTag(Tpoint);   
if(!TPnt_Tag){GePrint("obj Error: Point tag not found.");}   
PointTag *PntTag = (PointTag* )TPnt_Tag;   
Vector *WritePoints = (Vector* )PntTag->GetDataAddressW();   
WritePoints[0] = Vector(-100,-100,-100);   
WritePoints[1] = Vector(-100,100,-100);   
WritePoints[2] = Vector(100,-100,-100);   
WritePoints[3] = Vector(100,100,-100);   
WritePoints[4] = Vector(100,-100,100);   
WritePoints[5] = Vector(100,100,100);   
WritePoints[6] = Vector(-100,-100,100);   
WritePoints[7] = Vector(-100,100,100);   

I'm not worried about polys yet. Will have a go at working that one out for myself first. But from the code above, I'm trying to make a box with 8 points. I've gone a bit in circles so not really sure where to go to from here. I thought I may have to Alloc/Init the point count, but I'm not sure where that might fit in? Is someone able to bump me in the next possible direction here?



On 05/05/2013 at 11:23, xxxxxxxx wrote:

I deleted my first post because I misunderstood your question.
I thought you were asking about selection tags. :blush:

Here's an example of creating a custom hand built polygon object.
As far as I know. We can't add data to the tags that hold the points and polys directly.
AFAIK. We have to add them to the object itself. Then we can read and/or change the existing points in the tag after they have been created.

    BaseDocument *doc = GetActiveDocument();  
  AutoAlloc<PolygonObject> mypoly(4,1); //AutoAlloc automatically handles freeing memory for you   
  Vector *gp = mypoly->GetPointW();    //Gets the array of points in the polygon and assigns it to a variable "gp"  
  gp[0] = Vector(-100,0,-100);        // Place the first point here  
  gp[1] = Vector(-100,0,100);         // Place the first point here  
  gp[2] = Vector(100,0,100);          // Place the first point here  
  gp[3] = Vector(100,0,-100);         // Place the fourth point here  
  //Create the new poly based on the points  
  CPolygon *p = mypoly->GetPolygonW();  //Get the polygon we created above and assign it to a variable "p"  
  p->a = 0;              
  p->b = 1;  
  p->c = 2;                            //Assign the four points of the polygon to the same positions as the gp[] points above  
  p->d = 3;  
  p->d = mypoly->GetPointCount()-1;    //<---Creates a quad polygon  
  //p->a = mypoly->GetPointCount()-1;  //<---Creates a tri polygon if desired  
  mypoly->Message(MSG_UPDATE);        //Update the changes made to our polygon from memory  
  doc->InsertObject(mypoly.Release(), NULL, NULL); //Add it to the OM...Use Release() here so AutoAlloc doesn't automatically delete it  


On 05/05/2013 at 22:00, xxxxxxxx wrote:

Shucks, I was all around that last night. But I left out the mypoly.Release(). I think not having that was causing some issues, and occasional crashes!

Never-the-less, what I'm trying to do is close to the above, but instead of adding a polygon object to the scene, I want to update one that's already there. This is partly why I was trying to go through the tags at one stage.

If I want to add/rebuild a polygon object that's already in the scene, what would I need to adjust with the above? I'm trying to use:

// obj is a poly BaseObject taken from a link field   
AutoAlloc<PolygonObject> mypoly = ToPoly(obj);   

but Cinema crashes when it comes to adding the point vector data. I'm using:

Vector *WritePoints = mypoly->GetPointW();   
WritePoints[0] = Vector(-100,-100,-100); // crashes here on first point   

to resize the object. But it just won't go past adding the point vectors. It crashes.

How would I go about updating a poly object that's already in the scene?


EDIT: forgot to say thanks Scott!

On 06/05/2013 at 03:52, xxxxxxxx wrote:

You are allocating a PolygonObject 'mypoly' to a casted existing object.  Bad idea.  Do it this way:

PolygonObject* mypoly = ToPoly(obj);

You should also verify that the linked object is indeed a PolygonObject type (unless you already do this for MSG_DESCRIPTION_CHECKDRAGANDDROP).  If not checking in that Message(),  do this:

if (!mypoly->IsInstanceOf(Opolygon)) return someerror;

On 06/05/2013 at 04:41, xxxxxxxx wrote:

Thanks Rob,

I must admit, I wasn't checking for whether it's a polygon object or not! "if" statement put in now!

This seems to have given me enough of a kick, I should be able to work out the rest from here! Thanks again,