Blender V5.3
rigidbody_constraint.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 "BKE_collection.hh"
19#include "BKE_context.hh"
20#include "BKE_layer.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_library.hh"
23#include "BKE_report.hh"
24#include "BKE_rigidbody.h"
25#include "BKE_scene.hh"
26
27#include "DEG_depsgraph.hh"
29
30#include "RNA_access.hh"
31#include "RNA_define.hh"
32#include "RNA_enum_types.hh"
33
34#include "WM_api.hh"
35#include "WM_types.hh"
36
37#include "ED_object.hh"
38#include "ED_physics.hh"
39#include "ED_screen.hh"
40
41#include "physics_intern.hh"
42
43namespace blender {
44
45/* ********************************************** */
46/* Helper API's for RigidBody Constraint Editing */
47
49{
50 if (scene == nullptr || !ID_IS_EDITABLE(scene) || ID_IS_OVERRIDE_LIBRARY(scene) ||
51 (scene->rigidbody_world != nullptr && scene->rigidbody_world->constraints != nullptr &&
54 {
55 return false;
56 }
57 return true;
58}
59
70
72{
73 Scene *scene = CTX_data_scene(C);
75 return false;
76 }
78}
79
81 Main *bmain, Scene *scene, Object *ob, eRigidBodyCon_Type type, ReportList *reports)
82{
84
85 /* check that object doesn't already have a constraint */
86 if (ob->rigidbody_constraint) {
88 reports, RPT_INFO, "Object '%s' already has a Rigid Body Constraint", ob->id.name + 2);
89 return false;
90 }
91 /* create constraint group if it doesn't already exits */
92 if (rbw->constraints == nullptr) {
93 rbw->constraints = BKE_collection_add(bmain, nullptr, "RigidBodyConstraints");
95 }
96 /* make rigidbody constraint settings */
98
99 /* add constraint to rigid body constraint group */
100 BKE_collection_object_add(bmain, rbw->constraints, ob);
101
105
106 return true;
107}
108
110{
111 BKE_rigidbody_remove_constraint(bmain, scene, ob, false);
112
115}
116
117/* ********************************************** */
118/* Active Object Add/Remove Operators */
119
120/* ************ Add Rigid Body Constraint ************** */
121
123{
124 Main *bmain = CTX_data_main(C);
125 Scene *scene = CTX_data_scene(C);
129 bool changed;
130 /* Poll ensures. */
131 BLI_assert(scene && ob);
132
133 /* The rigid body world is not ensured by the poll. */
134 if (rbw == nullptr) {
135 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to add Rigid Body Constraint to");
136 return OPERATOR_CANCELLED;
137 }
138 /* Pinned objects could be from another scene. */
139 if (!BKE_scene_object_find(*bmain, scene, ob)) {
140 BKE_report(op->reports, RPT_ERROR, "No object in the scene to add Rigid Body Constraint to");
141 return OPERATOR_CANCELLED;
142 }
143 /* apply to active object */
144 changed = ED_rigidbody_constraint_add(bmain, scene, ob, type, op->reports);
145
146 if (changed) {
147 /* send updates */
149
150 /* done */
151 return OPERATOR_FINISHED;
152 }
153 return OPERATOR_CANCELLED;
154}
155
157{
158 /* identifiers */
159 ot->idname = "RIGIDBODY_OT_constraint_add";
160 ot->name = "Add Rigid Body Constraint";
161 ot->description = "Add Rigid Body Constraint to active object";
162
163 /* callbacks */
166
167 /* flags */
169
170 /* properties */
171 ot->prop = RNA_def_enum(ot->srna,
172 "type",
175 "Rigid Body Constraint Type",
176 "");
177}
178
179/* ************ Remove Rigid Body Constraint ************** */
180
182{
183 Main *bmain = CTX_data_main(C);
184 Scene *scene = CTX_data_scene(C);
186 /* Poll ensures. */
187 BLI_assert(scene && ob && ob->rigidbody_constraint);
188
189 /* Pinned objects could be from another scene. */
190 if (!BKE_scene_object_find(*bmain, scene, ob)) {
192 op->reports, RPT_ERROR, "No object in the scene to remove Rigid Body Constraint from");
193 return OPERATOR_CANCELLED;
194 }
195 ED_rigidbody_constraint_remove(bmain, scene, ob);
196
197 /* send updates */
199
200 /* done */
201 return OPERATOR_FINISHED;
202}
203
205{
206 /* identifiers */
207 ot->idname = "RIGIDBODY_OT_constraint_remove";
208 ot->name = "Remove Rigid Body Constraint";
209 ot->description = "Remove Rigid Body Constraint from Object";
210
211 /* callbacks */
214
215 /* flags */
217}
218
219} // namespace blender
API for Blender-side Rigid Body stuff.
#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 NC_OBJECT
Definition WM_types.hh:392
Object * context_active_object(const bContext *C)
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
int RNA_enum_get(PointerRNA *ptr, const char *name)
void ED_rigidbody_constraint_remove(Main *bmain, Scene *scene, Object *ob)
static wmOperatorStatus rigidbody_con_remove_exec(bContext *C, wmOperator *op)
void id_us_plus(ID *id)
Definition lib_id.cc:433
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
bool ED_rigidbody_constraint_add(Main *bmain, Scene *scene, Object *ob, eRigidBodyCon_Type type, ReportList *reports)
void RIGIDBODY_OT_constraint_add(wmOperatorType *ot)
struct RigidBodyCon * BKE_rigidbody_create_constraint(struct Scene *scene, struct Object *ob, eRigidBodyCon_Type type)
const EnumPropertyItem rna_enum_rigidbody_constraint_type_items[]
void DEG_id_tag_update(ID *id, unsigned int flags)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
bool ED_operator_object_active_editable(bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
static wmOperatorStatus rigidbody_con_add_exec(bContext *C, wmOperator *op)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
Main * CTX_data_main(const bContext *C)
static bool operator_rigidbody_con_add_poll(bContext *C)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
bool BKE_scene_object_find(const Main &bmain, Scene *scene, Object *ob)
Definition scene.cc:2209
void BKE_rigidbody_remove_constraint(struct Main *bmain, struct Scene *scene, struct Object *ob, bool free_us)
static bool operator_rigidbody_con_active_poll(bContext *C)
void DEG_relations_tag_update(Main *bmain)
static bool operator_rigidbody_constraints_editable_poll(Scene *scene)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
void RIGIDBODY_OT_constraint_remove(wmOperatorType *ot)
struct RigidBodyWorld * BKE_rigidbody_get_world(struct Scene *scene)
bool ED_operator_object_active_editable_ex(bContext *C, const Object *ob)
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
char name[258]
Definition DNA_ID.h:433
struct RigidBodyCon * rigidbody_constraint
struct Collection * constraints
struct RigidBodyWorld * rigidbody_world
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
struct ReportList * reports