Skip to content

extract_active_view_thumbnail

ExtractActiveViewThumbnail

Bases: MayaInstancePlugin

Set instance thumbnail to a screengrab of current active viewport.

This makes it so that if an instance does not have a thumbnail set yet that it will get a thumbnail of the currently active view at the time of publishing as a fallback.

Source code in client/ayon_maya/plugins/publish/extract_active_view_thumbnail.py
12
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
class ExtractActiveViewThumbnail(plugin.MayaInstancePlugin):
    """Set instance thumbnail to a screengrab of current active viewport.

    This makes it so that if an instance does not have a thumbnail set yet that
    it will get a thumbnail of the currently active view at the time of
    publishing as a fallback.

    """
    order = pyblish.api.ExtractorOrder + 0.49
    label = "Active View Thumbnail"
    families = ["workfile"]

    def process(self, instance):
        if IS_HEADLESS or is_in_tests():
            self.log.debug(
                "Skip extraction of active view thumbnail, due to being in"
                "headless mode."
            )
            return

        thumbnail = instance.data.get("thumbnailPath")
        if not thumbnail:
            view_thumbnail = self.get_view_thumbnail(instance)
            if not view_thumbnail:
                return

            self.log.debug("Setting instance thumbnail path to: {}".format(
                view_thumbnail
            ))
            instance.data["thumbnailPath"] = view_thumbnail

    def get_view_thumbnail(self, instance):
        cache_key = "__maya_view_thumbnail"
        context = instance.context

        if cache_key not in context.data:
            # Generate only a single thumbnail, even for multiple instances
            with tempfile.NamedTemporaryFile(suffix="_thumbnail.jpg",
                                             delete=False) as f:
                path = f.name

            view = omui.M3dView.active3dView()
            image = om.MImage()
            view.readColorBuffer(image, True)
            image.writeToFile(path, "jpg")
            self.log.debug("Generated thumbnail: {}".format(path))

            context.data["cleanupFullPaths"].append(path)
            context.data[cache_key] = path
        return context.data[cache_key]