If you’re interested in taking the first step into Python for 3D software, Or simply would like to browse some script examples, your welcome to visit my Gist page, It contains a useful library of Python code example for Blender, Maya, 3ds max and Unreal engine: https://gist.github.com/CGLion
This simple Unreal Editor Python example sets the Z axis location of all actors with names beginning with ‘Sphere_’ in a way that their bottom (minimum Z bound) is at height 0.0.
import unreal
from unreal import Vector
lst_actors = unreal.EditorLevelLibrary.get_all_level_actors()
print('place actors at 0 z')
for act in lst_actors:
act_label = act.get_actor_label()
if 'Sphere_' in act_label:
print('placing: {}'.format(act_label))
act_location = act.get_actor_location()
act_bounds = act.get_actor_bounds(False)
act_min_z = act_bounds[0].z - act_bounds[1].z
location_offset = Vector(act_location.x, act_location.y, act_location.z - act_min_z)
act.set_actor_location(location_offset, False, False)
* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.
Note:
The get_actor_boundsunreal.Actor class method returns a tuple object containing 2 unreal.Vector objects, the first being world space location of the actor geometric center, and the second is the corner of the bounding box relative to the center.
Importing assets into a project is done using the import_asset_tasks() function which is a member of the unreal.AssetTools class.
A reference to the AssetTools class is created by calling the get_asset_tools() function which is a member of the unreal.AssetToolHelpers class.
The import_asset_tasks() function requires a list of unreal.AssetImportTask objects as an argument, each unreal.AssetImportTask object in the supplied list represents the import action of a single asset, and contains properties needed for the import operation.
Asset import properties are set using the set_editor_property() function which is called through the AssetImportTask object.
Available asset import properties are listed here: https://api.unrealengine.com/INT/PythonAPI/class/AssetImportTask.html
In the following example a specified texture file is imported into the project and stored in the Content(Game) > Textures folder.
* If the folder doesn’t exist it will be created.
The following example imports all the JPG files from folder: D:\ into the project, stores the new assets in Content(Game)\Textures folder and saves them:
from os import listdir
from os.path import isfile, join
import unreal
dir = "D:\\"
files = [f for f in listdir(dir) if isfile(join(dir, f)) and f[-3:]=='jpg']
AssetTools = unreal.AssetToolsHelpers.get_asset_tools()
import_tasks = []
for f in files:
print join(dir, f)
AssetImportTask = unreal.AssetImportTask()
AssetImportTask.set_editor_property('filename', join(dir, f))
AssetImportTask.set_editor_property('destination_path', '/Game/Textures')
AssetImportTask.set_editor_property('save', True)
import_tasks.append(AssetImportTask)
AssetTools.import_asset_tasks(import_tasks)
* note that when copying and pasting a script from this example, the indentation may not be pasted correctly.
Go to: Edit > Plugins > Scripting
And enable the Python Editor Script Plugin.
* also recommended to enable Editor Scripting Utilities,
And Sequencer Scripting plugins
Restart the UE4 Editor.
Open: Window > Developer Tools > Output Log
Switch the command-line mode from Cmd to Python, write Python commands and press Enter to execute them:
Or in Cmd mode, write ‘py‘ with a path to a Python script file, and hit Enter to execute the script: