WIP: IO: C++ STL exporter #105598

Closed
Eyad Ahmed wants to merge 8 commits from (deleted):io-cpp-stl-exporter into main

When changing the target branch, be careful to rebase the branch in your fork to match. See documentation.
Contributor

A C++ .stl mesh file exporter for faster exports (especially with large meshes or large number of objects)

A C++ .stl mesh file exporter for faster exports (especially with large meshes or large number of objects)
Hans Goudey reviewed 2023-03-09 13:47:31 +01:00
@ -0,0 +72,4 @@
bm_convert_params.calc_face_normal = true;
bm_convert_params.calc_vert_normal = false;
BMesh *bmesh = BKE_mesh_to_bmesh_ex(mesh, &bm_create_params, &bm_convert_params);
Member

What do you think about using the Mesh triangulation instead of converting to BMesh and triangulating there? That can be accessed with Mesh.looptris()

What do you think about using the `Mesh` triangulation instead of converting to BMesh and triangulating there? That can be accessed with `Mesh.looptris()`
Author
Contributor

Oh awesome, I will use it then

Oh awesome, I will use it then
EyadAhmed marked this conversation as resolved
Eyad Ahmed force-pushed io-cpp-stl-exporter from 1a5f97220d to d0543ca26e 2023-03-09 17:53:01 +01:00 Compare
Eyad Ahmed force-pushed io-cpp-stl-exporter from d0543ca26e to 23c164317a 2023-03-09 18:40:17 +01:00 Compare
Aras Pranckevicius requested review from Aras Pranckevicius 2023-03-10 10:50:11 +01:00
Hans Goudey requested review from Hans Goudey 2023-03-10 21:03:40 +01:00
Eyad Ahmed force-pushed io-cpp-stl-exporter from 38a896dba0 to 7bc454f7c0 2023-03-11 15:31:03 +01:00 Compare
Eyad Ahmed force-pushed io-cpp-stl-exporter from 7bc454f7c0 to 4d03220e6b 2023-03-11 15:34:20 +01:00 Compare
Eyad Ahmed changed title from WIP: IO: C++ STL exporter to IO: C++ STL exporter 2023-03-12 00:29:52 +01:00
Aras Pranckevicius requested changes 2023-03-12 10:46:26 +01:00
Aras Pranckevicius left a comment
Member

Nice! Added some comments wrt non-ASCII file paths on windows, and naming of some paramters for the Python API.

Nice! Added some comments wrt non-ASCII file paths on windows, and naming of some paramters for the Python API.
@ -25,0 +47,4 @@
export_params.forward_axis = RNA_enum_get(op->ptr, "forward_axis");
export_params.up_axis = RNA_enum_get(op->ptr, "up_axis");
export_params.global_scale = RNA_float_get(op->ptr, "global_scale");
export_params.use_apply_modifiers = RNA_boolean_get(op->ptr, "use_apply_modifiers");

Maybe instead of use_apply_modifiers this could be apply_modifiers? That way it would be consistent with OBJ, PLY, Collada APIs.

Maybe instead of `use_apply_modifiers` this could be `apply_modifiers`? That way it would be consistent with OBJ, PLY, Collada APIs.
EyadAhmed marked this conversation as resolved
@ -25,0 +48,4 @@
export_params.up_axis = RNA_enum_get(op->ptr, "up_axis");
export_params.global_scale = RNA_float_get(op->ptr, "global_scale");
export_params.use_apply_modifiers = RNA_boolean_get(op->ptr, "use_apply_modifiers");
export_params.use_selection_only = RNA_boolean_get(op->ptr, "use_selection_only");

Similar, maybe instead of use_selection_only it should follow some existing export APIs. The trouble is... there's no consistency for that name. Alembic and Collada use selected, USD uses selected_objects_only, OBJ and PLY use export_selected_objects. But at least picking one of those is better than introducing yet another new name :)

Similar, maybe instead of `use_selection_only` it should follow some existing export APIs. The trouble is... there's no consistency for that name. Alembic and Collada use `selected`, USD uses `selected_objects_only`, OBJ and PLY use `export_selected_objects`. But at least picking one of those is better than introducing yet another new name :)
EyadAhmed marked this conversation as resolved
@ -25,0 +49,4 @@
export_params.global_scale = RNA_float_get(op->ptr, "global_scale");
export_params.use_apply_modifiers = RNA_boolean_get(op->ptr, "use_apply_modifiers");
export_params.use_selection_only = RNA_boolean_get(op->ptr, "use_selection_only");
export_params.use_ascii = RNA_boolean_get(op->ptr, "use_ascii");

PLY export uses ascii_format name for the same argument, so maybe follow that.

PLY export uses `ascii_format` name for the same argument, so maybe follow that.
EyadAhmed marked this conversation as resolved
@ -28,0 +35,4 @@
bool use_apply_modifiers;
bool use_ascii;
bool use_batch;
float global_scale;

Suuuper minor: I'd put the float member before the bool ones. Right now it wastes 3 bytes of padding between the last bool and the float, in order to align the float to 4-byte boundary. This does not really matter all that much for this struct, but as a general guidance it's a good one to learn.

Suuuper minor: I'd put the float member before the bool ones. Right now it wastes 3 bytes of padding between the last bool and the float, in order to align the float to 4-byte boundary. This does not *really* matter all that much for this struct, but as a general guidance it's a good one to learn.
EyadAhmed marked this conversation as resolved
@ -0,0 +21,4 @@
class ASCIIFileWriter : public FileWriter, NonCopyable {
private:
std::ofstream file_;

std::ofstream will fail writing a file on Windows if path contains non-ASCII characters. Use blender::fstream from BLI_fileops.hh instead.

`std::ofstream` will fail writing a file on Windows if path contains non-ASCII characters. Use `blender::fstream` from `BLI_fileops.hh` instead.
@ -0,0 +42,4 @@
BinaryFileWriter::BinaryFileWriter(const char *filepath)
{
file_ = fopen(filepath, "wb");

Similar to ASCII writer: fopen will fail correctly writing to a non-ASCII path on Windows. Use BLI_fopen from BLI_fileops.h instead.

Similar to ASCII writer: `fopen` will fail correctly writing to a non-ASCII path on Windows. Use `BLI_fopen` from `BLI_fileops.h` instead.

Would also be good if this exporter handles "collection instances" properly. There's an open patch for the python one: blender/blender-addons#104460

Would also be good if this exporter handles "collection instances" properly. There's an open patch for the python one: https://projects.blender.org/blender/blender-addons/pulls/104460
Author
Contributor

I found a much bigger problem, exporting two selected cubes only exports one cube, does not matter if selection only is enabled or not, trying to fix it,

Update 1: I think the problem is unapplied object transform is not taken into account somehow, so both cube end up in the same place, not sure if PLY exporter suffers same, CC: @aras_p

Update 2: I think PLY does not suffer, because it includes the object to world matrix for each object, I just need to do same
image

I found a much bigger problem, exporting two selected cubes only exports one cube, does not matter if selection only is enabled or not, trying to fix it, Update 1: I think the problem is unapplied object transform is not taken into account somehow, so both cube end up in the same place, not sure if PLY exporter suffers same, CC: @aras_p Update 2: I think PLY does not suffer, because it includes the object to world matrix for each object, I just need to do same ![image](/attachments/9656bad0-f5ab-478f-8f11-5869256ef41d)
135 KiB
Eyad Ahmed changed title from IO: C++ STL exporter to WIP: IO: C++ STL exporter 2023-03-13 15:28:31 +01:00
Hans Goudey requested changes 2023-03-13 16:53:59 +01:00
Hans Goudey left a comment
Member

It's nice that there really isn't much code here, a pretty simple change!
Would it be worth adding multithreading in this first commit? That could be done by just gathering a list of objects and making the file large enough in the first depsgraph loop, then writing all the data in parallel after. It's okay to do that later too.

Does the PR have a "WIP:" prefix for a specific reason at this point? If it's considered ready for review, probably best to remove it.

It's nice that there really isn't much code here, a pretty simple change! Would it be worth adding multithreading in this first commit? That could be done by just gathering a list of objects and making the file large enough in the first depsgraph loop, then writing all the data in parallel after. It's okay to do that later too. Does the PR have a "WIP:" prefix for a specific reason at this point? If it's considered ready for review, probably best to remove it.
@ -14,14 +14,162 @@
# include "DNA_space_types.h"
Member

Best to add this new file as a C++ file, that's the direction Blender is moving in generally.

Best to add this new file as a C++ file, that's the direction Blender is moving in generally.
@ -0,0 +85,4 @@
}
float scale_vec[3] = {global_scale, global_scale, global_scale};
float obmat3x3[3][3];
unit_m3(obmat3x3);
Member

It would be best to try looking into using the new matrix types recently added in https://archive.blender.org/developer/D16625. Should be a nice cleanup here too!

It would be best to try looking into using the new matrix types recently added in https://archive.blender.org/developer/D16625. Should be a nice cleanup here too!
@ -0,0 +95,4 @@
rescale_m4(obmat4x4, scale_vec);
/* Write triangles. */
auto loops = mesh->loops();
Member

In similar Blender code, auto isn't generally used in this context. The type information gives helpful context to the reader. Same for auto in the loop below.

In similar Blender code, `auto` isn't generally used in this context. The type information gives helpful context to the reader. Same for `auto` in the loop below.
EyadAhmed marked this conversation as resolved
@ -0,0 +99,4 @@
for (const auto &loop_tri : mesh->looptris()) {
Triangle t{};
for (int i = 0; i < 3; i++) {
auto co = mesh->vert_positions()[loops[loop_tri.tri[i]].v];
Member

I expect that these vert_positions() calls would show up in a profile. Each one does a string lookup in all vertex custom data layers. It would be better to retrieve the span once below.

I expect that these `vert_positions()` calls would show up in a profile. Each one does a string lookup in all vertex custom data layers. It would be better to retrieve the span once below.
EyadAhmed marked this conversation as resolved
@ -0,0 +20,4 @@
#pragma pack(push, 1)
struct STLBinaryTriangle {
float normal[3]{};
Member

Use the float3 type here instead

Use the `float3` type here instead
@ -0,0 +22,4 @@
struct STLBinaryTriangle {
float normal[3]{};
float vertices[3][3]{};
uint16_t attribute_byte_count{};
Member

The defaults with {} shouldn't be necessary here

The defaults with `{}` shouldn't be necessary here
@ -0,0 +18,4 @@
if (type == FileWriter::Type::ASCII) {
return std::make_unique<ASCIIFileWriter>(filepath);
}
else if (type == FileWriter::Type::BINARY) {
Member

else after return is redundant and can be removed here

else after return is redundant and can be removed here
Author
Contributor

It's nice that there really isn't much code here, a pretty simple change!
Would it be worth adding multithreading in this first commit? That could be done by just gathering a list of objects and making the file large enough in the first depsgraph loop, then writing all the data in parallel after. It's okay to do that later too.

Does the PR have a "WIP:" prefix for a specific reason at this point? If it's considered ready for review, probably best to remove it.

Yep added WIP back because of this: #105598 (comment)

> It's nice that there really isn't much code here, a pretty simple change! > Would it be worth adding multithreading in this first commit? That could be done by just gathering a list of objects and making the file large enough in the first depsgraph loop, then writing all the data in parallel after. It's okay to do that later too. > > Does the PR have a "WIP:" prefix for a specific reason at this point? If it's considered ready for review, probably best to remove it. Yep added WIP back because of this: https://projects.blender.org/blender/blender/pulls/105598#issuecomment-900081
Eyad Ahmed force-pushed io-cpp-stl-exporter from f2abaac62b to 9d00506ae7 2023-04-01 21:52:07 +02:00 Compare
Hans Goudey requested changes 2023-04-03 23:52:25 +02:00
@ -0,0 +96,4 @@
rescale_m4(obmat4x4, scale_vec);
/* Write triangles. */
const Span<float3> vertices = mesh->vert_positions();
Member

The typical name for this vert_positions span is positions rather than vertices. Vertices might refer to any/all vertex-domain attributes.

The typical name for this `vert_positions` span is `positions` rather than `vertices`. Vertices might refer to any/all vertex-domain attributes.
EyadAhmed marked this conversation as resolved
Eyad Ahmed force-pushed io-cpp-stl-exporter from 5cedc71ba2 to f973604fe6 2023-04-12 03:11:13 +02:00 Compare
Member

To be clear, I probably won't do more review here as long as the patch is marked as WIP. If you have questions though, feel free to ask here or in chat.

To be clear, I probably won't do more review here as long as the patch is marked as WIP. If you have questions though, feel free to ask here or in chat.
Hans Goudey added this to the Pipeline, Assets & IO project 2023-04-13 14:52:06 +02:00

Implemented as part of #114862

Implemented as part of https://projects.blender.org/blender/blender/pulls/114862

Pull request closed

Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No Assignees
3 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#105598
No description provided.