2

pickle.dump(pendulum, open("obj/pendulum1.obj", "wb")) returns TypeError: cannot pickle 'pydrake.examples.pendulum.PendulumPlant' object. Here, pendulum is an object of a self-defined class with an PendulumPlant object inside.

Is it because pydrake is not natively written in python?

Yukun Xia
  • 65
  • 7

1 Answers1

3

Correct. Most of the C++-backed classes in pydrake are not pickle-able

Some of the elementary pydrake classes are pickable though, such RigidTransform, RotationMatrix, etc. (See https://github.com/RobotLocomotion/drake/pull/11976.)

We could add pickling for some kinds of additional classes, but pickling a System or a Diagram is likely to be too difficult.

To save + restore a System like the pendulum, I'd say that re-creating the plant from scratch should be good -- it has no internal state so any one instance is the same as any other instance.

jwnimmer-tri
  • 1,994
  • 2
  • 5
  • 6
  • To add to this, one reason we don't support pickling things like like Systems or Diagrams in C++ is because we don't support serialization in general. If serialization were to be supported in C++, adding `pydrake` support (esp. in light of pickling) would be (nigh) trivial. However, serialization in general is a bit open-ended and non-trivial to implement, so recreating from scratch may be the better route. – Eric Cousineau May 09 '20 at 20:56
  • FTR Here is an example of doing custom pickling for objects that are not picklable (if you want to try it yourself): https://gist.github.com/EricCousineau-TRI/677cac69889a32326b91be1d233aa8c2#file-pydrake_custom_pickling_example-py. Additionally, here's another (smaller) PR that adds pickling to `CameraInfo`: https://github.com/RobotLocomotion/drake/pull/12131/files – Eric Cousineau May 09 '20 at 20:57