annotate simporter.nim @ 851:8f0c82acb922

did: finish importing system
author Sam <sam@basx.dev>
date Wed, 06 Dec 2023 00:05:55 +0700
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
851
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
1 import std/os
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
2 import std/paths
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
3 import std/dirs
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
4 import std/osproc
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
5 import std/cmdline
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
6 import std/strutils
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
7 import std/strformat
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
8
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
9 import ./semicongine/core/audiotypes
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
10
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
11 proc import_meshes*(files: seq[(string, string)]) =
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
12 let converter_script = currentSourcePath.parentDir().joinPath("scripts/blender_gltf_converter.py")
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
13
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
14 var args = @["--background", "--python", converter_script, "--"]
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
15 for (input, output) in files:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
16 args.add input
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
17 args.add output
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
18
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
19 let p = startProcess("blender", args=args, options={poStdErrToStdOut, poUsePath})
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
20 let exitCode = p.waitForExit()
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
21 p.close()
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
22 if exitCode != 0:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
23 raise newException(OSError, &"blender had exit code {exitCode}")
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
24
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
25 proc import_audio*(files: seq[(string, string)]) =
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
26 for (input, output) in files:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
27 let p = startProcess("ffmpeg", args=["-y", "-i", input, "-ar", $AUDIO_SAMPLE_RATE, output], options={poStdErrToStdOut, poUsePath})
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
28 let exitCode = p.waitForExit()
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
29 p.close()
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
30 if exitCode != 0:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
31 raise newException(OSError, &"ffmpeg had exit code {exitCode}")
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
32
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
33 when isMainModule:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
34 var meshfiles: seq[(string, string)]
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
35 var audiofiles: seq[(string, string)]
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
36
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
37 for arg in commandLineParams():
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
38 if arg.count(':') != 1:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
39 raise newException(Exception, &"Argument {arg} requires exactly one colon to separate input from output, but it contains {arg.count(':')} colons.")
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
40 let
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
41 input_output = arg.split(':', 1)
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
42 input = input_output[0]
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
43 output = input_output[1]
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
44 if not input.fileExists:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
45 raise newException(IOError, &"Not found: {input}")
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
46 if not output.fileExists or input.fileNewer(output):
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
47 echo &"{output} is outdated"
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
48 if input.endsWith("blend"):
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
49 meshfiles.add (input, output)
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
50 elif input.endsWith("mp3") or input.endsWith("ogg") or input.endsWith("wav"):
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
51 audiofiles.add (input, output)
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
52 else:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
53 raise newException(Exception, &"unkown file type: {input}")
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
54 Path(output.parentDir()).createDir()
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
55 else:
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
56 echo &"{output} is up-to-date"
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
57
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
58 import_meshes meshfiles
8f0c82acb922 did: finish importing system
Sam <sam@basx.dev>
parents:
diff changeset
59 import_audio audiofiles