Skip to content

connect_yeti_rig

ConnectYetiRig

Bases: InventoryAction

Connect Yeti Rig with an animation or pointcache.

Source code in client/ayon_maya/plugins/inventory/connect_yeti_rig.py
 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
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
class ConnectYetiRig(InventoryAction):
    """Connect Yeti Rig with an animation or pointcache."""

    label = "Connect Yeti Rig"
    icon = "link"
    color = "white"

    def process(self, containers):
        # Validate selection is more than 1.
        message = (
            "Only 1 container selected. 2+ containers needed for this action."
        )
        if len(containers) == 1:
            self.display_warning(message)
            return

        # Categorize containers by product type.
        containers_by_product_type = defaultdict(list)
        repre_ids = {
            container["representation"]
            for container in containers
        }
        repre_contexts_by_id = get_repres_contexts(repre_ids)
        for container in containers:
            repre_id = container["representation"]
            repre_context = repre_contexts_by_id[repre_id]

            product_type = repre_context["product"]["productType"]

            containers_by_product_type.setdefault(product_type, [])
            containers_by_product_type[product_type].append(container)

        # Validate to only 1 source container.
        source_containers = containers_by_product_type.get("animation", [])
        source_containers += containers_by_product_type.get("pointcache", [])
        source_container_namespaces = [
            x["namespace"] for x in source_containers
        ]
        message = (
            "{} animation containers selected:\n\n{}\n\nOnly select 1 of type "
            "\"animation\" or \"pointcache\".".format(
                len(source_containers), source_container_namespaces
            )
        )
        if len(source_containers) != 1:
            self.display_warning(message)
            return

        source_container = source_containers[0]
        source_ids = self.nodes_by_id(source_container)

        # Target containers.
        target_ids = {}
        inputs = []

        yeti_rig_containers = containers_by_product_type.get("yetiRig")
        if not yeti_rig_containers:
            self.display_warning(
                "Select at least one yetiRig container"
            )
            return

        for container in yeti_rig_containers:
            target_ids.update(self.nodes_by_id(container))
            repre_id = container["representation"]

            maya_file = get_representation_path(
                repre_contexts_by_id[repre_id]["representation"]
            )
            _, ext = os.path.splitext(maya_file)
            settings_file = maya_file.replace(ext, ".rigsettings")
            if not os.path.exists(settings_file):
                continue

            with open(settings_file) as f:
                inputs.extend(json.load(f)["inputs"])

            # Compare loaded connections to scene.
            for input in inputs:
                source_node = source_ids.get(input["sourceID"])
                target_node = target_ids.get(input["destinationID"])

                if not source_node or not target_node:
                    self.log.debug(
                        "Could not find nodes for input:\n" +
                        json.dumps(input, indent=4, sort_keys=True)
                    )
                    continue
                source_attr, target_attr = input["connections"]

                if not cmds.attributeQuery(
                    source_attr, node=source_node, exists=True
                ):
                    self.log.debug(
                        "Could not find attribute {} on node {} for "
                        "input:\n{}".format(
                            source_attr,
                            source_node,
                            json.dumps(input, indent=4, sort_keys=True)
                        )
                    )
                    continue

                if not cmds.attributeQuery(
                    target_attr, node=target_node, exists=True
                ):
                    self.log.debug(
                        "Could not find attribute {} on node {} for "
                        "input:\n{}".format(
                            target_attr,
                            target_node,
                            json.dumps(input, indent=4, sort_keys=True)
                        )
                    )
                    continue

                source_plug = "{}.{}".format(
                    source_node, source_attr
                )
                target_plug = "{}.{}".format(
                    target_node, target_attr
                )
                if cmds.isConnected(
                    source_plug, target_plug, ignoreUnitConversion=True
                ):
                    self.log.debug(
                        "Connection already exists: {} -> {}".format(
                            source_plug, target_plug
                        )
                    )
                    continue

                cmds.connectAttr(source_plug, target_plug, force=True)
                self.log.debug(
                    "Connected attributes: {} -> {}".format(
                        source_plug, target_plug
                    )
                )

    def nodes_by_id(self, container):
        ids = {}
        for member in get_container_members(container):
            id = get_id(member)
            if not id:
                continue
            ids[id] = member

        return ids

    def display_warning(self, message, show_cancel=False):
        """Show feedback to user.

        Returns:
            bool
        """

        from qtpy import QtWidgets

        accept = QtWidgets.QMessageBox.Ok
        if show_cancel:
            buttons = accept | QtWidgets.QMessageBox.Cancel
        else:
            buttons = accept

        state = QtWidgets.QMessageBox.warning(
            None,
            "",
            message,
            buttons=buttons,
            defaultButton=accept
        )

        return state == accept

display_warning(message, show_cancel=False)

Show feedback to user.

Returns:

Type Description

bool

Source code in client/ayon_maya/plugins/inventory/connect_yeti_rig.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
def display_warning(self, message, show_cancel=False):
    """Show feedback to user.

    Returns:
        bool
    """

    from qtpy import QtWidgets

    accept = QtWidgets.QMessageBox.Ok
    if show_cancel:
        buttons = accept | QtWidgets.QMessageBox.Cancel
    else:
        buttons = accept

    state = QtWidgets.QMessageBox.warning(
        None,
        "",
        message,
        buttons=buttons,
        defaultButton=accept
    )

    return state == accept