Skip to content

assign_look_placeholder

AssignLookPlaceholderPlugin

Bases: MayaPlaceholderPlugin

Assign a look product to members of the placeholder set.

Creates an objectSet. Any members will get the look assigned with the given product name if it exists.

Any containers loaded from other template placeholders will get the look assigned to their loaded containers.

Source code in client/ayon_maya/plugins/workfile_build/assign_look_placeholder.py
 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
class AssignLookPlaceholderPlugin(MayaPlaceholderPlugin):
    """Assign a look product to members of the placeholder set.

    Creates an objectSet. Any members will get the look assigned with the given
    product name if it exists.

    Any containers loaded from other template placeholders will get the look
    assigned to their loaded containers.

    """

    identifier = "maya.assignlook"
    label = "Assign Look"

    def get_placeholder_options(self, options=None):
        options = options or {}
        return [
            UISeparatorDef(),
            UILabelDef(label="<b>Description</b>"),
            UISeparatorDef(),
            UILabelDef(
                label=(
                    "Creates an objectSet. Any members will get the look\n"
                    "assigned with the given product name if it exists.\n\n"
                    "Any containers loaded from other template placeholders\n"
                    "will get the look assigned to their loaded containers."
                    ""
                )
            ),
            UISeparatorDef(),
            UILabelDef(label="<b>Settings</b>"),
            UISeparatorDef(),
            TextDef(
                "product_name",
                label="Product Name",
                tooltip="Look product to assign to containers loaded by "
                        "contained placeholders",
                multiline=False,
                default=options.get("product_name", "lookMain")
            ),
            BoolDef(
                "recurse",
                label="Recursive",
                tooltip="Assign look also to potential sub containers / "
                        "placeholders loaded from the load placeholder.\n"
                        "This will make sure that any placeholder contained "
                        "that itself loaded new geometry will recursively "
                        "also get the look assignment triggered.",
                default=options.get("recurse", False)
            ),
        ]

    def create_placeholder(self, placeholder_data):
        placeholder_data["plugin_identifier"] = self.identifier

        # Create maya objectSet on selection
        selection = cmds.ls(selection=True, long=True)
        product_name = placeholder_data["product_name"]
        name = "AssignLook_{}".format(product_name)
        node = cmds.sets(selection, name=name)

        self.imprint(node, placeholder_data)

    def populate_placeholder(self, placeholder):
        callback = weakref_partial(self.assign_look, placeholder)
        self.builder.add_on_depth_processed_callback(
            callback, order=placeholder.order)

        # If placeholder should be deleted, delete it after finish
        if not placeholder.data.get("keep_placeholder", True):
            delete_callback = weakref_partial(self.delete_placeholder,
                                              placeholder)
            self.builder.add_on_finished_callback(
                delete_callback, order=placeholder.order)

    def assign_look(self, placeholder):
        if placeholder.data.get("finished", False):
            # If not recursive we mark it finished after the first depth
            # iteration - otherwise run it again to find any new members
            return

        product_name = placeholder.data["product_name"]
        assert product_name, "Must have defined look product name to assign"

        members = cmds.ls(
            cmds.sets(placeholder.scene_identifier, query=True), long=True
        )
        if not members:
            return

        # Allow any children of members in the set to get assignments,
        # e.g. when a group is included there. Whenever a load placeholder
        # finishes it also adds loaded content into the object set the
        # placeholder was in, so this will also assign to loaded content
        # during this build.
        assign_nodes = set(members)
        assign_nodes.update(get_all_children(members))

        processed = placeholder.data.setdefault("processed", set())
        assign_nodes.difference_update(processed)
        processed.update(assign_nodes)

        if assign_nodes:
            self.log.info(
                "Assigning look {} for placeholder: {}".format(product_name,
                                                               placeholder)
            )
            assign_nodes = list(assign_nodes)
            assign_look(assign_nodes, product_name=product_name)

        if not placeholder.data.get("recurse", False):
            placeholder.data["finished"] = True