BaseMaterial Manual

About

BaseMaterial is the base class for all materials in Cinema 4D. Materials are typically assigned to a BaseObject using a TextureTag. They can be configured like any other BaseList2D based entity. Materials often own BaseShader elements. The most often used material is the standard material (Material class).

BaseMaterial objects are an instance of Mbase.

Access

Materials are stored in a BaseDocument. They can be accessed with:

See BaseDocument Materials

A TextureTag stores a reference to a material. This referenced material is accessed with:

// This example accesses the texture tag on the given BaseObject.
// The texture tag returns the linked material.
TextureTag* ttag = static_cast<TextureTag*>(object->GetTag(Ttexture));
if (!ttag)
return false;
BaseMaterial* material = ttag->GetMaterial();
if (material)
{
GePrint("Used Material: " + material->GetName());
}

See TextureTag Manual.

Allocation/Deallocation

BaseMaterial objects are created with the usual tools.

A newly created material is typically added to a BaseDocument that takes ownership:

A material is assigned to a BaseObject using a TextureTag. The referenced material is set with:

// This example creates a new "Cheen" material and assigns it
// to the given object with a new TextureTag.
// make new material
// make TextureTag
// check if the "Cheen" material and the
// "Texture" tag could be allocated
if (!cheen || !textureTag)
return false;
// assign material
textureTag->SetMaterial(cheen);
// insert material and tag
doc->InsertMaterial(cheen.Release(), nullptr);
object->InsertTag(textureTag.Release());

Navigation

The BaseMaterial elements of a BaseDocument are stored in a list:

// This example loops through all materials of the given BaseDocument.
BaseMaterial* material = doc->GetFirstMaterial();
while (material)
{
GePrint("Material: " + material->GetName());
material = material->GetNext();
}

Read-Only Properties

The following functions allow to access various properties of a BaseMaterial.

// This example accesses the material preview bitmap and shows it in the Picture Viewer.
BaseBitmap* preview = material->GetPreview(0);
ShowBitmap(preview);

Update

In certain situations it is needed to manually trigger an update of the material previews. This may be for example needed in a custom material plugin.

Sample Material

Materials define the surface properties of objects. They are sampled in the rendering pipeline e.g. in a VideoPost, another material or a shader.

Internal material resources are initiated and freed with these functions:

If needed the material calculation is initiated with:

The material can now be sampled and used with:

These examples sample a BaseMaterial in a VideoPostData plugin:

VolumeData* volumeData = vps->vd;
// init material
InitRenderStruct irs(vps->doc);
irs.vd = volumeData;
// if VOLUMEINFO_INITCALCULATION is set, call InitCalculation()
if (material->GetRenderInfo() & VOLUMEINFO_INITCALCULATION)
material->InitCalculation(volumeData, INITCALCULATION_SURFACE);
// init
const INITRENDERRESULT res = material->InitTextures(irs);
if (res != INITRENDERRESULT_OK)
// prepare VolumeData
volumeData->ray = &ray;
volumeData->n = si.n;
volumeData->p = si.p;
volumeData->tex = texData;
volumeData->calc_illum = 0;
volumeData->uvw = uvw;
// sample the material surface color
material->CalcSurface(volumeData);
// get result color
const Vector surfaceColor = volumeData->col;
// free material
material->UnlockTextures();

Compare Entity

Two BaseMaterial elements can be compared with:

// This example compares the given material with the first material of the document.
BaseMaterial* firstMaterial = doc->GetFirstMaterial();
if (!firstMaterial)
return false;
// compare the selected material with the first material
if (selectedMaterial->Compare(firstMaterial))
GePrint("The selected material has the same settings as the first material");
Note
The comparison is mostly based on the material's BaseContainer. Other internal data may not be compared.

Bits

If an element is copied it must mark the materials it uses so that these materials are copied along with the actual element.

  • BIT_MATMARK: This bit defines the material as a "marked" material.
// This example reacts to MSG_MULTI_MARKMATERIALS in an ObjectData::Message() function
// and marks a linked BaseMaterial.
{
BaseDocument* doc = node->GetDocument();
if (!doc)
return false;
// get referenced material
GeData geData;
node->GetParameter(EXAMPLE_MATERIAL_LINK, geData, DESCFLAGS_GET_0);
BaseMaterial* mat = static_cast<BaseMaterial*>(geData.GetLink(doc, Mbase));
if (data) // called when the element is pasted into the scene, update material links
{
MarkMaterials* markMaterial = static_cast<MarkMaterials*>(data);
// check original material
if (markMaterial->omat == mat)
{
// set new material
node->SetParameter(EXAMPLE_MATERIAL_LINK, markMaterial->nmat, DESCFLAGS_SET_0);
}
}
else // called when the element is copied, mark needed materials
{
if (mat)
}
break;
}

Further Reading