Blender V4.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
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_report.hh"
23#include "BKE_rigidbody.h"
24
25#include "DEG_depsgraph.hh"
27
28#include "RNA_access.hh"
29#include "RNA_define.hh"
30#include "RNA_enum_types.hh"
31
32#include "WM_api.hh"
33#include "WM_types.hh"
34
35#include "ED_object.hh"
36#include "ED_physics.hh"
37#include "ED_screen.hh"
38
39#include "physics_intern.hh"
40
41/* ********************************************** */
42/* Helper API's for RigidBody Constraint Editing */
43
45{
46 if (scene == nullptr || !ID_IS_EDITABLE(scene) || ID_IS_OVERRIDE_LIBRARY(scene) ||
47 (scene->rigidbody_world != nullptr && scene->rigidbody_world->constraints != nullptr &&
48 (!ID_IS_EDITABLE(scene->rigidbody_world->constraints) ||
49 ID_IS_OVERRIDE_LIBRARY(scene->rigidbody_world->constraints))))
50 {
51 return false;
52 }
53 return true;
54}
55
57{
58 Scene *scene = CTX_data_scene(C);
60 return false;
61 }
62
65 return (ob && ob->rigidbody_constraint);
66 }
67 return false;
68}
69
71{
72 Scene *scene = CTX_data_scene(C);
74 return false;
75 }
77}
78
80 Main *bmain, Scene *scene, Object *ob, int type, ReportList *reports)
81{
83
84 /* check that object doesn't already have a constraint */
85 if (ob->rigidbody_constraint) {
87 reports, RPT_INFO, "Object '%s' already has a Rigid Body Constraint", ob->id.name + 2);
88 return false;
89 }
90 /* create constraint group if it doesn't already exits */
91 if (rbw->constraints == nullptr) {
92 rbw->constraints = BKE_collection_add(bmain, nullptr, "RigidBodyConstraints");
94 }
95 /* make rigidbody constraint settings */
97
98 /* add constraint to rigid body constraint group */
100
104
105 return true;
106}
107
109{
110 BKE_rigidbody_remove_constraint(bmain, scene, ob, false);
111
114}
115
116/* ********************************************** */
117/* Active Object Add/Remove Operators */
118
119/* ************ Add Rigid Body Constraint ************** */
120
122{
123 Main *bmain = CTX_data_main(C);
124 Scene *scene = CTX_data_scene(C);
125 ViewLayer *view_layer = CTX_data_view_layer(C);
127 BKE_view_layer_synced_ensure(scene, view_layer);
129 int type = RNA_enum_get(op->ptr, "type");
130 bool changed;
131
132 /* sanity checks */
133 if (ELEM(nullptr, scene, rbw)) {
134 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to add Rigid Body Constraint to");
135 return OPERATOR_CANCELLED;
136 }
137 /* apply to active object */
138 changed = ED_rigidbody_constraint_add(bmain, scene, ob, type, op->reports);
139
140 if (changed) {
141 /* send updates */
143
144 /* done */
145 return OPERATOR_FINISHED;
146 }
147 return OPERATOR_CANCELLED;
148}
149
151{
152 /* identifiers */
153 ot->idname = "RIGIDBODY_OT_constraint_add";
154 ot->name = "Add Rigid Body Constraint";
155 ot->description = "Add Rigid Body Constraint to active object";
156
157 /* callbacks */
160
161 /* flags */
163
164 /* properties */
166 "type",
169 "Rigid Body Constraint Type",
170 "");
171}
172
173/* ************ Remove Rigid Body Constraint ************** */
174
176{
177 Main *bmain = CTX_data_main(C);
178 Scene *scene = CTX_data_scene(C);
179 ViewLayer *view_layer = CTX_data_view_layer(C);
180 BKE_view_layer_synced_ensure(scene, view_layer);
182
183 /* apply to active object */
184 if (ELEM(nullptr, ob, ob->rigidbody_constraint)) {
185 BKE_report(op->reports, RPT_ERROR, "Object has no Rigid Body Constraint to remove");
186 return OPERATOR_CANCELLED;
187 }
188 ED_rigidbody_constraint_remove(bmain, scene, ob);
189
190 /* send updates */
192
193 /* done */
194 return OPERATOR_FINISHED;
195}
196
198{
199 /* identifiers */
200 ot->idname = "RIGIDBODY_OT_constraint_remove";
201 ot->name = "Remove Rigid Body Constraint";
202 ot->description = "Remove Rigid Body Constraint from Object";
203
204 /* callbacks */
207
208 /* flags */
210}
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:351
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
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:1021
@ ID_RECALC_SYNC_TO_EVAL
Definition DNA_ID.h:1085
#define ID_IS_EDITABLE(_id)
Definition DNA_ID.h:658
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition DNA_ID.h:683
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
bool ED_operator_object_active_editable(bContext *C)
@ OPTYPE_UNDO
Definition WM_types.hh:162
@ OPTYPE_REGISTER
Definition WM_types.hh:160
#define ND_TRANSFORM
Definition WM_types.hh:423
#define NC_OBJECT
Definition WM_types.hh:346
Object * context_active_object(const bContext *C)
static bool ED_operator_rigidbody_con_active_poll(bContext *C)
void ED_rigidbody_constraint_remove(Main *bmain, Scene *scene, Object *ob)
static int rigidbody_con_add_exec(bContext *C, wmOperator *op)
void RIGIDBODY_OT_constraint_remove(wmOperatorType *ot)
static bool ED_operator_rigidbody_con_add_poll(bContext *C)
static int rigidbody_con_remove_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)
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[66]
Definition DNA_ID.h:425
struct RigidBodyCon * rigidbody_constraint
struct Collection * constraints
const char * name
Definition WM_types.hh:990
bool(* poll)(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1042
const char * idname
Definition WM_types.hh:992
int(* exec)(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1006
const char * description
Definition WM_types.hh:996
PropertyRNA * prop
Definition WM_types.hh:1092
StructRNA * srna
Definition WM_types.hh:1080
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4125