Download

Setting Static Switch parameters of a Material Instance in Python

Hi all,

Documentation at unreal.MaterialEditingLibrary — Unreal Python 4.27 (Experimental) documentation shows that we can get the material static switch parameters through get_material_default_static_switch_parameter_value(material, parameter_name). There are set methods for scalar, texture and vector paramters, but not for static switches. Can anyone please help me with a way to set static switch paramater values?

Thanks

I am wondering the same thing. Did you find any workaround?

Same here…

Tried setting them using the set scalar? Working in angelscript and that seem to be what I’ve used.

Didn’t manage to do it using scalar. Anyone knows a way to achieve this?

A workaround is to create material instances variants with these static parameters turned on/off and select those instead of settings the parameters via script

same problem here, really annoying.

still not possible on 4.27
really annoying.

There is no pure python way to solve it.
I develope a C++ BlueprintLibrary which can solve this problem.

C++ code I reference from a Japanese blog

And I found some issue with this code, So I write a modified version of this function.

here is my blog article, sorry but in chinese.
C++ Solution below

void URedArtToolkitBPLibrary::SetMaterialInstanceStaticSwitchParameterValue(UMaterialInstance *Instance, FName ParameterName, bool SwitchValue, bool bOverride)
{
    TArray<FGuid> Guids;
    TArray<FMaterialParameterInfo> OutParameterInfo;
    Instance->GetAllStaticSwitchParameterInfo(OutParameterInfo, Guids);
    FStaticParameterSet StaticParameters = Instance->GetStaticParameters();

    for (int32 ParameterIdx = 0; ParameterIdx < OutParameterInfo.Num(); ParameterIdx++)
    {
        const FMaterialParameterInfo &ParameterInfo = OutParameterInfo[ParameterIdx];
        const FGuid ExpressionId = Guids[ParameterIdx];
        if (ParameterInfo.Name == ParameterName)
        {
            FStaticSwitchParameter *NewParameter =
                new (StaticParameters.StaticSwitchParameters) FStaticSwitchParameter(ParameterInfo, SwitchValue, bOverride, ExpressionId);
            break;
        }
    }
    Instance->UpdateStaticPermutation(StaticParameters);
}

For the real world example, you can check my C++ Plugin Unreal-PyToolkit.

After add and compile Unreal-PyToolkit plugin into your project
you can use it with python below

import unreal
py_lib = unreal.PyToolkitBPLibrary
material = unreal.load_asset("material_path")
py_lib.set_material_instance_static_switch_parameter_value(material,"param",True)