Skip to content

load_image_plane

ImagePlaneLoader

Bases: Loader

Specific loader of plate for image planes on selected camera.

Source code in client/ayon_maya/plugins/load/load_image_plane.py
 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
class ImagePlaneLoader(plugin.Loader):
    """Specific loader of plate for image planes on selected camera."""

    product_types = {"image", "plate", "render"}
    label = "Load imagePlane"
    representations = {"mov", "exr", "preview", "png", "jpg", "jpeg"}
    icon = "image"
    color = "orange"

    def load(self, context, name, namespace, data, options=None):

        image_plane_depth = 1000
        folder_name = context["folder"]["name"]
        namespace = namespace or unique_namespace(
            folder_name + "_",
            prefix="_" if folder_name[0].isdigit() else "",
            suffix="_",
        )

        # Get camera from user selection.
        # is_static_image_plane = None
        # is_in_all_views = None
        camera = data.get("camera") if data else None

        if not camera:
            cameras = cmds.ls(type="camera")

            # Cameras by names
            camera_names = {}
            for camera in cameras:
                parent = cmds.listRelatives(camera, parent=True, path=True)[0]
                camera_names[parent] = camera

            camera_names["Create new camera."] = "create-camera"
            window = CameraWindow(camera_names.keys())
            window.exec_()
            # Skip if no camera was selected (Dialog was closed)
            if window.camera not in camera_names:
                return
            camera = camera_names[window.camera]

        if camera == "create-camera":
            camera = cmds.createNode("camera")

        if camera is None:
            return

        try:
            cmds.setAttr("{}.displayResolution".format(camera), True)
            cmds.setAttr("{}.farClipPlane".format(camera),
                         image_plane_depth * 10)
        except RuntimeError:
            pass

        # Create image plane
        with namespaced(namespace):
            # Create inside the namespace
            image_plane_transform, image_plane_shape = cmds.imagePlane(
                fileName=self.filepath_from_context(context),
                camera=camera
            )

        # Set colorspace
        colorspace = self.get_colorspace(context["representation"])
        if colorspace:
            cmds.setAttr(
                "{}.ignoreColorSpaceFileRules".format(image_plane_shape),
                True
            )
            cmds.setAttr("{}.colorSpace".format(image_plane_shape),
                         colorspace, type="string")

        # Set offset frame range
        start_frame = cmds.playbackOptions(query=True, min=True)
        end_frame = cmds.playbackOptions(query=True, max=True)

        for attr, value in {
            "depth": image_plane_depth,
            "frameOffset": 0,
            "frameIn": start_frame,
            "frameOut": end_frame,
            "frameCache": end_frame,
            "useFrameExtension": True
        }.items():
            plug = "{}.{}".format(image_plane_shape, attr)
            cmds.setAttr(plug, value)

        movie_representations = {"mov", "preview"}
        if context["representation"]["name"] in movie_representations:
            cmds.setAttr(image_plane_shape + ".type", 2)

        # Ask user whether to use sequence or still image.
        if context["representation"]["name"] == "exr":
            # Ensure OpenEXRLoader plugin is loaded.
            cmds.loadPlugin("OpenEXRLoader", quiet=True)

            message = (
                "Hold image sequence on first frame?"
                "\n{} files available.".format(
                    len(context["representation"]["files"])
                )
            )
            reply = QtWidgets.QMessageBox.information(
                None,
                "Frame Hold.",
                message,
                QtWidgets.QMessageBox.Yes,
                QtWidgets.QMessageBox.No
            )
            if reply == QtWidgets.QMessageBox.Yes:
                frame_extension_plug = "{}.frameExtension".format(image_plane_shape)  # noqa

                # Remove current frame expression
                disconnect_inputs(frame_extension_plug)

                cmds.setAttr(frame_extension_plug, start_frame)

        new_nodes = [image_plane_transform, image_plane_shape]

        return containerise(
            name=name,
            namespace=namespace,
            nodes=new_nodes,
            context=context,
            loader=self.__class__.__name__
        )

    def update(self, container, context):
        folder_entity = context["folder"]
        repre_entity = context["representation"]

        members = get_container_members(container)
        image_planes = cmds.ls(members, type="imagePlane")
        assert image_planes, "Image plane not found."
        image_plane_shape = image_planes[0]

        path = get_representation_path(repre_entity)
        cmds.setAttr("{}.imageName".format(image_plane_shape),
                     path,
                     type="string")
        cmds.setAttr("{}.representation".format(container["objectName"]),
                     repre_entity["id"],
                     type="string")

        colorspace = self.get_colorspace(repre_entity)
        if colorspace:
            cmds.setAttr(
                "{}.ignoreColorSpaceFileRules".format(image_plane_shape),
                True
            )
            cmds.setAttr("{}.colorSpace".format(image_plane_shape),
                         colorspace, type="string")

        # Set frame range.
        start_frame = folder_entity["attrib"]["frameStart"]
        end_frame = folder_entity["attrib"]["frameEnd"]

        for attr, value in {
            "frameOffset": 0,
            "frameIn": start_frame,
            "frameOut": end_frame,
            "frameCache": end_frame
        }:
            plug = "{}.{}".format(image_plane_shape, attr)
            cmds.setAttr(plug, value)

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

    def remove(self, container):
        members = cmds.sets(container['objectName'], query=True)
        cmds.lockNode(members, lock=False)
        cmds.delete([container['objectName']] + members)

        # Clean up the namespace
        try:
            cmds.namespace(removeNamespace=container['namespace'],
                           deleteNamespaceContent=True)
        except RuntimeError:
            pass

    def get_colorspace(self, representation):

        data = representation.get("data", {}).get("colorspaceData", {})
        if not data:
            return

        colorspace = data.get("colorspace")
        return colorspace