SOLVED Modeling Kernel Error : Invalid Object

I'm trying to change a vertex (point) of a cube and instead getting this error: "Modeling Kernel Error:Invalid Object".
Here is code. I'm using R20 version.

BaseObject* const cube = BaseObject::Alloc(Ocube);

doc->InsertObject(cube, nullptr, nullptr);
doc->AddUndo(UNDOTYPE::NEWOBJ, cube);

ModelingCommandData mcd;
BaseContainer bc;
mcd.doc = doc;
mcd.op = cube;
mcd.bc = &bc;

if (!SendModelingCommand(MCOMMAND_MAKEEDITABLE, mcd)) ApplicationOutput("Couldn't make object editable.");
PolygonObject* pol = static_cast<PolygonObject*>(mcd.result->GetIndex(0));

if (pol->GetType() != Opolygon) {
	ApplicationOutput("not polygon");

AutoAlloc<Modeling> mod;
if (!mod || !mod->InitObject(ToPoly(pol), 0)) {
	ApplicationOutput("modeling alloc failed"); return 0;

pol->ResizeObject(4, 1);
mod->SetPoint(pol, 3, Vector(-1, 0, 0));

if(mod) mod->Commit(pol);


Cube is created, resized, name is changed to "POLY" but zero changes from mod, there is error in console instead.
I'm new to plugin development and don't know how to properly use API for such purposes.

Hello and welcome to the forum,

For your next threads, please help us keeping things organised and clean. I know it's not your priority but it really simplify our work here.

I've added marked this thread as a question so when you considered it as solved, please change the state 🙂

In your code you Resize the object after initialized for the modeling command. That's why.

but probably a simpler way to achieve what you want is to access the polygon's data. The PolygonObject is inherit from PointObject where you can retrieve the pointer of the points' vectors with GetPointW

sometthing like so will work

	Vector* padr{ pol->GetPointW() };
	if (padr == nullptr)
		return maxon::NullptrError(MAXON_SOURCE_LOCATION);

	padr[3] = Vector(-2, 0, 0);

You can have a look at our "roundtube" example in the file that ship with every Cinema 4D version. This example will show you how to create a primitive from scratch.