Simulation Nodes: bake simulation states to disk #106937

Merged
Jacques Lucke merged 116 commits from JacquesLucke/blender:sim-bake into geometry-nodes-simulation 2023-04-22 14:48:56 +02:00
3 changed files with 49 additions and 21 deletions
Showing only changes of commit 03491d3785 - Show all commits

View File

@ -243,6 +243,9 @@ class ArrayValue : public ContainerValue<Vector<std::shared_ptr<Value>>, eValueT
void append_int(int value);
void append_str(std::string value);
void append_null();
std::shared_ptr<DictionaryValue> append_dict();
std::shared_ptr<ArrayValue> append_array();
};
/**
@ -284,6 +287,8 @@ class DictionaryValue
void append_int(std::string key, int value);
void append_str(std::string key, std::string value);
std::shared_ptr<DictionaryValue> append_dict(std::string key);
std::shared_ptr<ArrayValue> append_array(std::string key);
};
/**

View File

@ -207,6 +207,25 @@ void ArrayValue::append_str(std::string value)
this->append(std::make_shared<StringValue>(std::move(value)));
}
void ArrayValue::append_null()
{
this->append(std::make_shared<NullValue>());
}
std::shared_ptr<DictionaryValue> ArrayValue::append_dict()
{
auto value = std::make_shared<DictionaryValue>();
this->append(value);
return value;
}
std::shared_ptr<ArrayValue> ArrayValue::append_array()
{
auto value = std::make_shared<ArrayValue>();
this->append(value);
return value;
}
void DictionaryValue::append_int(std::string key, const int value)
{
this->append(std::move(key), std::make_shared<IntValue>(value));
@ -217,6 +236,20 @@ void DictionaryValue::append_str(std::string key, const std::string value)
this->append(std::move(key), std::make_shared<StringValue>(value));
}
std::shared_ptr<DictionaryValue> DictionaryValue::append_dict(std::string key)
{
auto value = std::make_shared<DictionaryValue>();
this->append(std::move(key), value);
return value;
}
std::shared_ptr<ArrayValue> DictionaryValue::append_array(std::string key)
{
auto value = std::make_shared<ArrayValue>();
this->append(std::move(key), value);
return value;
}
void JsonFormatter::serialize(std::ostream &os, const Value &value)
{
nlohmann::ordered_json j;

View File

@ -79,11 +79,10 @@ static std::shared_ptr<io::serialize::ArrayValue> serialize_material_slots(
auto io_materials = std::make_shared<io::serialize::ArrayValue>();
for (const Material *material : material_slots) {
if (material == nullptr) {
io_materials->append(std::make_shared<io::serialize::NullValue>());
io_materials->append_null();
}
else {
auto io_material = std::make_shared<io::serialize::DictionaryValue>();
io_materials->append(io_material);
auto io_material = io_materials->append_dict();
io_material->append_str("name", material->id.name + 2);
if (material->id.lib != nullptr) {
io_material->append_str("lib_name", material->id.lib->id.name + 2);
@ -99,8 +98,7 @@ static std::shared_ptr<io::serialize::ArrayValue> serialize_attributes(
auto io_attributes = std::make_shared<io::serialize::ArrayValue>();
attributes.for_all(
[&](const bke::AttributeIDRef &attribute_id, const bke::AttributeMetaData &meta_data) {
auto io_attribute = std::make_shared<io::serialize::DictionaryValue>();
io_attributes->append(io_attribute);
auto io_attribute = io_attributes->append_dict();
io_attribute->append_str("name", attribute_id.name());
@ -130,8 +128,7 @@ static std::shared_ptr<io::serialize::DictionaryValue> serialize_geometry_set(
auto io_geometry = std::make_shared<io::serialize::DictionaryValue>();
if (geometry.has_mesh()) {
const Mesh &mesh = *geometry.get_mesh_for_read();
auto io_mesh = std::make_shared<io::serialize::DictionaryValue>();
io_geometry->append("mesh", io_mesh);
auto io_mesh = io_geometry->append_dict("mesh");
io_mesh->append_int("num_vertices", mesh.totvert);
io_mesh->append_int("num_edges", mesh.totedge);
@ -154,8 +151,7 @@ static std::shared_ptr<io::serialize::DictionaryValue> serialize_geometry_set(
}
if (geometry.has_pointcloud()) {
const PointCloud &pointcloud = *geometry.get_pointcloud_for_read();
auto io_pointcloud = std::make_shared<io::serialize::DictionaryValue>();
io_geometry->append("pointcloud", io_pointcloud);
auto io_pointcloud = io_geometry->append_dict("pointcloud");
io_pointcloud->append_int("num_points", pointcloud.totpoint);
@ -167,8 +163,7 @@ static std::shared_ptr<io::serialize::DictionaryValue> serialize_geometry_set(
}
if (geometry.has_curves()) {
const Curves &curves = *geometry.get_curves_for_read();
auto io_curves = std::make_shared<io::serialize::DictionaryValue>();
io_geometry->append("curves", io_curves);
auto io_curves = io_geometry->append_dict("curves");
io_curves->append_int("num_points", curves.geometry.point_num);
io_curves->append_int("num_curves", curves.geometry.curve_num);
@ -288,8 +283,7 @@ static int bake_simulation_exec(bContext *C, wmOperator *op)
HooglyBoogly marked this conversation as resolved
Review
  if (objects.is_empty()) {
    return OPERATOR_CANCELLED;
  }
```cpp if (objects.is_empty()) { return OPERATOR_CANCELLED; } ```
io::serialize::DictionaryValue io_root;
io_root.append_int("version", 1);
auto io_zones = std::make_shared<io::serialize::ArrayValue>();
io_root.append("zones", io_zones);
auto io_zones = io_root.append_array("zones");
ModifierSimulationCache &sim_cache = *nmd.simulation_cache;
const ModifierSimulationState *sim_state = sim_cache.get_state_at_time(frame);

An error message when this fails maybe?

        if (BLI_exists(bake_directory.c_str())) {
          if (!BLI_delete(bake_directory.c_str(), true, true)) {
            BKE_reportf(op->reports,
                        RPT_ERROR,
                        "Failed to remove bake directory %s",
                        bake_directory.c_str());
          }
        }
An error message when this fails maybe? ```cpp if (BLI_exists(bake_directory.c_str())) { if (!BLI_delete(bake_directory.c_str(), true, true)) { BKE_reportf(op->reports, RPT_ERROR, "Failed to remove bake directory %s", bake_directory.c_str()); } } ```
@ -302,25 +296,21 @@ static int bake_simulation_exec(bContext *C, wmOperator *op)
const SimulationZoneID &zone_id = item.key;
const SimulationZoneState &zone_state = *item.value;
auto io_zone = std::make_shared<io::serialize::DictionaryValue>();
io_zones->append(io_zone);
auto io_zone = io_zones->append_dict();
auto io_zone_id = std::make_shared<io::serialize::ArrayValue>();
io_zone->append("zone_id", io_zone_id);
auto io_zone_id = io_zone->append_array("zone_id");
for (const int node_id : zone_id.node_ids) {
io_zone_id->append_int(node_id);
}
auto io_state_items = std::make_shared<io::serialize::ArrayValue>();
io_zone->append("state_items", io_state_items);
auto io_state_items = io_zone->append_array("state_items");
for (const std::unique_ptr<SimulationStateItem> &state_item : zone_state.items) {
/* TODO: Use better id. */
const std::string state_item_id = std::to_string(&state_item -
zone_state.items.begin());
auto io_state_item = std::make_shared<io::serialize::DictionaryValue>();
io_state_items->append(io_state_item);
auto io_state_item = io_state_items->append_dict();
io_state_item->append_str("id", state_item_id);