Skip to content

control

WorkfileToolController

This is a temporary controller for AYON.

Goal of this controller is to provide a way to get current context.

Source code in client/ayon_wrap/workfiles/control.py
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
class WorkfileToolController:
    """This is a temporary controller for AYON.

    Goal of this controller is to provide a way to get current context.
    """

    def __init__(self, launch_data):
        project_name = launch_data["project_name"]
        folder_id = launch_data["folder_id"]
        task_id = launch_data["task_id"]

        folder_entity = ayon_api.get_folder_by_id(project_name, folder_id)
        if not folder_entity:
            raise RuntimeError(f"Couldn't find folder for {folder_id}")

        task_entity = ayon_api.get_task_by_id(project_name, task_id)
        if not task_entity:
            raise RuntimeError(f"Couldn't find task for {task_id}")

        project_entity = ayon_api.get_project(project_name)

        self._current_project_name = project_name
        self._current_folder_id = folder_id
        self._current_task_id = task_id
        self._current_template_name = None

        self._anatomy = None
        self._project_entity = project_entity
        self._task_entity = task_entity
        self._folder_entity = folder_entity

        self._project_settings = get_project_settings(project_name)

        # template name in Anatomy - determines location of saved workfile
        self.wrap_workfile_template_key = get_workfile_template_key(
            project_name,
            task_entity["taskType"],
            "wrap",
            project_settings=self._project_settings
        )

        self._templates = {}
        self._template_workdirs = {}

        self._event_system = self._create_event_system()

        self._workfile_info_cache = {}

    def reset(self):
        self._templates = self._get_template_paths(
            self._project_settings,
            self.current_task_name,
            self.current_task_type
        )
        self._anatomy = None

        self.emit_event("controller.reset.finished")

    @property
    def current_task_name(self):
        return self._task_entity["name"]

    @property
    def current_task_type(self):
        return self._task_entity["taskType"]

    @property
    def current_folder_id(self):
        return self._current_folder_id

    @property
    def current_template_name(self):
        return self._current_template_name

    def emit_event(self, topic, data=None, source=None):
        if data is None:
            data = {}
        self._event_system.emit(topic, data, source)

    def register_event_callback(self, topic, callback):
        self._event_system.add_callback(topic, callback)

    def get_workfile_extensions(self):
        return [".wrap"]

    def get_host_name(self):
        return "wrap"

    def set_template(self, template_name):
        self._current_template_name = template_name
        data = {
            "template_name": template_name,
            "folder_id": self.current_folder_id,
            "task_name": self.current_task_name,
            "task_type": self.current_task_type,
        }
        self.emit_event(
            "template_changed",
            data=data
        )

    def get_workarea_file_items(self, template_name):
        workdir = self._template_workdirs.get(template_name)
        if workdir is None:
            workdir = self._get_workdir(template_name)
            self._template_workdirs[template_name] = workdir

        items = []
        if not os.path.exists(workdir):
            return items

        exts = self.get_workfile_extensions()
        for filename in os.listdir(workdir):
            filepath = os.path.join(workdir, filename)
            if not os.path.isfile(filepath):
                continue

            ext = os.path.splitext(filename)[1].lower()
            if ext not in exts:
                continue

            workfile_info = self._get_workfile_info(
                self._current_project_name,
                self._current_task_id,
                filepath
            )
            modified = os.path.getmtime(filepath)
            created_by = updated_by = None
            if workfile_info:
                created_by = workfile_info.created_by
                updated_by = workfile_info.updated_by
            items.append(FileItem(
                workdir,
                filename,
                modified,
                created_by,
                updated_by,
            ))
        return items

    def get_template_names(self):
        """Adds template names into templates_widget"""
        return list(self._templates.keys())

    def open_workfile(self, path):
        os.environ["WRAP_WORKFILE_PATH"] = path

    def create_new_workfile(self):
        """Copies template to workarea"""
        if not self._current_template_name:
            raise RuntimeError("No template chosen yet!")

        if not self._templates:
            raise RuntimeError("No templates found!")

        template_path = self._templates[self._current_template_name]

        workdir = self._template_workdirs.get(self._current_template_name)
        if not workdir:
            raise RuntimeError(
                f"Not workfile found for {self._current_template_name}"
            )

        work_filename = self._get_first_workfile_name(
            self._current_template_name)

        if not os.path.exists(workdir):
            os.makedirs(workdir, exist_ok=True)
        first_workfile_path = os.path.join(workdir, work_filename)
        shutil.copy(template_path, first_workfile_path)

        os.environ["WRAP_WORKFILE_PATH"] = first_workfile_path

    def _create_event_system(self):
        return QueuedEventSystem()

    def _get_current_anatomy(self):
        if self._anatomy is not None:
            return self._anatomy
        # Duplicate project so we can modify it
        project_entity = copy.deepcopy(self._project_entity)

        self._anatomy = Anatomy(
            self._current_project_name,
            project_entity=project_entity
        )
        return self._anatomy

    def _get_template_paths(self, project_settings, task_name, task_type):
        """Returns dictionary of templates and its paths."""
        templates = {}

        profile = get_multiple_templates_profile(
            project_settings,
            task_name,
            task_type
        )
        platform_name = platform.system().lower()
        for template in profile["templates"]:
            template_name = template["template_name"]
            templates[template_name] = template["path"][platform_name]

        return templates

    def _get_workfile_info(
            self, project_name, task_id, workfile_path):
        """Get DB stored info about work workfile."""
        workfile_info = self._workfile_info_cache.get(workfile_path)
        if workfile_info is not None:
            return workfile_info

        for workfile_info in ayon_api.get_workfiles_info(
            project_name,
            task_ids=[task_id],
            fields=["id", "path", "attrib", "createdBy", "updatedBy"],
        ):

            self._workfile_info_cache[workfile_path] = workfile_info
        return self._workfile_info_cache.get(workfile_path)

    def _get_template_data(self, template_name):
        project_entity = ayon_api.get_project(self._current_project_name)
        template_data = get_template_data(
            project_entity,
            self._folder_entity,
            self._task_entity,
            self.get_host_name(),
            self._project_settings
        )
        anatomy = self._get_current_anatomy()
        template_data["root"] = anatomy.roots
        template_data["template_name"] = template_name
        return template_data

    def _get_workdir(self, template_name):
        """Calculates workdir for template_name"""
        template_data = self._get_template_data(template_name)
        anatomy = self._get_current_anatomy()
        template = anatomy.get_template_item(
            "work", self.wrap_workfile_template_key, "directory")
        return template.format(template_data)

    def _get_first_workfile_name(self, template_name):
        template_data = self._get_template_data(template_name)
        ext = self.get_workfile_extensions()[0].lstrip(".")
        version = get_versioning_start(
            self._current_project_name,
            self.get_host_name(),
            task_name=self.current_task_name,
            task_type=self.current_task_type,
            project_settings=self._project_settings
        )
        template_data["version"] = version
        template_data["comment"] = ""
        template_data["ext"] = ext

        anatomy = self._get_current_anatomy()
        template = anatomy.get_template_item(
            "work", self.wrap_workfile_template_key, "file")

        return template.format(template_data)

create_new_workfile()

Copies template to workarea

Source code in client/ayon_wrap/workfiles/control.py
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
def create_new_workfile(self):
    """Copies template to workarea"""
    if not self._current_template_name:
        raise RuntimeError("No template chosen yet!")

    if not self._templates:
        raise RuntimeError("No templates found!")

    template_path = self._templates[self._current_template_name]

    workdir = self._template_workdirs.get(self._current_template_name)
    if not workdir:
        raise RuntimeError(
            f"Not workfile found for {self._current_template_name}"
        )

    work_filename = self._get_first_workfile_name(
        self._current_template_name)

    if not os.path.exists(workdir):
        os.makedirs(workdir, exist_ok=True)
    first_workfile_path = os.path.join(workdir, work_filename)
    shutil.copy(template_path, first_workfile_path)

    os.environ["WRAP_WORKFILE_PATH"] = first_workfile_path

get_template_names()

Adds template names into templates_widget

Source code in client/ayon_wrap/workfiles/control.py
160
161
162
def get_template_names(self):
    """Adds template names into templates_widget"""
    return list(self._templates.keys())