Notifications
Clear all

[Closed] Show Materials/Textures in Viewport C++

Hi Guys,
I’ve been trying to show different materials or texmaps within the viewport. The issue I have, is the code I’ve got seems to work on simple materials with texmaps (eg VRayMtl with a Checker texmap) But as soon as I try using it on a more complex object with multiple mtls/texmaps, it just shows the object as black untill I go into that specific texmap.
The code I’m using is below, if anyone has any ideas, let me know


if (mat) {
    for (int i = 0; i < mat->NumSubTexmaps(); i++) {
        Texmap* tmap = mat->GetSubTexmap(i);
        if (tmap) {
           // tmap->LoadMapFiles(t);
           // PStamp* ps = tmap->CreatePStamp(PS_TINY, TRUE);
            BitmapTex* TMap = static_cast<BitmapTex*>(tmap);
            if (TMap) {
                TMap->SetMtlFlag(MTL_TEX_DISPLAY_ENABLED, TRUE);
                TMap->ActivateTexDisplay(TRUE); //activate it
                TMap->NotifyDependents(FOREVER, PART_ALL, REFMSG_CHANGE);
                mat->SetMtlFlag(MTL_TEX_DISPLAY_ENABLED, TRUE);
                mat->NotifyDependents(FOREVER, PART_ALL, REFMSG_CHANGE);
            }
        }
    }
}

I know there is extra stuff like PStamp etc this was seeing if the texture needed renderering or loading (but for this, assume it is)
This is the closest I could get it where it atleast didn’t have strange colours showing in the viewport, but it doesn’t still want to show the texture.
For some context of why I need this, I do a lot Mtls clones/copys and once its clone or copied, it doesn’t want to show in the viewport. So I’m looking for a way to activate the textures.
Thanks !!

4 Replies
BitmapTex* TMap = static_cast<BitmapTex*>(tmap);

should be:

BitmapTex* TMap = dynamicc_cast<BitmapTex*>(tmap);

static_cast on pointers is bad, dynamic_cast will return NULL if tmap was not a BitmapTex
might be a noisemap

all there is to do is to activate the texture you want to see in viewport
you cant activate all of them, it’s your decision which one you want, usually diffuse map

GetCOREInterface()->ActivateTexture(TMap, mat);

and for the materials set the material flags

mtl->SetMtlFlag(MTL_DISPLAY_ENABLE_FLAGS);

not needed:

TMap->SetMtlFlag(MTL_TEX_DISPLAY_ENABLED, TRUE);
TMap->ActivateTexDisplay(TRUE); //activate it  <-- this doesn't activate it

check if you really need this one

TMap->NotifyDependents(FOREVER, PART_ALL, REFMSG_CHANGE);

if you need them there might be some error in the plugin code somewhere
sending useless notifications (or RedrawViews) will slow down max
many plugins do that, one reason why some operations in max are slower than needed

Ahh ok, that makes a lot of sense, but I now have a new question.
If I can only Activate one texture, is there a way to tell which one was Activated before (from original mtls/texmap), or which one is the Diffuse map (I know its usually the second one within SubTextures i.e VRay ) But I would like this to work with all mtls !!
Since there is no isActivatedTexmap, would it be some kind of flag ???
Thanks for the help so far !!!

Ok, I found a bit of code to see if the texture is activated which is


int tex_display = TMap->TestMtlFlag(MTL_TEX_DISPLAY_ENABLED);   //get active status

But even after using this, it still won’t show the texture untill I go into it. I’ll keep digging but any help is much appreciated !!

possible to share some more code about this, the problem might be anywhere ?
if not here, feel free to PM/mail me