Repository navigation
Expand file tree
/
Copy pathEmbryoExt.py
More file actions
67 lines (48 loc) · 2.23 KB
/
Copy pathEmbryoExt.py
File metadata and controls
67 lines (48 loc) · 2.23 KB
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
"""Bridge between this COMP and the on-disk ``embryo`` package.
Lives in a Text DAT named ``EmbryoExt`` inside the Embryo COMP. The COMP's
Component Editor points at it:
Extension 1 -> op('./EmbryoExt').module.EmbryoEngine(me)
Name -> Embryo
Promote -> on
WHERE THE PACKAGE COMES FROM
The ``Embryopath`` parameter on the COMP names the folder that CONTAINS the
``embryo`` directory. Leave it empty once the .toe sits beside the package
and ``project.folder`` is used instead -- TouchDesigner already has that on
``sys.path``, so nothing needs configuring in the common case.
It is a parameter rather than a constant in this file on purpose: a path is
the one thing that differs per machine, and as a Folder-style par it gets a
browse button and shows up in any audit that walks ``p.style == 'Folder'``.
"""
import sys
from pathlib import Path
_COMP = me.parent()
def _package_root() -> Path:
"""Folder containing the ``embryo`` package."""
par = _COMP.par['Embryopath']
folder = par.eval().strip() if par is not None else ''
return Path(folder).expanduser() if folder else Path(project.folder)
def _ensure_importable() -> str:
root = str(_package_root())
if root not in sys.path:
sys.path.insert(0, root)
return root
_ensure_importable()
from embryo.engine import EmbryoEngine # noqa: E402,F401
from embryo.dna import DNA # noqa: E402,F401
def Reload():
"""Re-import the package after editing it on disk, then re-init the
extension. Pulse the COMP's ``Reloadcode`` parameter to run this.
The ``global`` re-binding is the part that is easy to miss: this DAT's
module holds its own reference to ``EmbryoEngine``, and that is what
``ext0object`` reads. Reloading the package alone leaves that name bound
to the previous class object, so re-initialising would hand you the old
code back and the reload would look like it did nothing.
"""
global EmbryoEngine, DNA
import embryo
_ensure_importable()
embryo.reload_all()
from embryo.engine import EmbryoEngine
from embryo.dna import DNA
_COMP.par.reinitextensions.pulse()
print(f'Embryo: reloaded from {_package_root()} and re-initialised.')