//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 +  "." + $ext);
  }

}


//-------------------- ARNOLD --------------------//
global proc string addARMaterial(string $out_path, string $ext, string $name, int $normal_format){
    string $arMaterial = "AR_"+$name;
    string $material = `shadingNode -asShader aiStandardSurface -name $arMaterial`;
    string $arMaterialSG = $arMaterial + "SG";
    string $shader = `sets -renderable true -noSurfaceShader true -empty -name $arMaterialSG`;

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

    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 -type "string" ($basecolor + ".fileTextureName") ($out_path + "/" + $name +"_basecolor"+ "." + $ext);
    setAttr -type "string" ($basecolor + ".colorSpace") "sRGB";
    connectPlace2D($place2D,$basecolor);
    connectAttr -f ($basecolor + ".outColor") ($material+".baseColor");

    //Roughness
    string $rough_name = $material + "_roughness";
    string $rough = `shadingNode -asTexture -isColorManaged file -name $rough_name`;
    setAttr -type "string" ($rough + ".fileTextureName") ($out_path + "/" + $name +"_roughness"+ "." + $ext);
    setAttr -type "string" ($rough + ".colorSpace") "Raw";
    setAttr  ($rough + ".alphaIsLuminance") 1;
    connectPlace2D($place2D,$rough);

    string $rough_color_correct_name = $material + "_RoughColorCorrect";
    string $roughColorCorrect = `shadingNode -asUtility colorCorrect -name $rough_color_correct_name`;
    connectAttr -f ($rough + ".outAlpha") ($roughColorCorrect+".inAlpha");
    connectAttr -f ($roughColorCorrect + ".outAlpha") ($material+".specularRoughness");

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


    if(`filetest -f ($out_path + "/" + $name +"_emissive"+ "." + $ext)`  == 1){
        //Emission
        setAttr ($material + ".emission") 1;
        string $emission_name = $material + "_emissive";
        string $emission = `shadingNode -asTexture -isColorManaged file -name $emission_name`;
        setAttr -type "string" ($emission + ".fileTextureName") ($out_path + "/" + $name +"_emissive"+ "." + $ext);
        setAttr -type "string" ($emission + ".colorSpace") "sRGB";
        setAttr ($emission +".defaultColor") -type double3 0 0 0 ;
        connectPlace2D($place2D,$emission);
        connectAttr -f ($emission + ".outColor") ($material+".emissionColor");
    }

    if(`filetest -f ($out_path + "/" + $name +"_opacity"+ "." + $ext)`  == 1){
        //Opacity
        string $opacity_name = $material + "_opacity";
        string $opacity = `shadingNode -asTexture -isColorManaged file -name $opacity_name`;
        setAttr -type "string" ($opacity + ".fileTextureName") ($out_path + "/" + $name +"_opacity"+  "." + $ext);
        setAttr -type "string" ($opacity + ".colorSpace") "Raw";
        connectPlace2D($place2D,$opacity);
        connectAttr -f ($opacity + ".outColor") ($material+".opacity");
    }


    //Normal Map
    string $normal_name = $material + "_normal";
    string $normal = `shadingNode -asTexture -isColorManaged file -name $normal_name`;
    setAttr -type "string" ($normal + ".fileTextureName") ($out_path + "/" + $name +"_normal"+ "." + $ext);
    setAttr -type "string" ($normal + ".colorSpace") "Raw";
    connectPlace2D($place2D,$normal);
    string $normal_ai_name = $material + "_aiNormal";
    string $normal_ai = `shadingNode -asUtility aiNormalMap -name $normal_ai_name`;
    if($normal_format == 1){
      setAttr ($normal_ai + ".invertY") 0;
    }else{
      setAttr ($normal_ai + ".invertY") 1;
    }

    connectAttr -f ($normal + ".outColor") ($normal_ai+".input");
    connectAttr -f ($normal_ai + ".outValue") ($material+".normalCamera");

    //Displacement
    string $displacement_name = $material + "_height";
    string $displacement_disp = `shadingNode -asUtility displacementShader -name ($displacement_name+"_disp")`;
    string $displacement = `shadingNode -asTexture -isColorManaged file -name $displacement_name`;
    setAttr -type "string" ($displacement + ".fileTextureName") ($out_path + "/" + $name +"_height"+  "." + $ext);
    setAttr -type "string" ($displacement + ".colorSpace") "Raw";
    setAttr ($displacement_disp+".scale") 0.01;
    setAttr  ($displacement + ".alphaIsLuminance") 1;
    connectPlace2D($place2D,$displacement);
    connectAttr -f ($displacement + ".outAlpha") ($displacement_disp+".displacement");
    connectAttr -f ($displacement_disp + ".displacement") ($shader +".displacementShader");

    return "SUCCESS";
}

global proc string addARMaterialPkd(string $out_path, string $ext, string $name, int $normal_format){
    string $arMaterial = "AR_"+$name;
    string $material = `shadingNode -asShader aiStandardSurface -name $arMaterial`;
    string $arMaterialSG = $arMaterial + "SG";
    string $shader = `sets -renderable true -noSurfaceShader true -empty -name $arMaterialSG`;

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

    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 -type "string" ($basecolor + ".fileTextureName") ($out_path + "/" + $name +"_basecolor"+ "." + $ext);
    setAttr -type "string" ($basecolor + ".colorSpace") "sRGB";
    connectPlace2D($place2D,$basecolor);
    connectAttr -f ($basecolor + ".outColor") ($material+".baseColor");

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

    string $rough_color_correct_name = $material + "_RoughColorCorrect";
    string $roughColorCorrect = `shadingNode -asUtility colorCorrect -name $rough_color_correct_name`;
    connectAttr -f ($ao_rough_metal + ".outColorG") ($roughColorCorrect+".inAlpha");
    connectAttr -f ($roughColorCorrect + ".outAlpha") ($material+".specularRoughness");

    if(`filetest -f ($out_path + "/" + $name +"_emissive"+ "." + $ext)`  == 1){
        //Emission
        setAttr ($material + ".emission") 1;
        string $emission_name = $material + "_emissive";
        string $emission = `shadingNode -asTexture -isColorManaged file -name $emission_name`;
        setAttr -type "string" ($emission + ".fileTextureName") ($out_path + "/" + $name +"_emissive"+ "." + $ext);
        setAttr -type "string" ($emission + ".colorSpace") "sRGB";
        setAttr ($emission +".defaultColor") -type double3 0 0 0 ;
        connectPlace2D($place2D,$emission);
        connectAttr -f ($emission + ".outColor") ($material+".emissionColor");
    }

    if(`filetest -f ($out_path + "/" + $name +"_opacity"+ "." + $ext)`  == 1){
        //Opacity
        string $opacity_name = $material + "_opacity";
        string $opacity = `shadingNode -asTexture -isColorManaged file -name $opacity_name`;
        setAttr -type "string" ($opacity + ".fileTextureName") ($out_path + "/" + $name +"_opacity"+  "." + $ext);
        setAttr -type "string" ($opacity + ".colorSpace") "Raw";
        connectPlace2D($place2D,$opacity);
        connectAttr -f ($opacity + ".outColor") ($material+".opacity");
    }

    //Normal Map
    string $normal_name = $material + "_normal";
    string $normal = `shadingNode -asTexture -isColorManaged file -name $normal_name`;
    setAttr -type "string" ($normal + ".fileTextureName") ($out_path + "/" + $name +"_normal"+ "." + $ext);
    setAttr -type "string" ($normal + ".colorSpace") "Raw";
    connectPlace2D($place2D,$normal);
    string $normal_ai_name = $material + "_aiNormal";
    string $normal_ai = `shadingNode -asUtility aiNormalMap -name $normal_ai_name`;
    if($normal_format == 1){
      setAttr ($normal_ai + ".invertY") 0;
    }else{
      setAttr ($normal_ai + ".invertY") 1;
    }

    connectAttr -f ($normal + ".outColor") ($normal_ai+".input");
    connectAttr -f ($normal_ai + ".outValue") ($material+".normalCamera");

    //Displacement
    string $displacement_name = $material + "_height";
    string $displacement_disp = `shadingNode -asUtility displacementShader -name ($displacement_name+"_disp")`;
    string $displacement = `shadingNode -asTexture -isColorManaged file -name $displacement_name`;
    setAttr -type "string" ($displacement + ".fileTextureName") ($out_path + "/" + $name +"_height"+  "." + $ext);
    setAttr -type "string" ($displacement + ".colorSpace") "Raw";
    setAttr ($displacement_disp+".scale") 0.01;
    setAttr  ($displacement + ".alphaIsLuminance") 1;
    connectPlace2D($place2D,$displacement);
    connectAttr -f ($displacement + ".outAlpha") ($displacement_disp+".displacement");
    connectAttr -f ($displacement_disp + ".displacement") ($shader +".displacementShader");

    return "SUCCESS";
}

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

    string $material = "AR_"+$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);
    arnoldFlushCache -textures 1 ;

    return "SUCCESS";
}

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

    string $material = "AR_"+$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);

    arnoldFlushCache -textures 1 ;

    return "SUCCESS";
}
