Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ tests/
```

### Key conventions
- **Imports:** relative within the add-in (`from .lib import …`); nothing touches `sys.path`. Fusion loads the add-in folder as a package and runs every add-in in one interpreter, so an absolute `from lib import …` picks up whichever add-in's `lib` loaded first — this happened for real with a second add-in that also has a `lib` package. `tests/headless/test_entry.py` guards it.
- **Collections:** `SketchCurveVector` (from `.parentCurves`, `.childCurves`, `.curves`) uses `len()` + iteration, not `.count`. `ObjectCollection` uses `.count` + `.item(i)`.
- **Event handlers:** Always appended to `events._handlers` list (M-7). Never instantiate a handler without pinning it.
- **Palette sends:** Always gated on `palette.isVisible` (M-8 guard in `messaging.send()`).
Expand Down
12 changes: 7 additions & 5 deletions ConstraintLens/ConstraintLens.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
# ConstraintLens — Fusion 360 add-in entry point (see SPEC.md section 4).
#
# Owns only run(context) / stop(context). All logic lives under ./lib.
#
# Fusion loads each add-in folder as a package, so the import is relative. Fusion
# runs every add-in in one Python interpreter: an absolute `from lib import …`
# (with the folder added to sys.path) would share a module name with any other
# add-in that has a `lib` package, and whichever loaded first would win.

import os
import sys
import traceback

import adsk.core

_ADDIN_DIR = os.path.dirname(os.path.realpath(__file__))
if _ADDIN_DIR not in sys.path:
sys.path.insert(0, _ADDIN_DIR)
from .lib import lifecycle

from lib import lifecycle # noqa: E402
_ADDIN_DIR = os.path.dirname(os.path.realpath(__file__))


def run(context):
Expand Down
4 changes: 3 additions & 1 deletion tests/headless/_bootstrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Imported first by every test module here. The stubs directory has to come
before anything else so `import adsk.core` finds the fake rather than failing.
Fusion loads the add-in folder as a package, so tests import
`ConstraintLens.lib…` the same way.
"""

import os
Expand All @@ -10,6 +12,6 @@
_HERE = os.path.dirname(os.path.abspath(__file__))
_REPO_ROOT = os.path.dirname(os.path.dirname(_HERE))

for _path in (os.path.join(_HERE, "stubs"), os.path.join(_REPO_ROOT, "ConstraintLens")):
for _path in (os.path.join(_HERE, "stubs"), _REPO_ROOT):
if _path not in sys.path:
sys.path.insert(0, _path)
2 changes: 1 addition & 1 deletion tests/headless/test_dimensions.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import _bootstrap # noqa: F401

import fakes
from lib import dispatch, labels, scanner
from ConstraintLens.lib import dispatch, labels, scanner


def _linear(expression, value, unit="mm", entities=None):
Expand Down
28 changes: 28 additions & 0 deletions tests/headless/test_entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
"""ConstraintLens.py — the entry point loads its own code whatever else Fusion has loaded."""

import importlib
import sys
import types
import unittest

import _bootstrap # noqa: F401


class EntryPointTest(unittest.TestCase):
def test_uses_its_own_lib_when_another_add_in_s_lib_is_already_imported(self):
# Fusion runs every add-in in one interpreter. Another add-in that did
# `from lib import lifecycle` leaves its own `lib` package behind.
other = types.ModuleType("lib")
other.lifecycle = types.ModuleType("lib.lifecycle")
sys.modules.update({"lib": other, "lib.lifecycle": other.lifecycle})
sys.modules.pop("ConstraintLens.ConstraintLens", None) # import it afresh
try:
entry = importlib.import_module("ConstraintLens.ConstraintLens")
finally:
del sys.modules["lib"], sys.modules["lib.lifecycle"]

self.assertEqual(entry.lifecycle.__name__, "ConstraintLens.lib.lifecycle")


if __name__ == "__main__":
unittest.main()
2 changes: 1 addition & 1 deletion tests/headless/test_labels.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import _bootstrap # noqa: F401

import fakes
from lib import labels
from ConstraintLens.lib import labels


class LabelerTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/headless/test_lifecycle.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import _bootstrap # noqa: F401

import fakes
from lib import lifecycle
from ConstraintLens.lib import lifecycle


class DerivedUnitFormattingTest(unittest.TestCase):
Expand Down
2 changes: 1 addition & 1 deletion tests/headless/test_scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import _bootstrap # noqa: F401

import fakes
from lib import labels, scanner
from ConstraintLens.lib import labels, scanner


def _line(token):
Expand Down
Loading