Skip to content

load_vdb_to_vray

LoadVDBtoVRay

Bases: Loader

Load OpenVDB in a V-Ray Volume Grid

Source code in client/ayon_maya/plugins/load/load_vdb_to_vray.py
 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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
class LoadVDBtoVRay(plugin.Loader):
    """Load OpenVDB in a V-Ray Volume Grid"""

    product_types = {"vdbcache"}
    representations = {"vdb"}

    label = "Load VDB to VRay"
    icon = "cloud"
    color = "orange"

    def load(self, context, name, namespace, data):

        from ayon_maya.api.lib import unique_namespace
        from ayon_maya.api.pipeline import containerise

        path = self.filepath_from_context(context)
        assert os.path.exists(path), (
            "Path does not exist: %s" % path
        )

        product_type = context["product"]["productType"]

        # Ensure V-ray is loaded with the vrayvolumegrid
        if not cmds.pluginInfo("vrayformaya", query=True, loaded=True):
            cmds.loadPlugin("vrayformaya")
        if not cmds.pluginInfo("vrayvolumegrid", query=True, loaded=True):
            cmds.loadPlugin("vrayvolumegrid")

        # Check if viewport drawing engine is Open GL Core (compat)
        render_engine = None
        compatible = "OpenGLCoreProfileCompat"
        if cmds.optionVar(exists="vp2RenderingEngine"):
            render_engine = cmds.optionVar(query="vp2RenderingEngine")

        if not render_engine or render_engine != compatible:
            self.log.warning("Current scene's settings are incompatible."
                             "See Preferences > Display > Viewport 2.0 to "
                             "set the render engine to '%s'" % compatible)

        folder_name = context["folder"]["name"]
        namespace = namespace or unique_namespace(
            folder_name + "_",
            prefix="_" if folder_name[0].isdigit() else "",
            suffix="_",
        )

        # Root group
        label = "{}:{}_VDB".format(namespace, name)
        root = cmds.group(name=label, empty=True)

        project_name = context["project"]["name"]
        settings = get_project_settings(project_name)
        color = get_load_color_for_product_type(product_type, settings)
        if color is not None:
            red, green, blue = color
            cmds.setAttr(root + ".useOutlinerColor", 1)
            cmds.setAttr(root + ".outlinerColor", red, green, blue)

        # Create VRayVolumeGrid
        grid_node = cmds.createNode("VRayVolumeGrid",
                                    name="{}Shape".format(label),
                                    parent=root)

        # Ensure .currentTime is connected to time1.outTime
        cmds.connectAttr("time1.outTime", grid_node + ".currentTime")

        # Set path
        self._set_path(grid_node, path, show_preset_popup=True)

        # Lock the shape node so the user can't delete the transform/shape
        # as if it was referenced
        cmds.lockNode(grid_node, lock=True)

        nodes = [root, grid_node]
        self[:] = nodes

        return containerise(
            name=name,
            namespace=namespace,
            nodes=nodes,
            context=context,
            loader=self.__class__.__name__)

    def _set_path(self, grid_node, path, show_preset_popup=True):

        from ayon_maya.api.lib import attribute_values
        from maya import cmds

        def _get_filename_from_folder(path):
            # Using the sequence of .vdb files we check the frame range, etc.
            # to set the filename with #### padding.
            files = sorted(x for x in os.listdir(path) if x.endswith(".vdb"))
            if not files:
                raise RuntimeError("Couldn't find .vdb files in: %s" % path)

            if len(files) == 1:
                # Ensure check for single file is also done in folder
                fname = files[0]
            else:
                # Sequence
                import clique

                # todo: check support for negative frames as input
                collections, remainder = clique.assemble(files)
                assert len(collections) == 1, (
                    "Must find a single image sequence, "
                    "found: %s" % (collections,)
                )
                collection = collections[0]

                fname = collection.format('{head}{{padding}}{tail}')
                padding = collection.padding
                if padding == 0:
                    # Clique doesn't provide padding if the frame number never
                    # starts with a zero and thus has never any visual padding.
                    # So we fall back to the smallest frame number as padding.
                    padding = min(len(str(i)) for i in collection.indexes)

                # Supply frame/padding with # signs
                padding_str = "#" * padding
                fname = fname.format(padding=padding_str)

            return os.path.join(path, fname)

        # The path is either a single file or sequence in a folder so
        # we do a quick lookup for our files
        if os.path.isfile(path):
            path = os.path.dirname(path)
        path = _get_filename_from_folder(path)

        # Even when not applying a preset V-Ray will reset the 3rd Party
        # Channels Mapping of the VRayVolumeGrid when setting the .inPath
        # value. As such we try and preserve the values ourselves.
        # Reported as ChaosGroup bug ticket: 154-011-2909 
        # todo(roy): Remove when new V-Ray release preserves values
        original_user_mapping = cmds.getAttr(grid_node + ".usrchmap") or ""

        # Workaround for V-Ray bug: fix lag on path change, see function
        _fix_duplicate_vvg_callbacks()

        # Suppress preset pop-up if we want.
        popup_attr = "{0}.inDontOfferPresets".format(grid_node)
        popup = {popup_attr: not show_preset_popup}
        with attribute_values(popup):
            cmds.setAttr(grid_node + ".inPath", path, type="string")

        # Reapply the 3rd Party channels user mapping when no preset popup
        # was shown to the user
        if not show_preset_popup:
            channels = cmds.getAttr(grid_node + ".usrchmapallch").split(";")
            channels = set(channels)  # optimize lookup
            restored_mapping = ""
            for entry in original_user_mapping.split(";"):
                if not entry:
                    # Ignore empty entries
                    continue

                # If 3rd Party Channels selection channel still exists then
                # add it again.
                index, channel = entry.split(",")
                attr = THIRD_PARTY_CHANNELS.get(int(index),
                                                # Fallback for when a mapping
                                                # was set that is not in the
                                                # documentation
                                                "???")
                if channel in channels:
                    restored_mapping += entry + ";"
                else:
                    self.log.warning("Can't preserve '%s' mapping due to "
                                     "missing channel '%s' on node: "
                                     "%s" % (attr, channel, grid_node))

            if restored_mapping:
                cmds.setAttr(grid_node + ".usrchmap",
                             restored_mapping,
                             type="string")

    def update(self, container, context):
        repre_entity = context["representation"]

        path = get_representation_path(repre_entity)

        # Find VRayVolumeGrid
        members = cmds.sets(container['objectName'], query=True)
        grid_nodes = cmds.ls(members, type="VRayVolumeGrid", long=True)
        assert len(grid_nodes) > 0, "This is a bug"

        # Update the VRayVolumeGrid
        for grid_node in grid_nodes:
            self._set_path(grid_node, path=path, show_preset_popup=False)

        # Update container representation
        cmds.setAttr(container["objectName"] + ".representation",
                     repre_entity["id"],
                     type="string")

    def switch(self, container, context):
        self.update(container, context)

    def remove(self, container):

        # Get all members of the AYON container, ensure they are unlocked
        # and delete everything
        members = cmds.sets(container['objectName'], query=True)
        cmds.lockNode(members, lock=False)
        cmds.delete([container['objectName']] + members)

        # Clean up the namespace
        try:
            cmds.namespace(removeNamespace=container['namespace'],
                           deleteNamespaceContent=True)
        except RuntimeError:
            pass