Modme Forums

OLDER MATERIALS CONVERSION (COD4\WAW\BO1) - Python Script

Game Asset Reversing | Texture Assets


RaGe-74:

Hi, I have created this python script (below) with some help from GITHUB, STACKOVERFLOW, PYTHON DISCORD and little bits and pieces of old code scattered around the internet. (and no, I do NOT know much Python).
So far, it loads in a binary material file (without decoding it).

I'm using WAW materials at the moment until I get the script right.
So far the script finds the image name\material name between two keyword identifiers and writes a Black Ops 3 GDT file with the material name and image files in the correct places.

So the problems I'm having are:

1.. I have to rename all the material files to have an extension. (easy enough, but shouldn't have to)
2.. It only writes a new GDT based on the information from the last material file in a directory. So one GDT in a directory of over 7000 materials.
3.. If the image is not found in the original Material file, it completely halts the script, so it needs to replace the value with $specular, $normal, $colorMap. If the Material does not exist in the file, it skips the file altogether.
I am told it's because the code that searches for the Keyword Indentifiers needs to be between the "for filename..." and "with open" statements. However, I can't really move it, as it needs the "filecontent" variable to be defined first.


Here is an example of the input:

I'm assuming because the file is binary, this is causing the "finding image names" problem on some files that contain control characters than can't be passed through the decoding of Python.
I have tried converting ALL the materials to ASCII and removing all the NON-ASCII control characters. This did not fix the problem.

You can see using the above file as reference, I find the material and images like this:
Material Name: colorMap -2 places
colorMap: colorMap -1 place
normalMap: specularMap -1 place
specularMap: envMapParms -1 place

import os
import re
import glob
spath = r"*.txt"
for filename in glob.glob(spath):
    with open(filename) as f:
        filecontent = f.read()
#print(filename)
print ('Processing')
print(filename)
print ('Please wait...')
print ('')



#path
path = r"f:/rightclick"

# ---MATERIAL---
# ------------------------------------------------
identifiers = re.findall("([~_a-z0-9]{3,})", filecontent, flags=re.I)
matfname = identifiers[identifiers.index('colorMap') - 2]
material = matfname + '.gdt'
#---- THE FINAL VARIABLE
#print(material)
# --------------

# ---SPECULAR---
# ------------------------------------------------
identifiers = re.findall("([~_a-z0-9]{3,})", filecontent, flags=re.I)
specfname = identifiers[identifiers.index('envMapParms') - 1]
specular= specfname + '.png'
#---- THE FINAL VARIABLE
#print(specular)
# --------------

# ---NORMAL---
# FIND NORMAL NAME
identifiers = re.findall("([~_a-z0-9]{3,})", filecontent, flags=re.I)
normalfname = identifiers[identifiers.index('specularMap') - 1]
normal = normalfname + '.png'
#---- THE FINAL VARIABLE
#print(normal)
# --------------

# ---DIFFUSE COLORMAP---
# FIND COLORMAP NAME
identifiers = re.findall("([~_a-z0-9]{3,})", filecontent, flags=re.I)
colorfname = identifiers[identifiers.index('colorMap') - 1]
color1 = colorfname + '.png'
#---- THE FINAL VARIABLE
#print(color1)
# --------------


# GDT CREATION STARTS HERE # --------------------------------------------------------------

gdt = '{\r'
gdt += material
gdt += specular
gdt += normal
gdt += color1
gdt += '}\r'
gdt += '\r'
# GDT CREATION ENDS HERE # --------------------------------------------------------------

# CREATE THE GDT FILE WITH EXTENSION
filename_with_extension = material

#save the GDT into the gdt file
with open(filename_with_extension, 'w') as gdtfile:
    gdtfile.write(gdt)
   
    print ('Operation completed.')
    print ('')
    print ('')

If anyone could be bothered, please, have a look, stuff around with it, see if you can get it working. Or if you think you can do a better job in c++\C#, then go for it. I'm at wits end. I'm by no means a programmer, but I feel this is a program that needs to be done, for the sake of making COD4\WaW and BO1 forwards compatible, and to make porting to BO3 easier.


DTZxPorter:

Since I had some free time today, I'll give you a crash coarse on the material you posted. But you'll have to learn to program on your own...

Everything you're working with is binary, you'll never get anywhere fast using text, I use HxD https://mh-nexus.de/en/hxd/, some people say they use 010 editor but honestly it's cluttered and a waste of time IMO. Use what you can get comfortable with. If you're having trouble with hex, the inspector in HxD to the right can display in decimal, or hex, google will also convert for you: "0xC in decimal" or "12 in hex"



Good luck.