Task and Entity Link from object name

Hello,

I was wondering if there is a way to fill the Task and Entity Link in the collector or tk-multi-publish2 if I already Know what the task is based on the objects name?

when I print item.context.task it returns none until I fill in the 2 fields once I fill them in It will return the proper item.context.task. I’m mainly wondering how to set the item.context.task to a vale and what value it should be possibly something like
item.context.task = [{'type': 'Task', 'id': 12345, 'name': 'Actor'}]

thank you,

2 Likes

Just found the place we do it in our code, it should be
item._context = ctx

I guess you can also assign constituents like task individually.

3 Likes

OK so I figured it out thanks for the tip @mmoshev lead me to editing the collector had to do a parent_item.context._Context__task = task which comes from a shotgun.find of the task then for the link you have to find the link that is made for the task and feed it parent_item.context._Context__entity = entity so all in all I got it working.

Thank you

5 Likes

Yeah forgot to mention this is in the collector.
Glad you made it work.

The catch is (what I found, based on your post): whenever you need to set the task for the given item you should be doing something like this:

item.context._Context__task = task

where the item is the item you need to assign the task, and task is (who would say) a task.
Thanks for this!

It is recommended, though not compulsory, to just create a new instance of the context with the task set: sgtk.Context(tk, project=orig_ctx.project, entity=orig_ctx.entity, step=orig_ctx.step, task=task).

Me again!
You need to set the context with its hidden params (at least these two): _Context__entity and _Context__task
I create it with self.sgtk.context_from_entity(____) method.
Due to the nature of my problem, I needed to immediately set the Task and the Link based on the file name as the files were selected (if possible) . So I extended the publish file hook and placed this in the accept method

       asset, task, step = self.get_asset_task_and_step_from_name(item.name, context, sg)
       if asset is not None:
            item.context = self.sgtk.context_from_entity("Asset", asset.get("id"))

            asset["name"] = asset ["code"]  # for some reason asset dict needs a name, not code
            del asset["code"]
            item.context._Context__entity = asset
            item.context._Context__source_entity = asset

            if len(task_list) > 0:
                item.context._Context__task = task_list[0]
            if step is not None:
                item.context._Context__step = step
        else:
            item.context = self.parent.context

The else branch gets back old context, in case there is an asset, task, and step for one file and there is no asset, task, and step for the following file. This way following file that has not the name that can lead to the asset and task can have default context.