Skip to content

Commit

Permalink
improve frontend method chaining for add
Browse files Browse the repository at this point in the history
  • Loading branch information
ryichando committed Dec 28, 2024
1 parent 5bb155c commit 27f5e2d
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 34 deletions.
2 changes: 1 addition & 1 deletion examples/cards.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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();"
Expand Down
66 changes: 39 additions & 27 deletions examples/frontend/_scene_.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/stack.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 4 additions & 4 deletions examples/trampoline.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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();"
Expand Down
2 changes: 1 addition & 1 deletion examples/trapped.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 27f5e2d

Please sign in to comment.