Hello @krfft,
thank you for reaching out to us. While I do understand your question, I struggle to understand the technical details of your question. The line of code is very ambiguous, e.g. - what is the return type of your GetMaterial - and I also do not quite understand what you mean by '[...] [for] the type TextureTag that I got before from the material tags, I now get Material as type [...] '. Texture tags are still texture tags, nothing changed there. There is probably something going wrong in your GetMaterial
.
I wrote a small example which demonstrates the steps which must be taken to add takes and overrides for a material assignment. I did not include the asset browser in my example, which I think is part of what you are working on, and instead relied on stuff that is already inside a document.
Cheers,
Ferdinand
The result:
The code:
// A simple example for how to create new takes for a given object and material which assign that
// material to the object.
//
// As discussed in:
// https://plugincafe.maxon.net/topic/13658
#include "c4d_baselist.h"
#include "c4d_baseobject.h"
#include "c4d_basematerial.h"
#include "c4d_basedocument.h"
#include "lib_takesystem.h"
#include "ttexture.h"
/// A function which does create a new take with a material override for a given BaseObject
/// and BaseMaterial.
///
/// If there is no texture tag on the object, a new one will be created.
///
/// @param doc The document op and mat are part of.
/// @param op The object for which to add a material assignment override.
/// @param mat The material to override in the new take.
/// @return The material override for op and mat.
static maxon::Result<BaseOverride*> AddMaterialAssignmentTake(
BaseDocument* doc, BaseObject* op, BaseMaterial* mat)
{
iferr_scope_handler
{
ApplicationOutput("Error in AddMaterialAssignmentTake(): @", err);
return err;
};
// Get the take data of the document and create a new take for the material override.
TakeData* const takeData = doc->GetTakeData();
if (takeData == nullptr)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Could not retrieve take data."_s);
String takeName = FormatString("@: @", op->GetName(), mat->GetName());
BaseTake* const materialTake = takeData->AddTake(takeName, nullptr, nullptr);
if (materialTake == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION, "Could not add take."_s);
// Get the first texture tag on the object or create a new one when there is none present.
BaseTag* textureTag = op->GetTag(Ttexture);
if (textureTag == nullptr)
textureTag = op->MakeTag(Ttexture);
if (textureTag == nullptr)
return maxon::OutOfMemoryError(MAXON_SOURCE_LOCATION, "Could not add texture tag."_s);
// Add the override.
DescID idMatrialAssignment = DescID(DescLevel(TEXTURETAG_MATERIAL));
BaseOverride* result = materialTake->FindOrAddOverrideParam(
takeData, textureTag, idMatrialAssignment, GeData(mat));
if (result == nullptr)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Could not set override."_s);
// Set the new take as the active one and return the override result.
takeData->SetCurrentTake(materialTake);
return result;
}
/// Runs the example.
static maxon::Result<void> PC13658(BaseDocument* doc)
{
iferr_scope_handler
{
ApplicationOutput("Error in PC13658(): @", err);
return err;
};
// Get the active object and material in the document.
BaseObject* op = doc->GetActiveObject();
if (op == nullptr)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Please select an object."_s);
BaseMaterial* mat = doc->GetActiveMaterial();
if (mat == nullptr)
return maxon::UnexpectedError(MAXON_SOURCE_LOCATION, "Please select a material."_s);
// And pass them to our function and then print out the returned override.
BaseOverride* override = AddMaterialAssignmentTake(doc, op, mat) iferr_return;
ApplicationOutput("Added overide: @", override);
return maxon::OK;
}