Skip to content

Instantly share code, notes, and snippets.

@fwilleke80
Last active March 17, 2017 11:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fwilleke80/48116b67206ba245ea0db66b3e949129 to your computer and use it in GitHub Desktop.
Save fwilleke80/48116b67206ba245ea0db66b3e949129 to your computer and use it in GitHub Desktop.
[C4D] Calling MCOMMAND_CURRENTSTATETOOBJECT on an object... the proper way
/// Calls MCOMMAND_CURRENTSTATETOOBJECT on an object and returns the result
///
/// @note: This is time and memory consuming, so only do it when necessary, and then cache the result!
///
/// @param[in] op The object you want to CSTO
/// @return The resulting object. Caller owns the pointed object.
PolygonObject* CurrentStateToObject(BaseObject *op)
{
// Create AliasTranslate
AutoAlloc<AliasTrans> aliasTrans;
if (!aliasTrans || !aliasTrans->Init(op->GetDocument()))
return nullptr;
// Create clone of op. We only need this clone for the modeling command.
BaseObject *tmpOp = static_cast<BaseObject*>(op->GetClone(COPYFLAGS_0, aliasTrans));
if (!tmpOp)
return nullptr;
// Translate BaseLinks, maybe the cloned object needs that
aliasTrans->Translate(true);
// Create temporary document
AutoAlloc<BaseDocument> tmpDoc;
if (!tmpDoc)
{
// Free tmpOp and return
BaseObject::Free(tmpOp);
return nullptr;
}
// Insert tmpOp into tmpDoc. From now on, tmpDoc has the ownership over tmpOp,
// so we don't need to free tmpOp manually anymore (because tmpDoc will auto-free itself at the end of the scope, as we used AutoAlloc to create it).
tmpDoc->InsertObject(tmpOp, nullptr, nullptr);
// Build modeling command data
ModelingCommandData mcd;
mcd.doc = tmpDoc;
mcd.op = tmpOp;
// Perform modeling command
if (!SendModelingCommand(MCOMMAND_CURRENTSTATETOOBJECT, mcd))
return nullptr;
// Get result
PolygonObject *res = static_cast<PolygonObject*>(mcd.result->GetIndex(0));
// Set original matrix
if (res)
res->SetMg(op->GetMg());
// Return result
return res;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment