Cleanup: Miscellaneous changes to OBJ/import nodes

- Sort add menu alphabetically
- Use forward declaration for GeometrySet again
- Use `this->` to access class methods
- Use `MEM_cnew`
- Fix typo
- Pass Span by value
- Pass MutableSpan instead of Vector &
- Remove unnecessary whitespace
- Use `BLI_SCOPED_DEFER` for freeing non-RAII objects
- Use `is_empty()` instead of `size() == 0`
- Use `GeometrySet::from_mesh` ability to handle null argument
This commit is contained in:
Hans Goudey
2024-07-08 15:12:21 -04:00
parent 385dfb4650
commit 792efafa2c
9 changed files with 36 additions and 52 deletions

View File

@@ -456,8 +456,8 @@ class NODE_MT_category_import(Menu):
def draw(self, _context):
layout = self.layout
node_add_menu.add_node_type(layout, "GeometryNodeImportSTL")
node_add_menu.add_node_type(layout, "GeometryNodeImportOBJ")
node_add_menu.add_node_type(layout, "GeometryNodeImportSTL")
node_add_menu.draw_assets_for_catalog(layout, "Input/Import")

View File

@@ -29,8 +29,6 @@
#include "BLI_vector.hh"
#include "BLI_virtual_array_fwd.hh"
#include "BKE_geometry_set.hh"
#include "DNA_customdata_types.h"
struct Object;
@@ -43,6 +41,8 @@ class MutableAttributeAccessor;
namespace blender::bke {
struct GeometrySet;
/**
* Holds a reference to conceptually unique geometry or a pointer to object/collection data
* that is instanced with a transform in #Instances.
@@ -224,14 +224,6 @@ inline InstanceReference::InstanceReference(Collection &collection)
{
}
inline InstanceReference::InstanceReference(const InstanceReference &other)
: type_(other.type_), data_(other.data_)
{
if (other.geometry_set_) {
geometry_set_ = std::make_unique<GeometrySet>(*other.geometry_set_);
}
}
inline InstanceReference::InstanceReference(InstanceReference &&other)
: type_(other.type_), data_(other.data_), geometry_set_(std::move(other.geometry_set_))
{

View File

@@ -25,6 +25,14 @@ InstanceReference::InstanceReference(GeometrySet geometry_set)
{
}
InstanceReference::InstanceReference(const InstanceReference &other)
: type_(other.type_), data_(other.data_)
{
if (other.geometry_set_) {
geometry_set_ = std::make_unique<GeometrySet>(*other.geometry_set_);
}
}
void InstanceReference::ensure_owns_direct_data()
{
if (type_ != Type::GeometrySet) {

View File

@@ -39,7 +39,7 @@ Mesh *MeshFromGeometry::create_mesh(const OBJImportParams &import_params)
return nullptr;
}
fixup_invalid_faces();
this->fixup_invalid_faces();
/* Includes explicitly imported edges, not the ones belonging the faces to be created. */
Mesh *mesh = BKE_mesh_new_nomain(tot_verts_object,
@@ -47,12 +47,12 @@ Mesh *MeshFromGeometry::create_mesh(const OBJImportParams &import_params)
mesh_geometry_.face_elements_.size(),
mesh_geometry_.total_corner_);
create_vertices(mesh);
create_faces(mesh, import_params.import_vertex_groups && !import_params.use_split_groups);
create_edges(mesh);
create_uv_verts(mesh);
create_normals(mesh);
create_colors(mesh);
this->create_vertices(mesh);
this->create_faces(mesh, import_params.import_vertex_groups && !import_params.use_split_groups);
this->create_edges(mesh);
this->create_uv_verts(mesh);
this->create_normals(mesh);
this->create_colors(mesh);
if (import_params.validate_meshes || mesh_geometry_.has_invalid_faces_) {
bool verbose_validate = false;
@@ -71,7 +71,7 @@ Object *MeshFromGeometry::create_mesh_object(
Map<std::string, Material *> &created_materials,
const OBJImportParams &import_params)
{
Mesh *mesh = create_mesh(import_params);
Mesh *mesh = this->create_mesh(import_params);
if (mesh == nullptr) {
return nullptr;
@@ -86,14 +86,14 @@ Object *MeshFromGeometry::create_mesh_object(
Object *obj = BKE_object_add_only_object(bmain, OB_MESH, ob_name.c_str());
obj->data = BKE_object_obdata_add_from_type(bmain, OB_MESH, ob_name.c_str());
create_materials(bmain, materials, created_materials, obj, import_params.relative_paths);
this->create_materials(bmain, materials, created_materials, obj, import_params.relative_paths);
transform_object(obj, import_params);
BKE_mesh_nomain_to_mesh(mesh, static_cast<Mesh *>(obj->data), obj);
/* NOTE: vertex groups have to be created after final mesh is assigned to the object. */
create_vertex_groups(obj);
this->create_vertex_groups(obj);
return obj;
}

View File

@@ -34,9 +34,9 @@ Curve *blender::io::obj::CurveFromGeometry::create_curve()
/* Only one NURBS spline will be created in the curve object. */
curve->actnu = 0;
Nurb *nurb = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "OBJ import NURBS curve"));
Nurb *nurb = MEM_cnew<Nurb>(__func__);
BLI_addtail(BKE_curve_nurbs_get(curve), nurb);
create_nurbs(curve);
this->create_nurbs(curve);
return curve;
}
@@ -61,9 +61,9 @@ Object *CurveFromGeometry::create_curve_object(Main *bmain, const OBJImportParam
/* Only one NURBS spline will be created in the curve object. */
curve->actnu = 0;
Nurb *nurb = static_cast<Nurb *>(MEM_callocN(sizeof(Nurb), "OBJ import NURBS curve"));
Nurb *nurb = MEM_cnew<Nurb>(__func__);
BLI_addtail(BKE_curve_nurbs_get(curve), nurb);
create_nurbs(curve);
this->create_nurbs(curve);
obj->data = curve;
transform_object(obj, import_params);
@@ -79,7 +79,7 @@ void CurveFromGeometry::create_nurbs(Curve *curve)
nurb->type = CU_NURBS;
nurb->flag = CU_3D;
nurb->next = nurb->prev = nullptr;
/* BKE_nurb_points_add later on will update pntsu. If this were set to total curv points,
/* BKE_nurb_points_add later on will update pntsu. If this were set to total curve points,
* we get double the total points in viewport. */
nurb->pntsu = 0;
/* Total points = pntsu * pntsv. */

View File

@@ -76,7 +76,7 @@ static Collection *find_or_create_collection(Main *bmain,
}
static void geometry_to_blender_geometry_set(const OBJImportParams &import_params,
const Span<std::unique_ptr<Geometry>> &all_geometries,
const Span<std::unique_ptr<Geometry>> all_geometries,
const GlobalVertices &global_vertices,
Vector<bke::GeometrySet> &geometries)
{
@@ -106,7 +106,7 @@ static void geometry_to_blender_objects(Main *bmain,
Scene *scene,
ViewLayer *view_layer,
const OBJImportParams &import_params,
Vector<std::unique_ptr<Geometry>> &all_geometries,
MutableSpan<std::unique_ptr<Geometry>> all_geometries,
const GlobalVertices &global_vertices,
Map<std::string, std::unique_ptr<MTLMaterial>> &materials,
Map<std::string, Material *> &created_materials)
@@ -169,7 +169,7 @@ void importer_geometry(const OBJImportParams &import_params,
Vector<bke::GeometrySet> &geometries,
size_t read_buffer_size)
{
/* List of Geometry instances to be parsed from OBJ file. */
/* List of geometries to be parsed from OBJ file. */
Vector<std::unique_ptr<Geometry>> all_geometries;
/* Container for vertex and UV vertex coordinates. */
GlobalVertices global_vertices;
@@ -194,7 +194,7 @@ void importer_main(Main *bmain,
const OBJImportParams &import_params,
size_t read_buffer_size)
{
/* List of Geometry instances to be parsed from OBJ file. */
/* List of geometries to be parsed from OBJ file. */
Vector<std::unique_ptr<Geometry>> all_geometries;
/* Container for vertex and UV vertex coordinates. */
GlobalVertices global_vertices;

View File

@@ -355,7 +355,7 @@ DefNode(GeometryNode, GEO_NODE_IMAGE_INFO, 0, "IMAGE_INFO", ImageInfo, "Image In
DefNode(GeometryNode, GEO_NODE_IMAGE_TEXTURE, def_geo_image_texture, "IMAGE_TEXTURE", ImageTexture, "Image Texture", "Sample values from an image texture")
DefNode(GeometryNode, GEO_NODE_IMAGE, def_geo_image, "IMAGE", InputImage, "Image", "Input image")
DefNode(GeometryNode, GEO_NODE_IMPORT_STL, 0, "IMPORT_STL", ImportSTL, "Import STL", "Import a mesh from an STL file")
DefNode(GeometryNode, GEO_NODE_IMPORT_OBJ, 0, "IMPORT_OBJ", ImportOBJ, "Import OBJ", "Import a mesh from an OBJ file")
DefNode(GeometryNode, GEO_NODE_IMPORT_OBJ, 0, "IMPORT_OBJ", ImportOBJ, "Import OBJ", "Import geometry from an OBJ file")
DefNode(GeometryNode, GEO_NODE_INDEX_OF_NEAREST, 0, "INDEX_OF_NEAREST", IndexOfNearest, "Index of Nearest", "Find the nearest element in a group. Similar to the \"Sample Nearest\" node")
DefNode(GeometryNode, GEO_NODE_INDEX_SWITCH, def_geo_index_switch, "INDEX_SWITCH", IndexSwitch, "Index Switch", "Choose between an arbitrary number of values with an index")
DefNode(GeometryNode, GEO_NODE_INPUT_ACTIVE_CAMERA, 0, "INPUT_ACTIVE_CAMERA", InputActiveCamera, "Active Camera", "Retrieve the scene's active camera")

View File

@@ -29,27 +29,24 @@ static void node_declare(NodeDeclarationBuilder &b)
static void node_geo_exec(GeoNodeExecParams params)
{
const std::string path = params.extract_input<std::string>("Path");
if (path.empty()) {
params.set_default_remaining_outputs();
return;
}
OBJImportParams import_params;
STRNCPY(import_params.filepath, path.c_str());
ReportList reports;
BKE_reports_init(&reports, RPT_STORE);
BLI_SCOPED_DEFER([&]() { BKE_reports_free(&reports); });
import_params.reports = &reports;
Vector<bke::GeometrySet> geometries;
OBJ_import_geometries(&import_params, geometries);
LISTBASE_FOREACH (Report *, report, &(import_params.reports)->list) {
NodeWarningType type;
switch (report->type) {
case RPT_ERROR:
type = NodeWarningType::Error;
@@ -58,21 +55,17 @@ static void node_geo_exec(GeoNodeExecParams params)
type = NodeWarningType::Info;
break;
}
params.error_message_add(type, TIP_(report->message));
}
BKE_reports_free(&reports);
if (geometries.size() == 0) {
if (geometries.is_empty()) {
params.set_default_remaining_outputs();
return;
}
bke::Instances *instances = new bke::Instances();
for (GeometrySet geometry : geometries) {
const int handle = instances->add_reference(bke::InstanceReference{geometry});
const int handle = instances->add_reference(bke::InstanceReference{std::move(geometry)});
instances->add_instance(handle, float4x4::identity());
}

View File

@@ -29,14 +29,12 @@ static void node_geo_exec(GeoNodeExecParams params)
{
#ifdef WITH_IO_STL
const std::string path = params.extract_input<std::string>("Path");
if (path.empty()) {
params.set_default_remaining_outputs();
return;
}
STLImportParams import_params;
STRNCPY(import_params.filepath, path.c_str());
import_params.forward_axis = IO_AXIS_NEGATIVE_Z;
@@ -48,13 +46,13 @@ static void node_geo_exec(GeoNodeExecParams params)
ReportList reports;
BKE_reports_init(&reports, RPT_STORE);
BLI_SCOPED_DEFER([&]() { BKE_reports_free(&reports); })
import_params.reports = &reports;
Mesh *mesh = STL_import_mesh(&import_params);
LISTBASE_FOREACH (Report *, report, &(import_params.reports)->list) {
NodeWarningType type;
switch (report->type) {
case RPT_ERROR:
type = NodeWarningType::Error;
@@ -63,18 +61,11 @@ static void node_geo_exec(GeoNodeExecParams params)
type = NodeWarningType::Info;
break;
}
params.error_message_add(type, TIP_(report->message));
}
BKE_reports_free(&reports);
params.set_output("Mesh", GeometrySet::from_mesh(mesh));
if (mesh != nullptr) {
params.set_output("Mesh", GeometrySet::from_mesh(mesh));
}
else {
params.set_default_remaining_outputs();
}
#else
params.error_message_add(NodeWarningType::Error,
TIP_("Disabled, Blender was compiled without STL I/O"));