Batch Converting Materials

Hi,

I bought one of those model packs - this one is about 100 different medium poly cars. Look great with default software render but I’m using Sun + Sky setup and of course mental ray and the result is that it looks quite terrible under these circumstances.

All these cars are in separate files and since there is a 100+ of them, this would be an utter waste of time to convert each file that contains 10 materials each on a hundred different files - my question is that is it possible to batch covert these files and convert all the blinn’s into mia_materials somehow?

I’m not sure on where to start or how to do this.

you’d have to write a mel script that would look at the shaders assigned and replace them with another shader

Are the shaders on each car named the same things?

Sorry, thanks for replying.

They are all different names.

ie Alpha_logo
Alpha_red
then the next set is
Audi_logo
Audi_black etc… etc…

The only common denominator is that they are all phong materials. (I mentioned blinn in the first post, but I meant phong). Just wondering if it is possible.

I know Master Zap has released a script for max to convert ANY material to mia_material, including vray textures. However 3DS only

heres a template make sure you make the .. any other mapping you need comes here… more suiting your needs, So tell maya what attribute maps what attribute in mia end. (dont have time to do it)

 
proc connectAndSet(string $original,string $target){
  $conn=`connectionInfo -sfd $original`;
  if ($conn!=""){
    connectAttr -force $conn $target;
  } else {
    connectAttr -force $original $target;
    disconnectAttr $original $target;
  }
}
 
proc convertPhongToMia(string $original){
  $target=`mrCreateCustomNode -asShader "" mia_material`;
  connectAndSet($original+".color",$target+".diffuse");
  //... any other mapping you need comes here...
 
  // a bit weak test should work for simple materials,
  // not used in special context
  $sg=`connectionInfo -dfs ($original+".outColor")`;
  if ($sg[0]!=""){
    $sgr=`match "[^.]*" ((string)$sg[0])`;
    connectAttr -force ($target+".outValue") ($sgr+".miMaterialShader");
    connectAttr -force ($target+".outValue") ($sgr+".miPhotonShader");
    connectAttr -force ($target+".outValue") ($sgr+".miShadowShader");
  }
  delete  $original;
  rename $target $original;
}
 
 
 
 
for ($item in`ls -et phong`)
  convertPhongToMia($item);

Have I told you how awesome you are Joojaa? Many thanks!

I’ll try and fill in the gaps, but this will save an unbelievable amount of time. Thanks again.

Thank my no show up client, had a 20 minutes of spare time to spend on something useful

Hey, understand what you mean about clients. heh.

So I tried this today - I was able to add in extra attributes for it to connect (works a treat) but there’s only one major thing that didn’t work.

The script seemed to be looking for a material called “phong1” for it to execute. Which of course couldn’t find anything.

So I renamed all the materials to phong1, phong2, phong3 sequentially etc.. etc… but the script seemed to only work on the first material called phong1. It would break after converting the first material. Am I missing a step? I don’t know if I’m doing it right.

I look into it - I think there needs to be something adjusted in this line because the script fails here.

$conn=connectionInfo -sfd phong1.color;

I’m trying to figure out MEL - I think I’ll add in a wildcard or something that selects all the materials.

My bad lik i said its a bit fast done. Change:

$conn=connectionInfo -sfd phong1.color;

to:

$conn=connectionInfo -sfd ($original);

Its a bit of residue of how i tested it during developpment.

Perfect, that did the trick - it runs flawlessly. Thanks

Hi Joojaa

Thanks for that script

I have a small problem though and my knowledge on scripting is bad.

I have changed your script a little bit so that it converts to mia_material_x instead (for adding bump maps(im not sure how to do a standard bump on the normal mia_material))

My problem is that the material after I run the script, is connected to both shading groups. The objects that has the new material still has the old material’s shading group attached to it for the viewport. If I render it is fine, but it is green in the viewport. If I drag and drop the newly created shading group that was done in the script on the object, then everything seems fine.

Is there a way to remove the old shading group and have the newly created one assigned?

I also attached a screenshot if it may help

Here is the changed mel script:
//----------------------------------------------
proc connectAndSet(string $original,string $target){

$conn=connectionInfo -sfd $original;

if ($conn!=“”){

connectAttr -force $conn $target;

} else {

connectAttr -force $original $target;

disconnectAttr $original $target;

}

}

proc convertPhongToMia(string $original){

$target=mrCreateCustomNode -asShader "" mia\_material\_x;

connectAndSet($original+“.color”,$target+“.diffuse”);

//… any other mapping you need comes here…

// a bit weak test should work for simple materials,

// not used in special context

$sg=connectionInfo -dfs ($original+".outColor");

if ($sg[0]!=“”){

$sgr=match "[^.]*" ((string)$sg[0]);

connectAttr -force ($target+“.message”) ($sgr+“.miMaterialShader”);

connectAttr -force ($target+“.message”) ($sgr+“.miPhotonShader”);

connectAttr -force ($target+“.message”) ($sgr+“.miShadowShader”);

}

delete $original;

rename $target $original;

}

for ($item inls -et phong)

convertPhongToMia($item);

//------------------------------------------------------------------------------------

many thx in advance

Phil

Yeah

proc connectAndSet(string $original,string $target){
    $conn=`connectionInfo -sfd $original`;
    if ($conn!=""){
        connectAttr -force $conn $target;
    } else {
        connectAttr -force $original $target;
        disconnectAttr $original $target;
    }
}
 
proc convertPhongToMia(string $original){
   $target=`mrCreateCustomNode -asShader "" mia_material_x`;
 
   connectAndSet($original+".color",$target+".diffuse");
   //#... any other mapping you need comes here...
   //# a bit weak test should work for simple materials,
   //# not used in special context
    $sg=`connectionInfo -dfs ($target+".message")`;
    $sgr=`match "[^.]*" ((string)$sg[0])`;
    $sg0=`connectionInfo -dfs ($original+".outColor")`;
    $sgr0=`match "[^.]*" ((string)$sg0[0])`;
    sets -e -forceElement $sgr `sets -q $sgr0`;
    delete $original;
    rename $sgr $sgr0;
    rename $target $original;
}
 
for ($item in`ls -et phong`)
    convertPhongToMia($item);

but perhaps mia_material_x_layers would be better?

Awesome thanks Joojaa !

I will give this a try, and what are mia_material_x_layers for mostly?  For compositing?

Hi Joojaa,

Just wanted to say thanks again, I tested it and it works perfectly :slight_smile:

what are mia_material_x_layers for mostly?  For compositing?

Yeah, it allows you to split out the individual parts of the miamaterials shader to separate files (this is allways useful even if you dont use the layers, the pennalty is minimal). But yeah theres a upconvert button inside the AEtemplate that can do the conversion even at a later date.

I tried to modify this script to use Arnold shaders, the aiStandard in Maya 13.5 w/ MtoA. I didn’t quite get there. This script will make one arnold shader and then quit. The shader it makes has no shading group and is not assigned to the geo. Anyone have some friendly advice on this? One more strange thing I noticed that might be related is that when you create a new aiStandard shader it doesn’t create a shading group until you assign it to geo.

Here is the script w/ only mods being changing shader to aiStandard and mapping color to color:


proc connectAndSet(string $original,string $target){

$conn=connectionInfo -sfd $original;

if ($conn!=“”){

connectAttr -force $conn $target;

} else {

connectAttr -force $original $target;

disconnectAttr $original $target;

}

}

proc convertPhongToMia(string $original){

$target=mrCreateCustomNode -asShader "" aiStandard;

connectAndSet($original+“.color”,$target+“.color”);

//#… any other mapping you need comes here…

//# a bit weak test should work for simple materials,

//# not used in special context

$sg=connectionInfo -dfs ($target+".message");

$sgr=match "[^.]*" ((string)$sg[0]);

$sg0=connectionInfo -dfs ($original+".outColor");

$sgr0=match "[^.]*" ((string)$sg0[0]);

sets -e -forceElement $sgr sets -q $sgr0;

delete $original;

rename $sgr $sgr0;

rename $target $original;

}

for ($item inls -et phong)

convertPhongToMia($item);

Hi there and please help

ToastI modifi this script and works fine for me, i have a same problem like I just want to chage from phong to lambert.

How can`t convert the Ambient Color, not just the color ? please help.


[b]proc connectAndSet(string $original,string $target){
  $conn=connectionInfo -sfd ($original);
  if ($conn!=“”){
    connectAttr -force $conn $target;
  } else {
    connectAttr -force $original $target;
    disconnectAttr $original $target;
  }
}

proc convertPhongToMia(string $original){
  $target=mrCreateCustomNode -asShader "" lambert;
  connectAndSet($original+“.color”,$target+“.color”);
  //… any other mapping you need comes here…

// a bit weak test should work for simple materials,
  // not used in special context
  $sg=connectionInfo -dfs ($original+".outColor");
  if ($sg[0]!=“”){
    $sgr=match "[^.]*" ((string)$sg[0]);
    connectAttr -force ($target+“.outColor”) ($sgr+“.surfaceShader”);
  }
  delete  $original;
  rename $target $original;
}

for ($item inls -et phong)
  convertPhongToMia($item);[/b]

 

Do you mean mib_illum_lambert? Or mayas lambert shader?

if mib_illum_lambert just add:

connectAndSet($original+".ambientColor",$target+".ambient");

in the additional mappings section.

Anyway your in luck i dont frequent often and the thread is 5 years old.

Hi, I saw ur post , I have the same problem but sort of change… I have a huge file with mia_material_x_passes and mia_material_x shaders and want to convert them to alsurface shader of arnold… can u please give me a script for it.

This code saved my ass, it is so super helpfull! Thank you 1000 times :smile:

hey sorry to bring the old topic…
I am trying to use this template to convert VRayMtl to lambert and got this error on line 12
// Error: line 12: Too many arguments. Expected 0, found 1. //

proc connectAndSet(string $original, string $target){
    $conn = `connectionInfo -sfd $original`;
    if ($conn != ""){
        connectAttr -force $conn $target;
    } else {
        connectAttr -force $original $target;
        disconnectAttr $original $target;
    }
}

proc convertVrayToLambert(string $original){
    $target = `shadingNode -asShader "" lambert`;

    connectAndSet($original + ".color", $target + ".color");
    // ... any other mapping you need comes here...
    // a bit weak test should work for simple materials,
    // not used in special context
    $sg = `connectionInfo -dfs ($target + ".message")`;
    $sgr = `match "[^.]*" ((string)$sg[0])`;
    $sg0 = `connectionInfo -dfs ($original + ".outColor")`;
    $sgr0=`match "[^.]*" ((string)$sg0[0])`;
    sets -e -forceElement $sgr `sets -q $sgr0`;
    //delete $original;
    rename $sgr $sgr0;
    rename $target $original;
}

for ($item in `ls -et VRayMtl`)
    convertVrayToLambert($item);

any idea?
Thanks you!