Blender V5.3
rigidbody_object.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2013 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
9
10#include <cstdlib>
11#include <cstring>
12
14#include "DNA_object_types.h"
15#include "DNA_rigidbody_types.h"
16#include "DNA_scene_types.h"
17
18#include "BLT_translation.hh"
19
20#include "BKE_context.hh"
21#include "BKE_library.hh"
22#include "BKE_report.hh"
23#include "BKE_rigidbody.h"
24#include "BKE_scene.hh"
25
26#include "DEG_depsgraph.hh"
29
30#include "RNA_access.hh"
31#include "RNA_define.hh"
32#include "RNA_enum_types.hh"
33#include "RNA_prototypes.hh"
34
35#include "WM_api.hh"
36#include "WM_types.hh"
37
38#include "ED_object.hh"
39#include "ED_physics.hh"
40#include "ED_screen.hh"
41
42#include "physics_intern.hh"
43
44namespace blender {
45
46/* ********************************************** */
47/* Helper API's for RigidBody Objects Editing */
48
50{
51 if (scene == nullptr || !ID_IS_EDITABLE(scene) || ID_IS_OVERRIDE_LIBRARY(scene) ||
52 (scene->rigidbody_world != nullptr && scene->rigidbody_world->group != nullptr &&
55 {
56 return false;
57 }
58 return true;
59}
60
62{
63 Scene *scene = CTX_data_scene(C);
65 return false;
66 }
67
70 return (ob && ob->rigidbody_object);
71 }
72
73 return false;
74}
75
77{
78 Scene *scene = CTX_data_scene(C);
80 return false;
81 }
82
85 return (ob && ob->type == OB_MESH);
86 }
87
88 return false;
89}
90
91/* ----------------- */
92
94 Main *bmain, Scene *scene, Object *ob, eRigidBodyOb_Type type, ReportList *reports)
95{
96 return BKE_rigidbody_add_object(bmain, scene, ob, type, reports);
97}
98
100{
101 BKE_rigidbody_remove_object(bmain, scene, ob, false);
102}
103
104/* ********************************************** */
105/* Active Object Add/Remove Operators */
106
107/* ************ Add Rigid Body ************** */
108
110{
111 Main *bmain = CTX_data_main(C);
112 Scene *scene = CTX_data_scene(C);
115 bool changed;
116 /* Poll ensures. */
117 BLI_assert(scene && ob);
118
119 /* Pinned objects could be from another scene. */
120 if (!BKE_scene_object_find(*bmain, scene, ob)) {
121 BKE_report(op->reports, RPT_ERROR, "No object in the scene to add Rigid Body to");
122 return OPERATOR_CANCELLED;
123 }
124
125 /* apply to active object */
126 changed = ED_rigidbody_object_add(bmain, scene, ob, type, op->reports);
127
128 if (changed) {
129 /* send updates */
132
133 /* done */
134 return OPERATOR_FINISHED;
135 }
136 return OPERATOR_CANCELLED;
137}
138
140{
141 /* identifiers */
142 ot->idname = "RIGIDBODY_OT_object_add";
143 ot->name = "Add Rigid Body";
144 ot->description = "Add active object as Rigid Body";
145
146 /* callbacks */
149
150 /* flags */
152
153 /* properties */
154 ot->prop = RNA_def_enum(ot->srna,
155 "type",
158 "Rigid Body Type",
159 "");
160}
161
162/* ************ Remove Rigid Body ************** */
163
165{
166 Main *bmain = CTX_data_main(C);
167 Scene *scene = CTX_data_scene(C);
169 bool changed = false;
170 /* Poll ensures. */
171 BLI_assert(scene && ob && ob->rigidbody_object);
172
173 /* Pinned objects could be from another scene. */
174 if (!BKE_scene_object_find(*bmain, scene, ob)) {
175 BKE_report(op->reports, RPT_ERROR, "No object in the scene to remove Rigid Body from");
176 return OPERATOR_CANCELLED;
177 }
178
179 /* apply to active object */
180 if (!ELEM(nullptr, ob, ob->rigidbody_object)) {
181 ED_rigidbody_object_remove(bmain, scene, ob);
182 changed = true;
183 }
184
185 if (changed) {
186 /* send updates */
189
190 /* done */
191 return OPERATOR_FINISHED;
192 }
193
194 BKE_report(op->reports, RPT_ERROR, "Object has no Rigid Body settings to remove");
195 return OPERATOR_CANCELLED;
196}
197
199{
200 /* identifiers */
201 ot->idname = "RIGIDBODY_OT_object_remove";
202 ot->name = "Remove Rigid Body";
203 ot->description = "Remove Rigid Body settings from Object";
204
205 /* callbacks */
208
209 /* flags */
211}
212
213/* ********************************************** */
214/* Selected Object Add/Remove Operators */
215
216/* ************ Add Rigid Bodies ************** */
217
219{
220 Main *bmain = CTX_data_main(C);
221 Scene *scene = CTX_data_scene(C);
223 bool changed = false;
224
225 /* create rigid body objects and add them to the world's group */
226 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
227 changed |= ED_rigidbody_object_add(bmain, scene, ob, type, op->reports);
228 }
230
231 if (changed) {
232 /* send updates */
235
236 /* done */
237 return OPERATOR_FINISHED;
238 }
239 return OPERATOR_CANCELLED;
240}
241
243{
244 /* identifiers */
245 ot->idname = "RIGIDBODY_OT_objects_add";
246 ot->name = "Add Rigid Bodies";
247 ot->description = "Add selected objects as Rigid Bodies";
248
249 /* callbacks */
252
253 /* flags */
255
256 /* properties */
257 ot->prop = RNA_def_enum(ot->srna,
258 "type",
261 "Rigid Body Type",
262 "");
263}
264
265/* ************ Remove Rigid Bodies ************** */
266
268{
269 Main *bmain = CTX_data_main(C);
270 Scene *scene = CTX_data_scene(C);
271 bool changed = false;
272
273 /* apply this to all selected objects... */
274 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
275 if (ob->rigidbody_object) {
276 ED_rigidbody_object_remove(bmain, scene, ob);
277 changed = true;
278 }
279 }
281
282 if (changed) {
283 /* send updates */
286
287 /* done */
288 return OPERATOR_FINISHED;
289 }
290 return OPERATOR_CANCELLED;
291}
292
294{
295 /* identifiers */
296 ot->idname = "RIGIDBODY_OT_objects_remove";
297 ot->name = "Remove Rigid Bodies";
298 ot->description = "Remove selected objects from Rigid Body simulation";
299
300 /* callbacks */
303
304 /* flags */
306}
307
308/* ********************************************** */
309/* Utility Operators */
310
311/* ************ Change Collision Shapes ************** */
312
314{
315 int shape = RNA_enum_get(op->ptr, "type");
316 bool changed = false;
317
318 /* apply this to all selected objects... */
319 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
320 if (ob->rigidbody_object) {
321 /* use RNA-system to change the property and perform all necessary changes */
323 &ob->id, RNA_RigidBodyObject, ob->rigidbody_object);
324 RNA_enum_set(&ptr, "collision_shape", shape);
325
327
328 changed = true;
329 }
330 }
332
333 if (changed) {
334 /* send updates */
337
338 /* done */
339 return OPERATOR_FINISHED;
340 }
341 return OPERATOR_CANCELLED;
342}
343
345{
346 /* identifiers */
347 ot->idname = "RIGIDBODY_OT_shape_change";
348 ot->name = "Change Collision Shape";
349 ot->description = "Change collision shapes for selected Rigid Body Objects";
350
351 /* callbacks */
355
356 /* flags */
358
359 /* properties */
360 ot->prop = RNA_def_enum(ot->srna,
361 "type",
364 "Rigid Body Shape",
365 "");
366}
367
368/* ************ Calculate Mass ************** */
369
370/* Entry in material density table */
372 const char *name; /* Name of material */
373 float density; /* Density (kg/m^3) */
374};
375
376/* Preset density values for materials (kg/m^3)
377 * Selected values obtained from:
378 * 1) http://www.jaredzone.info/2010/09/densities.html
379 * 2) http://www.avlandesign.com/density_construction.htm
380 * 3) http://www.avlandesign.com/density_metal.htm
381 */
383 {N_("Air"), 1.0f}, /* not quite; adapted from 1.43 for oxygen for use as default */
384 {N_("Acrylic"), 1400.0f},
385 {N_("Asphalt (Crushed)"), 721.0f},
386 {N_("Bark"), 240.0f},
387 {N_("Beans (Cocoa)"), 593.0f},
388 {N_("Beans (Soy)"), 721.0f},
389 {N_("Brick (Pressed)"), 2400.0f},
390 {N_("Brick (Common)"), 2000.0f},
391 {N_("Brick (Soft)"), 1600.0f},
392 {N_("Brass"), 8216.0f},
393 {N_("Bronze"), 8860.0f},
394 {N_("Carbon (Solid)"), 2146.0f},
395 {N_("Cardboard"), 689.0f},
396 {N_("Cast Iron"), 7150.0f}, /* {N_("Cement"), 1442.0f}, */
397 {N_("Chalk (Solid)"), 2499.0f}, /* {N_("Coffee (Fresh/Roast)"), ~500}, */
398 {N_("Concrete"), 2320.0f},
399 {N_("Charcoal"), 208.0f},
400 {N_("Cork"), 240.0f},
401 {N_("Copper"), 8933.0f},
402 {N_("Garbage"), 481.0f},
403 {N_("Glass (Broken)"), 1940.0f},
404 {N_("Glass (Solid)"), 2190.0f},
405 {N_("Gold"), 19282.0f},
406 {N_("Granite (Broken)"), 1650.0f},
407 {N_("Granite (Solid)"), 2691.0f},
408 {N_("Gravel"), 2780.0f},
409 {N_("Ice (Crushed)"), 593.0f},
410 {N_("Ice (Solid)"), 919.0f},
411 {N_("Iron"), 7874.0f},
412 {N_("Lead"), 11342.0f},
413 {N_("Limestone (Broken)"), 1554.0f},
414 {N_("Limestone (Solid)"), 2611.0f},
415 {N_("Marble (Broken)"), 1570.0f},
416 {N_("Marble (Solid)"), 2563.0f},
417 {N_("Paper"), 1201.0f},
418 {N_("Peanuts (Shelled)"), 641.0f},
419 {N_("Peanuts (Not Shelled)"), 272.0f},
420 {N_("Plaster"), 849.0f},
421 {N_("Plastic"), 1200.0f},
422 {N_("Polystyrene"), 1050.0f},
423 {N_("Rubber"), 1522.0f},
424 {N_("Silver"), 10501.0f},
425 {N_("Steel"), 7860.0f},
426 {N_("Stone"), 2515.0f},
427 {N_("Stone (Crushed)"), 1602.0f},
428 {N_("Timber"), 610.0f},
429};
431 sizeof(rbMaterialDensityItem);
432
433/* dynamically generate list of items
434 * - Although there is a runtime cost, this has a lower maintenance cost
435 * in the long run than other two-list solutions...
436 */
438 PointerRNA * /*ptr*/,
439 PropertyRNA * /*prop*/,
440 bool *r_free)
441{
442 EnumPropertyItem item_tmp = {0};
443 EnumPropertyItem *item = nullptr;
444 int totitem = 0;
445 int i = 0;
446
447 /* add each preset to the list */
448 for (i = 0; i < NUM_RB_MATERIAL_PRESETS; i++) {
450
451 item_tmp.identifier = preset->name;
452 item_tmp.name = IFACE_(preset->name);
453 item_tmp.value = i;
454 RNA_enum_item_add(&item, &totitem, &item_tmp);
455 }
456
457 /* add special "custom" entry to the end of the list */
458 {
459 item_tmp.identifier = "Custom";
460 item_tmp.name = IFACE_("Custom");
461 item_tmp.value = -1;
462 RNA_enum_item_add(&item, &totitem, &item_tmp);
463 }
464
465 RNA_enum_item_end(&item, &totitem);
466 *r_free = true;
467
468 return item;
469}
470
471/* ------------------------------------------ */
472
474{
475 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
476 int material = RNA_enum_get(op->ptr, "material");
477 float density;
478 bool changed = false;
479
480 /* get density (kg/m^3) to apply */
481 if (material >= 0) {
482 /* get density from table, and store in props for later repeating */
483 if (material >= NUM_RB_MATERIAL_PRESETS) {
484 material = 0;
485 }
486
487 density = RB_MATERIAL_DENSITY_TABLE[material].density;
488 RNA_float_set(op->ptr, "density", density);
489 }
490 else {
491 /* custom - grab from whatever value is set */
492 density = RNA_float_get(op->ptr, "density");
493 }
494
495 /* Apply this to all selected objects (with rigid-bodies). */
496 CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
497 if (ob->rigidbody_object) {
498 float volume; /* m^3 */
499 float mass; /* kg */
500
501 /* mass is calculated from the approximate volume of the object,
502 * and the density of the material we're simulating
503 */
504 Object *ob_eval = DEG_get_evaluated(depsgraph, ob);
505 BKE_rigidbody_calc_volume(ob_eval, &volume);
506 mass = volume * density;
507
508 /* use RNA-system to change the property and perform all necessary changes */
510 &ob->id, RNA_RigidBodyObject, ob->rigidbody_object);
511 RNA_float_set(&ptr, "mass", mass);
512
514
515 changed = true;
516 }
517 }
519
520 if (changed) {
521 /* send updates */
523
524 /* done */
525 return OPERATOR_FINISHED;
526 }
527 return OPERATOR_CANCELLED;
528}
529
530static bool mass_calculate_poll_property(const bContext * /*C*/,
531 wmOperator *op,
532 const PropertyRNA *prop)
533{
534 const char *prop_id = RNA_property_identifier(prop);
535
536 /* Disable density input when not using the 'Custom' preset. */
537 if (STREQ(prop_id, "density")) {
538 int material = RNA_enum_get(op->ptr, "material");
539 if (material >= 0) {
541 }
542 else {
543 RNA_def_property_flag(const_cast<PropertyRNA *>(prop), PROP_EDITABLE);
544 }
545 }
546
547 return true;
548}
549
551{
552 PropertyRNA *prop;
553
554 /* identifiers */
555 ot->idname = "RIGIDBODY_OT_mass_calculate";
556 ot->name = "Calculate Mass";
557 ot->description = "Automatically calculate mass values for Rigid Body Objects based on volume";
558
559 /* callbacks */
560 ot->invoke = WM_enum_search_invoke; /* XXX */
564
565 /* flags */
567
568 /* properties */
569 ot->prop = prop = RNA_def_enum(
570 ot->srna,
571 "material",
573 0,
574 "Material Preset",
575 "Type of material that objects are made of (determines material density)");
578
580 "density",
581 1.0,
582 FLT_MIN,
583 FLT_MAX,
584 "Density",
585 "Density value (kg/m^3), allows custom value if the 'Custom' preset is used",
586 1.0f,
587 2500.0f);
588}
589
590/* ********************************************** */
591
592} // namespace blender
#define CTX_DATA_BEGIN(C, Type, instance, member)
#define CTX_DATA_END
API for Blender-side Rigid Body stuff.
#define IFACE_(msgid)
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:708
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:736
Object groups, one object can be in many groups at once.
Object is a sort of wrapper for general info.
Types and defines for representing Rigid Body entities.
#define C
Definition RandGen.cpp:29
#define ND_TRANSFORM
Definition WM_types.hh:475
#define ND_POINTCACHE
Definition WM_types.hh:507
#define ND_SPACE_VIEW3D
Definition WM_types.hh:571
#define NC_OBJECT
Definition WM_types.hh:392
#define NC_SPACE
Definition WM_types.hh:405
#define ELEM(...)
#define STREQ(a, b)
#define FLT_MAX
#define FLT_MIN
Object * context_active_object(const bContext *C)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
const EnumPropertyItem rna_enum_rigidbody_object_type_items[]
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
wmOperatorStatus WM_menu_invoke(bContext *C, wmOperator *op, const wmEvent *)
void RNA_enum_item_add(EnumPropertyItem **items, int *totitem, const EnumPropertyItem *item)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void BKE_rigidbody_calc_volume(struct Object *ob, float *r_vol)
static const int NUM_RB_MATERIAL_PRESETS
static bool mass_calculate_poll_property(const bContext *, wmOperator *op, const PropertyRNA *prop)
static rbMaterialDensityItem RB_MATERIAL_DENSITY_TABLE[]
void RIGIDBODY_OT_shape_change(wmOperatorType *ot)
static bool operator_rigidbody_add_poll(bContext *C)
const char * RNA_property_identifier(const PropertyRNA *prop)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
static bool operator_rigidbody_active_poll(bContext *C)
static const EnumPropertyItem * rigidbody_materials_itemf(bContext *, PointerRNA *, PropertyRNA *, bool *r_free)
void RNA_enum_set(PointerRNA *ptr, const char *name, int value)
static wmOperatorStatus rigidbody_object_add_exec(bContext *C, wmOperator *op)
void RIGIDBODY_OT_objects_remove(wmOperatorType *ot)
bool ED_rigidbody_object_add(Main *bmain, Scene *scene, Object *ob, eRigidBodyOb_Type type, ReportList *reports)
void ED_rigidbody_object_remove(Main *bmain, Scene *scene, Object *ob)
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
static wmOperatorStatus rigidbody_objects_calc_mass_exec(bContext *C, wmOperator *op)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void RNA_enum_item_end(EnumPropertyItem **items, int *totitem)
const EnumPropertyItem rna_enum_dummy_DEFAULT_items[]
Definition rna_rna.cc:34
bool BKE_rigidbody_add_object(struct Main *bmain, struct Scene *scene, struct Object *ob, eRigidBodyOb_Type type, struct ReportList *reports)
bool ED_operator_object_active_editable(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
void RNA_float_set(PointerRNA *ptr, const char *name, float value)
void RIGIDBODY_OT_object_add(wmOperatorType *ot)
void RNA_def_property_clear_flag(PropertyRNA *prop, PropertyFlag flag)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_rigidbody_remove_object(struct Main *bmain, struct Scene *scene, struct Object *ob, bool free_us)
Main * CTX_data_main(const bContext *C)
void RIGIDBODY_OT_object_remove(wmOperatorType *ot)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ PROP_EDITABLE
Definition RNA_types.hh:376
@ PROP_ENUM_NO_TRANSLATE
Definition RNA_types.hh:502
bool BKE_scene_object_find(const Main &bmain, Scene *scene, Object *ob)
Definition scene.cc:2209
wmOperatorStatus WM_enum_search_invoke(bContext *C, wmOperator *op, const wmEvent *)
const EnumPropertyItem rna_enum_rigidbody_object_shape_items[]
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
static wmOperatorStatus rigidbody_objects_shape_change_exec(bContext *C, wmOperator *op)
static wmOperatorStatus rigidbody_objects_add_exec(bContext *C, wmOperator *op)
static wmOperatorStatus rigidbody_objects_remove_exec(bContext *C, wmOperator *)
static wmOperatorStatus rigidbody_object_remove_exec(bContext *C, wmOperator *op)
void RIGIDBODY_OT_mass_calculate(wmOperatorType *ot)
PropertyRNA * RNA_def_float(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
float RNA_float_get(PointerRNA *ptr, const char *name)
static bool operator_rigidbody_editable_poll(Scene *scene)
void RIGIDBODY_OT_objects_add(wmOperatorType *ot)
struct RigidBodyOb * rigidbody_object
struct RigidBodyWorld * rigidbody_world
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
bool(*) poll_property(const bContext *C, wmOperator *op, const PropertyRNA *prop) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1171
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports
#define N_(msgid)