Point Cloud: Move "Random" default primitive creation to operator

Previously all new point clouds created in the main database would
have the random 400 points. Now that's only the point cloud created
from the add menu.

Similar to f98d74c80d.

Pull Request: https://projects.blender.org/blender/blender/pulls/138685
This commit is contained in:
Hans Goudey
2025-05-10 02:24:18 +02:00
committed by Hans Goudey
parent cd70b9d730
commit 8ac48880e7
8 changed files with 27 additions and 59 deletions

View File

@@ -2670,7 +2670,7 @@ class VIEW3D_MT_add(Menu):
layout.menu("VIEW3D_MT_surface_add", icon='OUTLINER_OB_SURFACE')
layout.menu("VIEW3D_MT_metaball_add", text="Metaball", icon='OUTLINER_OB_META')
layout.operator("object.text_add", text="Text", icon='OUTLINER_OB_FONT')
layout.operator("object.pointcloud_add", text="Point Cloud", icon='OUTLINER_OB_POINTCLOUD')
layout.operator("object.pointcloud_random_add", text="Point Cloud", icon='OUTLINER_OB_POINTCLOUD')
layout.menu("VIEW3D_MT_volume_add", text="Volume", text_ctxt=i18n_contexts.id_id, icon='OUTLINER_OB_VOLUME')
layout.menu("VIEW3D_MT_grease_pencil_add", text="Grease Pencil", icon='OUTLINER_OB_GREASEPENCIL')
@@ -6505,11 +6505,6 @@ class VIEW3D_PT_object_type_visibility(Panel):
col.separator()
continue
if attr == "curves" and not hasattr(bpy.data, "hair_curves"):
continue
elif attr == "pointcloud" and not hasattr(bpy.data, "pointclouds"):
continue
attr_v = "show_object_viewport_" + attr
icon_v = 'HIDE_OFF' if getattr(view, attr_v) else 'HIDE_ON'

View File

@@ -50,7 +50,6 @@ PointCloud *pointcloud_new_no_attributes(int totpoint);
} // namespace blender::bke
PointCloud *BKE_pointcloud_add(Main *bmain, const char *name);
PointCloud *BKE_pointcloud_add_default(Main *bmain, const char *name);
PointCloud *BKE_pointcloud_new_nomain(int totpoint);
void BKE_pointcloud_nomain_to_pointcloud(PointCloud *pointcloud_src, PointCloud *pointcloud_dst);

View File

@@ -2018,7 +2018,7 @@ void *BKE_object_obdata_add_from_type(Main *bmain, int type, const char *name)
case OB_CURVES:
return BKE_curves_add(bmain, name);
case OB_POINTCLOUD:
return BKE_pointcloud_add_default(bmain, name);
return BKE_pointcloud_add(bmain, name);
case OB_VOLUME:
return BKE_volume_add(bmain, name);
case OB_GREASE_PENCIL:

View File

@@ -17,7 +17,6 @@
#include "BLI_bounds.hh"
#include "BLI_index_range.hh"
#include "BLI_rand.h"
#include "BLI_resource_scope.hh"
#include "BLI_span.hh"
#include "BLI_utildefines.h"
@@ -53,12 +52,7 @@ using blender::StringRef;
using blender::VArray;
using blender::Vector;
/* PointCloud datablock */
static void pointcloud_random(PointCloud *pointcloud);
constexpr StringRef ATTR_POSITION = "position";
constexpr StringRef ATTR_RADIUS = "radius";
static void pointcloud_init_data(ID *id)
{
@@ -202,33 +196,6 @@ IDTypeInfo IDType_ID_PT = {
/*lib_override_apply_post*/ nullptr,
};
static void pointcloud_random(PointCloud *pointcloud)
{
using namespace blender;
using namespace blender::bke;
BLI_assert(pointcloud->totpoint == 0);
pointcloud->totpoint = 400;
CustomData_realloc(&pointcloud->pdata, 0, pointcloud->totpoint);
RNG *rng = BLI_rng_new(0);
MutableAttributeAccessor attributes = pointcloud->attributes_for_write();
MutableSpan<float3> positions = pointcloud->positions_for_write();
SpanAttributeWriter<float> radii = attributes.lookup_or_add_for_write_only_span<float>(
ATTR_RADIUS, AttrDomain::Point);
for (const int i : positions.index_range()) {
positions[i] = float3(BLI_rng_get_float(rng), BLI_rng_get_float(rng), BLI_rng_get_float(rng)) *
2.0f -
1.0f;
radii.span[i] = 0.05f * BLI_rng_get_float(rng);
}
radii.finish();
BLI_rng_free(rng);
}
template<typename T>
static VArray<T> get_varray_attribute(const PointCloud &pointcloud,
const StringRef name,
@@ -304,15 +271,6 @@ PointCloud *BKE_pointcloud_add(Main *bmain, const char *name)
return pointcloud;
}
PointCloud *BKE_pointcloud_add_default(Main *bmain, const char *name)
{
PointCloud *pointcloud = BKE_id_new<PointCloud>(bmain, name);
pointcloud_random(pointcloud);
return pointcloud;
}
PointCloud *BKE_pointcloud_new_nomain(const int totpoint)
{
PointCloud *pointcloud = static_cast<PointCloud *>(BKE_libblock_alloc(

View File

@@ -36,6 +36,7 @@
#include "BLI_math_matrix_types.hh"
#include "BLI_math_rotation.h"
#include "BLI_math_vector_types.hh"
#include "BLI_rand.hh"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_utildefines.h"
@@ -2133,23 +2134,38 @@ static wmOperatorStatus object_pointcloud_add_exec(bContext *C, wmOperator *op)
float loc[3], rot[3];
add_generic_get_opts(C, op, 'Z', loc, rot, nullptr, nullptr, &local_view_bits, nullptr);
add_type(C, OB_POINTCLOUD, nullptr, loc, rot, false, local_view_bits);
Object *object = add_type(C, OB_POINTCLOUD, nullptr, loc, rot, false, local_view_bits);
PointCloud &pointcloud = *static_cast<PointCloud *>(object->data);
pointcloud.totpoint = 400;
CustomData_realloc(&pointcloud.pdata, 0, pointcloud.totpoint);
bke::MutableAttributeAccessor attributes = pointcloud.attributes_for_write();
bke::SpanAttributeWriter<float3> position = attributes.lookup_or_add_for_write_only_span<float3>(
"position", bke::AttrDomain::Point);
bke::SpanAttributeWriter<float> radii = attributes.lookup_or_add_for_write_only_span<float>(
"radius", bke::AttrDomain::Point);
RandomNumberGenerator rng(0);
for (const int i : position.span.index_range()) {
position.span[i] = float3(rng.get_float(), rng.get_float(), rng.get_float()) * 2.0f - 1.0f;
radii.span[i] = 0.05f * rng.get_float();
}
position.finish();
radii.finish();
return OPERATOR_FINISHED;
}
void OBJECT_OT_pointcloud_add(wmOperatorType *ot)
void OBJECT_OT_pointcloud_random_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Point Cloud";
ot->description = "Add a point cloud object to the scene";
ot->idname = "OBJECT_OT_pointcloud_add";
ot->idname = "OBJECT_OT_pointcloud_random_add";
/* api callbacks */
ot->exec = object_pointcloud_add_exec;
ot->poll = ED_operator_objectmode;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
add_generic_props(ot, false);

View File

@@ -128,7 +128,7 @@ void OBJECT_OT_camera_add(wmOperatorType *ot);
void OBJECT_OT_speaker_add(wmOperatorType *ot);
void OBJECT_OT_curves_random_add(wmOperatorType *ot);
void OBJECT_OT_curves_empty_hair_add(wmOperatorType *ot);
void OBJECT_OT_pointcloud_add(wmOperatorType *ot);
void OBJECT_OT_pointcloud_random_add(wmOperatorType *ot);
/**
* Only used as menu.
*/

View File

@@ -97,7 +97,7 @@ void operatortypes_object()
WM_operatortype_append(OBJECT_OT_speaker_add);
WM_operatortype_append(OBJECT_OT_curves_random_add);
WM_operatortype_append(OBJECT_OT_curves_empty_hair_add);
WM_operatortype_append(OBJECT_OT_pointcloud_add);
WM_operatortype_append(OBJECT_OT_pointcloud_random_add);
WM_operatortype_append(OBJECT_OT_volume_add);
WM_operatortype_append(OBJECT_OT_volume_import);
WM_operatortype_append(OBJECT_OT_add);

View File

@@ -60,7 +60,7 @@ bool AbcPointsReader::accepts_object_type(
void AbcPointsReader::readObjectData(Main *bmain, const Alembic::Abc::ISampleSelector &sample_sel)
{
PointCloud *pointcloud = BKE_pointcloud_add_default(bmain, m_data_name.c_str());
PointCloud *pointcloud = BKE_pointcloud_add(bmain, m_data_name.c_str());
bke::GeometrySet geometry_set = bke::GeometrySet::from_pointcloud(
pointcloud, bke::GeometryOwnershipType::Editable);