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

//-------------------- MAIN --------------------------------

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

  string $imgName = $material + $map;
  if( `objExists imgName` ) {
    setAttr -type "string" ($imgName + ".filename") ($out_path + "/" + $name + $map +  "._MAPID_.tex");
  }

}


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



  string $rmMaterial = "RM_"+$name;
  string $material = `shadingNode -asShader PxrDisney -name $rmMaterial`;
  string $rmMaterialSG = $rmMaterial + "SG";
  string $shader = `sets -renderable true -noSurfaceShader true -empty -name $rmMaterialSG`;


  connectAttr -f ($material + ".outColor ") ($shader +".surfaceShader");



  //Base Color
  string $basecolor_name = $material + "_basecolor";
  string $basecolor = `shadingNode -asTexture PxrTexture -name $basecolor_name`;
  setAttr ($basecolor+".atlasStyle") 1;
  setAttr -type "string" ($basecolor + ".filename") ($out_path + "/" + $name +"_basecolor._MAPID_.tex");
  string $attr = `setAttr  ($basecolor + ".linearize") 1`;
  connectAttr -f ($basecolor + ".resultRGB") ($material+".baseColor");


  //Roughness
  string $rough_name = $material + "_roughness";
  string $rough = `shadingNode -asTexture PxrTexture -name $rough_name`;
  setAttr ($rough+".atlasStyle") 1;
  setAttr -type "string" ($rough + ".filename") ($out_path + "/" + $name +"_roughness._MAPID_.tex");
  string $attr = `setAttr  ($rough + ".linearize") 0`;
  connectAttr -f ($rough + ".resultR") ($material+".roughness");


  //Metallic
  string $metal_name = $material + "_metallic";
  string $metal = `shadingNode -asTexture PxrTexture -name $metal_name`;
  setAttr ($metal+".atlasStyle") 1;
  setAttr -type "string" ($metal + ".filename") ($out_path + "/" + $name +"_metallic._MAPID_.tex");
  string $attr = `setAttr  ($metal + ".linearize") 0`;
  connectAttr -f ($metal + ".resultR") ($material+".metallic");



  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 PxrTexture -name $emission_name`;
      setAttr ($emission+".atlasStyle") 1;
      setAttr -type "string" ($emission + ".filename") ($out_path + "/" + $name +"_emissive._MAPID_.tex");
      string $attr = `setAttr  ($emission + ".linearize") 1`;
      setAttr ($emission +".missingColor") -type float3 0 0 0 ;
      connectAttr -f ($emission + ".resultRGB") ($material+".emitColor");
  }

  if($isOpacity  == 1){
      //Opacity
      string $opacity_name = $material + "_opacity";
      string $opacity = `shadingNode -asTexture PxrTexture -name $opacity_name`;
      setAttr ($opacity+".atlasStyle") 1;
      setAttr -type "string" ($opacity + ".filename") ($out_path + "/" + $name +"_opacity._MAPID_.tex");
      string $attr = `setAttr  ($opacity + ".linearize") 0`;
      setAttr ($opacity +".missingColor") -type float3 1 1 1 ;
      connectAttr -f ($opacity + ".resultR") ($material+".presence");
  }


  //Normal
  string $normal_name = $material + "_normal";
  string $normal = `shadingNode -asTexture PxrNormalMap -name $normal_name`;
  setAttr ($normal+".atlasStyle") 1;
  setAttr -type "string" ($normal + ".filename") ($out_path + "/" + $name +"_normal._MAPID_.tex");
  if($normal_format == 1){
    setAttr ($normal + ".flipY") 0;
  }else{
    setAttr ($normal + ".flipY") 1;
  }
  connectAttr -f ($normal + ".resultN") ($material+".bumpNormal");

  //Displacement
  string $displacement_name = $material + "_height";
  string $displacement = `shadingNode -asTexture PxrTexture -name $displacement_name`;
  setAttr ($displacement+".atlasStyle") 1;
  setAttr -type "string" ($displacement + ".filename") ($out_path + "/" + $name +"_height._MAPID_.tex");
  string $attr = `setAttr  ($displacement + ".linearize") 0`;

  string $displacement_disp = `shadingNode -asUtility PxrDisplace -name ($displacement_name+"_disp")`;
  string $attr = `setAttr  ($displacement_disp + ".dispAmount") 0.1`;
  connectAttr -f ($displacement + ".resultRGB") ($displacement_disp+".dispVector");
  connectAttr -f ($displacement_disp + ".outColor") ($shader +".displacementShader");

  return "SUCCESS";
}


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

  string $rmMaterial = "RM_"+$name;
  string $material = `shadingNode -asShader PxrDisney -name $rmMaterial`;
  string $rmMaterialSG = $rmMaterial + "SG";
  string $shader = `sets -renderable true -noSurfaceShader true -empty -name $rmMaterialSG`;


  connectAttr -f ($material + ".outColor ") ($shader +".surfaceShader");


  //Base Color
  string $basecolor_name = $material + "_basecolor";
  string $basecolor = `shadingNode -asTexture PxrTexture -name $basecolor_name`;
  setAttr ($basecolor+".atlasStyle") 1;
  setAttr -type "string" ($basecolor + ".filename") ($out_path + "/" + $name +"_basecolor._MAPID_.tex");
  string $attr = `setAttr  ($basecolor + ".linearize") 1`;
  connectAttr -f ($basecolor + ".resultRGB") ($material+".baseColor");

  //Roughness & Metallic
  string $ao_rough_metal_name = $material + "_aoroughmetal";
  string $ao_rough_metal = `shadingNode -asTexture PxrTexture -name $ao_rough_metal_name`;
  setAttr ($ao_rough_metal+".atlasStyle") 1;
  setAttr -type "string" ($ao_rough_metal + ".filename") ($out_path + "/" + $name +"_aoroughmetal._MAPID_.tex");
  string $attr = `setAttr  ($ao_rough_metal + ".linearize") 0`;
  connectAttr -f ($ao_rough_metal + ".resultG") ($material+".roughness");
  connectAttr -f ($ao_rough_metal + ".resultB") ($material+".metallic");


  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 PxrTexture -name $emission_name`;
      setAttr ($emission+".atlasStyle") 1;
      setAttr -type "string" ($emission + ".filename") ($out_path + "/" + $name +"_emissive._MAPID_.tex");
      string $attr = `setAttr  ($emission + ".linearize") 1`;
      setAttr ($emission +".missingColor") -type float3 0 0 0 ;
      connectAttr -f ($emission + ".resultRGB") ($material+".emitColor");
  }

  if($isOpacity  == 1){
      //Opacity
      string $opacity_name = $material + "_opacity";
      string $opacity = `shadingNode -asTexture PxrTexture -name $opacity_name`;
      setAttr ($opacity+".atlasStyle") 1;
      setAttr -type "string" ($opacity + ".filename") ($out_path + "/" + $name +"_opacity._MAPID_.tex");
      string $attr = `setAttr  ($opacity + ".linearize") 0`;
      setAttr ($opacity +".missingColor") -type float3 1 1 1 ;
      connectAttr -f ($opacity + ".resultR") ($material+".presence");
  }


  //Normal
  string $normal_name = $material + "_normal";
  string $normal = `shadingNode -asTexture PxrNormalMap -name $normal_name`;
  setAttr ($normal+".atlasStyle") 1;
  setAttr -type "string" ($normal + ".filename") ($out_path + "/" + $name +"_normal._MAPID_.tex");
  if($normal_format == 1){
    setAttr ($normal + ".flipY") 0;
  }else{
    setAttr ($normal + ".flipY") 1;
  }
  connectAttr -f ($normal + ".resultN") ($material+".bumpNormal");

  //Displacement
  string $displacement_name = $material + "_height";
  string $displacement = `shadingNode -asTexture PxrTexture -name $displacement_name`;
  setAttr ($displacement+".atlasStyle") 1;
  setAttr -type "string" ($displacement + ".filename") ($out_path + "/" + $name +"_height._MAPID_.tex");
  string $attr = `setAttr  ($displacement + ".linearize") 0`;

  string $displacement_disp = `shadingNode -asUtility PxrDisplace -name ($displacement_name+"_disp")`;
  string $attr = `setAttr  ($displacement_disp + ".dispAmount") 0.1`;
  connectAttr -f ($displacement + ".resultRGB") ($displacement_disp+".dispVector");
  connectAttr -f ($displacement_disp + ".outColor") ($shader +".displacementShader");


  return "SUCCESS";
}


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

    string $material = "RM_"+$name;

    updateImage($name,$material,"_basecolor",$out_path,$ext);
    updateImage($name,$material,"_aoroughmetal",$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 updateRMMaterialUdims(string $out_path, string $ext, string $name){

    string $material = "RM_"+$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,"_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";
}
