Repository: ynput/OpenPype
Issue: Maya: Vray AOV names handling

## Bug

V-Ray in Maya has some very strange way determining resulting AOV name used in rendered file. It use one of `vray_name_*` or `vray_filename_*` or `vray_explicit_name_*` or even material names. Pype now expects only `vray_name_*` and will crash if AOV with different name determination method will be used - for example **material_ID**.

Changed files:
pype/plugins/maya/publish/extract_look.py, pype/hosts/maya/expected_files.py

diff --git a/pype/hosts/maya/expected_files.py b/pype/hosts/maya/expected_files.py
index 8d225bc13d..0b9b7d21d1 100644
--- a/pype/hosts/maya/expected_files.py
+++ b/pype/hosts/maya/expected_files.py
@@ -418,13 +418,12 @@ class AExpectedFiles:
         if connections:
             for connection in connections:
                 if connection:
-                    node_name = connection.split(".")[0]
-                    if cmds.nodeType(node_name) == "renderLayer":
-                        attr_name = "%s.value" % ".".join(
-                            connection.split(".")[:-1]
-                        )
-                        if node_name == layer:
-                            yield cmds.getAttr(attr_name)
+                    # node_name = connection.split(".")[0]
+
+                    attr_name = "%s.value" % ".".join(
+                        connection.split(".")[:-1]
+                    )
+                    yield cmds.getAttr(attr_name)
 
     def get_render_attribute(self, attr):
         """Get attribute from render options.
@@ -572,11 +571,17 @@ class ExpectedFilesVray(AExpectedFiles):
         expected_files = super(ExpectedFilesVray, self).get_files()
 
         layer_data = self._get_layer_data()
+        # remove 'beauty' from filenames as vray doesn't output it
+        update = {}
         if layer_data.get("enabledAOVs"):
-            expected_files[0][u"beauty"] = self._generate_single_file_sequence(
-                layer_data
-            )  # noqa: E501
-
+            for aov, seq in expected_files[0].items():
+                if aov.startswith("beauty"):
+                    new_list = []
+                    for f in seq:
+                        new_list.append(f.replace("_beauty", ""))
+                    update[aov] = new_list
+
+            expected_files[0].update(update)
         return expected_files
 
     def get_aovs(self):
@@ -630,28 +635,49 @@ class ExpectedFilesVray(AExpectedFiles):
                 # todo: find how vray set format for AOVs
                 enabled_aovs.append(
                     (self._get_vray_aov_name(aov), default_ext))
+        enabled_aovs.append(
+            (u"beauty", default_ext)
+        )
         return enabled_aovs
 
     def _get_vray_aov_name(self, node):
+        """Get AOVs name from Vray.
 
-        # Get render element pass type
-        vray_node_attr = next(
-            attr
-            for attr in cmds.listAttr(node)
-            if attr.startswith("vray_name")
-        )
-        pass_type = vray_node_attr.rsplit("_", 1)[-1]
-
-        # Support V-Ray extratex explicit name (if set by user)
-        if pass_type == "extratex":
-            explicit_attr = "{}.vray_explicit_name_extratex".format(node)
-            explicit_name = cmds.getAttr(explicit_attr)
-            if explicit_name:
-                return explicit_name
-
-        # Node type is in the attribute name but we need to check if value
-        # of the attribute as it can be changed
-        return cmds.getAttr("{}.{}".format(node, vray_node_attr))
+        Args:
+            node (str): aov node name.
+
+        Returns:
+            str: aov name.
+
+        """
+        vray_name = None
+        vray_explicit_name = None
+        vray_file_name = None
+        for attr in cmds.listAttr(node):
+            if attr.startswith("vray_filename"):
+                vray_file_name = cmds.getAttr("{}.{}".format(node, attr))
+            elif attr.startswith("vray_name"):
+                vray_name = cmds.getAttr("{}.{}".format(node, attr))
+            elif attr.startswith("vray_explicit_name"):
+                vray_explicit_name = cmds.getAttr("{}.{}".format(node, attr))
+
+            if vray_file_name is not None and vray_file_name != "":
+                final_name = vray_file_name
+            elif vray_explicit_name is not None and vray_explicit_name != "":
+                final_name = vray_explicit_name
+            elif vray_name is not None and vray_name != "":
+                final_name = vray_name
+            else:
+                continue
+            # special case for Material Select elements - these are named
+            # based on the materia they are connected to.
+            if "vray_mtl_mtlselect" in cmds.listAttr(node):
+                connections = cmds.listConnections(
+                    "{}.vray_mtl_mtlselect".format(node))
+                if connections:
+                    final_name += '_{}'.format(str(connections[0]))
+
+            return final_name
 
 
 class ExpectedFilesRedshift(AExpectedFiles):
diff --git a/pype/plugins/maya/publish/extract_look.py b/pype/plugins/maya/publish/extract_look.py
index 6bd202093f..7a5dc25fcf 100644
--- a/pype/plugins/maya/publish/extract_look.py
+++ b/pype/plugins/maya/publish/extract_look.py
@@ -350,7 +350,7 @@ class ExtractLook(pype.api.Extractor):
         if existing and not force:
             self.log.info("Found hash in database, preparing hardlink..")
             source = next((p for p in existing if os.path.exists(p)), None)
-            if filepath:
+            if source:
                 return source, HARDLINK, texture_hash
             else:
                 self.log.warning(