Game Development Community

Automatic Material Definitions for TGEA

by Michael Perry · in Artist Corner · 12/15/2006 (1:16 pm) · 8 replies

Greetings all!

Though I still suck at using Constructor, my scripting skills are holding strong. This is for all you TGEA users out there who haven't already written your own auto-material definition generator.


[b]In Constructor\client\scripts\editCmdScriptDlg.cs, copy this function:[/b]
function generateMaterialDef(%textureName, %fileExt)
{
   %materialDef = "new Material(Constr_"@%textureName@"){\n"@
                  "\t baseTex[0] = \"" @ %textureName @ %fileExt @"\";\n" @
                  "\t mapTo =  \"" @ %textureName @ "\";\n" @
                  "};";
   echo(%materialDef);
   return %materialDef;
}

Now, you are going to modify the CmdCopyTextures in the same script file

[b]In the same file, find [i]function CmdCopyTextures::execute(%this)[/i], and make these changes:[/b]

function CmdCopyTextures::execute(%this)
{
   %dir = expandCmdInserts(%this.directory);

   [b]%file = new FileObject();                       // <=== CODE CHANGE[/b]
   [b]%file.OpenForWrite(%dir @ "/materials.cs");     // <=== CODE CHANGE[/b]
//    ExecCmdScriptDlg.addLine("CopyTextures:");   
//    ExecCmdScriptDlg.addLine("   directory = " @ %this.directory @ " (" @ %dir @ ")");

   %map = scene.getCurrentMap();
   if(! isObject(%map))
   {
      ExecCmdScriptDlg.addLine("Couldnt copy map textures, no map found ?!");
      return false;
   }

   ExecCmdScriptDlg.addLine("Copying textures to " @ %dir @ " ...");

  // %file = new FileObject();
   
   for(%i = 0;%i < %map.getTextureCount();%i++)
   {
      %t = %map.getTextureName(%i);
      %f = MMGetTextureFullNameGivenName(%t);
      %ext = %this.getTextureFilename(%f);
      if(%ext !$= "")
      {
        [b] %file.writeLine(generateMaterialDef(%t, %ext));     // <=== CODE CHANGE[/b]
         %t = %t @ %ext;
         %f = %f @ %ext;
         %to = %dir @ "/" @ %t;

         ExecCmdScriptDlg.addLine("   " @ %t);
         %ret = pathCopy(%f, %to, false);
         if(! %ret)
         {
            ExecCmdScriptDlg.addLine("Failed to copy texture.");
            return false;
         }
      }
      else
         ExecCmdScriptDlg.addLine("Could not find texture " @ %t);
   }

   schedule(0, 0, executeNextCmd);
[b]   %file.close();     // <=== CODE CHANGE[/b]
[b]   %file.delete();     // <=== CODE CHANGE[/b]
   return true;
}

So, when you copy your textures and new .dif to your interiors folder, copy the "materials.cs" that is created with everything else. No more manual writing of materials definitions for you! Feel free to upload this to the Constructor TDN Wiki.

#1
12/15/2006 (1:35 pm)
Sweet!
#2
02/09/2007 (1:09 pm)
I wonder what I'm doing wrong but I keep getting 'could not find texture' errors upon export and get an empty materials.cs file as a result. I'm using stock Constructor Beta16 with a map in my interiors folder where all the textures reside. It compiles fine though and the map does work inside the engine.
#3
02/09/2007 (1:34 pm)
Did you copy and paste my code, or did you type it out by hand?
#4
02/09/2007 (2:25 pm)
I copied it but I think the problem is in that it is failing on the if statement and going to the else with or without your code.
#5
02/11/2007 (3:19 am)
I'm pretty sure the problem is in the map2dif Export Configuration. When it gets to the command 'Copy textures to $(OutputPath)' during the export is when all those 'Could not find texture' errors come up. All I did was open an existing map file that was created in Hammer. Do I need to have a copy of the textures elsewhere besides the interiors folder?

Nick
#6
02/11/2007 (11:12 am)
Not too sure about this.....most of my tests have been on interiors created from scratch, or a .map from TGE 1.5 demo. AFAIK, how the texture locations are stored is an internal operation of Constructor, and has nothing to do with my code. Try restoring your original script, and see if the textures are copied. Then try to hand type my changes in to see if the material definitions file is generated.
#7
02/12/2007 (6:06 am)
You gave me a hint on what to look at and solved the problem. Textures used on a map need to be defined in the Material Browser for the 'Copy textures' command to work right even though it exports right. I wasn't defining the textures. Thanks for your help.

Nick
#8
02/12/2007 (6:08 am)
Happy to help. . .