-
Notifications
You must be signed in to change notification settings - Fork 1
/
armcli.py
executable file
·255 lines (200 loc) · 9.13 KB
/
armcli.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/python
""" Armory CLI """
import argparse
import os
import subprocess
import sys
from subprocess import PIPE, STDOUT
args = None
if os.path.islink(sys.argv[0]):
clidir = os.path.dirname(os.readlink(sys.argv[0]))
else:
clidir = os.path.dirname(sys.argv[0])
script_dir = f"{clidir}/blender"
if not os.path.exists(script_dir) or not os.path.isdir(script_dir):
print("armcli/blender/ not found", file=sys.stderr)
sys.exit(1)
def abort(code=1, msg: str = None):
if msg is not None: print(msg, file=sys.stderr)
sys.exit(code)
def find_main_blend_file(blend: str = None):
if blend is None:
cwd = os.getcwd()
blend = f"{cwd}/{os.path.basename(cwd)}.blend"
if not os.path.exists(blend):
if os.path.exists("main.blend"):
return "main.blend"
else:
for e in os.scandir("."):
if e.name.endswith('.blend') and e.is_file():
return e.name
return blend
return blend
def execute_blender(params):
blender_executable = args.blender_executable
if blender_executable == None:
blender_executable = "blender"
cmd = [blender_executable, "--background"]
cmd.extend(params)
if args.verbose:
print(" ".join(cmd))
proc = subprocess.Popen(cmd, stderr=PIPE)
else:
proc = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
while proc.poll() is None:
out = proc.stderr.readline().decode("utf-8")
if len(out) > 0:
print(out, end="")
err = proc.stderr.readline().decode("utf-8")
if len(err) > 0:
print(err, end="")
sys.exit(proc.returncode)
def execute_blender_script(script: str, blend: str=None, params=None):
path = f"{script_dir}/{script}.py"
if not os.path.exists(path):
abort(f'[{path}] not found')
cmd = []
if blend is not None:
if not os.path.exists(blend):
abort(f'{blend} not found')
cmd.append(blend)
cmd.extend(["--python", path])
if params is not None and len(params) > 0:
cmd.append("--")
cmd.extend(params)
execute_blender(cmd)
def execute_blender_expr(expr: str, blend: str=None):
cmd = []
if blend is not None:
cmd.append(blend)
cmd.extend(["--python-expr", "'" + expr + "'"])
execute_blender(cmd)
def cli_build(_args):
execute_blender_script("build", find_main_blend_file(_args.blend))
def cli_publish(_args):
blend = find_main_blend_file(_args.blend)
params = []
if _args.exporter is not None:
params.append(_args.exporter)
execute_blender_script("publish", blend, params)
def cli_clean(_args):
execute_blender_script("clean", find_main_blend_file(_args.blend))
def cli_play(_args):
if _args.camera is None: _args.camera = ""
if _args.scene is None: _args.scene = ""
if _args.renderpath is None: _args.renderpath = ""
execute_blender_script("play", find_main_blend_file(_args.blend), [_args.runtime, _args.camera, _args.scene, _args.renderpath])
def cli_exporters(_args):
execute_blender_script(f"exporters_{_args.command}", find_main_blend_file(_args.blend))
def cli_renderpath(_args):
execute_blender_script(f"renderpath_{_args.action}", find_main_blend_file(_args.blend))
def cli_traits(_args):
blend = find_main_blend_file(_args.blend)
execute_blender_script("traits", blend)
# def cli_compile(_args):
# print(_args)
# if _args.build is not None:
# build_dir = f"build_{_args.build}"
# if not os.path.exists(build_dir):
# abort(f"{build_dir} not found")
# platforms = []
# build_debug_dir = f"{build_dir}/debug"
# if build_debug_dir:
# for file in os.listdir(build_debug_dir):
# if file.startswith("project-") and file.endswith(".hxml"):
# platform = file.split(".")[0][8:]
# path = f"{build_debug_dir}/{file}"
# print(platform)
# if os.path.isfile(path) and
# print(f)
# print(build_dir)
# subprocess.run([""])
# output_directories = []
# for d in os.listdir('.'):
# if d.startswith('build_') and os.path.isdir(d):
# output_directories.append(d)
# print(" ".join(output_directories))
def cli_versioninfo(_args):
execute_blender_script("versioninfo")
def cli_sdk(_args):
execute_blender_script("sdk")
def cli_kha(_args):
cmd = ["node", f"{args.armsdk}/Kha/make.js", "--shaderversion", "330"]
proc = subprocess.Popen(cmd, stdout=PIPE, stderr=PIPE)
while proc.poll() is None:
print(proc.stdout.readline().decode("utf-8"), end="", file=sys.stdout)
sys.exit(proc.returncode)
# def cli_launch(_args):
# for path in os.listdir("."):
# if path.startswith("build_") and os.path.isdir(path):
# debug_dir = f"{path}/debug"
# if os.path.exists(debug_dir) and os.path.isdir(debug_dir):
# krom_dir = f"{debug_dir}/krom"
# if os.path.exists(krom_dir):
# print(krom_dir)
# for d in os.listdir(d):
# print(d)
# subprocess.run([''])
argparser = argparse.ArgumentParser(prog="armory")
#argparser.add_argument("--blender-stdout", dest="print_blender_stdout", action="store_true", help="print blenders stdout", default=True)
argparser.add_argument("-b", "--blend", help="path to main blend file")
argparser.add_argument("-v", "--verbose", dest="verbose", action="store_true", help="print verbose outpout")
argparser.add_argument("--sdk", dest="armsdk", action="store", help="path to armsdk")
argparser.add_argument("--blender-executable", dest="blender_executable", action="store", default="blender", help="path to blender executable")
#argparser.add_argument("--json", help="Output in json format")
subparsers = argparser.add_subparsers()
parser_build = subparsers.add_parser("build", help="build project")
parser_build.add_argument("-b", "--blend", help="path to main blend file")
parser_build.add_argument("-e", "--exporter", help="exporter to use", type=ascii)
parser_build.set_defaults(func=cli_build)
parser_publish = subparsers.add_parser("publish", help="publish project")
parser_publish.add_argument("-b", "--blend", help="path to main blend file")
parser_publish.add_argument("-e", "--exporter", help="exporter to use")
parser_publish.set_defaults(func=cli_publish)
parser_clean = subparsers.add_parser("clean", help="clean project")
parser_clean.add_argument("-b", "--blend", help="path to main blend file")
parser_clean.set_defaults(func=cli_clean)
parser_play = subparsers.add_parser("play", help="play project")
parser_play.add_argument("-b", "--blend", help="path to main blend file")
parser_play.add_argument("-c", "--camera", default="Scene", help="viewport camera")
parser_play.add_argument("-s", "--scene", help="scene to launch")
parser_play.add_argument("-rp", "--renderpath", help="default render path")
parser_play.add_argument("-rt", "--runtime", default="krom", choices=["krom", "browser"], help="runtime to use")
parser_play.set_defaults(func=cli_play)
parser_exporters = subparsers.add_parser("exporters", help="manage exporters")
parser_exporters.add_argument("-b", "--blend", help="path to main blend file")
#parser_exporters.add_argument("command", choices=["list"], default="list", help="list exporters")
parser_exporters.set_defaults(func=cli_exporters)
# exporters_subparsers = parser_exporters.add_subparsers()
# parser_exporters_list = exporters_subparsers.add_parser('list')
# parser_exporters_list.set_defaults(func=cli_exporters_list)
# parser_exporters_list = parser_exporters.add_subparsers()
# parser_exporters_list.add_argument("list")
# parser_exporters.add_argument("command", choices=["add","remove","list"])
parser_renderpath = subparsers.add_parser("renderpath", help="manage renderpaths")
parser_renderpath.add_argument("-b", "--blend", help="path to blend file")
# group_renderpath = parser_renderpath.add_mutually_exclusive_group()
parser_renderpath.add_argument("action", choices=["list", "select", "add", "delete"], default="list", help="perform renderpath action")
# group_renderpath.add_argument('select', action='store_true')
parser_renderpath.set_defaults(func=cli_renderpath)
# parser_traits = subparsers.add_parser('traits', help='manage traits')
# parser_traits.add_argument('--blend', help='path to main blend file')
# parser_traits.set_defaults(func=traits)
# trait_subparsers = parser_traits.add_subparsers(help='sub-command help')
# parser_traits_list = trait_subparsers.add_parser('list')
# parser_traits_list.set_defaults(func=cli_traits)
# parser_compile = subparsers.add_parser("compile", help="compile project")
# parser_compile.add_argument("--build", help="build to compile")
# parser_compile.add_argument("--platform", help="platform to compile")
# parser_compile.set_defaults(func=cli_compile)
# parser_khamake = subparsers.add_parser("kha", help="execute khamake")
# parser_khamake.set_defaults(func=cli_kha)
parser_versioninfo = subparsers.add_parser("versioninfo", help="print version info")
parser_versioninfo.set_defaults(func=cli_versioninfo)
parser_sdk = subparsers.add_parser("sdk", help="manage armsdk")
parser_sdk.set_defaults(func=cli_sdk)
if len(sys.argv) == 1:
argparser.print_help()
sys.exit(1)
args = argparser.parse_args()
args.func(args)