Blender V5.3
MOD_particlesystem.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2005 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include <cstddef>
10#include <cstring>
11
12#include "BLI_utildefines.hh"
13
14#include "BLT_translation.hh"
15
16#include "DNA_mesh_types.h"
17#include "DNA_screen_types.h"
18
19#include "BKE_editmesh.hh"
20#include "BKE_lib_id.hh"
21#include "BKE_mesh.hh"
23#include "BKE_modifier.hh"
24#include "BKE_particle.h"
25
27#include "UI_resources.hh"
28
29#include "RNA_prototypes.hh"
30#include "RNA_types.hh"
31
33
34#include "BLO_read_write.hh"
35
36#include "MOD_ui_common.hh"
37
38namespace blender {
39
40static void init_data(ModifierData *md)
41{
42 ParticleSystemModifierData *psmd = reinterpret_cast<ParticleSystemModifierData *>(md);
44}
45static void free_data(ModifierData *md)
46{
47 ParticleSystemModifierData *psmd = reinterpret_cast<ParticleSystemModifierData *>(md);
48
49 if (psmd->mesh_final) {
50 BKE_id_free(nullptr, psmd->mesh_final);
51 psmd->mesh_final = nullptr;
52 if (psmd->mesh_original) {
53 BKE_id_free(nullptr, psmd->mesh_original);
54 psmd->mesh_original = nullptr;
55 }
56 }
57 psmd->totdmvert = psmd->totdmedge = psmd->totdmface = 0;
58
59 /* ed::object::modifier_remove may have freed this first before calling
60 * BKE_modifier_free (which calls this function) */
61 if (psmd->psys) {
62 psmd->psys->flag |= PSYS_DELETE;
63 }
64}
65
66static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
67{
68#if 0
70#endif
71 ParticleSystemModifierData *tpsmd = reinterpret_cast<ParticleSystemModifierData *>(target);
72
73 BKE_modifier_copydata_generic(md, target, flag);
74
75 /* NOTE: `psys` pointer here is just copied over from `md` to `target`. This is dangerous, as it
76 * will generate invalid data in case we are copying between different objects. Extra external
77 * code has to be called then to ensure proper remapping of that pointer. See e.g.
78 * `BKE_object_copy_particlesystems` or `BKE_object_copy_modifier`. */
79
80 tpsmd->mesh_final = nullptr;
81 tpsmd->mesh_original = nullptr;
82 tpsmd->totdmvert = tpsmd->totdmedge = tpsmd->totdmface = 0;
83}
84
85static void required_data_mask(ModifierData *md, CustomData_MeshMasks *r_cddata_masks)
86{
87 ParticleSystemModifierData *psmd = reinterpret_cast<ParticleSystemModifierData *>(md);
88
89 psys_emitter_customdata_mask(psmd->psys, r_cddata_masks);
90}
91
92/* saves the current emitter state for a particle system and calculates particles */
93static void deform_verts(ModifierData *md,
94 const ModifierEvalContext *ctx,
95 Mesh *mesh,
96 MutableSpan<float3> positions)
97{
98 ParticleSystemModifierData *psmd = reinterpret_cast<ParticleSystemModifierData *>(md);
99 ParticleSystem *psys = nullptr;
100
101 if (ctx->object->particlesystem.first()) {
102 psys = psmd->psys;
103 }
104 else {
105 return;
106 }
107
108 if (!psys_check_enabled(ctx->object, psys, (ctx->flag & MOD_APPLY_RENDER) != 0)) {
109 return;
110 }
111
112 /* Clear old evaluated mesh. */
113 bool had_mesh_final = (psmd->mesh_final != nullptr);
114 if (psmd->mesh_final) {
115 BKE_id_free(nullptr, psmd->mesh_final);
116 psmd->mesh_final = nullptr;
117 if (psmd->mesh_original) {
118 BKE_id_free(nullptr, psmd->mesh_original);
119 psmd->mesh_original = nullptr;
120 }
121 }
122 else if (psmd->flag & eParticleSystemFlag_file_loaded) {
123 /* in file read mesh just wasn't saved in file so no need to reset everything */
125 if (psys->particles == nullptr) {
127 }
128 /* TODO(sergey): This is not how particles were working prior to copy on
129 * write, but now evaluation is similar to case when one duplicates the
130 * object. In that case particles were doing reset here.
131 *
132 * Don't do reset when entering particle edit mode, as that will destroy the edit mode data.
133 * Shouldn't be an issue, since particles are supposed to be evaluated once prior to entering
134 * edit mode anyway.
135 * Could in theory be an issue when everything is done in a script, but then solution is
136 * not known to me. */
137 if (ctx->object->mode != OB_MODE_PARTICLE_EDIT) {
139 }
140 }
141
142 /* make new mesh */
144 psmd->mesh_final->vert_positions_for_write().copy_from(positions);
145 psmd->mesh_final->tag_positions_changed();
146
148
149 if (!psmd->mesh_final->runtime->deformed_only) {
150 /* Get the original mesh from the object, this is what the particles
151 * are attached to so in case of non-deform modifiers we need to remap
152 * them to the final mesh (typically subdivision surfaces). */
153 Mesh *mesh_original = nullptr;
154
155 if (ctx->object->type == OB_MESH) {
157 /* In edit mode get directly from the edit mesh. */
159 }
160 else {
161 /* Otherwise get regular mesh. */
162 mesh_original = id_cast<Mesh *>(ctx->object->data);
163 }
164 }
165 else {
166 mesh_original = mesh;
167 }
168
169 if (mesh_original) {
170 /* Make a persistent copy of the mesh. We don't actually need
171 * all this data, just some topology for remapping. Could be
172 * optimized once. */
173 psmd->mesh_original = BKE_mesh_copy_for_eval(*mesh_original);
174 }
175
177 }
178
179 /* Report change in mesh structure.
180 * This is an unreliable check for the topology check, but allows some
181 * handy configuration like emitting particles from inside particle
182 * instance. */
183 if (had_mesh_final && (psmd->mesh_final->verts_num != psmd->totdmvert ||
184 psmd->mesh_final->edges_num != psmd->totdmedge ||
185 psmd->mesh_final->totface_legacy != psmd->totdmface))
186 {
188 }
189 psmd->totdmvert = psmd->mesh_final->verts_num;
190 psmd->totdmedge = psmd->mesh_final->edges_num;
191 psmd->totdmface = psmd->mesh_final->totface_legacy;
192
193 {
197 ctx->depsgraph, scene, ctx->object, psys, (ctx->flag & MOD_APPLY_RENDER) != 0);
199 }
200
201 if (DEG_is_active(ctx->depsgraph)) {
202 Object *object_orig = DEG_get_original(ctx->object);
203 ModifierData *md_orig = BKE_modifiers_findby_name(object_orig, psmd->modifier.name);
204 BLI_assert(md_orig != nullptr);
205 ParticleSystemModifierData *psmd_orig = reinterpret_cast<ParticleSystemModifierData *>(
206 md_orig);
207 psmd_orig->flag = psmd->flag;
208 }
209}
210
211static void panel_draw(const bContext * /*C*/, Panel *panel)
212{
213 ui::Layout &layout = *panel->layout;
214
215 PointerRNA ob_ptr;
217
218 Object *ob = static_cast<Object *>(ob_ptr.data);
219 ModifierData *md = static_cast<ModifierData *>(ptr->data);
220 ParticleSystem *psys = (reinterpret_cast<ParticleSystemModifierData *>(md))->psys;
221
222 layout.label(RPT_("Settings are inside the Particles tab"), ICON_NONE);
223
224 if (!(ob->mode & OB_MODE_PARTICLE_EDIT)) {
225 if (ELEM(psys->part->ren_as, PART_DRAW_GR, PART_DRAW_OB)) {
226 layout.op("OBJECT_OT_duplicates_make_real",
227 CTX_IFACE_(BLT_I18NCONTEXT_OPERATOR_DEFAULT, "Make Instances Real"),
228 ICON_NONE);
229 }
230 else if (psys->part->ren_as == PART_DRAW_PATH) {
231 layout.op("OBJECT_OT_modifier_convert",
233 ICON_NONE);
234 }
235 }
236
237 modifier_error_message_draw(layout, ptr);
238}
239
240static void panel_register(ARegionType *region_type)
241{
243}
244
245static void blend_read(BlendDataReader *reader, ModifierData *md)
246{
247 ParticleSystemModifierData *psmd = reinterpret_cast<ParticleSystemModifierData *>(md);
248
249 psmd->mesh_final = nullptr;
250 psmd->mesh_original = nullptr;
251 /* This is written as part of ob->particlesystem. */
252 BLO_read_struct(reader, ParticleSystem, &psmd->psys);
255}
256
258 /*idname*/ "ParticleSystem",
259 /*name*/ N_("ParticleSystem"),
260 /*struct_name*/ "ParticleSystemModifierData",
261 /*struct_size*/ sizeof(ParticleSystemModifierData),
262 /*srna*/ &RNA_ParticleSystemModifier,
266 /*icon*/ ICON_MOD_PARTICLES,
267
268 /*copy_data*/ copy_data,
269
270 /*deform_verts*/ deform_verts,
271 /*deform_matrices*/ nullptr,
272 /*deform_verts_EM*/ nullptr,
273 /*deform_matrices_EM*/ nullptr,
274 /*modify_mesh*/ nullptr,
275 /*modify_geometry_set*/ nullptr,
276
277 /*init_data*/ init_data,
278 /*required_data_mask*/ required_data_mask,
279 /*free_data*/ free_data,
280 /*is_disabled*/ nullptr,
281 /*update_depsgraph*/ nullptr,
282 /*depends_on_time*/ nullptr,
283 /*depends_on_normals*/ nullptr,
284 /*foreach_ID_link*/ nullptr,
285 /*foreach_tex_link*/ nullptr,
286 /*free_runtime_data*/ nullptr,
287 /*panel_register*/ panel_register,
288 /*blend_write*/ nullptr,
289 /*blend_read*/ blend_read,
290 /*foreach_cache*/ nullptr,
291 /*foreach_working_space_color*/ nullptr,
292};
293
294} // namespace blender
#define BLO_read_struct(reader, struct_name, ptr_p)
#define RPT_(msgid)
#define CTX_IFACE_(context, msgid)
#define BLT_I18NCONTEXT_OPERATOR_DEFAULT
#define INIT_DEFAULT_STRUCT_AFTER(struct_dst, member)
#define ELEM(...)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
static void copy_data(const ModifierData *md, ModifierData *target, const int flag)
ModifierData * BKE_modifiers_findby_name(const Object *ob, const char *name)
PointerRNA * modifier_panel_get_property_pointers(Panel *panel, PointerRNA *r_ob_ptr)
ModifierTypeInfo modifierType_ParticleSystem
BMesh * BKE_editmesh_bmesh_get_for_write(Mesh *mesh)
Definition editmesh.cc:79
@ eParticleSystemFlag_file_loaded
@ eParticleSystemFlag_psys_updated
bool DEG_is_active(const Depsgraph *depsgraph)
Definition depsgraph.cc:324
Mesh * BKE_mesh_copy_for_eval(const Mesh &source)
@ eModifierType_ParticleSystem
static void init_data(ModifierData *md)
static void required_data_mask(ModifierData *, CustomData_MeshMasks *r_cddata_masks)
void modifier_error_message_draw(ui::Layout &layout, PointerRNA *ptr)
@ OB_MODE_PARTICLE_EDIT
void psys_emitter_customdata_mask(struct ParticleSystem *psys, struct CustomData_MeshMasks *r_cddata_masks)
Definition particle.cc:2246
static void free_data(ModifierData *md)
static void panel_register(ARegionType *region_type)
void BKE_id_free(Main *bmain, void *idv)
bool psys_check_enabled(struct Object *ob, struct ParticleSystem *psys, bool use_render_params)
Definition particle.cc:739
@ eModifierTypeFlag_SupportsMapping
@ eModifierTypeFlag_UsesPointCache
@ eModifierTypeFlag_AcceptsMesh
static void blend_read(BlendDataReader *, ModifierData *md)
void particle_system_update(struct Depsgraph *depsgraph, struct Scene *scene, struct Object *ob, struct ParticleSystem *psys, bool use_render_params)
T * DEG_get_original(T *id)
Mesh * BKE_mesh_from_bmesh_for_eval_nomain(BMesh *bm, const CustomData_MeshMasks *cd_mask_extra, const Mesh *me_settings)
PanelType * modifier_panel_register(ARegionType *region_type, ModifierType type, PanelDrawFn draw)
@ ID_RECALC_PSYS_RESET
Definition DNA_ID.h:1083
void BKE_mesh_tessface_ensure(Mesh *mesh)
static void panel_draw(const bContext *, Panel *panel)
static void deform_verts(ModifierData *md, const ModifierEvalContext *ctx, Mesh *mesh, MutableSpan< float3 > positions)
Scene * DEG_get_evaluated_scene(const Depsgraph *graph)
void BKE_modifier_copydata_generic(const ModifierData *md, ModifierData *md_dst, int flag)
bke::MeshRuntime * runtime
ListBaseT< struct ParticleSystem > particlesystem
ui::Layout * layout
eParticleSystem_Flag flag
void label(StringRef name, int icon)
PointerRNA op(wmOperatorType *ot, std::optional< StringRef > name, int icon, wm::OpCallContext context, eUI_Item_Flag flag)
#define N_(msgid)