Skip to content

Commit 8722202

Browse files
author
Roberto De Ioris
authored
Create MigrateAssets.md
1 parent 0fbead0 commit 8722202

1 file changed

Lines changed: 106 additions & 0 deletions

File tree

docs/MigrateAssets.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Assets migration
2+
3+
Assets migration is a procedure allowing you to export the assets of a project into another unreal engine project.
4+
5+
When triggering it from the editor, you will get a list of packages to export and where to save them.
6+
7+
Here we will see how to automate the task from python
8+
9+
## Step1 getting asset dependencies
10+
11+
An important topic you need to understand, is that you export packages not single assets. Packages are mapped into real files.
12+
13+
Packages can contain multiple assets (albeit highly discouraged).
14+
15+
When we want to migrate an asset we need to get its package and all othe packages referenced by the asset (as an example, a blueprint
16+
with a StaticMeshComponent, holds a reference to the StaticMesh asset, so weneed to export it to).
17+
18+
The unreal_engine.get_asset_dependencies(package_name) will do the trick:
19+
20+
```python
21+
import unreal_engine as ue
22+
23+
# get the currently selected asset
24+
asset = ue.get_selected_assets()[0]
25+
26+
# retrieve the package path
27+
package_path = asset.get_outermost().get_path_name()
28+
print(package_path)
29+
30+
def recursive_deps(pkg_path, deps):
31+
deps.append(pkg_path)
32+
for _dep in ue.get_asset_dependencies(pkg_path):
33+
if _dep not in deps:
34+
if not _dep.startswith('/Engine') and not _dep.startswith('/Script'):
35+
recursive_deps(_dep, deps)
36+
37+
38+
deps = []
39+
40+
recursive_deps(package_path, deps)
41+
42+
print(deps)
43+
```
44+
45+
## Step2: fixing dirty packages
46+
47+
A package is marked as dirty when it has been modified without saving it. In this step we will check each package we need to
48+
migrate and we will eventually save it:
49+
50+
```python
51+
for _dep in deps:
52+
# load the package
53+
pkg = ue.load_package(_dep)
54+
# check for dirty packages
55+
if pkg.package_is_dirty():
56+
pkg.save_package()
57+
```
58+
59+
## Step3: copying .uasset files in the destination project
60+
61+
This is the last step, we retieve the filename of each package and will copy it in the destination directory.
62+
63+
Full script for migration:
64+
65+
```python
66+
import unreal_engine as ue
67+
import os
68+
from shutil import copyfile
69+
70+
# get the currently selected asset
71+
asset = ue.get_selected_assets()[0]
72+
73+
# retrieve the package path
74+
package_path = asset.get_outermost().get_path_name()
75+
print(package_path)
76+
77+
def recursive_deps(pkg_path, deps):
78+
deps.append(pkg_path)
79+
for _dep in ue.get_asset_dependencies(pkg_path):
80+
if _dep not in deps:
81+
if not _dep.startswith('/Engine') and not _dep.startswith('/Script'):
82+
recursive_deps(_dep, deps)
83+
84+
85+
deps = []
86+
87+
recursive_deps(package_path, deps)
88+
89+
print(deps)
90+
91+
destination = 'D:/UdemyCast/Content'
92+
93+
for _dep in deps:
94+
# check for dirty packages
95+
pkg = ue.load_package(_dep)
96+
if pkg.package_is_dirty():
97+
pkg.save_package()
98+
src = pkg.package_get_filename()
99+
sub = src.split('/Content/', 1)[1]
100+
dst = os.path.join(destination, sub)
101+
copyfile(src, dst)
102+
103+
print('asset migrated')
104+
```
105+
106+
Ensure the destination directory is a /Content directory in UE4 project

0 commit comments

Comments
 (0)