Skip to content

load_xgen

XgenLoader

Bases: ReferenceLoader

Load Xgen as reference

Source code in client/ayon_maya/plugins/load/load_xgen.py
 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
class XgenLoader(plugin.ReferenceLoader):
    """Load Xgen as reference"""

    product_types = {"xgen"}
    representations = {"ma", "mb"}

    label = "Reference Xgen"
    icon = "code-fork"
    color = "orange"

    def get_xgen_xgd_paths(self, palette):
        _, maya_extension = os.path.splitext(current_file())
        xgen_file = current_file().replace(
            maya_extension,
            "__{}.xgen".format(palette.replace("|", "").replace(":", "__"))
        )
        xgd_file = xgen_file.replace(".xgen", ".xgd")
        return xgen_file, xgd_file

    def process_reference(self, context, name, namespace, options):
        # Validate workfile has a path.
        if current_file() is None:
            QtWidgets.QMessageBox.warning(
                None,
                "",
                "Current workfile has not been saved. Please save the workfile"
                " before loading an Xgen."
            )
            return

        maya_filepath = self.prepare_root_value(
            file_url=self.filepath_from_context(context),
            project_name=context["project"]["name"]
        )

        # Reference xgen. Xgen does not like being referenced in under a group.
        with maintained_selection():
            nodes = cmds.file(
                maya_filepath,
                namespace=namespace,
                sharedReferenceFile=False,
                reference=True,
                returnNewNodes=True
            )

            xgen_palette = cmds.ls(
                nodes, type="xgmPalette", long=True
            )[0].replace("|", "")

            xgen_file, xgd_file = self.get_xgen_xgd_paths(xgen_palette)
            self.set_palette_attributes(xgen_palette, xgen_file, xgd_file)

            # Change the cache and disk values of xgDataPath and xgProjectPath
            # to ensure paths are setup correctly.
            project_path = os.path.dirname(current_file()).replace("\\", "/")
            xgenm.setAttr("xgProjectPath", project_path, xgen_palette)
            data_path = "${{PROJECT}}xgen/collections/{};{}".format(
                xgen_palette.replace(":", "__ns__"),
                xgenm.getAttr("xgDataPath", xgen_palette)
            )
            xgenm.setAttr("xgDataPath", data_path, xgen_palette)

            data = {"xgProjectPath": project_path, "xgDataPath": data_path}
            write_xgen_file(data, xgen_file)

            # This create an expression attribute of float. If we did not add
            # any changes to collection, then Xgen does not create an xgd file
            # on save. This gives errors when launching the workfile again due
            # to trying to find the xgd file.
            name = "custom_float_ignore"
            if name not in xgenm.customAttrs(xgen_palette):
                xgenm.addCustomAttr(
                    "custom_float_ignore", xgen_palette
                )

            shapes = cmds.ls(nodes, shapes=True, long=True)

            new_nodes = (list(set(nodes) - set(shapes)))

            self[:] = new_nodes

        return new_nodes

    def set_palette_attributes(self, xgen_palette, xgen_file, xgd_file):
        cmds.setAttr(
            "{}.xgBaseFile".format(xgen_palette),
            os.path.basename(xgen_file),
            type="string"
        )
        cmds.setAttr(
            "{}.xgFileName".format(xgen_palette),
            os.path.basename(xgd_file),
            type="string"
        )
        cmds.setAttr("{}.xgExportAsDelta".format(xgen_palette), True)

    def update(self, container, context):
        """Workflow for updating Xgen.

        - Export changes to delta file.
        - Copy and overwrite the workspace .xgen file.
        - Set collection attributes to not include delta files.
        - Update xgen maya file reference.
        - Apply the delta file changes.
        - Reset collection attributes to include delta files.

        We have to do this workflow because when using referencing of the xgen
        collection, Maya implicitly imports the Xgen data from the xgen file so
        we dont have any control over when adding the delta file changes.

        There is an implicit increment of the xgen and delta files, due to
        using the workfile basename.
        """
        # Storing current description to try and maintain later.
        current_description = (
            xgenm.xgGlobal.DescriptionEditor.currentDescription()
        )

        container_node = container["objectName"]
        members = get_container_members(container_node)
        xgen_palette = cmds.ls(
            members, type="xgmPalette", long=True
        )[0].replace("|", "")
        xgen_file, xgd_file = self.get_xgen_xgd_paths(xgen_palette)

        # Export current changes to apply later.
        xgenm.createDelta(xgen_palette.replace("|", ""), xgd_file)

        self.set_palette_attributes(xgen_palette, xgen_file, xgd_file)

        repre_entity = context["representation"]
        maya_file = get_representation_path(repre_entity)
        _, extension = os.path.splitext(maya_file)
        new_xgen_file = maya_file.replace(extension, ".xgen")
        data_path = ""
        with open(new_xgen_file, "r") as f:
            for line in f:
                if line.startswith("\txgDataPath"):
                    line = line.rstrip()
                    data_path = line.split("\t")[-1]
                    break

        project_path = os.path.dirname(current_file()).replace("\\", "/")
        data_path = "${{PROJECT}}xgen/collections/{};{}".format(
            xgen_palette.replace(":", "__ns__"),
            data_path
        )
        data = {"xgProjectPath": project_path, "xgDataPath": data_path}
        shutil.copy(new_xgen_file, xgen_file)
        write_xgen_file(data, xgen_file)

        attribute_data = {
            "{}.xgFileName".format(xgen_palette): os.path.basename(xgen_file),
            "{}.xgBaseFile".format(xgen_palette): "",
            "{}.xgExportAsDelta".format(xgen_palette): False
        }
        with attribute_values(attribute_data):
            super().update(container, context)

            xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file)

        # Restore current selected description if it exists.
        if cmds.objExists(current_description):
            xgenm.xgGlobal.DescriptionEditor.setCurrentDescription(
                current_description
            )
        # Full UI refresh.
        xgenm.xgGlobal.DescriptionEditor.refresh("Full")

update(container, context)

Workflow for updating Xgen.

  • Export changes to delta file.
  • Copy and overwrite the workspace .xgen file.
  • Set collection attributes to not include delta files.
  • Update xgen maya file reference.
  • Apply the delta file changes.
  • Reset collection attributes to include delta files.

We have to do this workflow because when using referencing of the xgen collection, Maya implicitly imports the Xgen data from the xgen file so we dont have any control over when adding the delta file changes.

There is an implicit increment of the xgen and delta files, due to using the workfile basename.

Source code in client/ayon_maya/plugins/load/load_xgen.py
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
def update(self, container, context):
    """Workflow for updating Xgen.

    - Export changes to delta file.
    - Copy and overwrite the workspace .xgen file.
    - Set collection attributes to not include delta files.
    - Update xgen maya file reference.
    - Apply the delta file changes.
    - Reset collection attributes to include delta files.

    We have to do this workflow because when using referencing of the xgen
    collection, Maya implicitly imports the Xgen data from the xgen file so
    we dont have any control over when adding the delta file changes.

    There is an implicit increment of the xgen and delta files, due to
    using the workfile basename.
    """
    # Storing current description to try and maintain later.
    current_description = (
        xgenm.xgGlobal.DescriptionEditor.currentDescription()
    )

    container_node = container["objectName"]
    members = get_container_members(container_node)
    xgen_palette = cmds.ls(
        members, type="xgmPalette", long=True
    )[0].replace("|", "")
    xgen_file, xgd_file = self.get_xgen_xgd_paths(xgen_palette)

    # Export current changes to apply later.
    xgenm.createDelta(xgen_palette.replace("|", ""), xgd_file)

    self.set_palette_attributes(xgen_palette, xgen_file, xgd_file)

    repre_entity = context["representation"]
    maya_file = get_representation_path(repre_entity)
    _, extension = os.path.splitext(maya_file)
    new_xgen_file = maya_file.replace(extension, ".xgen")
    data_path = ""
    with open(new_xgen_file, "r") as f:
        for line in f:
            if line.startswith("\txgDataPath"):
                line = line.rstrip()
                data_path = line.split("\t")[-1]
                break

    project_path = os.path.dirname(current_file()).replace("\\", "/")
    data_path = "${{PROJECT}}xgen/collections/{};{}".format(
        xgen_palette.replace(":", "__ns__"),
        data_path
    )
    data = {"xgProjectPath": project_path, "xgDataPath": data_path}
    shutil.copy(new_xgen_file, xgen_file)
    write_xgen_file(data, xgen_file)

    attribute_data = {
        "{}.xgFileName".format(xgen_palette): os.path.basename(xgen_file),
        "{}.xgBaseFile".format(xgen_palette): "",
        "{}.xgExportAsDelta".format(xgen_palette): False
    }
    with attribute_values(attribute_data):
        super().update(container, context)

        xgenm.applyDelta(xgen_palette.replace("|", ""), xgd_file)

    # Restore current selected description if it exists.
    if cmds.objExists(current_description):
        xgenm.xgGlobal.DescriptionEditor.setCurrentDescription(
            current_description
        )
    # Full UI refresh.
    xgenm.xgGlobal.DescriptionEditor.refresh("Full")