Python Examples
Here are some Python scripts I wrote for UE that can automate a few simple tasks. Just run it in your Unreal Engine terminal :)
import unreal
@unreal.uclass()
class MyEditorUtility(unreal.GlobalEditorUtilityBase):
pass
@unreal.uclass()
class MyAnimationLibrary(unreal.AnimationLibrary):
pass
EdUtil = MyEditorUtility()
AnimLib = MyAnimationLibrary
selectedAssets = EdUtil.get_selected_assets()
for asset in selectedAssets:
asset.modify(True)
AnimLib.remove_all_animation_notify_tracks(asset)
Create Instances of a selected material
import unreal
totalRequiredInstances = 15
newAssetName = ""
sourceAssetPath = ""
createdAssetsPath = ""
@unreal.uclass()
class MyEditorUtility(unreal.GlobalEditorUtilityBase):
pass
@unreal.uclass()
class MyMaterialEditingLibrary(unreal.MaterialEditingLibrary):
pass
editorUtil = MyEditorUtility()
materialEditingLib = MyMaterialEditingLibrary()
selectedAssets = editorUtil.get_selected_assets()
factor = unreal.MaterialInstanceConstantFactoryNew()
assetTools = unreal.AssetToolsHelpers.get_asset_tools()
for selectedAsset in selectedAssets:
newAssetName = selectedAsset.get_name() + "_%s_%d"
sourceAssetPath = selectedAsset.get_path_name()
createdAssetsPath = sourceAssetPath.replace(selectedAsset.get_name(), "-")
createdAssetsPath = createdAssetsPath.replace("-.-", "")
for x in range (totalRequiredInstances):
newAsset = assetTools.create_asset(newAssetName %("inst", x+1), createdAssetsPath, None, factor)
materialEditingLib.set_material_instance_parent(newAsset, selectedAsset)
Create Instances of a selected material
import unreal
actorCount = 50
slowTaskDisplayText = "Spawning actors into the scene......"
@unreal.uclass()
class MyEditorUtility(unreal.GlobalEditorUtilityBase):
pass
selectedAssets = MyEditorUtility().get_selected_assets()
with unreal.ScopedSlowTask(actorCount, slowTaskDisplayText) as ST:
ST.make_dialog(True)
for x in range(actorCount):
if ST.should_cancel():
break
unreal.EditorLevelLibrary.spawn_actor_from_object(selectedAssets[0], unreal.Vector(1.0+x*100, 1.0+x*100, 0.0), unreal.Rotator(10.0*x, 0.0, 0.0))
unreal.log("Just added an actor to the level")
ST.enter_progress_frame(1)
Create Multiple Actors
import unreal
import sys
actorCount = int(float(sys.argv[1]))
rotationStep = int(float(sys.argv[2]))
positionOffset = float(sys.argv[3])
slowTaskDisplayText = "Spawning actors into the scene......"
@unreal.uclass()
class MyEditorUtility(unreal.GlobalEditorUtilityBase):
pass
selectedAssets = MyEditorUtility().get_selected_assets()
with unreal.ScopedSlowTask(actorCount, slowTaskDisplayText) as ST:
ST.make_dialog(True)
for x in range(actorCount):
if ST.should_cancel():
break
unreal.EditorLevelLibrary.spawn_actor_from_object(selectedAssets[0], unreal.Vector(positionOffset*x, positionOffset*x, 30.0), unreal.Rotator(rotationStep*x, 0.0, 0.0))
unreal.log("Just added an actor to the level")
ST.enter_progress_frame(1)
Create Multiple Actors with Arguments
import unreal
totalRequiredBlueprints = 70
newAssetName = "BP_pythonMade_%d"
createdAssetPath = "/Game/AutoCreated"
slowTaskDisplayText = "Creating new assets......"
factory = unreal.BlueprintFactory()
factory.set_editor_property("ParentClass", unreal.Pawn)
assetTools = unreal.AssetToolsHelpers.get_asset_tools()
with unreal.ScopedSlowTask(totalRequiredBlueprints, slowTaskDisplayText) as ST:
ST.make_dialog(True)
for x in range(totalRequiredBlueprints):
if ST.should_cancel():
break
newAsset = assetTools.create_asset(newAssetName%(x), createdAssetPath, None, factory)
unreal.EditorAssetLibrary.save_loaded_asset(newAsset)
unreal.log("Just created an asset BP_PythonMade_%d via PYTHON API" %~(x))
ST.enter_progress_frame(1)
Create Multiple Assets
import unreal
prefixAnimationBlueprint = "animBP"
prefixAnimationSequence = "anim"
prefixAnimation = "anim"
prefixBlendSpace = "animBlnd"
prefixBlueprint = "bp"
prefixCurveFloat = "crvF"
prefixCurveLinearColor = "crvL"
prefixLevel = "lvl"
prefixMaterial = "mat"
prefixMaterialFunction = "mat_func"
prefixMaterialInstance = "mat_inst"
prefixParticleSystem = "fx"
prefixPhysicsAsset = "phsx"
prefixSkeletalMesh = "sk"
prefixSkeleton = "skln"
prefixSoundCue = "cue"
prefixSoundWave = "wv"
prefixStaticMesh = "sm"
prefixTexture2D = "tex"
prefixTextureCube = "HDRI"
workingPath = "/Game/"
@unreal.uclass()
class MyEditorAssetLibrary(unreal.EditorAssetLibrary):
pass
def GetProperPrefix(className):
_prefix = ""
if className == "AnimBlueprint":
_prefix = prefixAnimationBlueprint
elif className == "AnimSequence":
_prefix = prefixAnimationSequence
elif className == "Animation":
_prefix = prefixAnimation
elif className == "BlendSpace1D":
_prefix = prefixBlendSpace
elif className == "Blueprint":
_prefix = prefixBlueprint
elif className == "CurveFloat":
_prefix = prefixCurveFloat
elif className == "CurveLinearColor":
_prefix = prefixCurveLinearColor
elif className == "Material":
_prefix = prefixMaterial
elif className == "MaterialFunction":
_prefix = prefixMaterialFunction
elif className == "MaterialInstance":
_prefix = prefixMaterialInstance
elif className == "ParticleSystem":
_prefix = prefixParticleSystem
elif className == "PhysicsAsset":
_prefix = prefixPhysicsAsset
elif className == "SkeletalMesh":
_prefix = prefixSkeletalMesh
elif className == "Skeleton":
_prefix = prefixSkeleton
elif className == "SoundCue":
_prefix = prefixSoundCue
elif className == "SoundWave":
_prefix = prefixSoundWave
elif className == "StaticMesh":
_prefix = prefixStaticMesh
elif className == "Texture2D":
_prefix = prefixTexture2D
elif className == "Map":
_prefix = prefixLevel
elif className == "TextureCube":
_prefix = prefixTextureCube
else:
_prefix = ""
return _prefix
editorAssetLib = MyEditorAssetLibrary()
allAssets = editorAssetLib.list_assets(workingPath, True, False)
allAssetsCount = len(allAssets)
selectedAssetPath = workingPath
with unreal.ScopedSlowTask(allAssetsCount, selectedAssetPath) as ST:
ST.make_dialog(True)
for asset in allAssets:
_assetData = editorAssetLib.find_asset_data(asset)
_assetName = _assetData.get_asset().get_name()
_assetPathName = _assetData.get_asset().get_path_name()
_assetPathOnly = _assetPathName.replace((_assetName + "." + _assetName), "")
_assetClassNmae = _assetData.get_asset().get_class().get_name()
_assetPrefix = GetProperPrefix(_assetClassNmae)
if _assetPrefix in _assetName:
continue
elif _assetPrefix == "":
continue
else:
_targetPathName = _assetPathOnly + ("%s%s%s%s%s%s%s" % (_assetPrefix, "_", _assetName, ".", _assetPrefix, "_", _assetName))
editorAssetLib.rename_asset(_assetPathName, _targetPathName)
unreal.log(">>>>>>> Renaming [%s] to [%s]" %(_assetPathName, _targetPathName))
if ST.should_cancel():
break
ST.enter_progress_frame(1, asset)