//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);
  }

}


//-------------------- VRAY --------------------//

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


  string $vrMaterial = "VR_"+$name;
  string $material = `shadingNode -asShader VRayMtl -name $vrMaterial`;
  string $vrMaterialSG = $vrMaterial + "SG";
  string $shader = `sets -renderable true -noSurfaceShader true -empty -name $vrMaterialSG`;


  connectAttr -f ($material + ".outColor ") ($shader +".surfaceShader");
  setAttr ($material + ".brdfType") 3;
  setAttr ($material + ".reflectionColor") -type double3 1 1 1;
  setAttr ($material + ".useFresnel") 1;
  setAttr ($material + ".lockFresnelIORToRefractionIOR") 0;
  setAttr ($material + ".bumpMapType") 1;

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

  //Diffuse
  string $diffuse_name = $material + "_diffuse";
  string $diffuse = `shadingNode -asTexture -isColorManaged file -name $diffuse_name`;
  setAttr ($diffuse+".uvTilingMode") 3;
  setAttr -type "string" ($diffuse + ".fileTextureName") ($out_path + "/" + $name +"_diffuse.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($diffuse + ".colorSpace") "sRGB";
  connectPlace2D($place2D,$diffuse);
  connectAttr -f ($diffuse + ".outColor") ($material+".diffuseColor");

  //Reflection
  string $reflection_name = $material + "_reflection";
  string $reflection = `shadingNode -asTexture -isColorManaged file -name $reflection_name`;
  setAttr ($reflection+".uvTilingMode") 3;
  setAttr -type "string" ($reflection + ".fileTextureName") ($out_path + "/" + $name +"_reflection.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($reflection + ".colorSpace") "sRGB";
  connectPlace2D($place2D,$reflection);
  connectAttr -f ($reflection + ".outColor") ($material+".reflectionColor");

  //Glossiness
  string $gloss_name = $material + "_glossiness";
  string $gloss = `shadingNode -asTexture -isColorManaged file -name $gloss_name`;
  setAttr ($gloss+".uvTilingMode") 3;
  setAttr -type "string" ($gloss + ".fileTextureName") ($out_path + "/" + $name +"_glossiness.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($gloss + ".colorSpace") "Raw";
  setAttr  ($gloss + ".alphaIsLuminance") 1;
  connectPlace2D($place2D,$gloss);
  connectAttr -f ($gloss + ".outAlpha") ($material+".reflectionGlossiness");

  //IOR
  string $fresnel_name = $material + "_ior";
  string $fresnel = `shadingNode -asTexture -isColorManaged file -name $fresnel_name`;
  setAttr ($fresnel+".uvTilingMode") 3;
  setAttr -type "string" ($fresnel + ".fileTextureName") ($out_path + "/" + $name +"_ior.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($fresnel + ".colorSpace") "Raw";
  setAttr  ($fresnel + ".alphaIsLuminance") 1;
  connectPlace2D($place2D,$fresnel);
  connectAttr -f ($fresnel + ".outAlpha") ($material+".fresnelIOR");

  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
      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 ;
      setAttr ($material +".illumGI") 1;
      connectPlace2D($place2D,$emission);
      connectAttr -f ($emission + ".outColor") ($material+".illumColor");
  }

  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+".opacityMap");
  }

  //Normal
  string $normal_name = $material + "_normal";
  string $normal = `shadingNode -asTexture -isColorManaged file -name $normal_name`;
  setAttr ($normal+".uvTilingMode") 3;
  setAttr -type "string" ($normal + ".fileTextureName") ($out_path + "/" + $name +"_normal.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($normal + ".colorSpace") "Raw";
  connectPlace2D($place2D,$normal);
  if($normal_format == 1){
    connectAttr -f ($normal + ".outColor") ($material+".bumpMap");
  }else{
    string $normal_remap = `shadingNode -asUtility remapColor`;
    setAttr ($normal_remap +".green[0].green_FloatValue") 1;
    setAttr ($normal_remap +".green[1].green_FloatValue") 0;
    connectAttr -f ($normal + ".outColor") ($normal_remap+".color");
    connectAttr -f ($normal_remap + ".outColor") ($material+".bumpMap");
  }

  return "SUCCESS";
}

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


  string $vrMaterial = "VR_"+$name;
  string $material = `shadingNode -asShader VRayMtl -name $vrMaterial`;
  string $vrMaterialSG = $vrMaterial + "SG";
  string $shader = `sets -renderable true -noSurfaceShader true -empty -name $vrMaterialSG`;


  connectAttr -f ($material + ".outColor ") ($shader +".surfaceShader");
  setAttr ($material + ".brdfType") 3;
  setAttr ($material + ".reflectionColor") -type double3 1 1 1;
  setAttr ($material + ".useFresnel") 1;
  setAttr ($material + ".lockFresnelIORToRefractionIOR") 0;
  setAttr ($material + ".bumpMapType") 1;

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

  //Diffuse
  string $diffuse_name = $material + "_diffuse";
  string $diffuse = `shadingNode -asTexture -isColorManaged file -name $diffuse_name`;
  setAttr ($diffuse+".uvTilingMode") 3;
  setAttr -type "string" ($diffuse + ".fileTextureName") ($out_path + "/" + $name +"_diffuse.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($diffuse + ".colorSpace") "sRGB";
  connectPlace2D($place2D,$diffuse);
  connectAttr -f ($diffuse + ".outColor") ($material+".diffuseColor");

  //Reflection
  string $reflection_name = $material + "_reflection";
  string $reflection = `shadingNode -asTexture -isColorManaged file -name $reflection_name`;
  setAttr ($reflection+".uvTilingMode") 3;
  setAttr -type "string" ($reflection + ".fileTextureName") ($out_path + "/" + $name +"_reflection.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($reflection + ".colorSpace") "sRGB";
  connectPlace2D($place2D,$reflection);
  connectAttr -f ($reflection + ".outColor") ($material+".reflectionColor");

  //Glossiness
  string $glossior_name = $material + "_glossior";
  string $glossior = `shadingNode -asTexture -isColorManaged file -name $glossior_name`;
  setAttr ($glossior+".uvTilingMode") 3;
  setAttr -type "string" ($glossior + ".fileTextureName") ($out_path + "/" + $name +"_glossior.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($glossior + ".colorSpace") "Raw";
  connectPlace2D($place2D,$glossior);
  connectAttr -f ($glossior + ".outColorR") ($material+".reflectionGlossiness");
  connectAttr -f ($glossior + ".outColorG") ($material+".fresnelIOR");

  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
      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 ;
      setAttr ($material +".illumGI") 1;
      connectPlace2D($place2D,$emission);
      connectAttr -f ($emission + ".outColor") ($material+".illumColor");
  }

  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+".opacityMap");
  }

  //Normal
  string $normal_name = $material + "_normal";
  string $normal = `shadingNode -asTexture -isColorManaged file -name $normal_name`;
  setAttr ($normal+".uvTilingMode") 3;
  setAttr -type "string" ($normal + ".fileTextureName") ($out_path + "/" + $name +"_normal.<UDIM>"+ "." + $ext);
  setAttr -type "string" ($normal + ".colorSpace") "Raw";
  connectPlace2D($place2D,$normal);
  if($normal_format == 1){
    connectAttr -f ($normal + ".outColor") ($material+".bumpMap");
  }else{
    string $normal_remap = `shadingNode -asUtility remapColor`;
    setAttr ($normal_remap +".green[0].green_FloatValue") 1;
    setAttr ($normal_remap +".green[1].green_FloatValue") 0;
    connectAttr -f ($normal + ".outColor") ($normal_remap+".color");
    connectAttr -f ($normal_remap + ".outColor") ($material+".bumpMap");
  }

  return "SUCCESS";
}

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

    string $material = "VR_"+$name;

    updateImage($name,$material,"_diffuse",$out_path,$ext);
    updateImage($name,$material,"_reflection",$out_path,$ext);
    updateImage($name,$material,"_ior",$out_path,$ext);
    updateImage($name,$material,"_glossiness",$out_path,$ext);
    updateImage($name,$material,"_normal",$out_path,$ext);
    updateImage($name,$material,"_height",$out_path,$ext);
    updateImage($name,$material,"_emissive",$out_path,$ext);
    updateImage($name,$material,"_opacity",$out_path,$ext);


    return "SUCCESS";
}

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

    string $material = "VR_"+$name;

    updateImage($name,$material,"_diffuse",$out_path,$ext);
    updateImage($name,$material,"_reflection",$out_path,$ext);
    updateImage($name,$material,"_glossior",$out_path,$ext);
    updateImage($name,$material,"_normal",$out_path,$ext);
    updateImage($name,$material,"_height",$out_path,$ext);
    updateImage($name,$material,"_emissive",$out_path,$ext);
    updateImage($name,$material,"_opacity",$out_path,$ext);


    return "SUCCESS";
}
