Invoking Blocks code from a Java Opmode

TL;DR I’m interested in making a Java class that can load and run block code, but I need help understanding how BlocksOpMode does it and how I could use that knowledge to do it myself

Our team has been using the MyBlocks feature of the FTC SDK to add advanced functionality to our blocks environment, and it’s been working quite well for accessibility and speed. However, we’ll eventually need to make several very similar autonomous programs (i.e for both sides of the field), and I’d like to come up with a better way of doing that than copy-pasting and tweaking code across projects.

A potential solution I came up with involves running block code from a Java Opmode. The idea is to have a single autonomous written in Blocks with variables to change the direction of certain robot movements. Several Java Opmodes would be able to run the block autonomous program with changes to the aforementioned variables. That way, the single Blocks program could be run with several different sets of parameters, instead of having four different projects to maintain.

I’ve been doing some poking around in the internal BlocksOpMode class code to see how it loads and runs a Blocks program. It seems quite a bit more complicated than I expected. From what I understand, it creates a WebVeiw object and loads the Blocks runtime into it along with the desired Blocks project. It also uses some strange magic I don’t understand to connect that to the robot components, and has a bunch of other bells and whistles I have yet to decipher. It would be awesome to use BlocksOpMode as a reference to make a class that runs blocks code and can be used from any other Java Opmode. Any help that can be provided would be greatly appreciated!

I don’t think that’s possible - blocks code under the hood is essentially Javascript. The SDK has the ability to call into the runOpMode function in order to execute the code, but you cannot call directly into any other function. Blocks can call Java through a myBlock (some miracle magic from @lizlooney) but not the other way around.

-Danny