# Manage assets

> How to add and manage assets with Python SDK.

## Before you begin

To access any assets, make sure the app has the right permissions. Read more about [managing Identity](/asset-manager/python-sdk/manage-identity.md).

> **Note:**
>
> The following code snippets first require an initialization. To learn more about the `initialize()` and `uninitialize()` methods, read about [getting started with the Unity Cloud Python SDK](/asset-manager/python-sdk/get-started.md).

## Create an asset

To add an asset into a project, follow these steps:

1. Use the method `create_asset` by passing all the information inside `AssetCreation`.
2. At least define `Name` and `Type` values to create an asset. The possible values for `Type` are:
   * `2D Asset`
   * `3D Model`
   * `Audio`
   * `Material`
   * `Script`
   * `Video`
   * `Unity Editor`
   * `Other`

The created asset is saved as a draft.

```python
    asset_creation = AssetCreation(
        name = "your-asset-name",
        description = "your-asset-description",
        type = unity_cloud.assets.AssetType.MODEL_3D,
        tags = ["your-tag-1", "your-tag-2", "your-tag-3"]
    )

    asset = unity_cloud.assets.create_asset(
        asset_creation = asset_creation,
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef"
    )
```

## Update an asset

To change the name, the description, the type, the tags, or the file used for the preview you need to:

1. Create an `AssetUpdate` method.
2. Call the `update_asset` method.

```python
    asset_update = AssetUpdate(
        name = "your-asset-name",
        description = "your-asset-description",
        type = unity_cloud.assets.AssetType.MODEL_3D,
        tags = ["your-tag-1", "your-tag-2", "your-tag-3"],
        preview_file = "Path/To/File.ext"
    )

    unity_cloud.assets.update_asset(
        asset_update = asset_update,
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = '1234abcd-ab12-cd34-ef56-123456abcdef'
    )
```

## Get an asset

To fetch a single asset, call `get_asset`.

```python
    asset = unity_cloud.assets.get_asset(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = '1234abcd-ab12-cd34-ef56-123456abcdef'
    )
```

## Get an asset by its label

When the version of the asset is not known, you can fetch an asset by its label using the `get_asset_by_label` method. Keep the `label` parameter empty to fetch the default version of the asset.

```python
    asset = unity_cloud.assets.get_asset_by_label(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        label = "your-asset-label"
    )
```

## Get a list of all assets within a project

To fetch all assets contained in a given project, call `get_asset_list`.

```python
    assets = unity_cloud.assets.get_asset_list(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef"
    )
```

## Perform an advanced search for assets

To search for assets across an organization, follow these steps:

1. Create your filters as dictionnaries or objects that will contain the conditions.
2. Create an array that will contain the projects in which to look up. Leave the array empty to search across all projects in the organization.
3. Create an array that will contain the collections in which to look up. Leave the array empty to search across all collections and assets not linked to a collection.
4. Call `search_assets_in_projects` with the organization ID, your project IDs, your filters, the amount of matches required in the any\_filter, the collections, and optionally the maximum number of results returned and the amount to skip for paging purpose.

```python
include_filter = dict()
exclude_filter = dict()
any_filter = dict()

include_filter[SearchableProperties.NAME] = StringFilter(StringLookUpTypes.REGEX, "asset\-.*")

project_ids = []
collections = []

search_result = unity_cloud.assets.search_assets_in_projects(org_id, project_ids, include_filter = include_filter, exclude_filter = exclude_filter, any_filter = any_filter, any_query_minimum_match = 1, collections = collections, limit_to = 10, skip = 0)
```

## Create filter for a search query

To create an `include_filter`, an `exclude_filter`, or an `any_filter`, you can use the enum `SearchableProperties` as a helper. This enum is provided with a comment on how each each properties should be created. You are not limited to properties in this enum and can write your own, as long as the properties you're writing exist either in an asset, dataset, or file.

To create a filter on a string property, you can either write a simple equal condition, or use an advanced `StringFilter`.

```python
include_filter = dict()
include_filter[SearchableProperties.DESCRIPTION] = "Description must be exactly equals to this"
include_filter[SearchableProperties.NAME] = StringFilter(StringLookUpTypes.wildcard, "incompl*te n*me")
```

To create a filter on a date property, you can either write a simple equal condition, or use an advanced `DateRangeCondition`. Date must be passed.

```python
date_to_compare = datetime.now().isoformat()

exclude_filter = dict()
exclude_filter[SearchableProperties.DATASETS_CREATED] = date_to_compare
data_condition = DateRangeCondition(RangeConditionTypes.LESS_THAN, date_to_compare)
date_filter = DateRangeFilter([numeric_condition])
exclude_filter[SearchableProperties.FILES_UPDATED] = DateRangeFilter([data_condition])
```

To search using a custom metadata field, write `metadata.<your-metadata-key>` as the property. To find the key of a custom metadata field, refer to [metadata documentation](/asset-manager/python-sdk/manage-metadata.md).
Complex filters cannot be used with custom metadata fields, only simple equal conditions.

```python

exclude_filter = dict()
exclude_filter["metadata.MyCustomMetadataFieldKey"] = "expected-value"
```

## Link assets to a project

To link assets to multiple projects, use the `link_assets_to_project` method with the `project_id` argument.

```python
    unity_cloud.assets.link_asset_to_project(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_ids = ["0123456789abcdefghijklmn"],
        new_project_id = "4567efgh-ab12-cd34-ef56-123456abcdef"
    )
```

## Remove assets link to a project

To remove assets link to multiple projects, use the `unlink_assets_from_project` method with the `project_id` argument.

```python

    unity_cloud.assets.unlink_asset_from_project(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_ids = ["0123456789abcdefghijklmn"]
    )
```

## Get an asset preview file URL

To get the URL of the preview file of an asset, use the `get_preview_file_url` method.

```python
    preview_file_url = unity_cloud.assets.get_preview_file_url(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = '1234abcd-ab12-cd34-ef56-123456abcdef'
    )
```

## Add an asset reference to an asset

To add a reference to an asset, use the `add_asset_reference` method. You can either add a reference by using an asset version or a version label assigned to it.

```python
    unity_cloud.assets.add_asset_reference(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        source_asset_id = "0123456789abcdefghijklmn",
        source_asset_version = '1234abcd-ab12-cd34-ef56-123456abcdef',
        target_asset_id = "4567efgh-ab12-cd34-ef56-123456abcdef",
        target_asset_version = '4567efgh-ab12-cd34-ef56-123456abcdef',
        target_asset_label = "your-asset-label"
    )
```

## List the references of an asset

To list the references of an asset, use the `list_asset_references` method. The `context` parameter can be  `Context.SOURCE`, `Context.TARGET` or `Context.BOTH` if you want everything.

```python
    references = unity_cloud.assets.list_asset_references(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = '1234abcd-ab12-cd34-ef56-123456abcdef',
        context = Context.SOURCE
    )
```

## Remove an asset reference

Using the result of the previous method, you can remove a reference to an asset by using the `remove_asset_reference` method with the id of a reference.

```python
    unity_cloud.assets.remove_asset_reference(
        org_id = "012345678912",
        project_id = "1234abcd-ab12-cd34-ef56-123456abcdef",
        asset_id = "0123456789abcdefghijklmn",
        asset_version = '1234abcd-ab12-cd34-ef56-123456abcdef',
        reference_id = "4567efgh-ab12-cd34-ef56-123456abcdef"
    )
```
