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

//-------------------- MAIN --------------------------------
proc connectPlace2D(string $place2D,string $map){
  connectAttr -f ($place2D + ".coverage") ($map+".coverage");
  connectAttr -f ($place2D + ".translateFrame") ($map+".translateFrame");
  connectAttr -f ($place2D + ".rotateFrame") ($map+".rotateFrame");
  connectAttr -f ($place2D + ".mirrorU") ($map+".mirrorU");
  connectAttr -f ($place2D + ".mirrorV") ($map+".mirrorV");
  connectAttr -f ($place2D + ".stagger") ($map+".stagger");
  connectAttr -f ($place2D + ".wrapU") ($map+".wrapU");
  connectAttr -f ($place2D + ".wrapV") ($map+".wrapV");
  connectAttr -f ($place2D + ".repeatUV") ($map+".repeatUV");
  connectAttr -f ($place2D + ".offset") ($map+".offset");
  connectAttr -f ($place2D + ".rotateUV") ($map+".rotateUV");
  connectAttr -f ($place2D + ".noiseUV") ($map+".noiseUV");
  connectAttr -f ($place2D + ".vertexUvOne") ($map+".vertexUvOne");
  connectAttr -f ($place2D + ".vertexUvTwo") ($map+".vertexUvTwo");
  connectAttr -f ($place2D + ".vertexUvThree") ($map+".vertexUvThree");
  connectAttr -f ($place2D + ".vertexCameraOne") ($map+".vertexCameraOne");
  connectAttr -f ($place2D + ".outUV") ($map+".uv");
  connectAttr -f ($place2D + ".outUvFilterSize") ($map+".uvFilterSize");

}

proc updateImage(string $name, string $material, string $map, string $out_path, string $ext){

  string $imgName = $material + $map;
  if( `objExists imgName` ) {
    setAttr -type "string" ($imgName + ".fileTextureName") ($out_path + "/" + $name + $map +  ".<UDIM>." + $ext);
  }

}


//-------------------- REDSHIFT --------------------//
global proc string addRSMaterialUdims(string $out_path, string $ext, string $name, string $materials, int $normal_format){


  string $rsMaterial = "RS_"+$name;
  string $material = `shadingNode -asShader RedshiftMaterial -name $rsMaterial`;
  string $rsMaterialSG = $rsMaterial + "SG";
  string $shader = `sets -renderable true -noSurfaceShader true -empty -name $rsMaterialSG`;


  connectAttr -f ($material + ".outColor ") ($shader +".surfaceShader");
  setAttr ($material + ".refl_brdf") 1;
  setAttr ($material + ".refl_fresnel_mode") 2;

  string $place2D_name = $material + "_place2D";
  string $place2D = `shadingNode -asUtility place2dTexture -name $place2D_name`;

  //Base Color
  string $basecolor_name = $material + "_basecolor";
  string $basecolor = `shadingNode -asTexture -isColorManaged file -name $basecolor_name`;
  setAttr ($basecolor+".uvTilingMode") 3;
  setAttr -type "string" ($basecolor + ".fileTextureName") ($out_path + "/" + $name +"_basecolor.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($basecolor + ".colorSpace") "sRGB";
  connectPlace2D($place2D,$basecolor);
  connectAttr -f ($basecolor + ".outColor") ($material+".diffuse_color");

  //Roughness
  string $rough_name = $material + "_roughness";
  string $rough = `shadingNode -asTexture -isColorManaged file -name $rough_name`;
  setAttr ($rough+".uvTilingMode") 3;
  setAttr -type "string" ($rough + ".fileTextureName") ($out_path + "/" + $name +"_roughness.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($rough + ".colorSpace") "Raw";
  setAttr  ($rough + ".alphaIsLuminance") 1;
  connectPlace2D($place2D,$rough);
  connectAttr -f ($rough + ".outAlpha") ($material+".refl_roughness");

  //Metallic
  string $metal_name = $material + "_metallic";
  string $metal = `shadingNode -asTexture -isColorManaged file -name $metal_name`;
  setAttr ($metal+".uvTilingMode") 3;
  setAttr -type "string" ($metal + ".fileTextureName") ($out_path + "/" + $name +"_metallic.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($metal + ".colorSpace") "Raw";
  setAttr  ($metal + ".alphaIsLuminance") 1;
  connectPlace2D($place2D,$metal);
  connectAttr -f ($metal + ".outAlpha") ($material+".refl_metalness");


  string $array[] = stringToStringArray($materials, "|");
  int $isEmissive = 0;
  int $isOpacity = 0;
  print($array);
  for ($m in $array){
      if(`filetest -f ($out_path + "/" + $name +"_emissive"+ "." +$m +"."+ $ext)`  == 1){
          $isEmissive = 1;
      }
      if(`filetest -f ($out_path + "/" + $name +"_opacity"+ "." +$m +"."+ $ext)`  == 1){
          $isOpacity = 1;
      }
  }

  if($isEmissive  == 1){
      //Emission
      setAttr ($material + ".emission_weight") 2;
      string $emission_name = $material + "_emissive";
      string $emission = `shadingNode -asTexture -isColorManaged file -name $emission_name`;
      setAttr ($emission+".uvTilingMode") 3;
      setAttr -type "string" ($emission + ".fileTextureName") ($out_path + "/" + $name +"_emissive.<UDIM>"+ "." + $ext);
      setAttr -type "string" ($emission + ".colorSpace") "sRGB";
      setAttr ($emission +".defaultColor") -type double3 0 0 0 ;
      connectPlace2D($place2D,$emission);
      connectAttr -f ($emission + ".outColor") ($material+".emission_color");
  }

  if($isOpacity  == 1){
      //Opacity
      string $opacity_name = $material + "_opacity";
      string $opacity = `shadingNode -asTexture -isColorManaged file -name $opacity_name`;
      setAttr ($opacity+".uvTilingMode") 3;
      setAttr -type "string" ($opacity + ".fileTextureName") ($out_path + "/" + $name +"_opacity.<UDIM>"+  "." + $ext);
      setAttr -type "string" ($opacity + ".colorSpace") "Raw";
      setAttr ($opacity +".defaultColor") -type double3 1 1 1 ;
      connectPlace2D($place2D,$opacity);
      connectAttr -f ($opacity + ".outColor") ($material+".opacity_color");
  }

  //Normal Map
  string $normal_name = $material + "_normal";
  string $normal = `shadingNode -asUtility RedshiftNormalMap -name $normal_name`;
  setAttr -type "string" ($normal + ".tex0") ($out_path + "/" + $name +"_normal.<UDIM>"+  "." + $ext);
  if($normal_format == 1){
    setAttr ($normal + ".flipY") 0;
  }else{
    setAttr ($normal + ".flipY") 1;
  }

  connectAttr -f ($normal + ".outDisplacementVector") ($material+".bump_input");


  //Displacement
  string $displacement_name = $material + "_height";
  string $displacement_disp = `shadingNode -asUtility RedshiftDisplacement -name ($displacement_name+"_disp")`;
  string $displacement = `shadingNode -asTexture -isColorManaged file -name $displacement_name`;
  setAttr ($displacement+".uvTilingMode") 3;
  setAttr -type "string" ($displacement + ".fileTextureName") ($out_path + "/" + $name +"_height.<UDIM>"+  "." + $ext);
  setAttr -type "string" ($displacement + ".colorSpace") "Raw";
  setAttr  ($displacement + ".alphaIsLuminance") 1;
  setAttr ($displacement_disp+".scale") 0.01;
  setAttr ($displacement_disp + ".newrange_min") -0.5;
  setAttr ($displacement_disp + ".newrange_max") 0.5;
  connectPlace2D($place2D,$displacement);
  connectAttr -f ($displacement + ".outColor") ($displacement_disp+".texMap");
  connectAttr -f ($displacement_disp + ".out") ($shader +".displacementShader");

  return "SUCCESS";
}



global proc string addRSMaterialPkdUdims(string $out_path, string $ext, string $name, string $materials, int $normal_format){


  string $rsMaterial = "RS_"+$name;
  string $material = `shadingNode -asShader RedshiftMaterial -name $rsMaterial`;
  string $rsMaterialSG = $rsMaterial + "SG";
  string $shader = `sets -renderable true -noSurfaceShader true -empty -name $rsMaterialSG`;


  connectAttr -f ($material + ".outColor ") ($shader +".surfaceShader");
  setAttr ($material + ".refl_brdf") 1;
  setAttr ($material + ".refl_fresnel_mode") 2;

  string $place2D_name = $material + "_place2D";
  string $place2D = `shadingNode -asUtility place2dTexture -name $place2D_name`;

  //Base Color
  string $basecolor_name = $material + "_basecolor";
  string $basecolor = `shadingNode -asTexture -isColorManaged file -name $basecolor_name`;
  setAttr ($basecolor+".uvTilingMode") 3;
  setAttr -type "string" ($basecolor + ".fileTextureName") ($out_path + "/" + $name +"_basecolor.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($basecolor + ".colorSpace") "sRGB";
  connectPlace2D($place2D,$basecolor);
  connectAttr -f ($basecolor + ".outColor") ($material+".diffuse_color");

  //Roughness & Metallic
  string $ao_rough_metal_name = $material + "_aoroughmetal";
  string $ao_rough_metal = `shadingNode -asTexture -isColorManaged file -name $ao_rough_metal_name`;
  setAttr ($ao_rough_metal+".uvTilingMode") 3;
  setAttr -type "string" ($ao_rough_metal + ".fileTextureName") ($out_path + "/" + $name +"_aoroughmetal.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($ao_rough_metal + ".colorSpace") "Raw";
  connectPlace2D($place2D,$ao_rough_metal);
  connectAttr -f ($ao_rough_metal + ".outColorG") ($material+".refl_roughness");
  connectAttr -f ($ao_rough_metal + ".outColorB") ($material+".refl_metalness");


  string $array[] = stringToStringArray($materials, "|");
  int $isEmissive = 0;
  int $isOpacity = 0;
  print($array);
  for ($m in $array){
      if(`filetest -f ($out_path + "/" + $name +"_emissive"+ "." +$m +"."+ $ext)`  == 1){
          $isEmissive = 1;
      }
      if(`filetest -f ($out_path + "/" + $name +"_opacity"+ "." +$m +"."+ $ext)`  == 1){
          $isOpacity = 1;
      }
  }

  if($isEmissive  == 1){
      //Emission
      setAttr ($material + ".emission_weight") 2;
      string $emission_name = $material + "_emissive";
      string $emission = `shadingNode -asTexture -isColorManaged file -name $emission_name`;
      setAttr ($emission+".uvTilingMode") 3;
      setAttr -type "string" ($emission + ".fileTextureName") ($out_path + "/" + $name +"_emissive.<UDIM>"+ "." + $ext);
      setAttr -type "string" ($emission + ".colorSpace") "sRGB";
      setAttr ($emission +".defaultColor") -type double3 0 0 0 ;
      connectPlace2D($place2D,$emission);
      connectAttr -f ($emission + ".outColor") ($material+".emission_color");
  }

  if($isOpacity  == 1){
      //Opacity
      string $opacity_name = $material + "_opacity";
      string $opacity = `shadingNode -asTexture -isColorManaged file -name $opacity_name`;
      setAttr ($opacity+".uvTilingMode") 3;
      setAttr -type "string" ($opacity + ".fileTextureName") ($out_path + "/" + $name +"_opacity.<UDIM>"+  "." + $ext);
      setAttr -type "string" ($opacity + ".colorSpace") "Raw";
      setAttr ($opacity +".defaultColor") -type double3 1 1 1 ;
      connectPlace2D($place2D,$opacity);
      connectAttr -f ($opacity + ".outColor") ($material+".opacity_color");
  }

  //Normal Map
  string $normal_name = $material + "_normal";
  string $normal = `shadingNode -asUtility RedshiftNormalMap -name $normal_name`;
  setAttr -type "string" ($normal + ".tex0") ($out_path + "/" + $name +"_normal.<UDIM>"+  "." + $ext);
  if($normal_format == 1){
    setAttr ($normal + ".flipY") 0;
  }else{
    setAttr ($normal + ".flipY") 1;
  }

  connectAttr -f ($normal + ".outDisplacementVector") ($material+".bump_input");


  //Displacement
  string $displacement_name = $material + "_height";
  string $displacement_disp = `shadingNode -asUtility RedshiftDisplacement -name ($displacement_name+"_disp")`;
  string $displacement = `shadingNode -asTexture -isColorManaged file -name $displacement_name`;
  setAttr ($displacement+".uvTilingMode") 3;
  setAttr -type "string" ($displacement + ".fileTextureName") ($out_path + "/" + $name +"_height.<UDIM>"+  "." + $ext);
  setAttr -type "string" ($displacement + ".colorSpace") "Raw";
  setAttr  ($displacement + ".alphaIsLuminance") 1;
  setAttr ($displacement_disp+".scale") 0.01;
  setAttr ($displacement_disp + ".newrange_min") -0.5;
  setAttr ($displacement_disp + ".newrange_max") 0.5;
  connectPlace2D($place2D,$displacement);
  connectAttr -f ($displacement + ".outColor") ($displacement_disp+".texMap");
  connectAttr -f ($displacement_disp + ".out") ($shader +".displacementShader");

  return "SUCCESS";
}


global proc string updateRSMaterialUdims(string $out_path, string $ext, string $name){

    string $material = "RS_"+$name;

    updateImage($name,$material,"_basecolor",$out_path,$ext);
    updateImage($name,$material,"_ao",$out_path,$ext);
    updateImage($name,$material,"_roughness",$out_path,$ext);
    updateImage($name,$material,"_metallic",$out_path,$ext);
    updateImage($name,$material,"_height",$out_path,$ext);
    updateImage($name,$material,"_emissive",$out_path,$ext);
    updateImage($name,$material,"_opacity",$out_path,$ext);

    string $normal_name = $material + "_normal";
    setAttr -type "string" ($normal_name + ".tex0") ($out_path + "/"+ $name +"_normal"+  "." + $ext);
    disconnectAttr ($normal_name+".outDisplacementVector") ($material+".bump_input");
    connectAttr -f ($normal_name + ".outDisplacementVector") ($material+".bump_input");

    return "SUCCESS";
}

global proc string updateRSMaterialPkdUdims(string $out_path, string $ext, string $name){

    string $material = "RS_"+$name;

    updateImage($name,$material,"_basecolor",$out_path,$ext);
    updateImage($name,$material,"_aoroughmetal",$out_path,$ext);
    updateImage($name,$material,"_height",$out_path,$ext);
    updateImage($name,$material,"_emissive",$out_path,$ext);
    updateImage($name,$material,"_opacity",$out_path,$ext);

    string $normal_name = $material + "_normal";
    setAttr -type "string" ($normal_name + ".tex0") ($out_path + "/"+ $name +"_normal.<UDIM>"+  "." + $ext);
    disconnectAttr ($normal_name+".outDisplacementVector") ($material+".bump_input");
    connectAttr -f ($normal_name + ".outDisplacementVector") ($material+".bump_input");

    return "SUCCESS";
}
