Skip to content

Commit 0d7fb9a

Browse files
release/0.1.0
1 parent 9c1e7cd commit 0d7fb9a

File tree

19 files changed

+1534
-6
lines changed

19 files changed

+1534
-6
lines changed

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Changelog
2+
3+
All notable changes to this package will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
6+
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
7+
8+
## [0.1.0] - 2024-09-04
9+
10+
Initial release
11+
12+
Features:
13+
- Source code of `bulk_upload_cli`
14+
- Source code of `bulk_download_script`

LICENSE.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Unity Cloud Python SDK Samples copyright © 2024 Unity Technologies SF
2+
3+
Licensed under the Unity Terms of Service ( see https://unity.com/legal/terms-of-service.).
4+
5+
Unless expressly provided otherwise, the Software under this license is made available strictly on an "AS IS" BASIS WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED. Please review the license for details on these and other terms and conditions.

README.md

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
1-
# unity-cloud-python-sdk-samples
2-
Public slack channel: [#uc-cs-am-python-sdk](https://unity.slack.com/messages/C04R01SGG68/) <br/>
3-
[View this project in Backstage](https://backstage.corp.unity3d.com/catalog/default/component/unity-cloud-python-sdk-samples) <br/>
4-
# Converting to public repository
5-
Any and all Unity software of any description (including components) (1) whose source is to be made available other than under a Unity source code license or (2) in respect of which a public announcement is to be made concerning its inner workings, may be licensed and released only upon the prior approval of Legal.
6-
The process for that is to access, complete, and submit this [FORM](https://airtable.com/appj757BYrNIMuTBI/shriEdWiQuxWmJOku).
1+
# Python SDK samples
2+
3+
This repository exposes a few samples that demonstrate the use of Python SDK in real life use-cases:
4+
5+
- [Bulk Upload CLI](./bulk_upload_cli/README.md)
6+
- [Bulk Download script](./bulk_download_script/README.md)
7+
8+
> **Note**: This repository does not accept pull requests, review requests, or any other GitHub-hosted issue management requests.
9+
10+
## Licenses
11+
12+
The Unity Cloud Python SDK samples are made available under the [Unity ToS license](./LICENSE.md).
13+
14+
## See also
15+
16+
- [Unity Cloud Python SDK documentation](https://docs.unity.com/cloud/en-us/asset-manager/python-sdk)
17+
18+
## Tell us what you think!
19+
20+
Thank you for taking a look at the project! To help us improve and provide greater value, please consider providing feedback in our [Help & Support page](https://cloud.unity.com/home/dashboard-support). Thank you!

bulk_download_script/README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Bulk download script
2+
3+
The sample script demonstrates how to use Python SDK to download assets from Unity Cloud Asset Manager.
4+
5+
To connect and find support, join the [Help & Support page](https://cloud.unity.com/home/dashboard-support)!
6+
7+
## Table of contents
8+
- [Bulk download script](#bulk-download-script)
9+
- [Table of contents](#table-of-contents)
10+
- [Prerequisites](#prerequisites)
11+
- [System requirements](#system-requirements)
12+
- [Licenses](#licenses)
13+
- [How do I run the sample ?](#how-do-i-run-the-sample-)
14+
- [1. Edit the `bulk_download.py` script with your requirements](#1-edit-the--bulk_downloadpy-script-with-your-requirements)
15+
- [2. Run the script](#2-run-the-script)
16+
- [See also](#see-also)
17+
- [Tell us what you think!](#tell-us-what-you-think)
18+
19+
## Prerequisites
20+
21+
### System requirements
22+
23+
To run the script, you need:
24+
- Python installed on your machine
25+
- An up-to-date Python SDK wheel installed ( > 0.5.0).
26+
- The right permissions to use Asset Manager. See [Get Started with Asset Manager](https://docs.unity.com/cloud/en-us/asset-manager/get-started) for more details.
27+
- A source project with assets manager enable and assets already uploaded in it.
28+
29+
### Licenses
30+
31+
The bulk download sample script is made available under the [Unity ToS license](../LICENSE.md).
32+
33+
## How do I run the sample ?
34+
35+
To run the sample, follow these steps:
36+
37+
### 1. Edit the `bulk_download.py` script with your requirements
38+
39+
In the `main` conditional section, you must edit some information to link the sample to your project.
40+
41+
- org_id: Your organization id.
42+
- project_id: Your project id.
43+
- download_directory: must be edited with the path where the assets will be downloaded.
44+
- overwrite: When set to `True`, the script will overwrite the files in the download directory if they already exist. Otherwise, it will skip the download.
45+
- include_filter/exclude_filter/any_filter: This dictionary contains the search criteria to fetch the assets. Some example are written in comments, otherwise please refer to [the Python SDK documentation](https://docs.unity.com/cloud/en-us/asset-manager/python-sdk/manage-assets#create-filter-for-a-search-query) to learn how to use search criteria.
46+
- collections: This list contains the collections to fetch the assets from. Leave it empty to search through all the assets in the project.
47+
48+
### 2. Run the script
49+
50+
With you favorite command line tool, run `python bulk_download.py` in this folder.
51+
52+
## See also
53+
54+
- [Unity Cloud Python SDK documentation](https://docs.unity.com/cloud/en-us/asset-manager/python-sdk)
55+
56+
## Tell us what you think!
57+
58+
Thank you for taking a look at the project! To help us improve and provide greater value, please consider providing feedback in our [Help & Support page](https://cloud.unity.com/home/dashboard-support). Thank you!

bulk_download_script/__init__.py

Whitespace-only changes.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
from concurrent.futures.thread import ThreadPoolExecutor
2+
3+
import unity_cloud as uc
4+
from pathlib import PurePath, Path
5+
from unity_cloud.models import *
6+
7+
8+
def login_with_user_account():
9+
uc.identity.user_login.use()
10+
auth_state = uc.identity.user_login.get_authentication_state()
11+
if auth_state != uc.identity.user_login.Authentication_State.LOGGED_IN:
12+
uc.identity.user_login.login()
13+
14+
15+
def download_asset(organization_id: str, project_id: str, asset: Asset, download_path: str, overwrite: bool = False):
16+
dataset = uc.assets.get_dataset_list(organization_id, project_id, asset.id, asset.version)[0]
17+
asset_files = uc.assets.get_file_list(organization_id, project_id, asset.id, asset.version, dataset.id)
18+
19+
with ThreadPoolExecutor(max_workers=10) as executor:
20+
for file in asset_files:
21+
file_download_info = FileDownloadInformation(organization_id, project_id, asset.id, asset.version,
22+
dataset.id, file.path, PurePath(download_path))
23+
24+
target_file = Path(download_path) / file.path
25+
26+
if not overwrite and target_file.exists():
27+
print(f"Skipping download of {file.path} as it already exists", flush=True)
28+
continue
29+
30+
print(f"Downloading file: {file.path}", flush=True)
31+
executor.submit(uc.assets.download_file, file_download_info)
32+
33+
34+
def download_assets(assets: [Asset], org_id: str, project_id: str, download_path: str, overwrite: bool = False):
35+
for asset in assets:
36+
print(f"Downloading files for asset: {asset.name}", flush=True)
37+
download_asset(org_id, project_id, asset, download_path, overwrite)
38+
39+
40+
if __name__ == '__main__':
41+
42+
uc.initialize()
43+
login_with_user_account()
44+
45+
org_id = '<org id>'
46+
project_id = '<project id>'
47+
download_directory = 'C:\\path\\to\\download\\directory\\'
48+
overwrite = False
49+
50+
include_filter = dict()
51+
52+
#to search by status uncomment one of the following lines
53+
#include_filter[SearchableProperties.STATUS] = "Published"
54+
#include_filter[SearchableProperties.STATUS] = "Draft"
55+
56+
#to search by tags uncomment one of the following lines and replace <tag_name> with the tag you want to search for
57+
#include_filter[SearchableProperties.TAGS] = ["<tag_name>""]
58+
#include_filter[SearchableProperties.FILES_TAGS] = ["<tag_name>""]
59+
60+
collections = []
61+
# collections = ['<collection_name>']
62+
63+
assets = uc.assets.search_assets_in_projects(org_id=org_id, project_ids=[project_id], include_filter=include_filter,
64+
collections=collections)
65+
download_assets(assets, org_id, project_id, download_directory, overwrite=overwrite)

bulk_upload_cli/README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Bulk upload CLI
2+
3+
The Bulk upload Command-Line Interface (CLI) is a cross-platform command-line tool to connect to Asset Manager and execute administrative commands. It allows you to create configuration files that you can save and run from a terminal. Using CLI, you can create and update assets in bulk from your local disk to Asset Manager based on several inputs to match your folder structure. This tool offers an interactive mode where you are prompted to provide the necessary information to create and save configuration files for future asset updates.
4+
5+
To connect and find support, join the [Help & Support page](https://cloud.unity.com/home/dashboard-support)!
6+
7+
## Table of contents
8+
- [Bulk upload CLI](#bulk-upload-cli)
9+
- [Table of contents](#table-of-contents)
10+
- [Prerequisites](#prerequisites)
11+
- [System requirements](#system-requirements)
12+
- [Licenses](#licenses)
13+
- [How do I...?](#how-do-i)
14+
- [Install the tool](#install-the-tool)
15+
- [Run the tool in interactive mode](#run-the-tool-in-interactive-mode)
16+
- [Select the input method](#select-the-input-method)
17+
- [See also](#see-also)
18+
- [Tell us what you think!](#tell-us-what-you-think)
19+
20+
## Prerequisites
21+
22+
### System requirements
23+
24+
To run the script, you need:
25+
- Python installed on your machine
26+
- An Asset Manager Contributor role on the project level or an Organization Owner role. For more information about roles, see [Roles and permissions](https://docs.unity.com/cloud/en-us/asset-manager/org-project-roles). You can upload up to 10 GB on the free tier of Unity Cloud.
27+
- A Unity Cloud project with asset manager service enabled to upload assets. For more information on how to create a new project on Unity Cloud, see [Create a new project](https://docs.unity.com/cloud/en-us/asset-manager/new-asset-manager-project).
28+
- An assigned seat if you are part of an entitled organization, that is, an organization with a Pro or Enterprise license. For more information, see the [Important notes](https://docs.unity.com/cloud/en-us/asset-manager/org-project-roles#project-level-roles) section.
29+
30+
### Licenses
31+
32+
The bulk download sample script is made available under the [Unity ToS license](../LICENSE.md).
33+
34+
## How do I...?
35+
36+
### Install the tool
37+
38+
1. Navigate to the current folder with your terminal.
39+
2. Run the following help command to install the tool:
40+
* On Mac: `python3 bulk_cli.py --install`
41+
* On Windows: `python bulk_cli.py --install`
42+
43+
### Run the tool in interactive mode
44+
45+
1. Navigate to the current folder with your terminal.
46+
2. Run the following command:
47+
* On Mac: `python3 bulk_cli.py --create`
48+
* On Windows: `python bulk_cli.py --create`
49+
50+
### Select the input method
51+
52+
Select one of the three strategies as the input method for bulk asset creation:
53+
54+
1. Answer the following CLI prompt: `Are you uploading assets from a Unity project?`
55+
* Enter Yes if you upload files that are either:
56+
* In a Unity project and have .meta files from the editor
57+
or:
58+
* In a Unity package, like content from the asset store
59+
60+
2. If you answered No to the prompt in step 1, select either of the following under the `Select a strategy` prompt:
61+
62+
* `group files by name`: Select this option if your assets are following a naming convention, for example, blueasset.fbx, blueasset.png.
63+
![Using the group by name convention](./documentation/group-by-name.png)
64+
65+
* `group files by folder`: Select this option if your assets are organized by folder, that is, all relevant files are in distinct folders.
66+
![Using the group by folder convention](./documentation/group-by-folder.png)
67+
68+
## See also
69+
70+
- [Unity Cloud Python SDK documentation](https://docs.unity.com/cloud/en-us/asset-manager/python-sdk)
71+
72+
## Tell us what you think!
73+
74+
Thank you for taking a look at the project! To help us improve and provide greater value, please consider providing feedback in our [Help & Support page](https://cloud.unity.com/home/dashboard-support). Thank you!

bulk_upload_cli/bulk_cli.py

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
import argparse
2+
import platform
3+
import json
4+
from shared.utils import OperationSystem, download_wheel, pip_install_wheel, pip_install_other_libraries, \
5+
check_install_requirements, check_python_version
6+
import os
7+
8+
source_folder = "../Source"
9+
wheels_path = os.curdir + "/wheels"
10+
11+
12+
def read_arguments():
13+
parser = argparse.ArgumentParser()
14+
parser.add_argument("--install", action="store_true", help="Install the requirements for the tool")
15+
parser.add_argument("--create", action="store_true", help="Bulk create assets in the cloud")
16+
parser.add_argument("--config-write", action="store_true",
17+
help="Write the configuration file instead of running the action. Use with --create.", default=False)
18+
parser.add_argument("--config-select", action="store_true",help="Select a configuration file to run. Use with --create.", default=False)
19+
parser.add_argument("--config", type=str, help="Path to the configuration file. Use with --create.", default=None)
20+
21+
args = parser.parse_args()
22+
return args
23+
24+
25+
def get_current_os():
26+
system = platform.system()
27+
if system == "Windows":
28+
return OperationSystem.windows
29+
elif system == "Linux":
30+
return "linux"
31+
elif system == "Darwin": # macOS
32+
return OperationSystem.macos
33+
else:
34+
raise Exception("Unsupported operating system:" + system)
35+
36+
37+
def install_requirements():
38+
current_os = get_current_os()
39+
download_wheel(wheels_path, current_os, False)
40+
pip_install_wheel(wheels_path, current_os)
41+
pip_install_other_libraries()
42+
43+
44+
def run_bulk_assets_creation(interactive=False, config=None, write_config=False, config_select=False):
45+
46+
if config_select:
47+
from bulk_upload import interactive_runner
48+
interactive_runner.run_with_config_select()
49+
elif interactive or write_config:
50+
from bulk_upload import interactive_runner
51+
interactive_runner.run(write_config=write_config)
52+
else:
53+
if config is None:
54+
raise Exception("Configuration file must be provided when running in non-interactive mode.")
55+
from bulk_upload import models, assets_uploader
56+
creation_config = models.ProjectUploaderConfig()
57+
with open(config, "r") as f:
58+
creation_config.load_from_json(json.load(f))
59+
uploader = assets_uploader.ProjectUploader()
60+
uploader.run(creation_config)
61+
62+
63+
if __name__ == "__main__":
64+
arguments = read_arguments()
65+
66+
config = arguments.config
67+
write_config = arguments.config_write
68+
config_select = arguments.config_select
69+
interactive = False
70+
71+
if not check_python_version():
72+
print("Python version is not supported. Please use Python 3.9 or higher.")
73+
exit(1)
74+
75+
if arguments.install:
76+
install_requirements()
77+
print("\n\n\n")
78+
print("===============================================")
79+
print("Requirements installed.")
80+
exit(0)
81+
82+
if not check_install_requirements():
83+
print("It seems that the requirements are not installed. Please run the script with --install first")
84+
exit(1)
85+
86+
if not arguments.create and not arguments.install:
87+
print("No action specified. Please always use --create.")
88+
exit(1)
89+
90+
if config is None and not write_config and not config_select:
91+
print("No config options provided. Interactive mode will be used.")
92+
interactive = True
93+
94+
if config is not None and write_config:
95+
raise Exception("Both --config and --write-config cannot be used at the same time.")
96+
97+
if config is not None and not os.path.exists(config):
98+
raise Exception("Configuration file not found.")
99+
100+
if arguments.create:
101+
run_bulk_assets_creation(interactive, config, write_config, config_select)
102+
else:
103+
print("No action specified. Please always use --create.")

bulk_upload_cli/bulk_upload/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)