10#include <system_error>
65 : export_params_(export_params), outfile_path_(filepath), outfile_(
nullptr)
69 throw std::system_error(errno, std::system_category(),
"Cannot open file " + outfile_path_);
74 if (outfile_ && std::fclose(outfile_)) {
76 "Error: could not close file '%s' properly, it may be corrupted.",
77 outfile_path_.c_str());
81void OBJWriter::write_vert_uv_normal_indices(
FormatHandler &fh,
89 vert_indices.
size() == normal_indices.
size());
93 const int n = vert_indices.
size();
96 for (
int j = 0; j < n; ++j) {
98 uv_indices[j] + uv_offset,
99 normal_indices[j] + normal_offset);
106 for (
int k = 0; k < n; ++k) {
107 int j = k == 0 ? 0 : n - k;
109 uv_indices[j] + uv_offset,
110 normal_indices[j] + normal_offset);
118 Span<int> vert_indices,
120 Span<int> normal_indices,
124 const int vertex_offset = offsets.vertex_offset + 1;
125 const int normal_offset = offsets.normal_offset + 1;
126 const int n = vert_indices.
size();
127 fh.write_obj_face_begin();
129 for (
int j = 0; j < n; ++j) {
130 fh.write_obj_face_v_normal(vert_indices[j] + vertex_offset,
131 normal_indices[j] + normal_offset);
135 for (
int k = 0; k < n; ++k) {
136 int j = k == 0 ? 0 : n - k;
137 fh.write_obj_face_v_normal(vert_indices[j] + vertex_offset,
138 normal_indices[j] + normal_offset);
141 fh.write_obj_face_end();
146 Span<int> vert_indices,
147 Span<int> uv_indices,
152 const int vertex_offset = offsets.vertex_offset + 1;
153 const int uv_offset = offsets.uv_vertex_offset + 1;
154 const int n = vert_indices.
size();
155 fh.write_obj_face_begin();
157 for (
int j = 0; j < n; ++j) {
158 fh.write_obj_face_v_uv(vert_indices[j] + vertex_offset, uv_indices[j] + uv_offset);
162 for (
int k = 0; k < n; ++k) {
163 int j = k == 0 ? 0 : n - k;
164 fh.write_obj_face_v_uv(vert_indices[j] + vertex_offset, uv_indices[j] + uv_offset);
167 fh.write_obj_face_end();
172 Span<int> vert_indices,
177 const int vertex_offset = offsets.vertex_offset + 1;
178 const int n = vert_indices.
size();
179 fh.write_obj_face_begin();
181 for (
int j = 0; j < n; ++j) {
182 fh.write_obj_face_v(vert_indices[j] + vertex_offset);
186 for (
int k = 0; k < n; ++k) {
187 int j = k == 0 ? 0 : n - k;
188 fh.write_obj_face_v(vert_indices[j] + vertex_offset);
191 fh.write_obj_face_end();
196 using namespace std::string_literals;
210 sizeof(mtl_dir_name),
212 sizeof(mtl_file_name));
220 std::replace(r_name.begin(), r_name.end(),
' ',
'_');
227 if (export_params_.export_object_groups) {
250template<
typename Function>
253 if (tot_count <= 0) {
260 if (chunk_count == 1) {
261 for (
int i = 0; i < tot_count; i++) {
269 for (
const int r : range) {
271 int i_end = std::min(i_start +
chunk_size, tot_count);
272 auto &buf = buffers[r];
273 for (
int i = i_start; i < i_end; i++) {
279 for (
auto &buf : buffers) {
286 bool write_colors)
const
296 if (write_colors && !name.
is_empty()) {
322 const float2 &uv_vertex = uv_coords[i];
323 buf.write_obj_uv(uv_vertex[0], uv_vertex[1]);
332 const float3 &normal = normal_coords[i];
333 buf.write_obj_normal(normal[0], normal[1], normal[2]);
337OBJWriter::func_vert_uv_normal_indices OBJWriter::get_face_element_writer(
338 const int total_uv_vertices)
const
341 if (export_params_.
export_uv && (total_uv_vertices > 0)) {
343 return &OBJWriter::write_vert_uv_normal_indices;
346 return &OBJWriter::write_vert_normal_indices;
349 if (export_params_.
export_uv && (total_uv_vertices > 0)) {
350 return &OBJWriter::write_vert_uv_indices;
353 return &OBJWriter::write_vert_indices;
362 if (
mesh.is_ith_face_smooth(face_idx)) {
373 const func_vert_uv_normal_indices face_element_writer = get_face_element_writer(
376 const int tot_faces = obj_mesh_data.
tot_faces();
396 const int prev_group =
get_smooth_group(obj_mesh_data, export_params_, prev_i);
398 if (group != prev_group) {
404 if (export_params_.export_vertex_groups) {
406 local_weights.
resize(tot_deform_groups);
409 prev_i, local_weights);
411 if (group != prev_group) {
418 if ((export_params_.export_materials || export_params_.export_material_groups) &&
422 const int16_t mat = std::max(0, material_indices[i]);
423 if (mat != prev_mat) {
425 if (export_params_.export_materials) {
430 const char *mat_name = matname_fn(mat);
434 if (export_params_.export_material_groups) {
439 if (export_params_.export_materials) {
447 (this->*face_element_writer)(buf,
458 const OBJMesh &obj_mesh_data)
const
462 mesh.loose_edges().foreach_index([&](
const int i) {
472 float axes_transform[3][3];
477 mul_m4_m3m4(world_axes_transform.
ptr(), axes_transform, object_to_world.ptr());
479 mul_v3_m3v3(world_axes_transform[3], axes_transform, object_to_world.location());
480 world_axes_transform[3][3] = object_to_world[3][3];
488 return world_axes_transform;
494 for (
int spline_idx = 0; spline_idx < total_splines; spline_idx++) {
518 dynamic_point_buffer);
523 vertex_coords.
size()) :
550 for (
const float &u : knots_u) {
575 "array size mismatch");
583 return fmt::format(
"{} {} {}", numbers[0], numbers[1], numbers[2]);
592 STRNCPY(mtl_path, obj_filepath);
596 throw std::system_error(ENAMETOOLONG, std::system_category(),
"");
599 mtl_filepath_ = mtl_path;
600 outfile_ =
BLI_fopen(mtl_filepath_.c_str(),
"wb");
602 throw std::system_error(errno, std::system_category(),
"Cannot open file " + mtl_filepath_);
608 fmt_handler_.write_to_file(outfile_);
609 if (std::fclose(outfile_)) {
611 "Error: could not close file '%s' properly, it may be corrupted.",
612 mtl_filepath_.c_str());
619 using namespace std::string_literals;
620 const char *blen_basename = (blen_filepath && blen_filepath[0] !=
'\0') ?
624 blen_basename +
"'");
625 fmt_handler_.write_string(
"# www.blender.org");
630 return mtl_filepath_;
633void MTLWriter::write_bsdf_properties(
const MTLMaterial &mtl,
bool write_pbr)
654 fmt_handler_.write_mtl_float3(
657 fmt_handler_.write_mtl_float(
"Ni", mtl.
ior);
659 fmt_handler_.write_mtl_float(
"d", mtl.
alpha);
665 fmt_handler_.write_mtl_float(
"Pr", mtl.
roughness);
668 fmt_handler_.write_mtl_float(
"Pm", mtl.
metallic);
671 fmt_handler_.write_mtl_float(
"Ps", mtl.
sheen);
679 if (mtl.
aniso >= 0.0f) {
680 fmt_handler_.write_mtl_float(
"aniso", mtl.
aniso);
683 fmt_handler_.write_mtl_float(
"anisor", mtl.
aniso_rot);
687 fmt_handler_.write_mtl_float3(
693void MTLWriter::write_texture_map(
const MTLMaterial &mtl_material,
696 const char *blen_filedir,
697 const char *dest_dir,
699 Set<std::pair<std::string, std::string>> ©_set)
703 if (texture_map.translation !=
float3{0.0f, 0.0f, 0.0f}) {
706 if (texture_map.scale !=
float3{1.0f, 1.0f, 1.0f}) {
710 options.append(
" -bm ").append(std::to_string(mtl_material.normal_strength));
714 texture_map.image_path.c_str(), blen_filedir, dest_dir, path_mode, ©_set);
716 std::replace(path.begin(), path.end(),
'\\',
'/');
734 const char *dest_dir,
737 if (mtlmaterials_.is_empty()) {
746 std::ranges::sort(mtlmaterials_,
750 fmt_handler_.write_string(
"");
751 fmt_handler_.write_mtl_newmtl(mtlmat.name);
752 write_bsdf_properties(mtlmat, write_pbr);
754 const MTLTexMap &tex = mtlmat.texture_maps[key];
765 mtlmat, (
MTLTexMapType)key, tex, blen_filedir, dest_dir, path_mode, copy_set);
781 int mtlmat_index = material_map_.lookup_default(material, -1);
782 if (mtlmat_index != -1) {
783 mtl_indices[i] = mtlmat_index;
787 mtl_indices[i] = mtlmaterials_.
size() - 1;
788 material_map_.add_new(material, mtl_indices[i]);
796 if (index < 0 || index >= mtlmaterials_.size()) {
799 return mtlmaterials_[
index].name.c_str();
File and directory operations.
#define CLOG_ERROR(clg_ref,...)
constexpr int64_t size() const
constexpr IndexRange drop_back(int64_t n) const
constexpr int64_t size() const
constexpr IndexRange()=default
constexpr IndexRange index_range() const
constexpr int64_t size() const
constexpr const T & last(const int64_t n=0) const
void resize(const int64_t new_size)
GAttributeReader lookup_or_default(StringRef name, AttrDomain domain, AttrType data_type, const void *default_value=nullptr) const
constexpr bool is_empty() const
constexpr const char * data() const
virtual int total_splines() const =0
virtual int num_control_points_v(int spline_index) const =0
virtual const char * get_curve_name() const =0
virtual Span< float3 > vertex_coordinates(int spline_index, Vector< float3 > &dynamic_point_buffer) const =0
virtual Span< float > get_knots_u(int spline_index, Vector< float > &buffer) const =0
virtual int num_control_points_u(int spline_index) const =0
virtual int get_nurbs_degree_u(int spline_index) const =0
virtual const float4x4 & object_transform() const =0
MTLWriter(const char *obj_filepath, bool write_file) noexcept(false)
Vector< int > add_materials(const OBJMesh &mesh_to_export)
void write_header(const char *blen_filepath)
void write_materials(const char *blen_filepath, ePathReferenceMode path_mode, const char *dest_dir, bool write_pbr)
StringRefNull mtl_file_path() const
const char * mtlmaterial_name(int index)
const char * get_face_deform_group_name(int16_t def_group_index) const
int16_t get_face_deform_group_index(int face_index, MutableSpan< float > group_weights) const
int tot_deform_groups() const
int16_t tot_materials() const
StringRef get_object_mesh_name() const
Span< int > get_face_uv_indices(const int face_index) const
bool is_mirrored_transform() const
int remap_face_index(int i) const
int tot_uv_vertices() const
Array< const Material * > materials
Span< float3 > get_normal_coords() const
const float4x4 & get_world_axes_transform() const
StringRef get_object_name() const
Span< int > get_face_normal_indices(const int face_index) const
Span< int > calc_face_vert_indices(const int face_index) const
Span< float2 > get_uv_coords() const
const Mesh * get_mesh() const
void write_normals(FormatHandler &fh, OBJMesh &obj_mesh_data)
OBJWriter(const char *filepath, const OBJExportParams &export_params) noexcept(false)
void write_mtllib_name(StringRefNull mtl_filepath) const
void write_uv_coords(FormatHandler &fh, OBJMesh &obj_mesh_data) const
void write_header() const
void write_nurbs_curve(FormatHandler &fh, const IOBJCurve &obj_nurbs_data) const
void write_face_elements(FormatHandler &fh, const IndexOffsets &offsets, const OBJMesh &obj_mesh_data, FunctionRef< const char *(int)> matname_fn)
void write_vertex_coords(FormatHandler &fh, const OBJMesh &obj_mesh_data, bool write_colors) const
void write_edges_indices(FormatHandler &fh, const IndexOffsets &offsets, const OBJMesh &obj_mesh_data) const
void write_object_name(FormatHandler &fh, const OBJMesh &obj_mesh_data) const
CCL_NAMESPACE_BEGIN struct Options options
static void spaces_to_underscores(std::string &r_name)
static bool is_pbr_map(MTLTexMapType type)
static const char * tex_map_type_to_string[]
const int SMOOTH_GROUP_DEFAULT
static const char * MATERIAL_GROUP_DISABLED
Span< float > valid_nurb_control_point_range(const int8_t order, const Span< float > knots, IndexRange &point_range)
MTLMaterial mtlmaterial_for_material(const Material *material)
const int SMOOTH_GROUP_DISABLED
static int get_smooth_group(const OBJMesh &mesh, const OBJExportParams ¶ms, int face_idx)
void obj_parallel_chunked_output(FormatHandler &fh, int tot_count, const Function &function)
static float4x4 compute_world_axes_transform(const OBJExportParams &export_params, const float4x4 &object_to_world)
BLI_STATIC_ASSERT(ARRAY_SIZE(tex_map_type_to_string)==int(MTLTexMapType::Count), "array size mismatch")
static bool is_non_pbr_map(MTLTexMapType type)
static std::string float3_to_string(const float3 &numbers)
static const int chunk_size
static int calc_chunk_count(int count)
static const char * DEFORM_GROUP_DISABLED
std::string path_reference(StringRefNull filepath, StringRefNull base_src, StringRefNull base_dst, ePathReferenceMode mode, Set< std::pair< std::string, std::string > > *copy_set)
void path_reference_copy(const Set< std::pair< std::string, std::string > > ©_set)
VecBase< T, 3 > transform_point(const CartesianBasis &basis, const VecBase< T, 3 > &v)
void parallel_for(const IndexRange range, const int64_t grain_size, const Function &function, const TaskSizeHints &size_hints=detail::TaskSizeHints_Static(1))
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
char * STRNCPY(char(&dst)[N], const char *src)
void linearrgb_to_srgb_v3_v3(float srgb[3], const float linear[3])
FILE * BLI_fopen(const char *filepath, const char *mode) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
MatBase< float, 4, 4 > float4x4
void BLI_path_slash_native(char *path) ATTR_NONNULL(1)
MINLINE void mul_v3_fl(float r[3], float f)
void mul_m4_v3(const float M[4][4], float r[3])
void void void const char * BLI_path_basename(const char *path) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
void void BLI_path_split_dir_part(const char *filepath, char *dir, size_t dir_maxncpy) ATTR_NONNULL(1
VecBase< int32_t, 2 > int2
constexpr size_t ARRAY_SIZE(const T(&arg)[N]) noexcept
bool mat3_from_axis_conversion(int src_forward, int src_up, int dst_forward, int dst_up, float r_mat[3][3])
int BLI_path_normalize(char *path) ATTR_NONNULL(1)
bool BLI_path_extension_replace(char *path, size_t path_maxncpy, const char *ext) ATTR_NONNULL(1
void unit_m3(float m[3][3])
void BLI_path_split_dir_file(const char *filepath, char *dir, size_t dir_maxncpy, char *file, size_t file_maxncpy) ATTR_NONNULL(1
ColorSceneLinear4f< eAlpha::Premultiplied > ColorGeometry4f
VecBase< float, 3 > float3
void mul_v3_m3v3(float r[3], const float M[3][3], const float a[3])
const char * BKE_blender_version_string()
void mul_m4_m3m4(float R[4][4], const float A[3][3], const float B[4][4])
const c_style_mat & ptr() const
bool export_smooth_groups
const MTLTexMap & tex_map_of_type(MTLTexMapType key) const