Skip to content

extract_camera

ExtractCamera

Bases: Extractor

3D camera extractor

Source code in client/ayon_nuke/plugins/publish/extract_camera.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
class ExtractCamera(publish.Extractor):
    """ 3D camera extractor
    """
    label = 'Extract Camera'
    order = pyblish.api.ExtractorOrder
    families = ["camera"]
    hosts = ["nuke"]

    settings_category = "nuke"

    def _get_camera_export_presets(self, instance):
        """
        Args:
            instance (dict): The current instance being published.

        Returns:
            list: The camera export presets to use.
        """
        write_geo_knobs = [
            ("writeGeometries", False),
            ("writePointClouds", False),
            ("writeAxes", False)
        ]

        publish_settings = get_publish_config()
        extract_camera_settings = publish_settings.get("ExtractCameraFormat", {})
        export_camera_settings = extract_camera_settings.get("export_camera_format", "abc")

        if export_camera_settings == "abc":
            write_geo_knobs.insert(0, ("file_type", "abc"))
            write_geo_knobs.append((("storageFormat", "Ogawa")))

        elif export_camera_settings == "fbx":
            write_geo_knobs.insert(0, ("file_type", "fbx"))
            write_geo_knobs.append(("writeLights", False))

        else:
            raise ValueError(
                f"Invalid Camera export format: {export_camera_settings}"
            )

        return write_geo_knobs

    def process(self, instance):

        # pass staging dir data
        staging_dir = instance.data.get("stagingDir")
        if not staging_dir:
            staging_dir = os.path.normpath(
                os.path.dirname(instance.data["path"]))
            instance.data["stagingDir"] = staging_dir

        camera_node = instance.data["transientData"]["node"]
        handle_start = instance.context.data["handleStart"]
        handle_end = instance.context.data["handleEnd"]
        first_frame = int(nuke.root()["first_frame"].getValue())
        last_frame = int(nuke.root()["last_frame"].getValue())
        step = 1
        output_range = str(nuke.FrameRange(first_frame, last_frame, step))

        rm_nodes = []
        self.log.debug("Creating additional nodes for 3D Camera Extractor")
        product_name = instance.data["productName"]

        # get extension form preset
        export_presets = self._get_camera_export_presets(instance)
        extension = next((k[1] for k in export_presets
                          if k[0] == "file_type"), None)
        if not extension:
            raise RuntimeError(
                "Bad config for extension in presets. "
                "Talk to your supervisor or pipeline admin")

        # create file name and path
        filename = product_name + ".{}".format(extension)
        file_path = os.path.join(staging_dir, filename).replace("\\", "/")

        with maintained_selection():
            # bake camera with axeses onto word coordinate XYZ
            rm_n = bakeCameraWithAxeses(
                camera_node, output_range)
            rm_nodes.append(rm_n)

            # create scene node
            rm_n = nuke.createNode("Scene")
            rm_nodes.append(rm_n)

            # create write geo node
            wg_n = nuke.createNode("WriteGeo")
            wg_n["file"].setValue(file_path)
            # add path to write to
            for k, v in export_presets:
                wg_n[k].setValue(v)
            rm_nodes.append(wg_n)

            # write out camera
            nuke.execute(
                wg_n,
                int(first_frame),
                int(last_frame)
            )
            # erase additional nodes
            for n in rm_nodes:
                nuke.delete(n)

        # create representation data
        if "representations" not in instance.data:
            instance.data["representations"] = []

        representation = {
            'name': extension,
            'ext': extension,
            'files': filename,
            "stagingDir": staging_dir,
            "frameStart": first_frame,
            "frameEnd": last_frame
        }
        instance.data["representations"].append(representation)

        instance.data.update({
            "path": file_path,
            "outputDir": staging_dir,
            "ext": extension,
            "handleStart": handle_start,
            "handleEnd": handle_end,
            "frameStart": first_frame + handle_start,
            "frameEnd": last_frame - handle_end,
            "frameStartHandle": first_frame,
            "frameEndHandle": last_frame,
        })

        self.log.debug("Extracted instance '{0}' to: {1}".format(
            instance.name, file_path))

bakeCameraWithAxeses(camera_node, output_range)

Baking all perent hierarchy of axeses into camera with transposition onto word XYZ coordinance

Source code in client/ayon_nuke/plugins/publish/extract_camera.py
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
def bakeCameraWithAxeses(camera_node, output_range):
    """ Baking all perent hierarchy of axeses into camera
    with transposition onto word XYZ coordinance
    """
    bakeFocal = False
    bakeHaperture = False
    bakeVaperture = False

    camera_matrix = camera_node['world_matrix']

    new_cam_n = nuke.createNode("Camera2")
    new_cam_n.setInput(0, None)
    new_cam_n['rotate'].setAnimated()
    new_cam_n['translate'].setAnimated()

    old_focal = camera_node['focal']
    if old_focal.isAnimated() and not (old_focal.animation(0).constant()):
        new_cam_n['focal'].setAnimated()
        bakeFocal = True
    else:
        new_cam_n['focal'].setValue(old_focal.value())

    old_haperture = camera_node['haperture']
    if old_haperture.isAnimated() and not (
            old_haperture.animation(0).constant()):
        new_cam_n['haperture'].setAnimated()
        bakeHaperture = True
    else:
        new_cam_n['haperture'].setValue(old_haperture.value())

    old_vaperture = camera_node['vaperture']
    if old_vaperture.isAnimated() and not (
            old_vaperture.animation(0).constant()):
        new_cam_n['vaperture'].setAnimated()
        bakeVaperture = True
    else:
        new_cam_n['vaperture'].setValue(old_vaperture.value())

    new_cam_n['win_translate'].setValue(camera_node['win_translate'].value())
    new_cam_n['win_scale'].setValue(camera_node['win_scale'].value())

    for x in nuke.FrameRange(output_range):
        math_matrix = nuke.math.Matrix4()
        for y in range(camera_matrix.height()):
            for z in range(camera_matrix.width()):
                matrix_pointer = z + (y * camera_matrix.width())
                math_matrix[matrix_pointer] = camera_matrix.getValueAt(
                    x, (y + (z * camera_matrix.width())))

        rot_matrix = nuke.math.Matrix4(math_matrix)
        rot_matrix.rotationOnly()
        rot = rot_matrix.rotationsZXY()

        new_cam_n['rotate'].setValueAt(math.degrees(rot[0]), x, 0)
        new_cam_n['rotate'].setValueAt(math.degrees(rot[1]), x, 1)
        new_cam_n['rotate'].setValueAt(math.degrees(rot[2]), x, 2)
        new_cam_n['translate'].setValueAt(
            camera_matrix.getValueAt(x, 3), x, 0)
        new_cam_n['translate'].setValueAt(
            camera_matrix.getValueAt(x, 7), x, 1)
        new_cam_n['translate'].setValueAt(
            camera_matrix.getValueAt(x, 11), x, 2)

        if bakeFocal:
            new_cam_n['focal'].setValueAt(old_focal.getValueAt(x), x)
        if bakeHaperture:
            new_cam_n['haperture'].setValueAt(old_haperture.getValueAt(x), x)
        if bakeVaperture:
            new_cam_n['vaperture'].setValueAt(old_vaperture.getValueAt(x), x)

    return new_cam_n