Skip to content

load_camera

AlembicCameraLoader

Bases: LoaderPlugin

This will load a camera into script.

Source code in client/ayon_nuke/plugins/load/load_camera.py
 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
class AlembicCameraLoader(load.LoaderPlugin):
    """
    This will load a camera into script.
    """

    product_types = {"camera"}
    representations = {"*"}
    extensions = {"abc"}
    label = "Load Alembic Camera"
    enabled = True

    settings_category = "nuke"

    icon = "camera"
    color = "orange"
    node_color = "0x3469ffff"

    def load(self, context, name, namespace, data):
        # get main variables
        version_entity = context["version"]

        version_attributes = version_entity["attrib"]
        first = version_attributes.get("frameStart")
        last = version_attributes.get("frameEnd")
        fps = version_attributes.get("fps") or nuke.root()["fps"].getValue()

        namespace = namespace or context["folder"]["name"]
        object_name = "{}_{}".format(name, namespace)

        # prepare data for imprinting
        # add additional metadata from the version to imprint to metadata knob
        data_imprint = {
            "frameStart": first,
            "frameEnd": last,
            "version": version_entity["version"],
        }
        for k in ["source", "fps"]:
            data_imprint[k] = version_attributes[k]

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

        with maintained_selection():

            try:
                camera_node = nuke.createNode(
                    "Camera3",
                    "name {} file {} read_from_file True".format(
                        object_name, file),
                    inpanel=False
                )
            except RuntimeError: # older nuke version
                camera_node = nuke.createNode(
                    "Camera2",
                    "name {} file {} read_from_file True".format(
                        object_name, file),
                    inpanel=False
                )                

            camera_node.forceValidate()
            camera_node["frame_rate"].setValue(float(fps))

            # workaround because nuke's bug is not adding
            # animation keys properly
            xpos = camera_node.xpos()
            ypos = camera_node.ypos()
            nuke.nodeCopy("%clipboard%")
            nuke.delete(camera_node)
            nuke.nodePaste("%clipboard%")
            camera_node = nuke.toNode(object_name)
            camera_node.setXYpos(xpos, ypos)

        # color node by correct color by actual version
        self.node_version_color(
            context["project"]["name"], version_entity, camera_node
        )

        return containerise(
            node=camera_node,
            name=name,
            namespace=namespace,
            context=context,
            loader=self.__class__.__name__,
            data=data_imprint)

    def update(self, container, context):
        """
            Called by Scene Inventory when look should be updated to current
            version.
            If any reference edits cannot be applied, eg. shader renamed and
            material not present, reference is unloaded and cleaned.
            All failed edits are highlighted to the user via message box.

        Args:
            container: object that has look to be updated
            representation: (dict): relationship data to get proper
                                    representation from DB and persisted
                                    data in .json
        Returns:
            None
        """
        # Get version from io
        version_entity = context["version"]
        repre_entity = context["representation"]

        # get main variables
        version_attributes = version_entity["attrib"]
        first = version_attributes.get("frameStart")
        last = version_attributes.get("frameEnd")
        fps = version_attributes.get("fps") or nuke.root()["fps"].getValue()

        # prepare data for imprinting
        data_imprint = {
            "representation": repre_entity["id"],
            "frameStart": first,
            "frameEnd": last,
            "version": version_entity["version"]
        }

        # add attributes from the version to imprint to metadata knob
        for k in ["source", "fps"]:
            data_imprint[k] = version_attributes[k]

        # getting file path
        file = get_representation_path(repre_entity).replace("\\", "/")

        with maintained_selection():
            camera_node = container["node"]
            camera_node['selected'].setValue(True)

            # collect input output dependencies
            dependencies = camera_node.dependencies()
            dependent = camera_node.dependent()

            camera_node["frame_rate"].setValue(float(fps))
            camera_node["file"].setValue(file)

            # workaround because nuke's bug is
            # not adding animation keys properly
            xpos = camera_node.xpos()
            ypos = camera_node.ypos()
            nuke.nodeCopy("%clipboard%")
            camera_name = camera_node.name()
            nuke.delete(camera_node)
            nuke.nodePaste("%clipboard%")
            camera_node = nuke.toNode(camera_name)
            camera_node.setXYpos(xpos, ypos)

            # link to original input nodes
            for i, input in enumerate(dependencies):
                camera_node.setInput(i, input)
            # link to original output nodes
            for d in dependent:
                index = next((i for i, dpcy in enumerate(
                              d.dependencies())
                              if camera_node is dpcy), 0)
                d.setInput(index, camera_node)

        # color node by correct color by actual version
        self.node_version_color(
            context["project"]["name"], version_entity, camera_node
        )

        self.log.info(
            "updated to version: {}".format(version_entity["version"])
        )

        return update_container(camera_node, data_imprint)

    def node_version_color(self, project_name, version_entity, node):
        """ Coloring a node by correct color by actual version
        """
        # get all versions in list
        last_version_entity = ayon_api.get_last_version_by_product_id(
            project_name, version_entity["productId"], fields={"id"}
        )

        # change color of node
        if version_entity["id"] == last_version_entity["id"]:
            color_value = self.node_color
        else:
            color_value = "0xd88467ff"
        node["tile_color"].setValue(int(color_value, 16))

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

    def remove(self, container):
        node = container["node"]
        with viewer_update_and_undo_stop():
            nuke.delete(node)

node_version_color(project_name, version_entity, node)

Coloring a node by correct color by actual version

Source code in client/ayon_nuke/plugins/load/load_camera.py
187
188
189
190
191
192
193
194
195
196
197
198
199
200
def node_version_color(self, project_name, version_entity, node):
    """ Coloring a node by correct color by actual version
    """
    # get all versions in list
    last_version_entity = ayon_api.get_last_version_by_product_id(
        project_name, version_entity["productId"], fields={"id"}
    )

    # change color of node
    if version_entity["id"] == last_version_entity["id"]:
        color_value = self.node_color
    else:
        color_value = "0xd88467ff"
    node["tile_color"].setValue(int(color_value, 16))

update(container, context)

Called by Scene Inventory when look should be updated to current
version.
If any reference edits cannot be applied, eg. shader renamed and
material not present, reference is unloaded and cleaned.
All failed edits are highlighted to the user via message box.

Parameters:

Name Type Description Default
container

object that has look to be updated

required
representation

(dict): relationship data to get proper representation from DB and persisted data in .json

required

Returns: None

Source code in client/ayon_nuke/plugins/load/load_camera.py
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
def update(self, container, context):
    """
        Called by Scene Inventory when look should be updated to current
        version.
        If any reference edits cannot be applied, eg. shader renamed and
        material not present, reference is unloaded and cleaned.
        All failed edits are highlighted to the user via message box.

    Args:
        container: object that has look to be updated
        representation: (dict): relationship data to get proper
                                representation from DB and persisted
                                data in .json
    Returns:
        None
    """
    # Get version from io
    version_entity = context["version"]
    repre_entity = context["representation"]

    # get main variables
    version_attributes = version_entity["attrib"]
    first = version_attributes.get("frameStart")
    last = version_attributes.get("frameEnd")
    fps = version_attributes.get("fps") or nuke.root()["fps"].getValue()

    # prepare data for imprinting
    data_imprint = {
        "representation": repre_entity["id"],
        "frameStart": first,
        "frameEnd": last,
        "version": version_entity["version"]
    }

    # add attributes from the version to imprint to metadata knob
    for k in ["source", "fps"]:
        data_imprint[k] = version_attributes[k]

    # getting file path
    file = get_representation_path(repre_entity).replace("\\", "/")

    with maintained_selection():
        camera_node = container["node"]
        camera_node['selected'].setValue(True)

        # collect input output dependencies
        dependencies = camera_node.dependencies()
        dependent = camera_node.dependent()

        camera_node["frame_rate"].setValue(float(fps))
        camera_node["file"].setValue(file)

        # workaround because nuke's bug is
        # not adding animation keys properly
        xpos = camera_node.xpos()
        ypos = camera_node.ypos()
        nuke.nodeCopy("%clipboard%")
        camera_name = camera_node.name()
        nuke.delete(camera_node)
        nuke.nodePaste("%clipboard%")
        camera_node = nuke.toNode(camera_name)
        camera_node.setXYpos(xpos, ypos)

        # link to original input nodes
        for i, input in enumerate(dependencies):
            camera_node.setInput(i, input)
        # link to original output nodes
        for d in dependent:
            index = next((i for i, dpcy in enumerate(
                          d.dependencies())
                          if camera_node is dpcy), 0)
            d.setInput(index, camera_node)

    # color node by correct color by actual version
    self.node_version_color(
        context["project"]["name"], version_entity, camera_node
    )

    self.log.info(
        "updated to version: {}".format(version_entity["version"])
    )

    return update_container(camera_node, data_imprint)

FbxCameraLoader

Bases: AlembicCameraLoader

This will load fbx camera into script.

Source code in client/ayon_nuke/plugins/load/load_camera.py
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
class FbxCameraLoader(AlembicCameraLoader):
    """
    This will load fbx camera into script.
    """
    extensions = {"fbx"}
    label = "Load FBX Camera"

    def load(self, context, name, namespace, data):
        fbx_camera_node = super().load(context, name, namespace,data)

        # Nuke forces 7 standard FBX cameras
        # Producer Perspective, Producer Top, Producer Bottom...
        # https://learn.foundry.com/nuke/11.1/content/comp_environment
        # /3d_compositing/importing_fbx_cameras.html
        # The following ensure the camera node is set to exported camera data.
        fbx_camera_node.forceValidate() 

        try:
            knob = fbx_camera_node["fbx_take_name"]
            knob.setValue(0)

        except NameError:
            # WARNING - Nuke 15: Camera3 validate does not play along
            # very well when mixing abc + fbx nodes in the same script.
            # They need to reloaded manually.
            #
            # Hopefully this will be fixed by upcoming Camera4            
            raise RuntimeError(
                "Could not load incoming FBX camera properly. "
                "This could be caused by a mix of abc and fbx "
                "into the script."
            )

        knob = fbx_camera_node["fbx_node_name"]
        knob.setValue(knob.values()[-1])

        return fbx_camera_node