diff --git a/examples/cards.ipynb b/examples/cards.ipynb index 21e9363..845881f 100644 --- a/examples/cards.ipynb +++ b/examples/cards.ipynb @@ -48,7 +48,7 @@ " _x, _y = make_row(i+1, _x, _y)\n", "\n", "scene.add(\"sphere\").at(-2,1,0).jitter().velocity(2.3,0,0)\n", - "scene.add_invisible_wall([0,0,0],[0,1,0])\n", + "scene.add.invisible.wall([0,0,0],[0,1,0])\n", "\n", "fixed = scene.build().report()\n", "fixed.preview();" diff --git a/examples/frontend/_scene_.py b/examples/frontend/_scene_.py index da90425..600e9fa 100644 --- a/examples/frontend/_scene_.py +++ b/examples/frontend/_scene_.py @@ -542,6 +542,44 @@ def __init__(self, name: str, scene: "Scene"): self.name = name +class InvisibleAdder: + def __init__(self, scene: "Scene"): + self._scene = scene + + def sphere(self, position: list[float], radius: float) -> Sphere: + sphere = Sphere().add(position, radius) + self._scene._sphere.append(sphere) + return sphere + + def wall(self, position: list[float], normal: list[float]) -> Wall: + wall = Wall().add(position, normal) + self._scene._wall.append(wall) + return wall + + +class SessionAdder: + def __init__(self, scene: "Scene"): + self._scene = scene + self.invisible = InvisibleAdder(scene) + + def __call__(self, mesh_name: str, ref_name: str = "") -> "Object": + if ref_name == "": + ref_name = mesh_name + count = 0 + while ref_name in self._scene._object.keys(): + count += 1 + ref_name = f"{mesh_name}_{count}" + mesh_list = self._scene._asset.list() + if mesh_name not in mesh_list: + raise Exception(f"mesh_name '{mesh_name}' does not exist") + elif ref_name in self._scene._object.keys(): + raise Exception(f"ref_name '{ref_name}' already exists") + else: + obj = Object(self._scene._asset, mesh_name) + self._scene._object[ref_name] = obj + return obj + + class Scene: def __init__(self, name: str, plot: PlotManager, asset: AssetManager, save_func): self._name = name @@ -551,45 +589,19 @@ def __init__(self, name: str, plot: PlotManager, asset: AssetManager, save_func) self._object = {} self._sphere = [] self._wall = [] + self.add = SessionAdder(self) self.info = SceneInfo(name, self) def clear(self) -> "Scene": self._object.clear() return self - def add(self, mesh_name: str, ref_name: str = "") -> "Object": - if ref_name == "": - ref_name = mesh_name - count = 0 - while ref_name in self._object.keys(): - count += 1 - ref_name = f"{mesh_name}_{count}" - mesh_list = self._asset.list() - if mesh_name not in mesh_list: - raise Exception(f"mesh_name '{mesh_name}' does not exist") - elif ref_name in self._object.keys(): - raise Exception(f"ref_name '{ref_name}' already exists") - else: - obj = Object(self._asset, mesh_name) - self._object[ref_name] = obj - return obj - def pick(self, name: str) -> "Object": if name not in self._object.keys(): raise Exception(f"object {name} does not exist") else: return self._object[name] - def add_invisible_sphere(self, position: list[float], radius: float) -> Sphere: - sphere = Sphere().add(position, radius) - self._sphere.append(sphere) - return sphere - - def add_invisible_wall(self, position: list[float], normal: list[float]) -> Wall: - wall = Wall().add(position, normal) - self._wall.append(wall) - return wall - def build(self) -> FixedScene: pbar = tqdm(total=10, desc="build", ncols=70) concat_count = 0 diff --git a/examples/stack.ipynb b/examples/stack.ipynb index 18f42c1..0bdda5f 100644 --- a/examples/stack.ipynb +++ b/examples/stack.ipynb @@ -18,7 +18,7 @@ "app.asset.add.tet(\"sphere\", V, F, T)\n", "\n", "scene = app.scene.create(\"ten-sheets\")\n", - "scene.add_invisible_wall([0,0,0],[0,1,0])\n", + "scene.add.invisible.wall([0,0,0],[0,1,0])\n", "\n", "n, space = 10, 0.05\n", "for i in range(n):\n", diff --git a/examples/trampoline.ipynb b/examples/trampoline.ipynb index 35c03e4..ce6c9cf 100644 --- a/examples/trampoline.ipynb +++ b/examples/trampoline.ipynb @@ -26,10 +26,10 @@ "armadillo.at(0,1,0).jitter().velocity(0,-5,0)\n", "\n", "gap = 0.025\n", - "scene.add_invisible_wall([1+gap,0,0],[-1,0,0])\n", - "scene.add_invisible_wall([-1-gap,0,0],[1,0,0])\n", - "scene.add_invisible_wall([0,0,1+gap],[0,0,-1])\n", - "scene.add_invisible_wall([0,0,-1-gap],[0,0,1])\n", + "scene.add.invisible.wall([1+gap,0,0],[-1,0,0])\n", + "scene.add.invisible.wall([-1-gap,0,0],[1,0,0])\n", + "scene.add.invisible.wall([0,0,1+gap],[0,0,-1])\n", + "scene.add.invisible.wall([0,0,-1-gap],[0,0,1])\n", "\n", "fixed = scene.build().report()\n", "fixed.preview();" diff --git a/examples/trapped.ipynb b/examples/trapped.ipynb index b29c8e4..3b41efc 100644 --- a/examples/trapped.ipynb +++ b/examples/trapped.ipynb @@ -16,7 +16,7 @@ "\n", "scene = app.scene.create(\"sphere-trap\")\n", "\n", - "scene.add_invisible_sphere([0,0,0],0.7).invert().radius(0.15,3).radius(100,4)\n", + "scene.add.invisible.sphere([0,0,0],0.7).invert().radius(0.15,3).radius(100,4)\n", "scene.add(\"armadillo\").jitter().rotate(180,\"y\")\n", "\n", "fixed = scene.build().report()\n",