//Xolotl Studio
//Created by Ymmanuel Flores
//Copyright 2018 Crowdorchestra SAPI de CV. All rights reserved.
//HHConnect v 1.0.0.4

source "hhconnect/arnold.mel";
source "hhconnect/redshift.mel";
source "hhconnect/renderman.mel";
source "hhconnect/vray.mel";




global proc string hhconnect_sp_link(string $out_path, string $mats,string $ext,string $rndr,int $packed, int $normal) {

    $materials = stringToStringArray($mats, "|");

    if($rndr == "ARNOLD"){
      for($m in $materials){
        if( `objExists ("AR_"+$m)` != 0 ) {
            if($packed == 1){
                updateARMaterialPkd($out_path,$ext,$m);
            }else{
                updateARMaterial($out_path,$ext,$m);
            }
        } else {
            if($packed == 1){
                addARMaterialPkd($out_path,$ext,$m,$normal);
            }else{
                addARMaterial($out_path,$ext,$m,$normal);
            }
        }
      }
    }else if($rndr == "REDSHIFT"){
      for($m in $materials){
        if( `objExists ("RS_"+$m)` != 0 ) {
            if($packed == 1){
                updateRSMaterialPkd($out_path,$ext,$m);
            }else{
                updateRSMaterial($out_path,$ext,$m);
            }
        } else {
            if($packed == 1){
                addRSMaterialPkd($out_path,$ext,$m,$normal);
            }else{
                addRSMaterial($out_path,$ext,$m,$normal);
            }
        }
      }
    }else if($rndr == "RENDERMAN"){
      for($m in $materials){
        if( `objExists ("RM_"+$m)` != 0 ) {
            if($packed == 1){
                updateRMMaterialPkd($out_path,$ext,$m);
            }else{
                updateRMMaterial($out_path,$ext,$m);
            }
        } else {
            if($packed == 1){
                addRMMaterialPkd($out_path,$ext,$m,$normal);
            }else{
                addRMMaterial($out_path,$ext,$m,$normal);
            }
        }
      }
    }else if($rndr == "VRAY"){
      for($m in $materials){
        if( `objExists ("VR_"+$m)` != 0 ) {
            if($packed == 1){
                updateVRMaterialPkd($out_path,$ext,$m);
            }else{
                updateVRMaterial($out_path,$ext,$m);
            }
        } else {
            if($packed == 1){
                addVRMaterialPkd($out_path,$ext,$m,$normal);
            }else{
                addVRMaterial($out_path,$ext,$m,$normal);
            }
        }
      }
    }

    return "SUCCESS";
}
