source: GTP/branches/IllumWPdeliver2008dec/IlluminationWP/precompiled/app/MayaExporter/GTPSceneExport.mel @ 3255

Revision 3255, 13.3 KB checked in by szirmay, 15 years ago (diff)
Line 
1// ===== Create Ogre menu
2namespace -add "GTPSceneExporter";
3namespace -set "GTPSceneExporter";
4
5setParent "MayaWindow";
6menu -label "GTP" -tearOff false;
7menuItem -label "Export Scene" -command "GTPSceneExporter";
8menuItem -label "Path Map" -subMenu true;
9menuItem -label "Add Path Map attributes" -command "GTPAddPathMapAttr";
10
11//GTPSceneExporter();
12
13global proc runGTPSceneExport()
14{
15        string $outputDir    = (`textField -query -text OutputDirectory`);
16        if (!endsWith($outputDir,"\\") && !endsWith($outputDir,"/") && (size($outputDir)>0))
17                $outputDir += "/";
18        string $sceneFile     = (`textField -query -text SceneFileName`);
19       
20        $sceneFileName = $outputDir + $sceneFile;
21    $fileId = `fopen $sceneFileName "w"`;
22   
23    string $options;
24    string $shapes[];
25    $shapes=`ls -geometry`;
26   
27    string $commandFile = $outputDir + "GTP_SceneExport.bat";
28    int $comandFileID;
29    string $commands = "";
30    if (`checkBox -query -value CreateBinary`)
31    {
32        $comandFileID = `fopen $commandFile "w"`;
33        string $OgreDir = (`textField -query -text OgrePath`);
34        $OgreDir = toNativePath($OgreDir);             
35        $commands += "path=" + $OgreDir + "\n";
36    }
37               
38    //geometry
39    for($index=0;$index<size($shapes);$index++)
40        {
41            string $shapename = $shapes[$index];
42            string $shapeNameTokens[];
43            tokenize $shapename "|" $shapeNameTokens;
44            $shapename = $shapeNameTokens[size($shapeNameTokens) - 1];
45            $meshFileName = encodeString(toNativePath($outputDir+$shapename + ".mesh.xml"));
46           
47            fprint $fileId("mesh "+$shapename+"\n{\n");
48            fprint $fileId("\togreXMLfile "+$shapename + ".mesh.xml"+"\n");
49            fprint $fileId("\togrefile "+$shapename + ".mesh"+"\n");
50            int $exportPM = `checkBox -q -v ExportPMAttr`;
51            if($exportPM)
52            {
53                   select $shapes[$index];
54                   $PRMRes = 64;
55                   if(attributeExists("PhotonMapResolution", $shapes[$index]))
56                      $PRMRes = `getAttr .prmRes`;
57                   fprint $fileId("\tpathmapresolution " + $PRMRes + "\n");
58                   $PRMDiv = 1;
59                   if(attributeExists("PhotonMapSubdivision", $shapes[$index]))
60                      $PRMDiv = `getAttr .prmDiv`;
61                   fprint $fileId("\tdivide " + $PRMDiv + "\n");
62            }   
63            fprint $fileId "}\n\n";
64           
65            //mesh export
66                int $exportMeshes = `checkBox -query -value ExportMeshes`;
67                if($exportMeshes)
68                {
69                        select $shapename;
70                        $options += " -sel";
71                        $options += " -obj";
72                        $options = "";
73                $options += " -mesh";
74                        $options += " \"" + $meshFileName + "\"";
75                        $options += " -shared -n -t";
76                        eval ("ogreExport" + $options);         
77                }
78               
79                //binary creation
80                if (`checkBox -query -value CreateBinary`)
81                {
82                        int $size = size($meshFileName);
83                        $size -= 4;
84                        string $binMeshFileName = startString($meshFileName, $size);
85                        $commands += "OgreXMLConverter";
86                        if (!`checkBox -query -value BuildEdgeList`)
87                                $commands += " -e ";
88                        if (`checkBox -query -value BuildTangents`)
89                                $commands += " -t ";
90                        $commands += " \"" + $meshFileName + "\"" + " \"" + $binMeshFileName + "\"" + "\n";
91                        //$commands += "pause \n";                                     
92                }
93               
94                //entities with transforms
95                string $transforms[] = `listRelatives -ap $shapes[$index]`;
96                for($i = 0; $i<size($transforms);$i++)
97                {
98                        $entityName = $transforms[$i];
99                           
100                        select $entityName;
101                        float $transformation[] = `getAttr .worldMatrix`;
102                        string $transformationString = "";
103                               
104                        for($start = 0; $start < 4 ; $start++)
105                           for($t = 0; $t < 4 ; $t++)
106                             $transformationString += ($transformation[$start + $t * 4] + " ");
107                                                   
108                        fprint $fileId("entity "+ $entityName + "\n{\n" );
109                fprint $fileId("\tmesh "+ $shapename + "\n");
110                fprint $fileId("\ttransformation "+ $transformationString + "\n");
111                if($exportPM)
112                {
113                      $PRMClus = 32;
114                      if(attributeExists("PathMapClusters", $shapes[$index]))
115                         $PRMClus = `getAttr .prmClus`;
116                      fprint $fileId("\tpathmapclusters " + $PRMClus + "\n");
117                      string $PMTexturePath = (`textField -query -text PMPath`);
118                  if (!endsWith($PMTexturePath,"\\") && !endsWith($PMTexturePath,"/") && (size($PMTexturePath)>0))
119                        $PMTexturePath += "/";
120                      fprint $fileId("\tpathmapfile " + toNativePath($PMTexturePath) + $entityName + "\n");
121                }
122                fprint $fileId "}\n\n";
123                }
124        }
125
126        if (`checkBox -query -value CreateBinary`)
127        {
128          fprint $comandFileID $commands;
129          fclose $comandFileID;
130          system("\"" + `toNativePath $commandFile` + "\"");     
131        }
132
133        //material export
134        int $exportMaterials = `checkBox -query -value ExportMaterials`;
135        if($exportMaterials)
136        {
137                string $materialFile = (`textField -query -text MaterialFilename`);
138                string $materialFileName = encodeString(toNativePath($outputDir + $materialFile));
139            $options = "-all";         
140                $options += " -mat \"" + $materialFileName + "\"";
141                string $matPrefix = `textField -query -text MaterialPrefix`;
142                if ($matPrefix != "")
143                {
144                        $options += " -matPrefix \"" + $matPrefix + "\"";
145                }
146                eval ("ogreExport" + $options);
147        }
148               
149        fclose $fileId;
150       
151        int $exportPathMapMaterials = `checkBox -query -value ExportPMMat`;
152        if($exportPathMapMaterials)
153        {
154                string $materialFile = (`textField -query -text PMMatFile`);
155                string $materialFileName = encodeString(toNativePath($outputDir + $materialFile));
156               
157                $PMMatfileId = `fopen $materialFileName "w"`;
158               
159                fprint $PMMatfileId "<materials>\n"; 
160               
161                string $materials[] = `ls -type lambert`;
162                for($i = 0; $i < size($materials); $i++)
163                {
164                        select $materials[$i];
165                        fprint $PMMatfileId ("<material name=\"" + $materials[$i] +"\" ");
166                        string $textureFiles[] = `listConnections -t file`;
167                        if(size($textureFiles) > 0)
168                        {       
169                                select $textureFiles[0];
170                                $textureName = `getAttr .fileTextureName`;
171                                fprint $PMMatfileId ("texture=\"" +  basename($textureName, "") + "\" ");
172                        }
173                        else
174                        {
175                                float $baseColor[] =  `getAttr .color`;
176                                string $colorString = $baseColor[0] + " " + $baseColor[1] + " " + $baseColor[2];                       
177                                fprint $PMMatfileId ("color=\"" + $colorString + "\" ");
178                        }
179                        fprint $PMMatfileId "/>\n";                                                                     
180                }
181                fprint $PMMatfileId "</materials>\n";
182                fclose $PMMatfileId;
183        }
184}
185
186// ===== Launch exporter UI
187global proc GTPSceneExporter()
188{
189        defineGTPExportUIView();       
190}
191
192global proc formatGTPExportUI()
193{
194        int $exportMaterials = `checkBox -q -v ExportMaterials`;
195        text -edit -enable $exportMaterials MaterialFilenameLabel;
196        textField -edit -enable $exportMaterials MaterialFilename;
197        text -edit -enable $exportMaterials MaterialPrefixLabel;
198        textField -edit -enable $exportMaterials MaterialPrefix;
199       
200        int $exportMeshes = `checkBox -q -v ExportMeshes`;
201        checkBox -edit -enable $exportMeshes CreateBinary;
202        checkBox -edit -enable $exportMeshes BuildEdgeList;
203        checkBox -edit -enable $exportMeshes BuildTangents;
204        if (!$exportMeshes)
205                checkBox -edit -value false CreateBinary;
206       
207        int $createBinary = `checkBox -q -v CreateBinary`;
208        checkBox -edit -enable $createBinary BuildEdgeList;
209        checkBox -edit -enable $createBinary BuildTangents;
210        text -edit -enable $createBinary OgrePathLabel;
211        textField -edit -enable $createBinary OgrePath;
212       
213        int $exportPM = `checkBox -q -v ExportPMAttr`;
214        text -edit -enable $exportPM PMPathLabel;
215        textField -edit -enable $exportPM PMPath;
216       
217        int $exportPMMat = `checkBox -q -v ExportPMMat`;
218        text -edit -enable $exportPMMat PMMatFileLabel;
219        textField -edit -enable $exportPMMat PMMatFile;
220}
221
222global proc defineGTPExportUIView()
223{
224        string $sceneFile    = `file -query -sceneName`;
225        string $sceneDir    = dirname($sceneFile);
226        string $mayaFile     = basename($sceneFile, "." + fileExtension($sceneFile));
227        if($mayaFile == "")
228        $mayaFile = "untitled";
229       
230        // --- Main window for Ogre exporter
231        if (`window -exists GTPSceneExportWindow`)
232        {
233        deleteUI GTPSceneExportWindow;
234        }
235        window
236            -title "GTP Scene Exporter"
237        -widthHeight 305 300
238                GTPSceneExportWindow;
239       
240        scrollLayout
241        GTPSceneExportScrollLayout;
242        columnLayout
243        GTPSceneExportMainLayout;
244        frameLayout
245                -parent GTPSceneExportMainLayout
246                -label "General"
247                -collapsable true
248                GeneralControls;
249                columnLayout
250                        GTPGeneralControlsLayout;
251                text 
252                        -parent GTPGeneralControlsLayout   
253                        -label "Output Directory"
254                        OutputDirectoryLabel;
255                textField
256                        -parent GTPGeneralControlsLayout
257                        -width 305
258                        -text $sceneDir
259                        OutputDirectory;
260                text 
261                        -parent GTPGeneralControlsLayout   
262                        -label "Scene File Name"
263                        SceneFileNameLabel;
264                textField
265                        -parent GTPGeneralControlsLayout
266                        -width 305
267                        -text ($mayaFile + ".level")
268                        SceneFileName;
269                       
270        frameLayout
271                -parent GTPSceneExportMainLayout
272                -label "Materials"
273                -collapsable true
274                MaterialControls;
275                columnLayout
276                        MaterialControlsLayout;
277                checkBox
278                        -parent MaterialControlsLayout
279                        -value false
280                        -changeCommand "formatGTPExportUI"
281                        -label "Export Materials"
282                        ExportMaterials;
283                text
284                        -parent MaterialControlsLayout
285                        -label "Material File Name"
286                        -enable false
287                        MaterialFilenameLabel;
288                textField
289                        -parent MaterialControlsLayout
290                        -width 305
291                        -enable false
292                        -text ($mayaFile + ".material")
293                        MaterialFilename;       
294                text
295                        -parent MaterialControlsLayout
296                        -label "Material name prefix"
297                        -enable false
298                        MaterialPrefixLabel;
299                textField
300                        -parent MaterialControlsLayout
301                        -width 305
302                        -enable false
303                        -text $mayaFile
304                        MaterialPrefix;
305                       
306        frameLayout
307                -parent GTPSceneExportMainLayout
308                -label "Meshes"
309                -collapsable true
310                -width 309             
311                MeshControls;
312                columnLayout
313                        MeshControlsLayout;
314                checkBox
315                        -parent MeshControlsLayout
316                        -value true
317                        -changeCommand "formatGTPExportUI"
318                        -label "Export Meshes"
319                        ExportMeshes;
320                checkBox
321                        -parent MeshControlsLayout
322                        -value true
323                        -changeCommand "formatGTPExportUI"
324                        -label "Create Binary Mesh Files"
325                        CreateBinary;
326                checkBox
327                        -parent MeshControlsLayout
328                        -value false
329                        -changeCommand "formatGTPExportUI"
330                        -label "Build Edge List"
331                        BuildEdgeList;
332                checkBox
333                        -parent MeshControlsLayout
334                        -value false
335                        -changeCommand "formatGTPExportUI"
336                        -label "Build Tangent Vectors"
337                        BuildTangents;
338                text
339                        -parent MeshControlsLayout
340                        -label "Ogre Dir(where dlls and OgreXMLConverter.exe are located)"
341                        -enable true
342                        OgrePathLabel;
343                textField
344                        -parent MeshControlsLayout
345                        -width 305
346                        -enable true
347                        -text "%OGRE_PATH%/Samples/Common/bin/Release"
348                        OgrePath;
349       
350        frameLayout
351                -parent GTPSceneExportMainLayout
352                -label "Path Map Options"
353                -collapsable true
354                -width 309             
355                PathMapControls;
356                columnLayout
357                        PathMapControlsLayout;
358                checkBox
359                        -parent PathMapControlsLayout
360                        -value false
361                        -changeCommand "formatGTPExportUI"
362                        -label "Export Path Map Attributes"
363                        ExportPMAttr;
364                text
365                        -parent PathMapControlsLayout
366                        -label "Path Map Texture Directory"
367                        -enable false
368                        PMPathLabel;
369                textField
370                        -parent PathMapControlsLayout
371                        -width 305
372                        -enable false
373                        -text "prm"
374                        PMPath;
375                checkBox
376                        -parent PathMapControlsLayout
377                        -value false
378                        -changeCommand "formatGTPExportUI"
379                        -label "Export Path Map Materials"
380                        ExportPMMat;
381                text
382                        -parent PathMapControlsLayout
383                        -label "Path Map Material File"
384                        -enable false
385                        PMMatFileLabel;
386                textField
387                        -parent PathMapControlsLayout
388                        -width 305
389                        -enable false
390                        -text ($mayaFile + ".materials")
391                        PMMatFile;     
392        button
393                -parent GTPSceneExportMainLayout
394                -label "EXPORT"
395                -command "runGTPSceneExport"
396                -width 305
397                ButtonExport;
398       
399        showWindow GTPSceneExportWindow;
400}
401
402global proc GTPAddPathMapAttr()
403{
404  string $selected[] = `selectedNodes`;
405  for($i = 0; $i < size($selected); $i++ )
406  {
407    select $selected[$i];
408    $isGeometry = `objectType -isType mesh $selected[$i]`;
409    if($isGeometry)
410    {
411      if(!attributeExists("PhotonMapResolution", $selected[$i]))
412       addAttr -at short -ln PhotonMapResolution -sn prmRes -min 16 -max 1024 -dv 64;
413      if(!attributeExists("PhotonMapSubdivision", $selected[$i]))
414       addAttr -at short -ln PhotonMapSubdivision -sn prmDiv -min 1 -dv 1;
415       
416      string $transforms[] = `listRelatives -ap $selected[$i]`;
417          for($j = 0; $j<size($transforms);$j++)
418          {
419                  select $transforms[$j];
420                  if(!attributeExists("PathMapClusters", $transforms[$j]))
421           addAttr -at short -ln PathMapClusters -sn prmRes -min 1 -max 64 -dv 32;
422          }
423    }
424    else
425    {
426     select $selected[$i];
427     $isTransform = `objectType -isType transform $selected[$i]`;
428     if($isTransform)
429     {
430      string $relatives[] = `listRelatives $selected[$i]`;
431      int $numRelatives = size($relatives);
432      for($j=0;$j<$numRelatives;$j++)
433      {
434        $isGeometry = `objectType -isType mesh $relatives[$j]`;
435                if($isGeometry)
436                {         
437              select $relatives[$j];
438          if(!attributeExists("PhotonMapResolution", $relatives[$j]))
439           addAttr -at short -ln PhotonMapResolution -sn prmRes -min 16 -max 1024 -dv 64;
440          if(!attributeExists("PhotonMapSubdivision", $relatives[$j]))
441           addAttr -at short -ln PhotonMapSubdivision -sn prmDiv -min 1 -dv 1;
442
443                  select $selected[$i];
444          if(!attributeExists("PathMapClusters", $selected[$i]))
445           addAttr -at short -ln PathMapClusters -sn prmClus -min 1 -max 64 -dv 32;
446        }
447      }
448     }
449    }
450  }
451}
452
453namespace -set ":";
Note: See TracBrowser for help on using the repository browser.