void SPropertyMenuAssetPicker::OnCreateNewAssetSelected(TWeakObjectPtr<UFactory> FactoryPtr) { if (FactoryPtr.IsValid()) { UFactory* FactoryInstance = DuplicateObject<UFactory>(FactoryPtr.Get(), GetTransientPackage()); FAssetToolsModule& AssetToolsModule = FAssetToolsModule::GetModule(); UObject* NewAsset = AssetToolsModule.Get().CreateAsset(FactoryInstance->GetSupportedClass(), FactoryInstance); if (NewAsset != nullptr) { SetValue(NewAsset); } } }
TOptional<ECurrentState> FAutoReimportManager::ProcessAdditions(const FTimeLimit& TimeLimit) { // Override the global feedback context while we do this to avoid popping up dialogs TGuardValue<FFeedbackContext*> ScopedContextOverride(GWarn, FeedbackContextOverride.Get()); TGuardValue<bool> ScopedAssetChangesGuard(bGuardAssetChanges, true); FeedbackContextOverride->GetContent()->SetMainText(GetProgressText()); TMap<FString, TArray<UFactory*>> Factories; TArray<FString> FactoryExtensions; FactoryExtensions.Reserve(16); // Get the list of valid factories for (TObjectIterator<UClass> It ; It ; ++It) { UClass* CurrentClass = (*It); if (CurrentClass->IsChildOf(UFactory::StaticClass()) && !(CurrentClass->HasAnyClassFlags(CLASS_Abstract))) { UFactory* Factory = Cast<UFactory>(CurrentClass->GetDefaultObject()); if (Factory->bEditorImport && Factory->ImportPriority >= 0) { FactoryExtensions.Reset(); Factory->GetSupportedFileExtensions(FactoryExtensions); for (const auto& Ext : FactoryExtensions) { auto& Array = Factories.FindOrAdd(Ext); Array.Add(Factory); } } } } for (auto& Pair : Factories) { Pair.Value.Sort([](const UFactory& A, const UFactory& B) { return A.ImportPriority > B.ImportPriority; }); } const IAssetRegistry& Registry = FModuleManager::LoadModuleChecked<FAssetRegistryModule>("AssetRegistry").Get(); for (auto& Monitor : DirectoryMonitors) { Monitor.ProcessAdditions(Registry, TimeLimit, PackagesToSave, Factories, *FeedbackContextOverride); yield TOptional<ECurrentState>(); } return ECurrentState::ProcessModifications; }
/** Callback for creating a new level sequence asset in the level. */ static void OnCreateActorInLevel() { // Create a new level sequence IAssetTools& AssetTools = FModuleManager::GetModuleChecked<FAssetToolsModule>("AssetTools").Get(); UObject* NewAsset = nullptr; // Attempt to create a new asset for (TObjectIterator<UClass> It ; It ; ++It) { UClass* CurrentClass = *It; if (CurrentClass->IsChildOf(UFactory::StaticClass()) && !(CurrentClass->HasAnyClassFlags(CLASS_Abstract))) { UFactory* Factory = Cast<UFactory>(CurrentClass->GetDefaultObject()); if (Factory->CanCreateNew() && Factory->ImportPriority >= 0 && Factory->SupportedClass == ULevelSequence::StaticClass()) { NewAsset = AssetTools.CreateAsset(ULevelSequence::StaticClass(), Factory); break; } } } if (!NewAsset) { return; } // Spawn an actor at the origin, and either move infront of the camera or focus camera on it (depending on the viewport) and open for edit UActorFactory* ActorFactory = GEditor->FindActorFactoryForActorClass(ALevelSequenceActor::StaticClass()); if (!ensure(ActorFactory)) { return; } ALevelSequenceActor* NewActor = CastChecked<ALevelSequenceActor>(GEditor->UseActorFactory(ActorFactory, FAssetData(NewAsset), &FTransform::Identity)); if (GCurrentLevelEditingViewportClient != nullptr && GCurrentLevelEditingViewportClient->IsPerspective()) { GEditor->MoveActorInFrontOfCamera(*NewActor, GCurrentLevelEditingViewportClient->GetViewLocation(), GCurrentLevelEditingViewportClient->GetViewRotation().Vector()); } else { GEditor->MoveViewportCamerasToActor(*NewActor, false); } FAssetEditorManager::Get().OpenEditorForAsset(NewAsset); }
TArray<FFactoryItem> FindFactoriesInCategory(EAssetTypeCategories::Type AssetTypeCategory) { TArray<FFactoryItem> FactoriesInThisCategory; for (TObjectIterator<UClass> It; It; ++It) { UClass* Class = *It; if (Class->IsChildOf(UFactory::StaticClass()) && !Class->HasAnyClassFlags(CLASS_Abstract)) { UFactory* Factory = Class->GetDefaultObject<UFactory>(); if (Factory->ShouldShowInNewMenu() && ensure(!Factory->GetDisplayName().IsEmpty())) { uint32 FactoryCategories = Factory->GetMenuCategories(); if (FactoryCategories & AssetTypeCategory) { new(FactoriesInThisCategory)FFactoryItem(Factory, Factory->GetDisplayName()); } } } } return FactoriesInThisCategory; }
/** * gets a factory class based off an asset file extension * * @param AssetExtension - The file extension to use to find a supporting UFactory */ UClass* GetFactoryClassForType(const FString& AssetExtension) { // First instantiate one factory for each file extension encountered that supports the extension for (TObjectIterator<UClass> ClassIt; ClassIt; ++ClassIt) { if ((*ClassIt)->IsChildOf(UFactory::StaticClass()) && !((*ClassIt)->HasAnyClassFlags(CLASS_Abstract))) { UFactory* Factory = Cast<UFactory>((*ClassIt)->GetDefaultObject()); if (Factory->bEditorImport) { TArray<FString> FactoryExtensions; Factory->GetSupportedFileExtensions(FactoryExtensions); // Case insensitive string compare with supported formats of this factory if (FactoryExtensions.Contains(AssetExtension)) { return *ClassIt; } } } } return NULL; }
/** * Creates the new item */ void CreateAsset() { const FString PackageName = AssetPath + TEXT("/") + AssetName; AssetPackage = CreatePackage(NULL, *PackageName); EObjectFlags Flags = RF_Public | RF_Standalone; CreatedAsset = Factory->FactoryCreateNew(Class, AssetPackage, FName(*AssetName), Flags, NULL, GWarn); if (CreatedAsset) { // Notify the asset registry FAssetRegistryModule::AssetCreated(CreatedAsset); // Mark the package dirty... AssetPackage->MarkPackageDirty(); TestStats->NumCreated++; UE_LOG(LogEditorAssetAutomationTests, Display, TEXT("Created asset %s (%s)"), *AssetName, *Class->GetName()); } else { UE_LOG(LogEditorAssetAutomationTests, Error, TEXT("Unable to create asset of type %s"), *Class->GetName()); } }
UObject* UFactory::StaticImportObject ( UClass* Class, UObject* InOuter, FName Name, EObjectFlags Flags, bool& bOutOperationCanceled, const TCHAR* Filename, UObject* Context, UFactory* InFactory, const TCHAR* Parms, FFeedbackContext* Warn, int32 MaxImportFileSize ) { check(Class); CurrentFilename = Filename; // Make list of all applicable factories. TArray<UFactory*> Factories; if( InFactory ) { // Use just the specified factory. if (ensureMsgf( !InFactory->SupportedClass || Class->IsChildOf(InFactory->SupportedClass), TEXT("Factory is (%s), SupportedClass is (%s) and Class name is (%s)"), *InFactory->GetName(), (InFactory->SupportedClass)? *InFactory->SupportedClass->GetName() : TEXT("None"), *Class->GetName() )) { Factories.Add( InFactory ); } } else { auto TransientPackage = GetTransientPackage(); // Try all automatic factories, sorted by priority. for( TObjectIterator<UClass> It; It; ++It ) { if( It->IsChildOf( UFactory::StaticClass() ) ) { UFactory* Default = It->GetDefaultObject<UFactory>(); if (Class->IsChildOf(Default->SupportedClass) && Default->ImportPriority >= 0) { Factories.Add(NewObject<UFactory>(TransientPackage, *It)); } } } Factories.Sort([](const UFactory& A, const UFactory& B) -> bool { // First sort so that higher priorities are earlier in the list if( A.ImportPriority > B.ImportPriority ) { return true; } else if( A.ImportPriority < B.ImportPriority ) { return false; } // Then sort so that factories that only create new assets are tried after those that actually import the file data (when they have an equivalent priority) const bool bFactoryAImportsFiles = !A.CanCreateNew(); const bool bFactoryBImportsFiles = !B.CanCreateNew(); if( bFactoryAImportsFiles && !bFactoryBImportsFiles ) { return true; } return false; }); } bool bLoadedFile = false; // Try each factory in turn. for( int32 i=0; i<Factories.Num(); i++ ) { UFactory* Factory = Factories[i]; UObject* Result = NULL; if( Factory->CanCreateNew() ) { UE_LOG(LogFactory, Log, TEXT("FactoryCreateNew: %s with %s (%i %i %s)"), *Class->GetName(), *Factories[i]->GetClass()->GetName(), Factory->bCreateNew, Factory->bText, Filename ); Factory->ParseParms( Parms ); Result = Factory->FactoryCreateNew( Class, InOuter, Name, Flags, NULL, Warn ); } else if( FCString::Stricmp(Filename,TEXT(""))!=0 ) { if( Factory->bText ) { //UE_LOG(LogFactory, Log, TEXT("FactoryCreateText: %s with %s (%i %i %s)"), *Class->GetName(), *Factories(i)->GetClass()->GetName(), Factory->bCreateNew, Factory->bText, Filename ); FString Data; if( FFileHelper::LoadFileToString( Data, Filename ) ) { bLoadedFile = true; const TCHAR* Ptr = *Data; Factory->ParseParms( Parms ); Result = Factory->FactoryCreateText( Class, InOuter, Name, Flags, NULL, *FPaths::GetExtension(Filename), Ptr, Ptr+Data.Len(), Warn ); } } else { UE_LOG(LogFactory, Log, TEXT("FactoryCreateBinary: %s with %s (%i %i %s)"), *Class->GetName(), *Factories[i]->GetClass()->GetName(), Factory->bCreateNew, Factory->bText, Filename ); // Sanity check the file size of the impending import and prompt the user if they wish to continue if the file size is very large const int32 FileSize = IFileManager::Get().FileSize( Filename ); bool bValidFileSize = true; if ( FileSize == INDEX_NONE ) { UE_LOG(LogFactory, Error,TEXT("File '%s' does not exist"), Filename ); bValidFileSize = false; } TArray<uint8> Data; if( bValidFileSize && FFileHelper::LoadFileToArray( Data, Filename ) ) { bLoadedFile = true; Data.Add( 0 ); const uint8* Ptr = &Data[ 0 ]; Factory->ParseParms( Parms ); Result = Factory->FactoryCreateBinary( Class, InOuter, Name, Flags, NULL, *FPaths::GetExtension(Filename), Ptr, Ptr+Data.Num()-1, Warn, bOutOperationCanceled ); } } } if( Result ) { // prevent UTextureCube created from UTextureFactory check(Result->IsA(Class)); Result->MarkPackageDirty(); ULevel::LevelDirtiedEvent.Broadcast(); Result->PostEditChange(); CurrentFilename = TEXT(""); return Result; } } if ( !bLoadedFile && !bOutOperationCanceled ) { Warn->Logf( *FText::Format( NSLOCTEXT( "UnrealEd", "NoFindImport", "Can't find file '{0}' for import" ), FText::FromString( FString(Filename) ) ).ToString() ); } CurrentFilename = TEXT(""); return NULL; }