diff --git a/surfaces/blender/brokkr_kitbash/__init__.py b/surfaces/blender/brokkr_kitbash/__init__.py index cb5b956..52a9ce0 100644 --- a/surfaces/blender/brokkr_kitbash/__init__.py +++ b/surfaces/blender/brokkr_kitbash/__init__.py @@ -199,14 +199,16 @@ def free(part, socket: str) -> None: def mated(parent, parent_part, parent_built, child, child_part, child_built, stated, how="on"): """(parent socket, child socket) of the connection `stated` between two built parts, or a refusal that names - it: a roll that is not a number, parts of two kits, a socket name a part does not have, or sockets - core.mate_refusal will not mate. A closure is checked here too (how "closing on"), the part it names as the - parent.""" + it: a roll that is not a number, a socket name a part does not have, or sockets core.mate_refusal will not + mate. The parts may be of two kits: they mate when both kits declare the socket's type alike. A closure is + checked here too (how "closing on"), the part it names as the parent.""" if stated[2] is None: raise Refused(f"{child.name!r} on {parent.name!r}: its {ROLL} is not a number: detach it and attach it again; nothing changed.") try: sockets = core.joint(parent_part, parent_built, stated[1], child_part, child_built, stated[0]) - refusal = core.mate_refusal(core.load_kit(parent_part["kit"])["socketTypes"], *sockets) + kits = (parent_part["kit"], child_part["kit"]) + refusal = core.mate_refusal(core.load_kit(kits[0])["socketTypes"], *sockets, + None if kits[0] == kits[1] else core.load_kit(kits[1])["socketTypes"], kits) except core.KitError as error: refusal = str(error) if refusal: @@ -221,8 +223,8 @@ def seat(child, basis) -> None: def connection_report(obj, part, built): - """None for a part that states no connection, else what it states and whether it holds: the parent is a part - of the same kit, both sockets exist, the roll is a number, the parent inverse is the identity and the child + """None for a part that states no connection, else what it states and whether it holds: the parent is a kit + part, both sockets exist, the roll is a number, the parent inverse is the identity and the child stands within HELD of core.mate. A child moved or re-parented by another action reads holds false until set_params seats it again.""" stated = link(obj) @@ -244,8 +246,8 @@ def connection_report(obj, part, built): def closure_report(obj, part, built, held=None, kits=None): """None for a part that states no closure, else what it states, whether it holds and, when it does not, why - not: the other part is gone (`to` null); it is not a part this one can be read against (not a kit part, of - another kit, scaled, or without that socket); another part's closure names the same socket (a duplicated + not: the other part is gone (`to` null); it is not a part this one can be read against (not a kit part, + scaled, or without that socket); another part's closure names the same socket (a duplicated closing part); or the two sockets stand more than HELD from meeting (core.closure_residual). Derived at each report; stored nowhere. `held` is closers() and `kits` kit_part_of's, for a caller that reports many.""" stated = closure(obj) @@ -464,8 +466,8 @@ class BROKKR_KITBASH_OT_set_params(PolishOperator): class BROKKR_KITBASH_OT_attach(PolishOperator): - """Seat a socket of a kit part on a socket of another part of its kit and parent it there: origins together, - normals opposed, turned by roll about the parent socket's normal""" + """Seat a socket of a kit part on a socket of another part, of its kit or of any kit whose socket types agree, + and parent it there: origins together, normals opposed, turned by roll about the parent socket's normal""" bl_idname = "brokkr_kitbash.attach" bl_label = "Attach Kit Part" object_name: bpy.props.StringProperty(name="Object", description="The child: the part that moves") @@ -525,8 +527,8 @@ class BROKKR_KITBASH_OT_detach(PolishOperator): class BROKKR_KITBASH_OT_close(PolishOperator): - """State that a socket of a kit part meets a socket of another part of its kit, closing a loop the tree of - connections cannot: accepted only when the two already meet, origins together and normals opposed, and it + """State that a socket of a kit part meets a socket of another part, of its kit or of any kit whose socket types + agree, closing a loop the tree of connections cannot: accepted only when the two already meet, origins together and normals opposed, and it moves nothing""" bl_idname = "brokkr_kitbash.close" bl_label = "Close Kit Loop" diff --git a/surfaces/blender/brokkr_kitbash/core.py b/surfaces/blender/brokkr_kitbash/core.py index 3f7c3cf..5713ab6 100644 --- a/surfaces/blender/brokkr_kitbash/core.py +++ b/surfaces/blender/brokkr_kitbash/core.py @@ -11,7 +11,8 @@ whitelist; nothing in a kit is ever run as Python. built = build(part(kit, "tank"), {"length": 1.2}) # Built: V, faces, counts, smooth, sharp, shell_count, sockets Two sockets mate through mate (the child's matrix under its parent), mate_refusal (what may mate) and residual -(how far a placed child stands from its mate). closure_residual is how far two sockets stand from meeting as a pin, +(how far a placed child stands from its mate). Socket types are a vocabulary kits share: parts of two kits mate when +both kits declare the socket's type with the same match rule. closure_residual is how far two sockets stand from meeting as a pin, whatever the roll: what closes a loop the tree of connections cannot state. Every refusal is a KitError whose message names the kit, part and field. @@ -633,13 +634,20 @@ def closure_residual(world_a, socket_a: dict, world_b, socket_b: dict) -> float: EQUAL, SLACK = 0.01, 1e-9 # 'equal' sizes may differ by 1 percent of the larger; SLACK keeps a size at the limit inside it -def mate_refusal(kit_types: dict, parent_socket: dict, child_socket: dict) -> str | None: +def mate_refusal(kit_types: dict, parent_socket: dict, child_socket: dict, child_types: dict | None = None, kits=("the parent's", "the child's")) -> str | None: """None when the two sockets may mate, else a sentence naming both: they are of one type, and their sizes - agree as that type's match says ('equal': within 1 percent of the larger; 'fits': the child at most the parent).""" + agree as that type's match says ('equal': within 1 percent of the larger; 'fits': the child at most the parent). + kit_types is the socketTypes of the parent's kit. For parts of two kits, child_types is the socketTypes of the + child's and `kits` the two kits' names: socket types are a vocabulary kits share, and the two sockets mate only + when the child's kit declares the type with the same match.""" p, c = parent_socket, child_socket both = f"socket {c['name']!r} ({c['type']}, size {c['size']:.6g}) and parent socket {p['name']!r} ({p['type']}, size {p['size']:.6g})" if p["type"] != c["type"]: return f"{both} are of different types" + if child_types is not None and child_types.get(c["type"], {}).get("match") != kit_types[p["type"]]["match"]: + theirs = child_types.get(c["type"], {}).get("match") + return (f"{both}: kit {kits[0]!r} declares socket type {p['type']!r} with match {kit_types[p['type']]['match']!r} and kit {kits[1]!r} " + f"{'with match ' + repr(theirs) if theirs else 'does not declare it'}; parts of two kits mate by socket types both declare alike") if kit_types[p["type"]]["match"] == "equal": if abs(p["size"] - c["size"]) > EQUAL * max(p["size"], c["size"]) * (1 + SLACK): return f"{both} are {p['type']} sockets, which mate at equal sizes (within 1 percent)" @@ -649,11 +657,8 @@ def mate_refusal(kit_types: dict, parent_socket: dict, child_socket: dict) -> st def joint(parent_part: dict, parent_built: Built, parent_socket: str, child_part: dict, child_built: Built, child_socket: str): - """(parent socket, child socket) of a connection between two built parts, by socket name. Refuses parts of - different kits (socket types belong to a kit) and a name the part has no socket for.""" - if parent_part["kit"] != child_part["kit"]: - raise KitError(f"part {child_part['name']!r} is of kit {child_part['kit']!r} and part {parent_part['name']!r} of kit " - f"{parent_part['kit']!r}: socket types belong to a kit, so parts of different kits do not mate") + """(parent socket, child socket) of a connection between two built parts, of one kit or of two, by socket name. + Refuses a name the part has no socket for. Whether the two may mate is mate_refusal's: their socket types decide.""" found = [] for part, built, name in ((parent_part, parent_built, parent_socket), (child_part, child_built, child_socket)): match = [s for s in built.sockets if s["name"] == name] diff --git a/surfaces/blender/tests/test_kitbash.py b/surfaces/blender/tests/test_kitbash.py index 44f97d0..fc9fd43 100644 --- a/surfaces/blender/tests/test_kitbash.py +++ b/surfaces/blender/tests/test_kitbash.py @@ -724,6 +724,24 @@ class MateTests(unittest.TestCase): self.assertIn("it is larger", core.mate_refusal(types, socket("p", "mount", 0.1), socket("c", "mount", 0.3))) self.assertIn("it is larger", core.mate_refusal(types, socket("p", "mount", 0.3), socket("c", "mount", 0.3 * (1 + 2.2e-6)))) + def test_kits_mate_by_the_socket_types_they_share(self): + """cross-kit-types. Mutant S5 (mate_refusal ignores the child kit's match): a mount another kit declares 'equal' mates.""" + types = self.kit["socketTypes"] + socket = lambda name, kind, size: {"name": name, "type": kind, "size": size, "matrix": np.eye(4)} # noqa: E731 + hydraulic = copy.deepcopy(types) + for kind, big, small in (("flange", 100.0, 99.0), ("pivot", 0.2, 0.198), ("mount", 0.3, 0.1)): + self.assertIsNone(core.mate_refusal(types, socket("p", kind, big), socket("c", kind, small), hydraulic, ("mechanical", "hydraulic"))) + self.assertIn("mate at equal sizes", core.mate_refusal(types, socket("p", "flange", 100.0), socket("c", "flange", 98.0), hydraulic, ("mechanical", "hydraulic"))) + odd = {**hydraulic, "mount": {"match": "equal", "doc": ""}} + self.assertRegex(core.mate_refusal(types, socket("p", "mount", 0.3), socket("c", "mount", 0.3), odd, ("mechanical", "odd")), + r"kit 'mechanical' declares socket type 'mount' with match 'fits' and kit 'odd' with match 'equal'; parts of two kits mate by socket types both declare alike") + self.assertRegex(core.mate_refusal(odd, socket("p", "mount", 0.3), socket("c", "mount", 0.3), types, ("odd", "mechanical")), + r"kit 'odd' declares socket type 'mount' with match 'equal' and kit 'mechanical' with match 'fits'") + self.assertRegex(core.mate_refusal(types, socket("p", "pivot", 0.2), socket("c", "pivot", 0.2), {"mount": types["mount"]}, ("mechanical", "bare")), + r"kit 'mechanical' declares socket type 'pivot' with match 'equal' and kit 'bare' does not declare it") + self.assertIn("are of different types", core.mate_refusal(types, socket("p", "flange", 0.1), socket("c", "mount", 0.1), odd, ("mechanical", "odd"))) + self.assertIsNone(core.mate_refusal(types, socket("p", "mount", 0.3), socket("c", "mount", 0.3), None)) # one kit: as before + def test_the_kit_s_own_flanges_do_not_meet_everywhere(self): """A chamber flange (ribbed_cylinder, 0.16 and up) and the plumbing (pipe_fitting, 0.14 and down) never mate face to face.""" @@ -757,14 +775,16 @@ class MateTests(unittest.TestCase): self.assertEqual([s["matrix"][:3, 2].tolist() for s in reducer.sockets], [[0.0, 0.0, -1.0], [0.0, 0.0, 1.0]]) def test_a_joint_is_two_named_sockets_of_one_kit(self): - """The cross-kit refusal (one kit ships, so no scene can show it) and the unknown-socket refusals.""" + """A joint of one kit or of two (what may mate is mate_refusal's), and the unknown-socket refusals.""" parts = self.kit["parts"] tank, fitting = core.build(parts["tank"]), core.build(parts["pipe_fitting"]) sp, sc = core.joint(parts["tank"], tank, "port_b", parts["pipe_fitting"], fitting, "a") self.assertEqual((sp["name"], sc["name"]), ("port_b", "a")) other = {**parts["pipe_fitting"], "kit": "hydraulic"} - with self.assertRaisesRegex(core.KitError, "part 'pipe_fitting' is of kit 'hydraulic' and part 'tank' of kit 'mechanical'.*do not mate"): - core.joint(parts["tank"], tank, "port_b", other, fitting, "a") + sp, sc = core.joint(parts["tank"], tank, "port_b", other, fitting, "a") + self.assertEqual((sp["name"], sp["size"], sc["name"], sc["size"]), ("port_b", tank.sockets[1]["size"], "a", fitting.sockets[0]["size"])) + with self.assertRaisesRegex(core.KitError, "kit 'hydraulic' part 'pipe_fitting' has no socket 'd'; its sockets: a, b, c"): + core.joint(parts["tank"], tank, "port_b", other, fitting, "d") with self.assertRaisesRegex(core.KitError, "part 'tank' has no socket 'port_c'; its sockets: port_a, port_b, saddle, lug_a, lug_b, lug_a_back, lug_b_back"): core.joint(parts["tank"], tank, "port_c", parts["pipe_fitting"], fitting, "a") with self.assertRaisesRegex(core.KitError, "part 'pipe_fitting' has no socket 'd'; its sockets: a, b, c"):