Skip to content

Commit 25218c0

Browse files
committed
Added default option to mtlGen.py
1 parent f07b416 commit 25218c0

File tree

2 files changed

+50
-35
lines changed

2 files changed

+50
-35
lines changed

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,5 @@ mtlGen
2020
*.ow
2121
*.txt
2222
*Check*
23-
tests/benchmark
23+
tests/benchmark
24+
mtlFiles

mtlGen.py

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22
from PIL import Image
33
import sys
44
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")
515

616
#a python script for generating mtl files
717

@@ -22,41 +32,45 @@
2232
a = True
2333
if a and not s:
2434
print("-a ignored")
35+
if not s:
36+
try:
37+
os.mkdir("./mtlFiles")
38+
except FileExistsError:
39+
pass
2540
with open("out.mtl", "w") as o:
2641
for filename in os.listdir(path):
2742
f = os.path.join(path, filename)
2843
#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

Comments
 (0)