Material Change...Save

Material Change...Save

I'm trying to write a script that will change the color of a material and save it off into a new JAS. Here's what I have so far, but Cheetah just closes due to something it disagrees with in my script:

Code:
function main(doc) {
     //get path for file
   var path=OS.runSavePanel("jas");
   if (path==null) {
      return;
   }

   // find the "CommandColor"
   for(mat_indx=0;mat_indx < doc.materialCount(); ++mat_indx) {
      var mat=doc.materialAtIndex(mat_indx);
      if (mat.getParameter("name") == "CommandColor") {
         break;
      }
   }

   // i assume red, green, blue
   var colors = Array(Vec4D(255,0,0,0), Vec4D(0,255,0,0), Vec4D(0,0,255,0));
	
   for (color_indx= 0; color_indx < 3; ++color_indx) {
      mat.setParameter("color", colors[color_indx], false);
		
      doc.saveToFile(path,"file" + color_indx + ".jas");
   }
}

This is in my "scripts/Macros" directory, as that seems to have some meaning.
ANY help would be greatly appreciated.
Also, will new documents for scripting be coming?

Thanks
 
Hello.

please check this.

Code:
   var colors = Array(new Vec4D(255,0,0,0),new Vec4D(0,255,0,0),new Vec4D(0,0,255,0));
   
   for (color_indx= 0; color_indx < 3; ++color_indx) {
      var path_array = path.split("/");
      var file_name = path_array.pop(); // getting filename.
      var file_name_array = file_name.split(".");
      file_name_array.pop(); // remove extension.
      file_name = file_name_array.join(".") + "_" + color_indx + ".jas"; // change filename.
      path_array.push(file_name); // back changed filename to path
      var file_path = path_array.join("/"); // create full path for file.
      
      mat.setParameter("colorColor", colors[color_indx], false);

      doc.saveToFile(file_path,"jas");
   }

* 'new' needs for creating Object.
* parameter name of script is different from propertie's name. not 'color', but 'colorColor'.
* manually change file name of path returned from OS.runSavePanel()

and script parameter name of Material is below. ( I checked it with v.3.5 )
you can dump full list of object parameter name and type with getParamterInfo() function.

Code:
Material Parameters for script.

scriptName [String]
locked [Bool]
shaderType [String]
name [String]
colorColor [Vec4D]
colorTex [Texture]
colorMix [Float]
ambientIntensity [Float]
diffIntensity [Float]
specularColor [Vec4D]
specTex [Texture]
specMix [Float]
specSize [Float]
emissionColor [Vec4D]
bumpTex [Texture]
bumpIntensity [Float]
reflColor [Vec4D]
reflTex [Texture]
reflMix [Float]
reflIntensity [Float]
reflAngle [Float]
reflSamples [Float]
transColor [Vec4D]
transTex [Texture]
transMix [Float]
transIntensity [Float]
transEta [Float]
transUseAlpha [Bool]
transFresnel [Bool]

regards.

tg_jp, Hiroto.
 
Last edited:
Thank you

Thank you. thank you. thank you.

There is certainly a lot of power to wielded with these scripts. But they take some ingenuity on the part of the user to wrestle it out of them.
I was headed way out into left field after I posted.

Oh yeah...thank you.
 
Nitpicking

This is nitpicking now, but after the material has been modified by the script, the change is not reflected in the materials drawer.

document.redrawAll() didn't seem to touch it.

Is there anything to force the materials to check itself?

Thanks
 
Hello.

please try this.
Code:
      mat.setParameter("colorColor", colors[color_indx], true); // or just
      // mat.setParameter("colorClor", colors[color_indx]);
I checked preview of material view is updated after running script. but color-well of properties view is not.;)

regards.

tg_jp, Hiroto.
 
Update

Hey all, I'm also trying to change the diffuse colour of a material with no luck.

These example seem really old.

Here is what I have so far. Anybody know what I am missing?
PHP:
    for(i=0;i<doc.materialCount();i++){
    var mat = doc.materialAtIndex(i);
    var matcolor = mat.color();
    var matname = mat.getParameter("name");

    if (matname == "ship body") {
        print("changing");
        matcolor.set(0, 0, 1, 0);
    }


Also, should Document.render() bring up the render window? Seems to do nothing?
 
I think I have it working using

PHP:
            var n0 = mat.nodeAtIndex(0);
            n0.setParameter("diffColor", new Vec4D(255,128,0,1));


Would still love some help working out how to kick off a render.!



Thanks!
 
Thanks Martin, I was using Document.render not doc.render!

since you are around,

Any Idea how I get the scene to update if I want to render multiple times?

PHP:
            n0.setParameter("diffColor", new Vec4D(255,0,0,1), true);
            doc.render();

            n0.setParameter("diffColor", new Vec4D(0,255,0,1), true);
            doc.render();

update: or is there some kind of call back I can hook into for when a frame is finished rendering?
 
Last edited:
Hi,
the doc.render(); function doesn't return until the render job is finished. So there won't be any updates during the rendering.

Bye
Martin
 
Back
Top