-
Notifications
You must be signed in to change notification settings - Fork 1
/
JSXToJS.py
60 lines (47 loc) · 2.59 KB
/
JSXToJS.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
import os
import argparse
import re
def convert_file(input_file_path, output_file_path, scale, rotation):
with open(input_file_path, 'r') as file:
content = file.read()
glb_path_match = re.search(r"\/([\w_-]+)\.glb", content)
if glb_path_match:
glb_file_name = glb_path_match.group(1)
else:
print(f"GLB path not found in {input_file_path}.")
return
new_import = f"import {glb_file_name} from './{glb_file_name}.glb';\nimport {{ useGLTF }} from '@react-three/drei';\n"
preload_statement = f"useGLTF.preload({glb_file_name})"
additional_properties = ""
if scale:
additional_properties += f", scale: {scale}"
if rotation:
additional_properties += f", rotation: {rotation}"
new_export_function = f"""export default function Model(props) {{
const {{ nodes }} = useGLTF({glb_file_name});
return [{{type:'raw', geometry:nodes.{glb_file_name}.geometry, material: nodes.{glb_file_name}.material{additional_properties}}}]
}}
"""
new_content = "/*\nAuto-generated by: https://github.com/pmndrs/gltfjsx\n*/\n\n" + new_import + new_export_function + preload_statement
with open(output_file_path, 'w') as file:
file.write(new_content)
print(f"Conversion completed for {input_file_path}, output saved to {output_file_path}\n")
def convert_directory(input_dir, output_dir, scale, rotation):
if not os.path.isdir(input_dir):
print(f"The folder '{input_dir}' does not exist.")
return
os.makedirs(output_dir, exist_ok=True)
for file_name in os.listdir(input_dir):
if file_name.endswith(".jsx"):
input_file_path = os.path.join(input_dir, file_name)
output_file_name = os.path.splitext(file_name)[0] + ".js"
output_file_path = os.path.join(output_dir, output_file_name)
convert_file(input_file_path, output_file_path, scale, rotation)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert .jsx files to .js files with specified conversion logic.")
parser.add_argument("input_dir", type=str, help="Input directory containing .jsx files.")
parser.add_argument("output_dir", type=str, help="Output directory for the converted .js files.")
parser.add_argument("--scale", type=str, help="Scale in the format [x,y,z].", default=None)
parser.add_argument("--rotation", type=str, help="Rotation in the format [x,y,z].", default=None)
args = parser.parse_args()
convert_directory(args.input_dir, args.output_dir,args.scale, args.rotation)