This repository has been archived by the owner on Mar 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathqr.py
79 lines (75 loc) · 2.44 KB
/
qr.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import config
import pyqrcode
import string
def get_safe_name(s: str) -> str:
return ''.join((i for i in s if i in string.ascii_letters + string.digits + '_'))
MODEL_NAME = f'qr_{get_safe_name(input("Model name: "))}'
TEXT = get_safe_name(input('Text: '))
WIDTH = input(f'Width of QR code (empty for {config.PLATE_WIDTH}): ')
try:
WIDTH = float(WIDTH)
except ValueError:
WIDTH = config.PLATE_WIDTH
MODEL_DIR = config.MODELS_DIR / MODEL_NAME
assert not MODEL_DIR.exists(), (
f'This model ({MODEL_NAME}) is also created. Remove directory `output/{MODEL_NAME}` before!'
)
(MODEL_DIR / 'materials' / 'textures').mkdir(parents=True)
(MODEL_DIR / 'materials' / 'scripts').mkdir(parents=True)
pyqrcode.create(TEXT).png(MODEL_DIR / 'materials' / 'textures' / f'{MODEL_NAME}.png', scale=1)
with open(MODEL_DIR / 'model.config', 'x') as config_file:
config_file.write(
f'''<?xml version="1.0"?>
<model>
<name>{MODEL_NAME}</name>
<version>1.0</version>
<sdf version="1.5">model.sdf</sdf>
<author>
<name>QR code Generator script</name>
<email>me@mkme.ml</email>
</author>
<description>{MODEL_NAME}</description>
</model>'''
)
with open(MODEL_DIR / 'model.sdf', 'x') as model_file:
model_file.write(
f'''<?xml version="1.0"?>
<sdf version="1.5">
<model name="{MODEL_NAME}">
<static>true</static>
<link name="{MODEL_NAME}_link">
<pose>0 0 1e-3 0 0 1.5707963267948966</pose>
<visual name="visual_{MODEL_NAME}">
<cast_shadows>false</cast_shadows>
<geometry><box><size>{WIDTH} {WIDTH} 1e-3</size></box></geometry>
<material>
<script>
<uri>model://{MODEL_NAME}/materials/scripts</uri>
<uri>model://{MODEL_NAME}/materials/textures</uri>
<name>{MODEL_NAME}/{MODEL_NAME}</name>
</script>
</material>
</visual>
</link>
</model>
</sdf>'''
)
with open(MODEL_DIR / 'materials' / 'scripts' / 'model.material', 'x') as material_file:
material_file.write(
f'''material {MODEL_NAME}/{MODEL_NAME}
{{
technique
{{
pass
{{
texture_unit
{{
texture {MODEL_NAME}.png
filtering none
scale 1.0 1.0
}}
}}
}}
}}'''
)
print(f'\nModel:\t{MODEL_DIR.__str__()}\nCode:\t{TEXT}\nWidth:\t{WIDTH}')