I do something wrong but I can’t find the problem.
The C++ code:
VALUE create_layer_materials(VALUE self, VALUE filename){
SUInitialize();
SUModelRef model = SU_INVALID;
// Load the SketchUp model.
std::string path = GetRubyString(filename);
SUModelCreateFromFile(&model, path.c_str());
// Load materials
size_t num_materials;
size_t count;
SUModelGetNumMaterials(model, &num_materials);
std::vector<SUMaterialRef> materials(num_materials);
for (size_t i = 0; i < num_materials; ++i) {
SUSetInvalid(materials[i]);
}
SUModelGetMaterials(model, num_materials, &materials[0], &count);
// Create layers
std::vector<SULayerRef> add_layers(num_materials);
SUTextureRef texture = SU_INVALID;
SUMaterialRef layer_material = SU_INVALID;
for (size_t i = 0; i < num_materials; ++i) {
SUSetInvalid(add_layers[i]);
SULayerCreate(&add_layers[i]);
std::string new_name = "Layer " + std::to_string(i);
SULayerSetName(add_layers[i], new_name.c_str());
SULayerGetMaterial(add_layers[i], &layer_material);
SUMaterialGetTexture(materials[i], &texture);
SUMaterialSetTexture(layer_material, texture);
}
SUModelAddLayers(model, num_materials, &add_layers[0]);
// Save the in-memory model to a file
SUModelSaveToFile(model, path.c_str());
//Release model
SUModelRelease(&model);
add_layers.clear();
materials.clear();
SUTerminate();
return Qtrue;
}
The skp-file is created correctly, but SketchUp crashes.
If I remove the line:
SUMaterialSetTexture(layer_material, texture);
SU doesn’t crash but of course the result isn’t what I need.
If I remove the line:
SUModelRelease(&model);
SU doesn’t crash and I get the result I need. But not releasing the model is bad.