0

as_template_fields command

Hi Guys

Get a problem with this command. 

I try to retrieve my context variables from a work file path.

lets say my file is here

path = '/s/prods/shotgun_test/projects/sahara_test/assets/char/Totor/maya/scenes/mod/gdubost/char-Totor-base-zbr-v001.ma'

so i do 

import sgtk
tk = sgtk.sgtk_from_path( path )
context = tk.context_from_path(path)

my context is

# Result: <Sgtk Context: Project: {'type': 'Project', 'id': 73, 'name': 'Sahara Test'}
Entity: {'type': 'Asset', 'id': 842, 'name': 'Totor'}
Step: {'type': 'Step', 'id': 10, 'name': 'Model'}
Task: None
User: {'type': 'HumanUser', 'id': 39, 'name': 'gdubost'}
Shotgun URL: https://mikrosca.shotgunstudio.com/detail/Asset/842
Additional Entities: []> #
tpl = tk.template_from_path(path)

<Sgtk TemplatePath maya_asset_work: assets/{sg_asset_type}/{Asset}/maya/scenes/{Step}/{user}/{sg_asset_type}-{Asset}-{asset_variant}-{task_name}-v{version}.ma> 

when i try the following

context.as_template_fields(tpl)

I got 

# Error: TankError: file /s/prods/shotgun_test/software/sahara_test_dev_gdubost/install/core/python/tank/context.py line 445: Key '<Sgtk StringKey task_name>' in template '<Sgtk TemplatePath maya_asset_work: assets/{sg_asset_type}/{Asset}/maya/scenes/{Step}/{user}/{sg_asset_type}-{Asset}-{asset_variant}-{task_name}-v{version}.ma>' could not be populated by context 'Model, Asset Totor' because the context does not contain a shotgun entity of type 'Task'! #

 

the "task_name" has been declared in my asset_step.yml like that

task_name:
  type: str
  shotgun_entity_type: Task
  shotgun_field_name: sg_task_code #where sg_task_code is a new field declared in the Task entity

So i dont undertsand why the script not able to get task_name='zbr' in my exemple

Thx for helping

Cheers

Guy

9 comments

  • 0
    Avatar
    Guy Dubost

    i got the result i wished with 

    tpl.get_fields(path)

    but curious about what i was doing wrong with context.as_template_fields(tpl)

     

  • 0
    Avatar
    Julien Dubuisson

    Hi Guy, nice to see you here!

    When you're doing context.as_template_fields(tpl), you are trying to fill the template tokens with fields extracted from your context. If your context doesn't have a Task it's totally normal that the function fails. Try again with a task set.

    Doing tpl.get_fields(path), you are extracting the fields from your path so if your path is valid you will get the right fields

  • 0
    Avatar
    Guy Dubost

    Hi Julien,

    thats weird cause if I change the template

    tpl = tk.templates['asset_work_area_maya']
    # Result: <Sgtk TemplatePath asset_work_area_maya: assets/{sg_asset_type}/{Asset}/maya/scenes/{Step}/{user}> # 
    context.as_template_fields(tpl)
    # Result: {'Asset': u'Totor',
    'Step': u'mod',
    'sg_asset_type': u'char',
    'user': 'gdubost'} #

    it doesnt fail 

  • 0
    Avatar
    Julien Dubuisson

    Yeah but it looks like your asset_work_area_maya template doesn't needs the Task to be set.

    All you need is sg_asset_type, Asset, Step and user which are all defined in your context

  • 0
    Avatar
    Guy Dubost

    The fact that Task is not in the context is a problem in this case

    I tried to remove task_name from my path and from the templates.yml

    so now i got

    path = '/s/prods/shotgun_test/projects/sahara_test/assets/char/Totor/maya/scenes/mod/gdubost/char-Totor-base-v001.ma' (no zbr task_name)

    tk = sgtk.sgtk_from_path( path )
    context = tk.context_from_path(path)

    Result: <Sgtk Context: Project: {'type': 'Project', 'id': 73, 'name': 'Sahara Test'}
    Entity: {'type': 'Asset', 'id': 842, 'name': 'Totor'}
    Step: {'type': 'Step', 'id': 10, 'name': 'Model'}
    Task: None
    User: {'type': 'HumanUser', 'id': 39, 'name': 'gdubost'}
    Shotgun URL: https://mikrosca.shotgunstudio.com/detail/Asset/842
    Additional Entities: []> #

     

    then 

    tpl = tk.template_from_path(path)
    # Result: <Sgtk TemplatePath maya_asset_work: assets/{sg_asset_type}/{Asset}/maya/scenes/{Step}/{user}/{sg_asset_type}-{Asset}-{asset_variant}-v{version}.ma> #  (i removed task_name from templates.yml)

    and with succes i got 

    tpl.get_fields(path)
    # Result: {'Asset': 'Totor',
    'Step': 'mod',
    'asset_variant': 'base',
    'sg_asset_type': 'char',
    'user': 'gdubost',
    'version': 1} #

    So thats weird that task_name , if present , requires Task in the context. I would expect task_name retrieved from the path even if Task is None.

    Is that linked to my key definition of task_name in ther templates.yml ?

    task_name:
      type: str
      shotgun_entity_type: Task
      shotgun_field_name: sg_task_code
  • 0
    Avatar
    Julien Dubuisson

    Ah right I didn't see you were building the context from the path.

    So yeah there's something wrong in the context_from_path which doesn't build the right context. Do you have many tasks called zbr in Shotgun? That would explain everything as you can't pick task based only on it's name if there are many of them with the same name.

  • 0
    Avatar
    Guy Dubost

    zbr is just a short code name of the task Zbrushing (part of Modeling Step)

    no we just have one Task with this name

  • 0
    Avatar
    Permanently deleted user

    Hey guys!

    KP asked me reach out and quickly check if you managed to solve this issue!

    There is some longstanding confusion about how the context operates - especially related to tasks and steps - which we are hoping to clear up at some stage.

    Basically, there are two ways to create a context object - either from a path or from a shotgun object. Typically, when you for example launch Maya from a task in Shotgun, you create a context based on that Task. This context will contain a project, an entity a step and a task. Then, when you save out a file on disk, the path will be generated (via a template) based on your current context. This is what the as_template_fields() is doing. 

    The problem arises when you don't push all the values that are stored in your current context into the path on disk. A good example is our default config, which doesn't organize items by task, but merely by step (step in this case becomes something a bit like a department, which seems like a good default setup for organizing files). So when you save your work files, those paths will contain all the context fields - except the task bit, which isn't needed for those templates.

    The problem then arises when you run context_from_path() on those paths - the context generated will not contain the task field, because this is not part of the task. This basically leads to asymmetry - you start off with a task in shotgun, set up a state (the context) inside your DCC (e.g. Maya), the save out a file. And when you save the file, you basically truncate some of the state information - meaning that trying to recover the context state from that file will only partially restore it.

    Hope that gives you a better picture of what is going on. It has been on our todo-list for quite some time to extend this code to provide a much more symmetrical experience. For now, it's a matter of getting the task into your context somehow - either by creating it based on a Task entity in Shotgun rather than from a path, or by extending the context in some way to add the task.

    Thanks!
    Manne

  • 0
    Avatar
    Guy Dubost

    Thx Manne,

    It makes sense. 

    And Hi Julien (Rodeo), didnt realize at first who you were ...;o)

     

Please sign in to leave a comment.