Skip to content

Custom Regex SGTK template path examples ​

As main example we will take this input string:

txt
assets/{sg_asset_type}/{Asset}/{Step}/{Task}/work/katana/{Asset}[.{name}][.{usage}].v{version}.katana

This is string is use in sgtk, to define the path to a katana scene file. It is used to define the path to the scene file, and to extract the metadata from the path.

Get all fields keyword ​

Fields, are the keyword that are used to extract metadata from the path. In this example, the fields are:

txt
{sg_asset_type}
{Asset}
{Step}
{Task}
{name}
{usage}
{version}

To get all fields, we can use the following regex:

python
import re

input_string = "assets/{sg_asset_type}/{Asset}/{Step}/{Task}/work/katana/{Asset}[.{name}][.{usage}].v{version}.katana"

# get all static keyword
all_keyword = re.findall(r"{(\w*)}", input_string)
print(all_keyword) # ['sg_asset_type', 'Asset', 'Step', 'Task', 'Asset', 'name', 'usage', 'version']

Get all optional keyword ​

Optional keyword are keyword that are between square bracket. In this example, the optional keyword are:

txt
{name}
{usage}

To get all optional keyword, we can use the following regex:

python
import re

input_string = "assets/{sg_asset_type}/{Asset}/{Step}/{Task}/work/katana/{Asset}[.{name}][.{usage}].v{version}.katana"

# get all optional keyword
optional_keyword = re.findall(r"\[\.*{(.\w*)\}\]", input_string)
print(optional_keyword) # ['name', 'usage']

Get required keyword ​

Required keyword are keyword that are not between square bracket. In this example, the required keyword are:

txt
{sg_asset_type}
{Asset}
{Step}
{Task}
{version}

To get all required keyword, we can use the following regex:

python
import re

input_string = "assets/{sg_asset_type}/{Asset}/{Step}/{Task}/work/katana/{Asset}[.{name}][.{usage}].v{version}.katana"

# get all keyword
all_keyword = re.findall(r"{(\w*)}", input_string)
optional_keyword = re.findall(r"\[\.*{(.\w*)\}\]", input_string)
print([key for key in all_keyword if key not in optional_keyword]) # ['sg_asset_type', 'Asset', 'Step', 'Task', 'version']

Get static word ​

Static word are just simple word without any bracket. In this example, the static word are:

txt
assets
work
katana
v
katana

To get all static word, we can use the following regex:

python
import re

input_string = "assets/{sg_asset_type}/{Asset}/{Step}/{Task}/work/katana/{Asset}[.{name}][.{usage}].v{version}.katana"

# get all static keyword
static_word_groups = re.findall(r"[\.](v)|(\w+)[\/]|[\/]?(\w+)$", input_string)

# flatten the list
static_keyword = [field for group in static_word_groups for field in group if field]
print(static_word) # ['assets', 'work', 'katana', 'v', 'katana']