blackjack icon indicating copy to clipboard operation
blackjack copied to clipboard

Ability to have an existing file as an input

Open novhack opened this issue 2 years ago • 2 comments

I've been experimenting with this software for a few days and so far I've been loving it.

I ventured into how nodes are made in Lua and been wondering if there is a way to use an existing file as an input. So far I found out that "file" type is for now only meant for saving files since it opens "save" system dialog via rfd::FileDialog::new().save_file().

An ability to change a "mode" of file inputs to "save" or "open" would help me a lot because I would like to make a custom node which reads a pre-generated list of 2D positions from a file.

novhack avatar Aug 05 '22 14:08 novhack

I've been experimenting with this software for a few days and so far I've been loving it.

Thanks!! :smile: I'd love to see what people are doing with Blackjack, so far the discussion forum has been quiet. Please feel free to share your progress / ideas / frustrations on the discussion forums, it'd help a lot! :heart:

been wondering if there is a way to use an existing file as an input

Not currently, but it's also not a very difficult thing to add. Here's what we would need:

  • [ ] The file node parameter type needs to take an additional parameter specifying whether it's a file "read" or "write", then we can change the rfd call to use open_file or save_file accordingly.
  • [ ] Some functions for file IO would need to be exposed to the Lua API. Blackjack is currently using Luau, which is more sandboxed than vanilla lua and functions like io.open are not available. Instead, blackjack needs to provide implementations using Rust's std APIs.

After this, users should be able to define their own nodes that can read existing files as strings and do their own parsing. On top of that, we can provide additional functions to parse common file formats. I already have a function to load an OBJ file into a mesh somewhere in the codebase, so it would only need to get exposed to the Lua API.

Let me see if I can tackle this :thinking:

setzer22 avatar Aug 09 '22 17:08 setzer22

Alright, that was faster than I expected :smile: All the required bits are there, but the code still lives on my development branch. You can find the changes in 86c89d5dcbb0, or feature/lua_api_proc_macro. Things should work if you stay on that specific commit, but I make no promises about the branch itself.

In case you do that, there's a couple of things to keep in mind:

  • You can now use Io.read_to_string(path) and Io.write(path, contents) inside Lua code. These are equivalent to std::fs::read_to_string and std::fs::write from Rust. These should work on any custom node. I've added a node_params.lua file creating a custom node that reads some data.
  • You can now use require inside lua code. To define new nodes, it is now recommended to use local P = require("params") at the top of your file, and define any inputs and outputs using that module, so where you used v3, you can now use P.v3("some_vector", vector(1,2,3)). After this, you can get rid of all the ad-hoc definitions at the top of the file. You can find documented versions of these functions in blackjack_engine/src/lua_engine/node_params.lua.
  • User code does no longer live inside the node_libraries folder. It is now inside blackjack_lua/run. Anything you put on blackjack_lua/lib will be available to require, so blackjack_lua/lib/foo.lua can be required as require('foo').

This is part of a bunch of upcoming features, together with better ergonomics to expose lua functions and documentation powered by ldoc.

setzer22 avatar Aug 09 '22 17:08 setzer22

This was merged a couple days ago as part of #45. There is a Import OBJ node that should serve as example for custom code.

setzer22 avatar Oct 09 '22 05:10 setzer22