py systems: Unable to downcast System
to known intermediate type (e.g. Diagram
)
#13298
Labels
System
to known intermediate type (e.g. Diagram
)
#13298
I'm sure this is a known issue, but doesn't look like it's already covered here:
https://pybind11.readthedocs.io/en/stable/classes.html#inheritance-and-automatic-downcasting
Issue
Basically, if you have a type, say
class MyDiagram : public Diagram<double>
, that you add within a nested hierarchy of diagrams viaDiagramBuilder
, and then you want to hack-debug this (e.g. add a printer in Python which just digs out the system, callsGetMyContextFromRoot
, and evaluate ports), you can kind of do this, if you only need the ports.However, if you need
Diagram
functionality (likeGetSubsystemByName
) from an API that only returnsSystem
(e.g.DiagramBuilder.AddSystem
), it won't work.Since
pybind11
doesn't know aboutMyDiagram
, it doesn't know about intermediate bases, so it can only downcast toSystem
.Use Case
For example, I'd love to write a Python script which just crawls the overall structure and give me direct pointers to systems.
I can see a structure from GraphViz, but I'd like to easily dig in as well.
AFAIK, this isn't currently possible in
pydrake
.Repro
EricCousineau-TRI@09b73385e4
Workaround
For specific things, like crawling around the full structure of nested diagrams in Python, it'd be nice to have a
as_diagram()
method which attempts to cast theSystem_[T]
toDiagram_[T]
, or returnsNone
if it fails.The text was updated successfully, but these errors were encountered: