|
2 | 2 | from PIL import Image |
3 | 3 | import sys |
4 | 4 | import os |
| 5 | +from io import TextIOWrapper |
| 6 | +import shutil |
| 7 | + |
| 8 | +def writeMaterialSection(o:TextIOWrapper, name:str, rgb:tuple, d:float) -> None: |
| 9 | + o.write("newmtl %s\n" % name) |
| 10 | + o.write("Ka %.3f %.3f %.3f\n" % (rgb[0], rgb[1], rgb[2])) |
| 11 | + o.write("Kd %.3f %.3f %.3f\n" % (rgb[0], rgb[1], rgb[2])) |
| 12 | + o.write("Ks 0.000 0.000 0.000\n") |
| 13 | + o.write("d %.1f\n" % d) |
| 14 | + o.write("illum 1\n") |
5 | 15 |
|
6 | 16 | #a python script for generating mtl files |
7 | 17 |
|
|
22 | 32 | a = True |
23 | 33 | if a and not s: |
24 | 34 | print("-a ignored") |
| 35 | + if not s: |
| 36 | + try: |
| 37 | + os.mkdir("./mtlFiles") |
| 38 | + except FileExistsError: |
| 39 | + pass |
25 | 40 | with open("out.mtl", "w") as o: |
26 | 41 | for filename in os.listdir(path): |
27 | 42 | f = os.path.join(path, filename) |
28 | 43 | #simplified functioning that doesn't require textures |
29 | | - if os.path.isfile(f) and filename[-4:] == ".png" and s: |
30 | | - rgb = [0, 0, 0, 0] |
31 | | - img = Image.open(f).convert('RGBA') |
32 | | - pixels = 0 |
33 | | - for x in range(img.size[0]): |
34 | | - for y in range(img.size[1]): |
35 | | - pixel = img.getpixel((x, y)) |
36 | | - if not (pixel[3] == 0 and a): |
37 | | - rgb[0] += pixel[0] |
38 | | - rgb[1] += pixel[1] |
39 | | - rgb[2] += pixel[2] |
40 | | - rgb[3] += pixel[3] |
41 | | - pixels += 1 |
42 | | - if pixels > 0: |
43 | | - rgb[0] /= (pixels * 255) |
44 | | - rgb[1] /= (pixels * 255) |
45 | | - rgb[2] /= (pixels * 255) |
46 | | - rgb[3] /= (pixels * 255) |
47 | | - if "grass" in filename or "leaves" in filename: |
48 | | - # #8eb971 |
49 | | - rgb[0] = 142 / 255 |
50 | | - rgb[1] = 185 / 255 |
51 | | - rgb[2] = 113 / 255 |
52 | | - if "top" in filename and t: |
53 | | - filename = filename.replace("_top", "", 1) |
54 | | - o.write("newmtl %s\n" % filename[:-4]) |
55 | | - o.write("Ka %.3f %.3f %.3f\n" % (rgb[0], rgb[1], rgb[2])) |
56 | | - o.write("Kd %.3f %.3f %.3f\n" % (rgb[0], rgb[1], rgb[2])) |
57 | | - o.write("Ks 0.000 0.000 0.000\n") |
58 | | - o.write("d %.1f\n" % rgb[3]) |
59 | | - o.write("illum 1\n") |
60 | | - |
61 | | - |
62 | | - |
| 44 | + if os.path.isfile(f) and filename[-4:] == ".png": |
| 45 | + if s: |
| 46 | + rgb = [0, 0, 0, 0] |
| 47 | + img = Image.open(f).convert('RGBA') |
| 48 | + pixels = 0 |
| 49 | + for x in range(img.size[0]): |
| 50 | + for y in range(img.size[1]): |
| 51 | + pixel = img.getpixel((x, y)) |
| 52 | + if not (pixel[3] == 0 and a): |
| 53 | + rgb[0] += pixel[0] |
| 54 | + rgb[1] += pixel[1] |
| 55 | + rgb[2] += pixel[2] |
| 56 | + rgb[3] += pixel[3] |
| 57 | + pixels += 1 |
| 58 | + if pixels > 0: |
| 59 | + rgb[0] /= (pixels * 255) |
| 60 | + rgb[1] /= (pixels * 255) |
| 61 | + rgb[2] /= (pixels * 255) |
| 62 | + rgb[3] /= (pixels * 255) |
| 63 | + if "grass" in filename or "leaves" in filename: |
| 64 | + # #8eb971 |
| 65 | + rgb[0] = 142 / 255 |
| 66 | + rgb[1] = 185 / 255 |
| 67 | + rgb[2] = 113 / 255 |
| 68 | + if "top" in filename and t: |
| 69 | + filename = filename.replace("_top", "", 1) |
| 70 | + writeMaterialSection(o, filename[:-4], rgb, rgb[3]) |
| 71 | + else: |
| 72 | + writeMaterialSection(o, filename[:-4], (1, 1, 1), 1) |
| 73 | + locFilename = "./mtlFiles/" + filename |
| 74 | + o.write("map_Ka %s\n" % locFilename) |
| 75 | + o.write("map_Kd %s\n" % locFilename) |
| 76 | + shutil.copyfile(f, locFilename) |
0 commit comments