Skip to content

load_effects

LoadEffects

Bases: LoaderPlugin

Loading colorspace soft effect exported from nukestudio

Source code in client/ayon_hiero/plugins/load/load_effects.py
 13
 14
 15
 16
 17
 18
 19
 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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
class LoadEffects(load.LoaderPlugin):
    """Loading colorspace soft effect exported from nukestudio"""

    product_types = {"effect"}
    representations = {"*"}
    extension = {"json"}

    label = "Load Effects"
    order = 0
    icon = "cc"
    color = "white"

    log = Logger.get_logger(__name__)

    def load(self, context, name, namespace, data):
        """
        Loading function to get the soft effects to particular read node

        Arguments:
            context (dict): context of version
            name (str): name of the version
            namespace (str): Folder name.
            data (dict): compulsory attribute > not used

        Returns:
            nuke node: containerised nuke node object
        """
        active_sequence = phiero.get_current_sequence()
        active_track = phiero.get_current_track(
            active_sequence, "Loaded_{}".format(name))

        # get main variables
        namespace = namespace or context["folder"]["name"]
        object_name = "{}_{}".format(name, namespace)
        clip_in = context["folder"]["attrib"]["clipIn"]
        clip_out = context["folder"]["attrib"]["clipOut"]

        data_imprint = {
            "objectName": object_name,
            "children_names": []
        }

        # getting file path
        file = self.filepath_from_context(context)
        file = file.replace("\\", "/")

        if self._shared_loading(
            file,
            active_track,
            clip_in,
            clip_out,
            data_imprint
        ):
            self.containerise(
                active_track,
                name=name,
                namespace=namespace,
                object_name=object_name,
                context=context,
                loader=self.__class__.__name__,
                data=data_imprint)

    def _shared_loading(
        self,
        file,
        active_track,
        clip_in,
        clip_out,
        data_imprint,
        update=False
    ):
        # getting data from json file with unicode conversion
        with open(file, "r") as f:
            json_f = {self.byteify(key): self.byteify(value)
                      for key, value in json.load(f).items()}

        # get correct order of nodes by positions on track and subtrack
        nodes_order = self.reorder_nodes(json_f)

        used_subtracks = {
            stitem.name(): stitem
            for stitem in phiero.flatten(active_track.subTrackItems())
        }

        loaded = False
        for index_order, (ef_name, ef_val) in enumerate(nodes_order.items()):
            new_name = "{}_loaded".format(ef_name)
            if new_name not in used_subtracks:
                effect_track_item = active_track.createEffect(
                    effectType=ef_val["class"],
                    timelineIn=clip_in,
                    timelineOut=clip_out,
                    subTrackIndex=index_order

                )
                effect_track_item.setName(new_name)
            else:
                effect_track_item = used_subtracks[new_name]

            node = effect_track_item.node()
            for knob_name, knob_value in ef_val["node"].items():
                if (
                    not knob_value
                    or knob_name == "name"
                ):
                    continue

                try:
                    # assume list means animation
                    # except 4 values could be RGBA or vector
                    if isinstance(knob_value, list) and len(knob_value) > 4:
                        node[knob_name].setAnimated()
                        for i, value in enumerate(knob_value):
                            if isinstance(value, list):
                                # list can have vector animation
                                for ci, cv in enumerate(value):
                                    node[knob_name].setValueAt(
                                        cv,
                                        (clip_in + i),
                                        ci
                                    )
                            else:
                                # list is single values
                                node[knob_name].setValueAt(
                                    value,
                                    (clip_in + i)
                                )
                    else:
                        node[knob_name].setValue(knob_value)
                except NameError:
                    self.log.warning("Knob: {} cannot be set".format(
                        knob_name))

            # register all loaded children
            data_imprint["children_names"].append(new_name)

            # make sure containerisation will happen
            loaded = True

        return loaded

    def update(self, container, context):
        """ Updating previously loaded effects
        """
        version_entity = context["version"]
        repre_entity = context["representation"]
        active_track = container["_item"]
        file = get_representation_path(repre_entity).replace("\\", "/")

        # get main variables
        name = container['name']
        namespace = container['namespace']

        # get timeline in out data
        version_attributes = version_entity["attrib"]
        clip_in = version_attributes["clipIn"]
        clip_out = version_attributes["clipOut"]

        object_name = "{}_{}".format(name, namespace)

        # Disable previously created nodes
        used_subtracks = {
            stitem.name(): stitem
            for stitem in phiero.flatten(active_track.subTrackItems())
        }
        container = phiero.get_track_ayon_data(
            active_track, object_name
        )

        loaded_subtrack_items = container["children_names"]
        for loaded_stitem in loaded_subtrack_items:
            if loaded_stitem not in used_subtracks:
                continue
            item_to_remove = used_subtracks.pop(loaded_stitem)
            # TODO: find a way to erase nodes
            self.log.debug(
                "This node needs to be removed: {}".format(item_to_remove))

        data_imprint = {
            "objectName": object_name,
            "name": name,
            "representation": repre_entity["id"],
            "children_names": []
        }

        if self._shared_loading(
            file,
            active_track,
            clip_in,
            clip_out,
            data_imprint,
            update=True
        ):
            return phiero.update_container(active_track, data_imprint)

    def reorder_nodes(self, data):
        new_order = OrderedDict()
        trackNums = [v["trackIndex"] for k, v in data.items()
                     if isinstance(v, dict)]
        subTrackNums = [v["subTrackIndex"] for k, v in data.items()
                        if isinstance(v, dict)]

        for trackIndex in range(
                min(trackNums), max(trackNums) + 1):
            for subTrackIndex in range(
                    min(subTrackNums), max(subTrackNums) + 1):
                item = self.get_item(data, trackIndex, subTrackIndex)
                if item:
                    new_order.update(item)
        return new_order

    def get_item(self, data, trackIndex, subTrackIndex):
        return {key: val for key, val in data.items()
                if isinstance(val, dict)
                if subTrackIndex == val["subTrackIndex"]
                if trackIndex == val["trackIndex"]}

    def byteify(self, input):
        """
        Converts unicode strings to strings
        It goes through all dictionary

        Arguments:
            input (dict/str): input

        Returns:
            dict: with fixed values and keys

        """

        if isinstance(input, dict):
            return {self.byteify(key): self.byteify(value)
                    for key, value in input.items()}
        elif isinstance(input, list):
            return [self.byteify(element) for element in input]
        elif isinstance(input, str):
            return str(input)
        else:
            return input

    def switch(self, container, context):
        self.update(container, context)

    def remove(self, container):
        pass

    def containerise(
        self,
        track,
        name,
        namespace,
        object_name,
        context,
        loader=None,
        data=None
    ):
        """Bundle Hiero's object into an assembly and imprint it with metadata

        Containerisation enables a tracking of version, author and origin
        for loaded assets.

        Arguments:
            track (hiero.core.VideoTrack): object to imprint as container
            name (str): Name of resulting assembly
            namespace (str): Namespace under which to host container
            object_name (str): name of container
            context (dict): Asset information
            loader (str, optional): Name of node used to produce this
                                    container.

        Returns:
            track_item (hiero.core.TrackItem): containerised object

        """

        data_imprint = {
            object_name: {
                "schema": "ayon:container-2.0",
                "id": AVALON_CONTAINER_ID,
                "name": str(name),
                "namespace": str(namespace),
                "loader": str(loader),
                "representation": context["representation"]["id"],
            }
        }

        if data:
            for k, v in data.items():
                data_imprint[object_name].update({k: v})

        self.log.debug("_ data_imprint: {}".format(data_imprint))
        phiero.set_track_ayon_tag(track, data_imprint)

byteify(input)

Converts unicode strings to strings It goes through all dictionary

Parameters:

Name Type Description Default
input dict / str

input

required

Returns:

Name Type Description
dict

with fixed values and keys

Source code in client/ayon_hiero/plugins/load/load_effects.py
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
def byteify(self, input):
    """
    Converts unicode strings to strings
    It goes through all dictionary

    Arguments:
        input (dict/str): input

    Returns:
        dict: with fixed values and keys

    """

    if isinstance(input, dict):
        return {self.byteify(key): self.byteify(value)
                for key, value in input.items()}
    elif isinstance(input, list):
        return [self.byteify(element) for element in input]
    elif isinstance(input, str):
        return str(input)
    else:
        return input

containerise(track, name, namespace, object_name, context, loader=None, data=None)

Bundle Hiero's object into an assembly and imprint it with metadata

Containerisation enables a tracking of version, author and origin for loaded assets.

Parameters:

Name Type Description Default
track VideoTrack

object to imprint as container

required
name str

Name of resulting assembly

required
namespace str

Namespace under which to host container

required
object_name str

name of container

required
context dict

Asset information

required
loader str

Name of node used to produce this container.

None

Returns:

Name Type Description
track_item TrackItem

containerised object

Source code in client/ayon_hiero/plugins/load/load_effects.py
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
def containerise(
    self,
    track,
    name,
    namespace,
    object_name,
    context,
    loader=None,
    data=None
):
    """Bundle Hiero's object into an assembly and imprint it with metadata

    Containerisation enables a tracking of version, author and origin
    for loaded assets.

    Arguments:
        track (hiero.core.VideoTrack): object to imprint as container
        name (str): Name of resulting assembly
        namespace (str): Namespace under which to host container
        object_name (str): name of container
        context (dict): Asset information
        loader (str, optional): Name of node used to produce this
                                container.

    Returns:
        track_item (hiero.core.TrackItem): containerised object

    """

    data_imprint = {
        object_name: {
            "schema": "ayon:container-2.0",
            "id": AVALON_CONTAINER_ID,
            "name": str(name),
            "namespace": str(namespace),
            "loader": str(loader),
            "representation": context["representation"]["id"],
        }
    }

    if data:
        for k, v in data.items():
            data_imprint[object_name].update({k: v})

    self.log.debug("_ data_imprint: {}".format(data_imprint))
    phiero.set_track_ayon_tag(track, data_imprint)

load(context, name, namespace, data)

Loading function to get the soft effects to particular read node

Parameters:

Name Type Description Default
context dict

context of version

required
name str

name of the version

required
namespace str

Folder name.

required
data dict

compulsory attribute > not used

required

Returns:

Type Description

nuke node: containerised nuke node object

Source code in client/ayon_hiero/plugins/load/load_effects.py
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
def load(self, context, name, namespace, data):
    """
    Loading function to get the soft effects to particular read node

    Arguments:
        context (dict): context of version
        name (str): name of the version
        namespace (str): Folder name.
        data (dict): compulsory attribute > not used

    Returns:
        nuke node: containerised nuke node object
    """
    active_sequence = phiero.get_current_sequence()
    active_track = phiero.get_current_track(
        active_sequence, "Loaded_{}".format(name))

    # get main variables
    namespace = namespace or context["folder"]["name"]
    object_name = "{}_{}".format(name, namespace)
    clip_in = context["folder"]["attrib"]["clipIn"]
    clip_out = context["folder"]["attrib"]["clipOut"]

    data_imprint = {
        "objectName": object_name,
        "children_names": []
    }

    # getting file path
    file = self.filepath_from_context(context)
    file = file.replace("\\", "/")

    if self._shared_loading(
        file,
        active_track,
        clip_in,
        clip_out,
        data_imprint
    ):
        self.containerise(
            active_track,
            name=name,
            namespace=namespace,
            object_name=object_name,
            context=context,
            loader=self.__class__.__name__,
            data=data_imprint)

update(container, context)

Updating previously loaded effects

Source code in client/ayon_hiero/plugins/load/load_effects.py
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
def update(self, container, context):
    """ Updating previously loaded effects
    """
    version_entity = context["version"]
    repre_entity = context["representation"]
    active_track = container["_item"]
    file = get_representation_path(repre_entity).replace("\\", "/")

    # get main variables
    name = container['name']
    namespace = container['namespace']

    # get timeline in out data
    version_attributes = version_entity["attrib"]
    clip_in = version_attributes["clipIn"]
    clip_out = version_attributes["clipOut"]

    object_name = "{}_{}".format(name, namespace)

    # Disable previously created nodes
    used_subtracks = {
        stitem.name(): stitem
        for stitem in phiero.flatten(active_track.subTrackItems())
    }
    container = phiero.get_track_ayon_data(
        active_track, object_name
    )

    loaded_subtrack_items = container["children_names"]
    for loaded_stitem in loaded_subtrack_items:
        if loaded_stitem not in used_subtracks:
            continue
        item_to_remove = used_subtracks.pop(loaded_stitem)
        # TODO: find a way to erase nodes
        self.log.debug(
            "This node needs to be removed: {}".format(item_to_remove))

    data_imprint = {
        "objectName": object_name,
        "name": name,
        "representation": repre_entity["id"],
        "children_names": []
    }

    if self._shared_loading(
        file,
        active_track,
        clip_in,
        clip_out,
        data_imprint,
        update=True
    ):
        return phiero.update_container(active_track, data_imprint)