Skip to content

extract_arnold_scene_source

ExtractArnoldSceneSource

Bases: MayaExtractorPlugin

Extract the content of the instance to an Arnold Scene Source file.

Source code in client/ayon_maya/plugins/publish/extract_arnold_scene_source.py
 10
 11
 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
 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
class ExtractArnoldSceneSource(plugin.MayaExtractorPlugin):
    """Extract the content of the instance to an Arnold Scene Source file."""

    label = "Extract Arnold Scene Source"
    families = ["ass"]
    asciiAss = False

    def _pre_process(self, instance, staging_dir):
        file_path = os.path.join(staging_dir, "{}.ass".format(instance.name))

        # Mask
        mask = arnold.AI_NODE_ALL

        node_types = {
            "options": arnold.AI_NODE_OPTIONS,
            "camera": arnold.AI_NODE_CAMERA,
            "light": arnold.AI_NODE_LIGHT,
            "shape": arnold.AI_NODE_SHAPE,
            "shader": arnold.AI_NODE_SHADER,
            "override": arnold.AI_NODE_OVERRIDE,
            "driver": arnold.AI_NODE_DRIVER,
            "filter": arnold.AI_NODE_FILTER,
            "color_manager": arnold.AI_NODE_COLOR_MANAGER,
            "operator": arnold.AI_NODE_OPERATOR
        }
        if hasattr(arnold, "AI_NODE_IMAGER"):
            # Added in Arnold 6.1.0.0 with MtoA 4.1.0 (Oct. 2020)
            node_types["imager"] = arnold.AI_NODE_IMAGER

        for key in node_types.keys():
            if instance.data.get("mask" + key.title()):
                mask = mask ^ node_types[key]

        # Motion blur
        attribute_data = {
            "defaultArnoldRenderOptions.motion_blur_enable": instance.data.get(
                "motionBlur", True
            ),
            "defaultArnoldRenderOptions.motion_steps": instance.data.get(
                "motionBlurKeys", 2
            ),
            "defaultArnoldRenderOptions.motion_frames": instance.data.get(
                "motionBlurLength", 0.5
            )
        }

        # Write out .ass file
        kwargs = {
            "filename": file_path,
            "startFrame": instance.data.get("frameStartHandle", 1),
            "endFrame": instance.data.get("frameEndHandle", 1),
            "frameStep": instance.data.get("step", 1),
            "selected": True,
            "asciiAss": self.asciiAss,
            "shadowLinks": True,
            "lightLinks": True,
            "boundingBox": instance.data.get("boundingBox", True),
            "compressed": instance.data.get("compressed", False),
            "expandProcedurals": instance.data.get("expandProcedurals", False),
            "camera": instance.data["camera"],
            "mask": mask
        }

        if "representations" not in instance.data:
            instance.data["representations"] = []

        return attribute_data, kwargs

    def process(self, instance):
        staging_dir = self.staging_dir(instance)
        attribute_data, kwargs = self._pre_process(instance, staging_dir)

        filenames = self._extract(
            instance.data["members"], attribute_data, kwargs
        )

        self._post_process(
            instance, filenames, staging_dir, kwargs["startFrame"]
        )

    def _post_process(self, instance, filenames, staging_dir, frame_start):
        nodes_by_id = self._nodes_by_id(instance[:])

        ext = "ass.gz" if instance.data.get("compressed") else "ass"

        representation = {
            "name": "ass",
            "ext": ext,
            "files": filenames if len(filenames) > 1 else filenames[0],
            "stagingDir": staging_dir,
            "frameStart": frame_start
        }

        instance.data["representations"].append(representation)

        json_path = os.path.join(
            staging_dir, "{}.json".format(instance.name)
        )
        with open(json_path, "w") as f:
            json.dump(nodes_by_id, f)

        representation = {
            "name": "json",
            "ext": "json",
            "files": os.path.basename(json_path),
            "stagingDir": staging_dir
        }

        instance.data["representations"].append(representation)

        self.log.debug(
            "Extracted instance {} to: {}".format(instance.name, staging_dir)
        )

    def _nodes_by_id(self, nodes):
        nodes_by_id = defaultdict(list)

        for node in nodes:
            id = lib.get_id(node)

            if id is None:
                continue

            # Converting Maya hierarchy separator "|" to Arnold separator "/".
            nodes_by_id[id].append(node.replace("|", "/"))

        return nodes_by_id

    def _extract(self, nodes, attribute_data, kwargs):
        filenames = []
        with lib.attribute_values(attribute_data):
            with lib.maintained_selection():
                self.log.debug(
                    "Writing: {}".format(nodes)
                )
                cmds.select(nodes, noExpand=True)

                self.log.debug(
                    "Extracting ass sequence with: {}".format(kwargs)
                )

                exported_files = cmds.arnoldExportAss(**kwargs)

                for file in exported_files:
                    filenames.append(os.path.split(file)[1])

                self.log.debug("Exported: {}".format(filenames))

        return filenames

ExtractArnoldSceneSourceProxy

Bases: ExtractArnoldSceneSource

Extract the content of the instance to an Arnold Scene Source file.

Source code in client/ayon_maya/plugins/publish/extract_arnold_scene_source.py
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
class ExtractArnoldSceneSourceProxy(ExtractArnoldSceneSource):
    """Extract the content of the instance to an Arnold Scene Source file."""

    label = "Extract Arnold Scene Source Proxy"
    hosts = ["maya"]
    families = ["assProxy"]
    asciiAss = True

    def process(self, instance):
        staging_dir = self.staging_dir(instance)
        attribute_data, kwargs = self._pre_process(instance, staging_dir)

        filenames, _ = self._duplicate_extract(
            instance.data["members"], attribute_data, kwargs
        )

        self._post_process(
            instance, filenames, staging_dir, kwargs["startFrame"]
        )

        kwargs["filename"] = os.path.join(
            staging_dir, "{}_proxy.ass".format(instance.name)
        )

        filenames, _ = self._duplicate_extract(
            instance.data["proxy"], attribute_data, kwargs
        )

        representation = {
            "name": "proxy",
            "ext": "ass",
            "files": filenames if len(filenames) > 1 else filenames[0],
            "stagingDir": staging_dir,
            "frameStart": kwargs["startFrame"],
            "outputName": "proxy"
        }

        instance.data["representations"].append(representation)

    def _duplicate_extract(self, nodes, attribute_data, kwargs):
        self.log.debug(
            "Writing {} with:\n{}".format(kwargs["filename"], kwargs)
        )
        filenames = []
        # Duplicating nodes so they are direct children of the world. This
        # makes the hierarchy of any exported ass file the same.
        with lib.delete_after() as delete_bin:
            duplicate_nodes = []
            for node in nodes:
                # Only interested in transforms:
                if cmds.nodeType(node) != "transform":
                    continue

                # Only interested in transforms with shapes.
                shapes = cmds.listRelatives(
                    node, shapes=True, noIntermediate=True
                )
                if not shapes:
                    continue

                basename = cmds.duplicate(node)[0]
                parents = cmds.ls(node, long=True)[0].split("|")[:-1]
                duplicate_transform = "|".join(parents + [basename])

                if cmds.listRelatives(duplicate_transform, parent=True):
                    duplicate_transform = cmds.parent(
                        duplicate_transform, world=True
                    )[0]

                basename = node.rsplit("|", 1)[-1].rsplit(":", 1)[-1]
                duplicate_transform = cmds.rename(
                    duplicate_transform, basename
                )

                # Discard children nodes that are not shapes
                shapes = cmds.listRelatives(
                    duplicate_transform, shapes=True, fullPath=True
                )
                children = cmds.listRelatives(
                    duplicate_transform, children=True, fullPath=True
                )
                cmds.delete(set(children) - set(shapes))

                duplicate_nodes.append(duplicate_transform)
                duplicate_nodes.extend(shapes)
                delete_bin.append(duplicate_transform)

            nodes_by_id = self._nodes_by_id(duplicate_nodes)
            filenames = self._extract(duplicate_nodes, attribute_data, kwargs)

        return filenames, nodes_by_id