Python API - How to create sprites from textures?

Hey folks,

I’m using the Python API to bulk import thousands of assets for an enterprise/industrial use case. This is working great, but along the way some of these assets should also have a sprite created.

I have looked through all the PaperSprite Python API docs and it’s still unclear to me how to trigger a sprite to be created from specific imported textures/images. Currently I’m batching imports via AssetImportTask in a list along with AssetTools.import_asset_tasks.

Any suggestions on starting points or something I missed in the docs? My goal is to discover all assets in a specific folder and if ‘XYZ_texture.uasset’ doesn’t have a corresponding sprite to create one.

Part of my pipeline involves periodically importing new and updating some existing assets and getting this scripted is pretty much essential.

Thanks in advance for any pointers in the right direction!

This may point you in the right direction. With Paper2D, when you right-click a texture asset in the Content Browser and go to “Sprite Actions” > “Create Sprite”, this C++ code gets run. You may be able to use this as a starting point by essentialy remaking the function in Python.

You can see that the actual creation of the sprite from a texture looks like this (really simple):

                     UPaperSpriteFactory* SpriteFactory = NewObject<UPaperSpriteFactory>();
                     SpriteFactory->InitialTexture = Texture;

The entire function is this (you probably won’t need most of it):

void CreateSpritesFromTextures(TArray<UTexture2D*>& Textures)
        {
                FAssetToolsModule& AssetToolsModule = FModuleManager::Get().LoadModuleChecked<FAssetToolsModule>("AssetTools");
                FContentBrowserModule& ContentBrowserModule = FModuleManager::LoadModuleChecked<FContentBrowserModule>("ContentBrowser");

                TArray<UObject*> ObjectsToSync;

                for (auto TextureIt = Textures.CreateConstIterator(); TextureIt; ++TextureIt)
                {
                        UTexture2D* Texture = *TextureIt;

                        // Create the factory used to generate the sprite
                        UPaperSpriteFactory* SpriteFactory = NewObject<UPaperSpriteFactory>();
                        SpriteFactory->InitialTexture = Texture;

                        // Create the sprite
                        FString Name;
                        FString PackageName;

                        if (!bExtractSprites)
                        {
                                // Get a unique name for the sprite
                                const FString DefaultSuffix = TEXT("_Sprite");
                                AssetToolsModule.Get().CreateUniqueAssetName(Texture->GetOutermost()->GetName(), DefaultSuffix, /*out*/ PackageName, /*out*/ Name);
                                const FString PackagePath = FPackageName::GetLongPackagePath(PackageName);

                                if (UObject* NewAsset = AssetToolsModule.Get().CreateAsset(Name, PackagePath, UPaperSprite::StaticClass(), SpriteFactory))
                                {
                                        ObjectsToSync.Add(NewAsset);
                                }
                        }
                        else
                        {
                                SPaperExtractSpritesDialog::ShowWindow(Texture);
                        }
                }

                if (ObjectsToSync.Num() > 0)
                {
                        ContentBrowserModule.Get().SyncBrowserToAssets(ObjectsToSync);
                }
        }

Hope this helps

This is an excellent starting point to accomplish what I’m looking to do. Many thanks for nudge in the right direction!