Editor scripting: I'd like to set the default class values on a newly generated BP asset in the content browser, but I don't know how.

I have some code to generate a new Blueprint asset in the content browser of a certain class. I’d like to be able to programmatically fill in some of that object’s default values before I save the package (or resave it afterward, if necessary).

The trouble is, I have no clue how to do this! I’m not very familiar with the relationship between Blueprint, GeneratedBlueprint, and the actual class that contains the properties I want to modify.

Is anybody more familiar with this and can give me some pointers?

This is the code I have to create the asset. I use the EditorUtilityBlueprintFactory to create the object, then cast that object to an EditorUtilityBlueprint - expecting that’s a step I’ll need to do. After this code block I save the asset with UPackage::Save().

UEditorUtilityBlueprintFactory* EditorUtilityBlueprintFactory = NewObject<UEditorUtilityBlueprintFactory>(UEditorUtilityBlueprintFactory::StaticClass());

EditorUtilityBlueprintFactory->ParentClass = MyEditorUtilityObjectClass::StaticClass();

UObject* NewObject = AssetToolsModule.Get().CreateAsset(Name, PackagePath, UEditorUtilityBlueprint::StaticClass(), EditorUtilityBlueprintFactory);

if (UEditorUtilityBlueprint* NewBlueprint = Cast<UEditorUtilityBlueprint>(NewObject))
{
    // Code reaches here. But not sure how to set default properties on the asset!
}

Solved by ChatGPT (though property types should have F prefix and not U)

// Assuming you have already spawned and saved the blueprint asset
// Let's assume you have a reference to the blueprint asset called 'MyBlueprint'

// Get the default object of the blueprint
UObject* DefaultObject = MyBlueprint->GeneratedClass->GetDefaultObject();

// Change the property value
UProperty* Property = DefaultObject->GetClass()->FindPropertyByName(TEXT("PropertyName"));
if (Property)
{
    if (Property->IsA<UIntProperty>())
    {
        UIntProperty* IntProperty = Cast<UIntProperty>(Property);
        IntProperty->SetPropertyValue_InContainer(DefaultObject, NewValue);
    }
    else if (Property->IsA<UFloatProperty>())
    {
        UFloatProperty* FloatProperty = Cast<UFloatProperty>(Property);
        FloatProperty->SetPropertyValue_InContainer(DefaultObject, NewValue);
    }
    // Add additional checks for other property types if needed
    // String, bool, etc.
}

// Save the changes to the blueprint
MyBlueprint->Modify();
MyBlueprint->PostEditChange();
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.