Accessing the defaults of an arbitrary class from Blueprint at editor time

Sometimes you're making an Editor Utility and you find that you can't access the default variables of arbitrary class or a component inside it. Maybe this'll help, from Ryan.

Ryan DowlingSoka:

If you are at runtime you gotta do it by class and you gotta expose it to code. but it's code every project should have.
You can use Get Class Defaults I think if it isn't a runtime class check, like if it is a static class.

UObject* UYourBlueprintLibrary::GetDefaultObject(UClass* Class)
{
    if (!IsValid(Class)) return nullptr;

    return Class->GetDefaultObject<UObject>();
}

But in editor scripting you want this:
image

UObject* URedTechArtToolsBlueprintLibrary::GetDefaultObjectFromBlueprint(const UObject* Blueprint)
{
    if(!IsValid(Blueprint)) return nullptr;
    
    const UBlueprint* BlueprintObj = Cast<UBlueprint>(Blueprint);
    if(IsValid(BlueprintObj))
    {
        if(IsValid(BlueprintObj->GeneratedClass))
        {
            return BlueprintObj->GeneratedClass->GetDefaultObject();
        }
        
        FName ClassName;
        FName SkeletonClassName;
        BlueprintObj->GetBlueprintCDONames(ClassName, SkeletonClassName);
        return FindObject<UObject>(nullptr, *ClassName.ToString());
    }
    
    if(IsValid(Blueprint->GetClass()))
    {
        return Blueprint->GetClass()->GetDefaultObject();
    }
    
    return nullptr;
}
UFUNCTION(BlueprintCallable, Category = EditorScripting)
    static UObject* GetDefaultObjectFromBlueprint(const UObject* Blueprint);
1 Like