Setting an item's context in Publish2

[Edit by @philip.scadding] Moved this out from the old topic: [tk-multi-publish2] Where do I add resolving logic for a custom key?

One more question, what sort of information does the “item.context” on a publish item expect?

I want to programmatically change this but it doesn’t seem to accept any type of information I supply it.

I have tried feeding it a:

  • entity dict
  • tk.context_from_entity
  • tk.context_from_path
  • tk.context_from_path.entity

but none of them work :confused:

2 Likes

Briefly looking at the code, it doesn’t appear like the item.context setter method, applies any restrictions specifically on the type of object, though it should be a Context object.

Where are you trying to set the item.context? in the collector?
You might possibly need to set the item.allows_context_change.

1 Like

This is an excerpt of my code:

item.context_change_allowed = True
context = tk.context_from_entity(version["entity"].get("type"), version["entity"].get("id"))
item.context = context

also tried:

item.context = context.entity

And yes, setting it in the collector

2 Likes

This looks good, the second one wouldn’t work. So I guess my question becomes when you say it doesn’t accept it, what error is it throwing when using that code snippet?
Also have you logged the context value, to ensure that you got back a Context object and not None?

1 Like

It continues but the UI becomes weird and all items become some kind of thing that is#nt selectable:

(Whited out NDA info)

Commenting out only that line results in the proper behaviour and my code works fine:

1 Like

And I am getting a context item, if I use the logger to print it it outputs this:

DEBUG: Shot NDA_NDA007_0005
1 Like

AH OK, I think I might know what is going on here.
When you set a context for an item, it basically performs the whole environment check for that context, to find the right publish app settings.

So if you context is a Shot with no Task, then the core hook pick_environment.py will select the shot.yml environment file (assuming your config is default and you’ve not changed this). It will then check the current engine settings in that environment for the tk-multi-publish2 app and it’s settings, and then run any plugins found in those settings.

So it would seem that perhaps it’s jumping into the shot.yml environment and then not finding any publish app settings so the created items have no matching plugins.

I’m going to break this out into a new topic if that is OK?

3 Likes

Break it!

Okay, hmm… any way to stop it doing that?

Basically I’m using the publisher to load versions from a playlist (selected in a gui before the collector runs) and the versions are then loaded in as items. It would be nice to link them to their proper link, but if thsi will become too much trouble I can leave it out, we don’t have to manually change context for anything at this moment.

1 Like

The quick thing to do would be to add - ./includes/settings/tk-shotgun.yml to the includes section and tk-desktop: "@settings.tk-desktop.project" to the engines section of the shot.yml file.

I’ve not tested that, but I think it would work. It would basically mean that it would pick up exactly the same publish app settings when in a Shot context.

I have chatted with our team about this before, and this was the intended design, though I can appreciate it can come as unexpected behaviour.

1 Like

Actually I think that design choice is great, I am just breaking it because I am loading in already published data from Shotgun to make it do other stuff :stuck_out_tongue:

I’ll just turn the boxes off… then it just says project context, I don’t care because I still have the context from each version that I can pass along to the plugins

2 Likes

So, now I’m back to this issue…

Your solution doesn’t seem to work, it breaks the whole publisher UI. :frowning:

I simply would like to programmatically fill the publisher UI with items and figure out their context and set that.
They will all be for the same project but different Shots or Assets…

1 Like

Weird, we do this all the time.

As Philip suggested, you should probably add tk-multi-publish configuration to the given env file (e.g. shot_step.yml). This is because different environments could have different templates for the publish, etc.

1 Like

Could it have to do with the issue @philip.scadding suggested about creating items underneath a parent item?

It’s not really clear… the collected item itself has a context selection ui, which should match the context you are passing it.
Perhaps for each context you should have a separate root, indeed.
Otherwise, the other thing is to verify that tk-multi-publish2 has been configured for each environment needed - shot.yml, shot_step.yml, project.yml, etc.

Have you tried clicking the text bottom middle to see what the log says?

1 Like

I had the same problem. If I change the item context while I’m creating items, the problem happens. However, if I change it after all assets are created it works fine (despite the UI is not updated automatically). To test it, try to do it in the validade step, for example.

Unfortunately, the collector has not a post function to do this kind of thing.

1 Like