Vizard15
(Vizard15)
January 13, 2020, 1:29pm
1
Hi all,
Documentation at unreal.MaterialEditingLibrary — Unreal Python 5.1 (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
bkulig
(bkulig)
March 10, 2020, 8:14pm
2
I am wondering the same thing. Did you find any workaround?
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.
dpotuznik
(dpotuznik)
September 13, 2021, 11:19am
8
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 .
UFUNCTION(BlueprintCallable, Category = "PyToolkit|Socket")
static FName GetSkeletonBoneName(USkeleton *InSkeleton, int32 BoneIndex);
#pragma endregion
#pragma region Material>
UFUNCTION(BlueprintCallable, Category = "RedArtToolkit|Material")
static UMaterialInstanceConstant *GetMaterialEditorSourceInstance(UMaterialEditorInstanceConstant *Editor);
UFUNCTION(BlueprintCallable, Category = "RedArtToolkit|Material")
static void SetMaterialInstanceStaticSwitchParameterValue(UMaterialInstance *Instance, FName ParameterName, bool Value);
UFUNCTION(BlueprintCallable, Category = "RedArtToolkit|Material")
static TArray<UMaterialExpression *> GetMaterialExpressions(UMaterial *Material);
UFUNCTION(BlueprintCallable, Category = "RedArtToolkit|Material")
static TArray<UMaterialExpression *> GetMaterialFunctionExpressions(UMaterialFunction *Function);
#pragma endregion
#pragma region Msic
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)
1 Like
schauk
(schauk)
October 20, 2022, 12:50pm
10
bump, that rally needs a fix. not possible in 5.0 also
YuBeDev
(yubedev)
December 29, 2023, 7:38pm
12
Found workaround. Use LERP function and pass it scalar hard 0 or 1, then you can use set scalar from python. Would work for something like replacement to “isMetallic?” bool or “isAlpha?”. Might not be suitable for all applications tho.
Original: controlling a StaticSwitch with a Float/Scalar/Constant 1 in a Material - #2 by Lovecraft_K