Blender V5.0
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
26#include "DEG_depsgraph.hh"
28
29#include "RNA_access.hh"
30#include "RNA_define.hh"
31#include "RNA_enum_types.hh"
32
33#include "WM_api.hh"
34#include "WM_types.hh"
35
36#include "ED_object.hh"
37#include "ED_physics.hh"
38#include "ED_screen.hh"
39
40#include "physics_intern.hh"
41
42/* ********************************************** */
43/* Helper API's for RigidBody Constraint Editing */
44
46{
47 if (scene == nullptr || !ID_IS_EDITABLE(scene) || ID_IS_OVERRIDE_LIBRARY(scene) ||
48 (scene->rigidbody_world != nullptr && scene->rigidbody_world->constraints != nullptr &&
51 {
52 return false;
53 }
54 return true;
55}
56
58{
59 Scene *scene = CTX_data_scene(C);
61 return false;
62 }
63
66 return (ob && ob->rigidbody_constraint);
67 }
68 return false;
69}
70
72{
73 Scene *scene = CTX_data_scene(C);
75 return false;
76 }
78}
79
81 Main *bmain, Scene *scene, Object *ob, int 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);
126 ViewLayer *view_layer = CTX_data_view_layer(C);
128 BKE_view_layer_synced_ensure(scene, view_layer);
130 int type = RNA_enum_get(op->ptr, "type");
131 bool changed;
132
133 /* sanity checks */
134 if (ELEM(nullptr, scene, rbw)) {
135 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to add Rigid Body Constraint to");
136 return OPERATOR_CANCELLED;
137 }
138 /* apply to active object */
139 changed = ED_rigidbody_constraint_add(bmain, scene, ob, type, op->reports);
140
141 if (changed) {
142 /* send updates */
144
145 /* done */
146 return OPERATOR_FINISHED;
147 }
148 return OPERATOR_CANCELLED;
149}
150
152{
153 /* identifiers */
154 ot->idname = "RIGIDBODY_OT_constraint_add";
155 ot->name = "Add Rigid Body Constraint";
156 ot->description = "Add Rigid Body Constraint to active object";
157
158 /* callbacks */
161
162 /* flags */
164
165 /* properties */
166 ot->prop = RNA_def_enum(ot->srna,
167 "type",
170 "Rigid Body Constraint Type",
171 "");
172}
173
174/* ************ Remove Rigid Body Constraint ************** */
175
177{
178 Main *bmain = CTX_data_main(C);
179 Scene *scene = CTX_data_scene(C);
180 ViewLayer *view_layer = CTX_data_view_layer(C);
181 BKE_view_layer_synced_ensure(scene, view_layer);
183
184 /* apply to active object */
185 if (ELEM(nullptr, ob, ob->rigidbody_constraint)) {
186 BKE_report(op->reports, RPT_ERROR, "Object has no Rigid Body Constraint to remove");
187 return OPERATOR_CANCELLED;
188 }
189 ED_rigidbody_constraint_remove(bmain, scene, ob);
190
191 /* send updates */
193
194 /* done */
195 return OPERATOR_FINISHED;
196}
197
199{
200 /* identifiers */
201 ot->idname = "RIGIDBODY_OT_constraint_remove";
202 ot->name = "Remove Rigid Body Constraint";
203 ot->description = "Remove Rigid Body Constraint from Object";
204
205 /* callbacks */
208
209 /* flags */
211}
Collection * BKE_collection_add(Main *bmain, Collection *collection_parent, const char *name_custom)
bool BKE_collection_object_add(Main *bmain, Collection *collection, Object *ob)
Scene * CTX_data_scene(const bContext *C)
Main * CTX_data_main(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
void BKE_view_layer_synced_ensure(const Scene *scene, ViewLayer *view_layer)
Object * BKE_view_layer_active_object_get(const ViewLayer *view_layer)
void id_us_plus(ID *id)
Definition lib_id.cc:358
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_INFO
Definition BKE_report.hh:35
@ RPT_ERROR
Definition BKE_report.hh:39
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:153
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_remove_constraint(struct Main *bmain, struct Scene *scene, struct Object *ob, bool free_us)
struct RigidBodyCon * BKE_rigidbody_create_constraint(struct Scene *scene, struct Object *ob, short type)
struct RigidBodyWorld * BKE_rigidbody_get_world(struct Scene *scene)
#define ELEM(...)
void DEG_id_tag_update(ID *id, unsigned int flags)
void DEG_relations_tag_update(Main *bmain)
@ ID_RECALC_TRANSFORM
Definition DNA_ID.h:1054
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1118
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:705
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:730
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.
@ RBC_TYPE_FIXED
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_operator_object_active_editable(bContext *C)
#define C
Definition RandGen.cpp:29
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define ND_TRANSFORM
Definition WM_types.hh:456
#define NC_OBJECT
Definition WM_types.hh:379
Object * context_active_object(const bContext *C)
void ED_rigidbody_constraint_remove(Main *bmain, Scene *scene, Object *ob)
static wmOperatorStatus rigidbody_con_remove_exec(bContext *C, wmOperator *op)
static bool operator_rigidbody_con_add_poll(bContext *C)
void RIGIDBODY_OT_constraint_remove(wmOperatorType *ot)
static wmOperatorStatus rigidbody_con_add_exec(bContext *C, wmOperator *op)
bool ED_rigidbody_constraint_add(Main *bmain, Scene *scene, Object *ob, int type, ReportList *reports)
static bool operator_rigidbody_constraints_editable_poll(Scene *scene)
void RIGIDBODY_OT_constraint_add(wmOperatorType *ot)
static bool operator_rigidbody_con_active_poll(bContext *C)
int RNA_enum_get(PointerRNA *ptr, const char *name)
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, const int default_value, const char *ui_name, const char *ui_description)
const EnumPropertyItem rna_enum_rigidbody_constraint_type_items[]
char name[258]
Definition DNA_ID.h:432
struct RigidBodyCon * rigidbody_constraint
struct Collection * constraints
struct RigidBodyWorld * rigidbody_world
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4237