39using Alembic::Abc::FloatArraySample;
40using Alembic::Abc::Int32ArraySample;
41using Alembic::Abc::OObject;
42using Alembic::Abc::V2fArraySample;
43using Alembic::Abc::V3fArraySample;
45using Alembic::AbcGeom::kFacevaryingScope;
46using Alembic::AbcGeom::OBoolProperty;
47using Alembic::AbcGeom::OCompoundProperty;
48using Alembic::AbcGeom::OFaceSet;
49using Alembic::AbcGeom::OFaceSetSchema;
50using Alembic::AbcGeom::OInt32Property;
51using Alembic::AbcGeom::ON3fGeomParam;
52using Alembic::AbcGeom::OPolyMesh;
53using Alembic::AbcGeom::OPolyMeshSchema;
54using Alembic::AbcGeom::OStringProperty;
55using Alembic::AbcGeom::OSubD;
56using Alembic::AbcGeom::OSubDSchema;
57using Alembic::AbcGeom::OV2fGeomParam;
58using Alembic::AbcGeom::UInt32ArraySample;
60namespace io::alembic {
65 std::vector<int32_t> &face_verts,
66 std::vector<int32_t> &loop_counts);
68 std::vector<int32_t> &indices,
69 std::vector<int32_t> &lengths,
70 std::vector<float> &sharpnesses);
72 std::vector<int32_t> &indices,
73 std::vector<float> &sharpnesses);
88 while (md !=
nullptr) {
103 if ((md->
mode & mod_mode) != mod_mode) {
128 abc_subdiv_schema_ = abc_subdiv_.getSchema();
130 abc_custom_data_container_ = abc_subdiv_schema_.getUserProperties();
131 abc_subdiv_render_levels_ = OInt32Property(
133 abc_subdiv_viewport_levels_ = OInt32Property(
139 abc_poly_mesh_schema_ = abc_poly_mesh_.getSchema();
141 abc_custom_data_container_ = abc_poly_mesh_.getSchema().getUserProperties();
142 OBoolProperty type(abc_custom_data_container_,
"meshtype");
143 type.set(subsurf_modifier_ ==
nullptr);
146 if (context->object->data->id_type() ==
ID_ME) {
147 Mesh *
mesh = id_cast<Mesh *>(context->object->data);
148 if (
mesh->active_color_attribute &&
mesh->default_color_attribute) {
151 OStringProperty active_color_attribute(abc_custom_data_container_,
152 ABC_ACTIVE_COLOR_ATTRIBUTE_PROPNAME);
155 OStringProperty default_color_attribute(abc_custom_data_container_,
156 ABC_DEFAULT_COLOR_ATTRIBUTE_PROPNAME);
167 return abc_poly_mesh_;
182 for (; md; md = md->
prev) {
200 Object *
object = context.object;
201 bool needsfree =
false;
205 if (
mesh ==
nullptr) {
213 const bool tag_only =
false;
231 mesh = triangulated_mesh;
236 m_custom_data_config.mesh =
mesh;
237 m_custom_data_config.face_offsets =
mesh->face_offsets_for_write().data();
238 m_custom_data_config.corner_verts =
mesh->corner_verts_for_write().data();
239 m_custom_data_config.faces_num =
mesh->faces_num;
240 m_custom_data_config.totloop =
mesh->corners_num;
241 m_custom_data_config.totvert =
mesh->verts_num;
246 write_subd(context,
mesh);
249 write_mesh(context,
mesh);
271 std::vector<Imath::V3f> points, normals;
272 std::vector<int32_t> face_verts, loop_counts;
273 std::vector<Imath::V3f> velocities;
279 write_face_sets(context.object,
mesh, abc_poly_mesh_schema_);
282 OPolyMeshSchema::Sample mesh_sample = OPolyMeshSchema::Sample(
283 V3fArraySample(points), Int32ArraySample(face_verts), Int32ArraySample(loop_counts));
290 if (!uvs_and_indices.
indices.empty() && !uvs_and_indices.
uvs.empty()) {
291 OV2fGeomParam::Sample uv_sample;
292 uv_sample.setVals(V2fArraySample(uvs_and_indices.
uvs));
293 uv_sample.setIndices(UInt32ArraySample(uvs_and_indices.
indices));
294 uv_sample.setScope(kFacevaryingScope);
296 abc_poly_mesh_schema_.setUVSourceName(name);
297 mesh_sample.setUVs(uv_sample);
300 write_uv_maps(abc_poly_mesh_schema_.getArbGeomParams(), m_custom_data_config, *
mesh);
306 ON3fGeomParam::Sample normals_sample;
307 if (!normals.empty()) {
308 normals_sample.setScope(kFacevaryingScope);
309 normals_sample.setVals(V3fArraySample(normals));
312 mesh_sample.setNormals(normals_sample);
320 mesh_sample.setVelocities(V3fArraySample(velocities));
326 abc_poly_mesh_schema_.set(mesh_sample);
328 write_arb_geo_params(mesh, *
context.object, abc_poly_mesh_schema_.getNumSamples());
331void ABCGenericMeshWriter::write_subd(HierarchyContext &context, Mesh *mesh)
333 std::vector<float> edge_crease_sharpness, vert_crease_sharpness;
334 std::vector<Imath::V3f> points;
335 std::vector<int32_t> face_verts, loop_counts;
336 std::vector<int32_t> edge_crease_indices, edge_crease_lengths, vert_crease_indices;
340 get_edge_creases(mesh, edge_crease_indices, edge_crease_lengths, edge_crease_sharpness);
344 write_face_sets(
context.object, mesh, abc_subdiv_schema_);
347 OSubDSchema::Sample subdiv_sample = OSubDSchema::Sample(
348 V3fArraySample(points), Int32ArraySample(face_verts), Int32ArraySample(loop_counts));
355 OV2fGeomParam::Sample uv_sample;
356 uv_sample.setVals(V2fArraySample(
sample.uvs));
357 uv_sample.setIndices(UInt32ArraySample(
sample.indices));
358 uv_sample.setScope(kFacevaryingScope);
360 abc_subdiv_schema_.setUVSourceName(name);
361 subdiv_sample.setUVs(uv_sample);
364 write_uv_maps(abc_subdiv_schema_.getArbGeomParams(), m_custom_data_config, *mesh);
371 if (!edge_crease_indices.empty()) {
372 subdiv_sample.setCreaseIndices(Int32ArraySample(edge_crease_indices));
373 subdiv_sample.setCreaseLengths(Int32ArraySample(edge_crease_lengths));
374 subdiv_sample.setCreaseSharpnesses(FloatArraySample(edge_crease_sharpness));
377 if (!vert_crease_indices.empty()) {
378 subdiv_sample.setCornerIndices(Int32ArraySample(vert_crease_indices));
379 subdiv_sample.setCornerSharpnesses(FloatArraySample(vert_crease_sharpness));
389 int propagate_corners = 0;
392 switch (subsurf_data->uv_smooth) {
402 propagate_corners = 1;
412 switch (subsurf_data->boundary_smooth) {
421 subdiv_sample.setFaceVaryingInterpolateBoundary(
int(fvar_interpolate_boundary));
422 subdiv_sample.setFaceVaryingPropagateCorners(propagate_corners);
423 subdiv_sample.setInterpolateBoundary(
int(interpolate_boundary));
425 abc_subdiv_viewport_levels_.set(subsurf_data->levels);
426 abc_subdiv_render_levels_.set(subsurf_data->renderLevels);
431 abc_subdiv_schema_.set(subdiv_sample);
433 write_arb_geo_params(mesh, *
context.object, abc_subdiv_schema_.getNumSamples());
438 OFaceSetSchema &face_set_schema =
face_set.getSchema();
439 OFaceSetSchema::Sample samp;
445 static int address_for_empty_sample = 0;
446 samp.setFaces(Int32ArraySample(&address_for_empty_sample, 0));
447 for (
size_t i = face_set_schema.getNumSamples(); i < num_geom_samples; i++) {
448 face_set_schema.set(samp);
452template<
typename Schema>
453void ABCGenericMeshWriter::write_face_sets(
Object *
object,
Mesh *
mesh, Schema &schema)
455 std::map<std::string, std::vector<int32_t>> geo_groups;
456 get_geo_groups(
object,
mesh, geo_groups);
458 std::map<std::string, std::vector<int32_t>>
::iterator it;
459 for (it = geo_groups.begin(); it != geo_groups.end(); ++it) {
461 if (schema.hasFaceSet(it->first)) {
462 face_set = schema.getFaceSet(it->first);
465 face_set = schema.createFaceSet(it->first);
466 face_set_names_.append(it->first);
471 OFaceSetSchema &face_set_schema = face_set.getSchema();
474 OFaceSetSchema::Sample samp;
475 samp.setFaces(Int32ArraySample(it->second));
476 face_set_schema.set(samp);
480 for (
const std::string &name : face_set_names_) {
481 OFaceSet face_set = schema.getFaceSet(name);
487void ABCGenericMeshWriter::write_arb_geo_params(Mesh *mesh,
488 const Object &
object,
489 const size_t num_geom_samples)
491 OCompoundProperty arb_geom_params;
493 arb_geom_params = abc_subdiv_.getSchema().getArbGeomParams();
496 arb_geom_params = abc_poly_mesh_.getSchema().getArbGeomParams();
499 const bke::AttributeAccessor attributes = mesh->
attributes();
501 if (attribute_maps_) {
504 attribute_maps_->write_empty_samples(num_geom_samples - 1);
507 attributes.foreach_attribute([&](
const bke::AttributeIter &iter) {
538 AttributeParamMaps ¶m_maps = get_attribute_param_maps();
547 num_geom_samples - 1);
550 if (attribute_maps_) {
554 attribute_maps_->write_empty_samples(num_geom_samples);
560 if (!attribute_maps_) {
561 attribute_maps_ = std::make_unique<AttributeParamMaps>();
563 return *attribute_maps_.get();
566void ABCGenericMeshWriter::get_geo_groups(Object *
object,
568 std::map<std::string, std::vector<int32_t>> &geo_groups)
570 const bke::AttributeAccessor attributes = mesh->
attributes();
571 const VArraySpan<int> material_indices = *attributes.lookup_or_default<
int>(
574 for (
const int i : material_indices.index_range()) {
575 short mnr = material_indices[i];
585 if (!geo_groups.contains(name)) {
586 std::vector<int32_t> faceArray;
587 geo_groups[name] = faceArray;
590 geo_groups[name].push_back(i);
593 if (geo_groups.empty()) {
598 std::vector<int32_t> faceArray;
600 for (
int i = 0,
e = mesh->faces().size(); i <
e; i++) {
601 faceArray.push_back(i);
604 geo_groups[name] = faceArray;
611 std::vector<int32_t> &face_verts,
612 std::vector<int32_t> &loop_counts)
619 face_verts.reserve(corner_verts.
size());
620 loop_counts.reserve(faces.
size());
625 loop_counts.push_back(face.
size());
627 int corner = face.
start() + (face.
size() - 1);
628 for (
int j = 0; j < face.
size(); j++, corner--) {
629 face_verts.push_back(corner_verts[corner]);
635 std::vector<int32_t> &indices,
636 std::vector<int32_t> &lengths,
637 std::vector<float> &sharpnesses)
652 const float crease = std::clamp(creases[i], 0.0f, 1.0f);
654 if (crease != 0.0f) {
655 indices.push_back(edges[i][0]);
656 indices.push_back(edges[i][1]);
661 lengths.resize(sharpnesses.size(), 2);
665 std::vector<int32_t> &indices,
666 std::vector<float> &sharpnesses)
679 const float crease = std::clamp(creases[i], 0.0f, 1.0f);
681 if (crease != 0.0f) {
682 indices.push_back(i);
692 switch (
mesh->normals_domain()) {
699 normals.resize(
mesh->corners_num);
700 MutableSpan dst_normals(
reinterpret_cast<float3 *
>(normals.data()), normals.size());
705 for (const int i : range) {
707 copy_yup_from_zup(y_up, face_normals[i]);
708 dst_normals.slice(faces[i]).fill(y_up);
714 normals.resize(
mesh->corners_num);
715 MutableSpan dst_normals(
reinterpret_cast<float3 *
>(normals.data()), normals.size());
721 for (const int i : range) {
722 const IndexRange face = faces[i];
723 for (const int i : face.index_range()) {
724 copy_yup_from_zup(dst_normals[face.last(i)], corner_normals[face[i]]);
Generic geometry attributes built on CustomData.
General operations, lookup, etc. for materials.
General operations, lookup, etc. for blender objects.
#define CLOG_DEBUG(clg_ref,...)
Object is a sort of wrapper for general info.
constexpr int64_t size() const
constexpr int64_t start() const
constexpr int64_t size() const
constexpr IndexRange index_range() const
GAttributeReader lookup(const StringRef name) const
constexpr IndexRange index_range() const
virtual std::string get_id_name(const ID *id) const
ABCAbstractWriter(const ABCWriterConstructorArgs &args)
uint32_t timesample_index() const
Alembic::Abc::OCompoundProperty abc_schema_prop_for_custom_props(T abc_schema)
uint32_t timesample_index_
Imath::Box3d bounding_box_
const ABCWriterConstructorArgs args_
virtual void update_bounding_box(Object *object)
void do_write(HierarchyContext &context) override
ABCGenericMeshWriter(const ABCWriterConstructorArgs &args)
Alembic::Abc::OObject get_alembic_object() const override
virtual void free_export_mesh(Mesh *mesh)
bool is_supported(const HierarchyContext *context) const override
virtual Mesh * get_export_mesh(Object *object_eval, bool &r_needsfree)=0
Alembic::Abc::OCompoundProperty abc_prop_for_custom_props() override
void create_alembic_objects(const HierarchyContext *context) override
virtual bool export_as_subdivision_surface(Object *ob_eval) const
Mesh * get_export_mesh(Object *object_eval, bool &r_needsfree) override
ABCMeshWriter(const ABCWriterConstructorArgs &args)
IndexRange index_range() const
BLI_INLINE float crease_to_sharpness(float crease)
bool attribute_name_is_anonymous(const StringRef name)
int context(const bContext *C, const char *member, bContextDataResult *result)
void write_uv_maps(const OCompoundProperty &prop, CDStreamConfig &config, const Mesh &mesh)
void create_geom_param_for_attribute(const Alembic::Abc::OCompoundProperty &prop, AttributeParamMaps ¶m_maps, const bke::AttributeIter &attr, const int timesample_index, const OffsetIndices< int > faces, const StringRefNull object_name, const size_t num_geom_samples)
static void get_vert_creases(Mesh *mesh, std::vector< int32_t > &indices, std::vector< float > &sharpnesses)
static void get_edge_creases(Mesh *mesh, std::vector< int32_t > &indices, std::vector< int32_t > &lengths, std::vector< float > &sharpnesses)
static const SubsurfModifierData * get_last_subdiv_modifier(eEvaluationMode eval_mode, Object *obj)
std::string get_valid_abc_name(const char *name)
AbcFaceVaryingInterpolateBoundary
static void get_loop_normals(const Mesh *mesh, std::vector< Imath::V3f > &normals)
static void get_topology(Mesh *mesh, std::vector< int32_t > &face_verts, std::vector< int32_t > &loop_counts)
static void write_empty_samples(OFaceSet &face_set, const size_t num_geom_samples)
bool get_velocities(const bke::AttributeAccessor &attributes, std::vector< Imath::V3f > &velocities)
void write_generated_coordinates(const OCompoundProperty &prop, CDStreamConfig &config)
void get_positions(const Span< float3 > positions, std::vector< Imath::V3f > &points)
const char * get_uv_sample(UVSample &sample, const CDStreamConfig &config, const Mesh &mesh)
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)
void BM_mesh_free(BMesh *bm)
BMesh Free Mesh.
@ SUBSURF_UV_SMOOTH_PRESERVE_CORNERS_AND_JUNCTIONS
@ SUBSURF_UV_SMOOTH_PRESERVE_BOUNDARIES
@ SUBSURF_UV_SMOOTH_PRESERVE_CORNERS
@ SUBSURF_UV_SMOOTH_PRESERVE_CORNERS_JUNCTIONS_AND_CONCAVE
@ eModifierType_ParticleSystem
void BKE_mesh_wrapper_ensure_mdata(Mesh *mesh)
const char * BKE_id_name(const ID &id)
BMesh * BKE_mesh_to_bmesh_ex(const Mesh *mesh, const BMeshCreateParams *create_params, const BMeshFromMeshParams *convert_params)
@ eModifierMode_DisableTemporary
void BKE_id_free(Main *bmain, void *idv)
Mesh * BKE_mesh_from_bmesh_for_eval_nomain(BMesh *bm, const CustomData_MeshMasks *cd_mask_extra, const Mesh *me_settings)
@ SUBSURF_BOUNDARY_SMOOTH_PRESERVE_CORNERS
@ SUBSURF_BOUNDARY_SMOOTH_ALL
Material * BKE_object_material_get_eval(Object *ob, short act)
void BM_mesh_triangulate(BMesh *bm, const int quad_method, const int ngon_method, const int min_vertices, const bool tag_only, BMOperator *op, BMOpSlot *slot_facemap_out, BMOpSlot *slot_facemap_double_out)
VecBase< float, 3 > float3
ATTR_WARN_UNUSED_RESULT const BMVert const BMEdge * e
Mesh * BKE_object_get_evaluated_mesh(const Object *object_eval)
enum eEvaluationMode evaluation_mode
struct ModifierData * prev
ListBaseT< ModifierData > modifiers
const ABCHierarchyIterator * hierarchy_iterator
Alembic::Abc::OObject abc_parent
const AlembicExportParams * export_params
std::vector< Imath::V2f > uvs
std::vector< uint32_t > indices