Skip to content

Commit fa02e64

Browse files
author
Marcus Ottosson
committed
Add section on Template API
1 parent 459c171 commit fa02e64

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

_build.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ def on_template(template):
3636

3737
def on_block(language, block):
3838
if language == "python":
39+
if block[0].startswith("# Untested"):
40+
return "```python\n%s```" % "".join(block)
41+
3942
return on_python(block)
4043
return ""
4144

pages/2.0/reference.md

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,131 @@ PYTHONPATH = [
528528
]
529529
```
530530

531+
<br>
532+
533+
## Project Template API
534+
535+
The vast majority of data managed by Avalon is stored as files on disk, and every file has a path. The path is an integral part of any production and requires fine-grained control.
536+
537+
Avalon provides this control in the form of `templates`.
538+
539+
A template is a string of `{placeholders}`, each placeholder representing some data.
540+
541+
```python
542+
template = "{project}/{asset}/myfile_v{version:0>3}.ma"
543+
```
544+
545+
The keywords within the `{}` are known as `placeholders` and may be dynamically replaced at run-time by your code.
546+
547+
**Example**
548+
549+
On saving a file, this template could be used to construct a path.
550+
551+
```python
552+
template = "{project}/{asset}/myfile_{version:0>3}.ma"
553+
fname = template.format(
554+
project="hulk",
555+
asset="bruce",
556+
version=13
557+
)
558+
print(fname)
559+
```
560+
561+
Provided that pass the same data, the template itself can vary, whilst leaving your code intact.
562+
563+
```python
564+
template = "c:/assets/{asset}/{project}/myfile_version{version:0>2}.mb"
565+
```
566+
567+
Viola, a drastically different directory layout, with identical data and code to process it. Notice how this templates includes reference to `c:\` which not only asserts a particular operating system (Windows) but also leaves no room for an alternative "root". See [Root Template](#root-template) below for an example of how to overcome this limitation.
568+
569+
This is how Avalon works.
570+
571+
In this way, you can establish *convention* at a high-level whilst still remaning *specific* in places where you need to either read from or write to disk. More importantly, this is the aspect which allows Avalon to not make assumptions about your directory layout; it is up to you to write this template.
572+
573+
Having said that, though Avalon doesn't make assumptions about *where* data is written, it *does* make assumptions on the types of data written - referred to as the Avalon Object Model - and your directory layout must accommodate for these types at minimum.
574+
575+
- See [Object Model](#object-model) for a high-level overview of these types
576+
577+
For any project, Avalon assumes a `work` template to be available in your [Project Configuration](#project-configuration-api). This template is used to generate the initial directory layout for your work-in-progress files.
578+
579+
The layout and ordering of placeholders in this template is within your control, and these are the ones available to you.
580+
581+
```json
582+
{
583+
"root": "Currently registered root, e.g. 'c:/projects'",
584+
"project": "Current project, e.g. 'hulk'",
585+
"silo": "Current silo, e.g. 'assets', 'film', 'episodes'",
586+
"asset": "Current asset, e.g. 'Bruce'",
587+
"task": "Current task",
588+
"app": "Current app, e.g. 'maya2019'",
589+
"user": "Current user, e.g. 'rick'"
590+
}
591+
```
592+
593+
Here's an example of a `work` template.
594+
595+
```python
596+
template = "{root}/{project}/{silo}/{asset}/work/{task}/{user}/{app}"
597+
```
598+
599+
- See [Project Configuration API](#project-configuration-api) for more.
600+
601+
### Template Access
602+
603+
Templates are generally stored in your [project configuration](#project-configuration-api).
604+
605+
```python
606+
# Untested
607+
from avalon import io
608+
project = io.find_one({"type": "project"})
609+
template = project["config"]["template"]["work"]
610+
```
611+
612+
### Root Template
613+
614+
Every path has a "root"; the first part of a path.
615+
616+
```
617+
c:\
618+
/root
619+
/projects/
620+
```
621+
622+
When a template includes a root explicitly, like any of the above, then a template is limited to (1) a particular platform and (2) a particular mount on that platform.
623+
624+
For example, if your studio is exclusively on Windows and all share the drive `z:\`, then it is safe to embed this into your template.
625+
626+
```python
627+
template = "z:/projects/{project}/{asset}/..."
628+
```
629+
630+
However, when there are multiple operating systems and/or multiple mount points - e.g. `z:\` on some machines and and `x:\` on others - then the above template no longer works.
631+
632+
To work around this, Avalon provides the idea of a "root" directory.
633+
634+
```python
635+
# Windows box, from our London branch
636+
from avalon import api
637+
api.register_root(r"c:\projects")
638+
```
639+
640+
At run-time, a root directory is registered relative your platform and mount point, such that your template can look like..
641+
642+
```python
643+
template = "{root}/{project}{asset}..."
644+
```
645+
646+
Such that, on another platform or machine, the root registration can change to account for a differing root.
647+
648+
```python
649+
# Linux box, in LA
650+
from avalon import api
651+
api.register_root("/mnt/projects")
652+
```
653+
654+
As a result, your folder creation and publishing code can remain as-is, whilst assets are being read and written all over the place.
655+
531656
<br>
532657
<br>
533658
<br>

0 commit comments

Comments
 (0)