New File setting context too late

Hello,

I’m trying to bypass the need for artists to manually save their scene after clicking the “new file” button in tk-multi-workfiles2 for Nuke.

in scene_operation_tk-nuke.py I have this:

 elif operation == "reset":
     if parent_action == "new_file":
         _create_first_nuke_version()
         return True

where _create_first_nuke_version() is this code:

def _create_first_nuke_version():
    engine = sgtk.platform.current_engine()
    tk = engine.sgtk
    ctx = engine.context
    templates = tk.templates

    template_obj = templates["nuke_shot_work"]
    latest_work, work_version = get_latest_work(context=ctx, template_name="nuke_shot_work", tk=tk)

    work_version += 1

    fields = ctx.as_template_fields(template_obj)
    fields["version"] = work_version
    work_path = template_obj.apply_fields(fields)

    engine.ensure_folder_exists(os.path.dirname(work_path))
    sgtk.util.filesystem.touch_file(work_path)

    nuke.scriptSaveAs(work_path, 1)

it works when it’s a fresh instance of Nuke, but if I’ve already got a shot open and then want to create a new file for a different shot, the work_path that gets returned is that of the shot I was switching away from.

I did see this post:

and I feel it’s very similar issues, but I couldn’t get very far with it.

I want to defer my script until after the context has finished changing.

Any help is appreciated,
Tim

new day new brain, using a timer seems to do the trick!

def _create_first_nuke_version():
    QtCore.QTimer.singleShot(500, _do_create_first_nuke_version)
1 Like