Game Development Community

[Google Sketchup] .kmz create unique name in .dae, textures and materials

by JesseL · in Torque 3D Professional · 07/15/2009 (2:55 am) · 7 replies

Welcome all:
I am tired of renaming the files in the .dae and renaming the textures and the keyword materials in the .dae file so it doesn't conflict with other materials with the same name or other textures.

I searched for a couple of things online and came up with this strategi.
Create a CMD file that replaces strings in a file. replace *.dae "this" "that"
Create a BAT file that runs a couple of commands.

So lets see how much we can get done here:
BAT file needs to do the following:
1) Search all of the Torque Directory for .kmz files.
2) In the same directory of the .kmz file create a file folder with the name of the .kmz file.
3) In that folder extract the contance of the .kmz\images and .kmz\models folders into the new file folder named the .kmz file name.
4) In the new file folder rename all texture keywords to the .kmz file name to make it unique.
5) In the .dae file find the ../images/texture and replace it with the .kmz file name.
6) In the .dae file find all material_ keywords and replace them with the .kmz file name.

About the author

I just realized that if I wanted to create a cat that caught on fire and ran up a telephone pole and then burst into a blue waterfall. That wouldn't be to hard!


#1
07/15/2009 (2:57 am)
The command file that does replacement of string text. Lets name the file replace.cmd

@echo off
setlocal enabledelayedexpansion

if not exist "%1" (echo this file does not exist...)&goto :eof

set /p findthis=find this text string:
set /p replacewith=and replace it with this:


for /f "tokens=*" %%a in (%1) do (

set write=%%a
if %%a==%findthis% set write=%replacewith%

echo !write! 
echo !write! >>%~n1.replaced%~x1
)

So

replace *.dae "../images/texture" someVariable
replace *.dae "material_" someVariable

#2
07/15/2009 (10:30 pm)
Hi JesseL,

Nice one!

Beta4 (coming soon!) will address some of these issues. Namely:

1. No need to change the path to the textures. Relative paths (../images/texture1.jpg) in Collada files will work just fine.

2. TSShapeConstructor can prefix material names in the Collada model for you to make them unique. There is no need to make texture filenames unique, since their path essentially does this for you. ie. there is only one C:\images\texture1.jpg, and it is clearly different from C:\temp\texture1.jpg.

You still need to unzip the KMZ file, but you can leave the unzipped contents as-is, and just add a TSShapeConstructor script to fixup the collada material names. eg. my_model.kmz would be unzipped to:

my_model
+-images
  +-texture1.jpg
  +-texture2.jpg
+-models
  +-my_model.dae

Then you'd add a TSShapeConstructor script called my_model.cs to the 'models' folder:

singleton TSShapeConstructor(MyModelDae)
{
   baseShape = "./my_model.dae";
   matNamePrefix = "my_model_";
};

Once the TSShapeConstructor script is in place, you can re-export (and re-unzip) your Sketchup model and it will reload correctly in Torque.
#3
07/15/2009 (10:49 pm)
Great news Chris :)

Could the collada to dts converter automatically create the my_model.cs file?
#4
07/16/2009 (1:15 am)
@Chris, ok I could easily create an array script for that.
function createModels()
{
   %modelArray[0,name] = "Bob";
   %modelArray[1,name] = "George";
   %modelArray[2,name] = "Henry";
   %modelArray[3,name] = "Calvin";
   %modelArray[4,name] = "Tom";
   %modelMax = 5;

   for (%i = 0; %i < %modelMax; %i++)
   {
      %modelArray[%i] = %modelArray[%1,name] @ %i;
      createSingletonModel(%modelArray[%i]);
   }

}

function createSingletonModel(%modelName)
{
   %baseShapePath = "./" @ %modelName @ ".dae";
   %matNamePrefix = %modelName @ "_";
   singleton TSShapeConstructor(%modelName)
   {
      baseShape = %baseShapePath;
      matNamePrefix = %matNamePrefix;
   };
}
#5
07/16/2009 (1:26 am)
Ohh, Opps I get it now lol. So then I could do something simple like:
1) check to see if the directory before the .kmz file has the same name
2) if not then take the *.kmz file and rename it to *.zip.
3) Unzip the file to directory and move the .zip file into its directory.
4) goto the .zip/model file directory and create a file
5) rename the file to be the name of the .dae object with a .cs extention
6) In the file type the following information.

singleton TSShapeConstructor(%%a)
{
baseShape = "./" @ %%a @ ".dae";
matNamePrefix = %%a @ "_";
};

7) rename *.zip to *.kmz;
then you could just rexport the model to the directory and reunzip it.
#6
07/16/2009 (4:45 pm)
Exactly.
#7
08/04/2009 (3:19 am)
Ok, after 3 weeks. I am almost there. This is what the bat file looks like right now.

First our goals
REM 1) check to see if the directory before the .kmz file has the same name
REM 2) if not then take the *.kmz file and rename it to *.zip.
REM 3) Unzip the file to directory and move the .zip file into its directory.
REM 4) goto the .zip/model file directory and create a file
REM 5) rename the file to be the name of the "".dae object with a .cs extention
REM 6) In a new file type the information.
REM 7) Rename the file to .kmz for extracting later and rename the .txt to .cs

setLocal

rem Expands %1 to a fuly qualified path name.
set Source = %~f1 

REM For all files in all folders after this one check it with function ProcessFile

for /R %%z in (%Source%*.*) do call :ProcessFile "%%z"
GOTO :eof

:ProcessFile
REM lets get the different parts of the file name / path / file / ext and set them to variables FP , FN, FE, FSP

set FP=%~1&set FN=%~n1&set FE=%~x1&set FSP=%~p1

REM IF /i insensative to case has a file extention of .kmz go to the function Process KMZ File otherwise goto function end of file EOF.

IF /I %FE% EQU .kmz (goto :ProcessKMZFile
) ELSE (
goto :eof
)

:ProcessKMZFile
ECHO The Folder name is %FSP%
ECHO Type in the folder name this file was found in

REM set the userInput to the userInput variable

SET /P userInput=

IF /I "%userInput%" EQU "%FN%" (
GOTO :EOF
) ELSE (
GOTO :ProcessZipFile
)


:ProcessZipFile
ECHO PROCESSZIPFILE
rename %FN%%FE% %FN%.ZIP
MD %FN%
Move %FN%.ZIP %FN%
CD %FN%

ECHO Two New Windows will open up with the contents of the zip file please Drag the models folder and texture folders to the other window 

EXPLORER /n,%FSP%%FN%
rundll32.exe zipfldr.dll,RouteTheCall %FN%.zip
PAUSE

GOTO :WriteCSFile

:WriteCSFile
ECHO singleton TSShapeConstructor(%FN%) 	>> %FN%.TXT
ECHO { 						>> %FN%.TXT
ECHO    baseShape = "./%FN%.dae"; 		>> %FN%.TXT
ECHO    matNamePrefix = "%FN%_"; 		>> %FN%.TXT
ECHO }; 					>> %FN%.TXT

RENAME %FN%.zip %FN%%FE%
RENAME %FN%.TXT %FN%.cs

MOVE %FN%.cs models

REM go back a directory to process the next .kmz file
CD..

:EOF
endLocal