How to pass variables to OpenModalDialogCommand
Solved.
Thanks @kiamlaluno.
I was convinced the variables were OK. But after your comment and a fresh look I saw what I was missing:
$this-&gt;nid, $this-&gt;foo, $this-&gt;bar are optional parameters for the UploadForm formbuilder. I was passing only nid and bar, but missing foo, which messed-up things.
So, instead of:

\Drupal::formBuilder()-&gt;getForm('UploadForm', $this-&gt;nid, $this-&gt;bar) //don't need foo


I needed to call:

\Drupal::formBuilder()-&gt;getForm('UploadForm', $this-&gt;nid, null, $this-&gt;bar)


A bit obvious, I'm afraid...