On 02/09/2016 at 15:12, xxxxxxxx wrote:
There are two ways to change the image in a material's Xbitmap type shader
-Using the BaseContanier
-Using the Get() & Set() parameter functions
Both work equally well. But Maxon prefers us to use the later whenever possible.
BaseContainer method
//The target material
BaseMaterial *mat = doc->GetActiveMaterial();
if (!mat) return FALSE;
//The file path to the new image
//Change this as desired
Filename file = Filename("C:\\Users\\user\\Desktop\\myimage.jpg");
//Target the color channel and get it's BaseContainer
BaseChannel *ch = mat->GetChannel(CHANNEL_COLOR);
BaseContainer bc = ch->GetData();
//Change the container to point to the new image file path
bc.SetString(BASECHANNEL_TEXTURE, file.GetString());
//Update the container so it uses the changes
ch->SetData(bc);
mat->Update(TRUE, TRUE); //Updates the thumbnail image
mat->Message(MSG_UPDATE); //Updates the material
EventAdd();
Get() & Set() parameter method
//The target material
BaseMaterial *mat = doc->GetActiveMaterial();
if (!mat) return FALSE;
//The file path to the new image
//Change this as desired
Filename file = Filename("C:\\Users\\user\\Desktop\\myimage.jpg");
//First test if there is already an Xbitmap shader holding an image in the color channel
//Bail out if it doesn't exit
GeData data;
mat->GetParameter(DescID(MATERIAL_COLOR_SHADER), data, DESCFLAGS_GET_0);
BaseShader *shader = static_cast<BaseShader*>(data.GetLink(doc, Xbitmap));
if (!shader || !shader->IsInstanceOf(Xbitmap)) return false;
//Get the image that is currently in the shader and print it to the console
//This code can be skipped if desired
shader->GetParameter(DescID(BITMAPSHADER_FILENAME), data, DESCFLAGS_GET_0);
GePrint(data.GetFilename().GetString());
//Change the file path by setting it in the data variable (similar to changing a BaseContainer value)
data.SetFilename(file);
//Target the file option in the shader and appy the data variable. Which now holds the new file path
//The ID for the file option is: BITMAPSHADER_FILENAME
shader->SetParameter(DescLevel(BITMAPSHADER_FILENAME), data, DESCFLAGS_SET_0);
mat->Update(TRUE, TRUE); //Updates the thumbnail image
mat->Message(MSG_UPDATE); //Updates the material
EventAdd();
-ScottA