Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release 0.1.0 #3

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Changelog

All notable changes to this package will be documented in this file.

The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [0.1.0] - 2024-09-04

Initial release

Features:
- Source code of `bulk_upload_cli`
- Source code of `bulk_download_script`
5 changes: 5 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
Unity Cloud Python SDK copyright © 2023 Unity Technologies SF

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Unity Cloud Python SDK copyright © 2023 Unity Technologies SF
Unity Cloud Python SDK Samples copyright © 2024 Unity Technologies SF


Licensed under the Unity Terms of Service ( see https://unity.com/legal/terms-of-service.).

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.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,20 @@
# unity-cloud-python-sdk-samples
Public slack channel: [#uc-cs-am-python-sdk](https://unity.slack.com/messages/C04R01SGG68/) <br/>
[View this project in Backstage](https://backstage.corp.unity3d.com/catalog/default/component/unity-cloud-python-sdk-samples) <br/>
# Converting to public repository
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.
The process for that is to access, complete, and submit this [FORM](https://airtable.com/appj757BYrNIMuTBI/shriEdWiQuxWmJOku).
# Python SDK samples

This repository exposes a few samples that demonstrate the use of Python SDK in real life use-cases:

- [Bulk Upload CLI](./bulk_upload_cli/README.md)
- [Bulk Download script](./bulk_download_script/README.md)

> **Note**: This repository does not accept pull requests, review requests, or any other GitHub-hosted issue management requests.

## Licenses

The bulk Python SDK samples are made available under the [Unity ToS license](./LICENSE.md).

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
The bulk Python SDK samples are made available under the [Unity ToS license](./LICENSE.md).
The Unity Cloud Python SDK samples are made available under the [Unity ToS license](./LICENSE.md).

## See also

- [Unity Cloud Python SDK documentation](https://docs.unity.com/cloud/en-us/asset-manager/python-sdk)

## Tell us what you think!

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!
58 changes: 58 additions & 0 deletions bulk_download_script/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# Bulk download script

The sample script demonstrates how to use Python SDK to download assets from Unity Cloud Asset Manager.

To connect and find support, join the [Help & Support page](https://cloud.unity.com/home/dashboard-support)!

## Table of contents
- [Bulk download script](#bulk-download-script)
- [Table of contents](#table-of-contents)
- [Prerequisites](#prerequisites)
- [System requirements](#system-requirements)
- [Licenses](#licenses)
- [How do I run the sample ?](#how-do-i-run-the-sample-)
- [1. Edit the `bulk_download.py` script with your requirements](#1-edit-the--bulk_downloadpy-script-with-your-requirements)
- [2. Run the script](#2-run-the-script)
- [See also](#see-also)
- [Tell us what you think!](#tell-us-what-you-think)

## Prerequisites

### System requirements

To run the script, you need:
- Python installed on your machine
- An up-to-date Python SDK wheel installed ( > 0.5.0).
- 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.
- A source project with assets manager enable and assets already uploaded in it.

### Licenses

The bulk download sample script is made available under the [Unity ToS license](../LICENSE.md).

## How do I run the sample ?

To run the sample, follow these steps:

### 1. Edit the `bulk_download.py` script with your requirements

In the `main` conditional section, you must edit some information to link the sample to your project.

- org_id: Your organization id.
- project_id: Your project id.
- download_directory: must be edited with the path where the assets will be downloaded.
- 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.
- 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.
- collections: This list contains the collections to fetch the assets from. Leave it empty to search through all the assets in the project.

### 2. Run the script

With you favorite command line tool, run `python bulk_download.py` in this folder.

## See also

- [Unity Cloud Python SDK documentation](https://docs.unity.com/cloud/en-us/asset-manager/python-sdk)

## Tell us what you think!

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!
Empty file.
65 changes: 65 additions & 0 deletions bulk_download_script/bulk_download.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from concurrent.futures.thread import ThreadPoolExecutor

import unity_cloud as uc
from pathlib import PurePath, Path
from unity_cloud.models import *


def login_with_user_account():
uc.identity.user_login.use()
auth_state = uc.identity.user_login.get_authentication_state()
if auth_state != uc.identity.user_login.Authentication_State.LOGGED_IN:
uc.identity.user_login.login()


def download_asset(organization_id: str, project_id: str, asset: Asset, download_path: str, overwrite: bool = False):
dataset = uc.assets.get_dataset_list(organization_id, project_id, asset.id, asset.version)[0]
asset_files = uc.assets.get_file_list(organization_id, project_id, asset.id, asset.version, dataset.id)

with ThreadPoolExecutor(max_workers=10) as executor:
for file in asset_files:
file_download_info = FileDownloadInformation(organization_id, project_id, asset.id, asset.version,
dataset.id, file.path, PurePath(download_path))

target_file = Path(download_path) / file.path

if not overwrite and target_file.exists():
print(f"Skipping download of {file.path} as it already exists", flush=True)
continue

print(f"Downloading file: {file.path}", flush=True)
executor.submit(uc.assets.download_file, file_download_info)


def download_assets(assets: [Asset], org_id: str, project_id: str, download_path: str, overwrite: bool = False):
for asset in assets:
print(f"Downloading files for asset: {asset.name}", flush=True)
download_asset(org_id, project_id, asset, download_path, overwrite)


if __name__ == '__main__':

uc.initialize()
login_with_user_account()

org_id = '<org id>'
project_id = '<project id>'
download_directory = 'C:\\path\\to\\download\\directory\\'
overwrite = False

include_filter = dict()

#to search by status uncomment one of the following lines
#include_filter[SearchableProperties.STATUS] = "Published"
#include_filter[SearchableProperties.STATUS] = "Draft"

#to search by tags uncomment one of the following lines and replace <tag_name> with the tag you want to search for
#include_filter[SearchableProperties.TAGS] = ["<tag_name>""]
#include_filter[SearchableProperties.FILES_TAGS] = ["<tag_name>""]

collections = []
# collections = ['<collection_name>']

assets = uc.assets.search_assets_in_projects(org_id=org_id, project_ids=[project_id], include_filter=include_filter,
collections=collections)
download_assets(assets, org_id, project_id, download_directory, overwrite=overwrite)
74 changes: 74 additions & 0 deletions bulk_upload_cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Bulk upload CLI

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.

To connect and find support, join the [Help & Support page](https://cloud.unity.com/home/dashboard-support)!

## Table of contents
- [Bulk upload CLI](#bulk-upload-cli)
- [Table of contents](#table-of-contents)
- [Prerequisites](#prerequisites)
- [System requirements](#system-requirements)
- [Licenses](#licenses)
- [How do I...?](#how-do-i)
- [Install the tool](#install-the-tool)
- [Run the tool in interactive mode](#run-the-tool-in-interactive-mode)
- [Select the input method](#select-the-input-method)
- [See also](#see-also)
- [Tell us what you think!](#tell-us-what-you-think)

## Prerequisites

### System requirements

To run the script, you need:
- Python installed on your machine
- 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.
- 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).
- 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.

### Licenses

The bulk download sample script is made available under the [Unity ToS license](../LICENSE.md).

## How do I...?

### Install the tool

1. Navigate to the current folder with your terminal.
2. Run the following help command to install the tool:
* On Mac: `python3 bulk_cli.py --install`
* On Windows: `python bulk_cli.py --install`

### Run the tool in interactive mode

1. Navigate to the current folder with your terminal.
2. Run the following command:
* On Mac: `python3 bulk_cli.py --create`
* On Windows: `python bulk_cli.py --create`

### Select the input method

Select one of the three strategies as the input method for bulk asset creation:

1. Answer the following CLI prompt: `Are you uploading assets from a Unity project?`
* Enter Yes if you upload files that are either:
* In a Unity project and have .meta files from the editor
or:
* In a Unity package, like content from the asset store

2. If you answered No to the prompt in step 1, select either of the following under the `Select a strategy` prompt:

* `group files by name`: Select this option if your assets are following a naming convention, for example, blueasset.fbx, blueasset.png.
![Using the group by name convention](./documentation/group-by-name.png)

* `group files by folder`: Select this option if your assets are organized by folder, that is, all relevant files are in distinct folders.
![Using the group by folder convention](./documentation/group-by-folder.png)

## See also

- [Unity Cloud Python SDK documentation](https://docs.unity.com/cloud/en-us/asset-manager/python-sdk)

## Tell us what you think!

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!
103 changes: 103 additions & 0 deletions bulk_upload_cli/bulk_cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
import argparse
import platform
import json
from shared.utils import OperationSystem, download_wheel, pip_install_wheel, pip_install_other_libraries, \
check_install_requirements, check_python_version
import os

source_folder = "../Source"
wheels_path = os.curdir + "/wheels"


def read_arguments():
parser = argparse.ArgumentParser()
parser.add_argument("--install", action="store_true", help="Install the requirements for the tool")
parser.add_argument("--create", action="store_true", help="Bulk create assets in the cloud")
parser.add_argument("--config-write", action="store_true",
help="Write the configuration file instead of running the action. Use with --create.", default=False)
parser.add_argument("--config-select", action="store_true",help="Select a configuration file to run. Use with --create.", default=False)
parser.add_argument("--config", type=str, help="Path to the configuration file. Use with --create.", default=None)

args = parser.parse_args()
return args


def get_current_os():
system = platform.system()
if system == "Windows":
return OperationSystem.windows
elif system == "Linux":
return "linux"
elif system == "Darwin": # macOS
return OperationSystem.macos
else:
raise Exception("Unsupported operating system:" + system)


def install_requirements():
current_os = get_current_os()
download_wheel(wheels_path, current_os, False)
pip_install_wheel(wheels_path, current_os)
pip_install_other_libraries()


def run_bulk_assets_creation(interactive=False, config=None, write_config=False, config_select=False):

if config_select:
from bulk_upload import interactive_runner
interactive_runner.run_with_config_select()
elif interactive or write_config:
from bulk_upload import interactive_runner
interactive_runner.run(write_config=write_config)
else:
if config is None:
raise Exception("Configuration file must be provided when running in non-interactive mode.")
from bulk_upload import models, assets_uploader
creation_config = models.ProjectUploaderConfig()
with open(config, "r") as f:
creation_config.load_from_json(json.load(f))
uploader = assets_uploader.ProjectUploader()
uploader.run(creation_config)


if __name__ == "__main__":
arguments = read_arguments()

config = arguments.config
write_config = arguments.config_write
config_select = arguments.config_select
interactive = False

if not check_python_version():
print("Python version is not supported. Please use Python 3.9 or higher.")
exit(1)

if arguments.install:
install_requirements()
print("\n\n\n")
print("===============================================")
print("Requirements installed.")
exit(0)

if not check_install_requirements():
print("It seems that the requirements are not installed. Please run the script with --install first")
exit(1)

if not arguments.create and not arguments.install:
print("No action specified. Please always use --create.")
exit(1)

if config is None and not write_config and not config_select:
print("No config options provided. Interactive mode will be used.")
interactive = True

if config is not None and write_config:
raise Exception("Both --config and --write-config cannot be used at the same time.")

if config is not None and not os.path.exists(config):
raise Exception("Configuration file not found.")

if arguments.create:
run_bulk_assets_creation(interactive, config, write_config, config_select)
else:
print("No action specified. Please always use --create.")
Empty file.
Loading
Loading