Blender  V3.3
rigidbody_constraint.c
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2013 Blender Foundation. All rights reserved. */
3 
9 #include <stdlib.h>
10 #include <string.h>
11 
12 #include "DNA_collection_types.h"
13 #include "DNA_object_types.h"
14 #include "DNA_rigidbody_types.h"
15 #include "DNA_scene_types.h"
16 
17 #include "BKE_collection.h"
18 #include "BKE_context.h"
19 #include "BKE_lib_id.h"
20 #include "BKE_main.h"
21 #include "BKE_report.h"
22 #include "BKE_rigidbody.h"
23 
24 #include "DEG_depsgraph.h"
25 #include "DEG_depsgraph_build.h"
26 
27 #include "RNA_access.h"
28 #include "RNA_define.h"
29 #include "RNA_enum_types.h"
30 
31 #include "WM_api.h"
32 #include "WM_types.h"
33 
34 #include "ED_object.h"
35 #include "ED_physics.h"
36 #include "ED_screen.h"
37 
38 #include "physics_intern.h"
39 
40 /* ********************************************** */
41 /* Helper API's for RigidBody Constraint Editing */
42 
44 {
49  return false;
50  }
51  return true;
52 }
53 
55 {
58  return false;
59  }
60 
63  return (ob && ob->rigidbody_constraint);
64  }
65  return false;
66 }
67 
69 {
72  return false;
73  }
75 }
76 
78  Main *bmain, Scene *scene, Object *ob, int type, ReportList *reports)
79 {
81 
82  /* check that object doesn't already have a constraint */
83  if (ob->rigidbody_constraint) {
85  reports, RPT_INFO, "Object '%s' already has a Rigid Body Constraint", ob->id.name + 2);
86  return false;
87  }
88  /* create constraint group if it doesn't already exits */
89  if (rbw->constraints == NULL) {
90  rbw->constraints = BKE_collection_add(bmain, NULL, "RigidBodyConstraints");
91  id_us_plus(&rbw->constraints->id);
92  }
93  /* make rigidbody constraint settings */
95 
96  /* add constraint to rigid body constraint group */
97  BKE_collection_object_add(bmain, rbw->constraints, ob);
98 
102 
103  return true;
104 }
105 
107 {
108  BKE_rigidbody_remove_constraint(bmain, scene, ob, false);
109 
112 }
113 
114 /* ********************************************** */
115 /* Active Object Add/Remove Operators */
116 
117 /* ************ Add Rigid Body Constraint ************** */
118 
120 {
121  Main *bmain = CTX_data_main(C);
123  ViewLayer *view_layer = CTX_data_view_layer(C);
125  Object *ob = OBACT(view_layer);
126  int type = RNA_enum_get(op->ptr, "type");
127  bool changed;
128 
129  /* sanity checks */
130  if (ELEM(NULL, scene, rbw)) {
131  BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to add Rigid Body Constraint to");
132  return OPERATOR_CANCELLED;
133  }
134  /* apply to active object */
135  changed = ED_rigidbody_constraint_add(bmain, scene, ob, type, op->reports);
136 
137  if (changed) {
138  /* send updates */
140 
141  /* done */
142  return OPERATOR_FINISHED;
143  }
144  return OPERATOR_CANCELLED;
145 }
146 
148 {
149  /* identifiers */
150  ot->idname = "RIGIDBODY_OT_constraint_add";
151  ot->name = "Add Rigid Body Constraint";
152  ot->description = "Add Rigid Body Constraint to active object";
153 
154  /* callbacks */
157 
158  /* flags */
160 
161  /* properties */
162  ot->prop = RNA_def_enum(ot->srna,
163  "type",
166  "Rigid Body Constraint Type",
167  "");
168 }
169 
170 /* ************ Remove Rigid Body Constraint ************** */
171 
173 {
174  Main *bmain = CTX_data_main(C);
176  ViewLayer *view_layer = CTX_data_view_layer(C);
177  Object *ob = OBACT(view_layer);
178 
179  /* apply to active object */
180  if (ELEM(NULL, ob, ob->rigidbody_constraint)) {
181  BKE_report(op->reports, RPT_ERROR, "Object has no Rigid Body Constraint to remove");
182  return OPERATOR_CANCELLED;
183  }
185 
186  /* send updates */
188 
189  /* done */
190  return OPERATOR_FINISHED;
191 }
192 
194 {
195  /* identifiers */
196  ot->idname = "RIGIDBODY_OT_constraint_remove";
197  ot->name = "Remove Rigid Body Constraint";
198  ot->description = "Remove Rigid Body Constraint from Object";
199 
200  /* callbacks */
203 
204  /* flags */
206 }
struct Collection * BKE_collection_add(struct Main *bmain, struct Collection *parent, const char *name)
Definition: collection.c:425
bool BKE_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *ob)
Definition: collection.c:1125
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1090
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1100
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1074
void id_us_plus(struct ID *id)
Definition: lib_id.c:305
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.c:83
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_remove_constraint(struct Main *bmain, struct Scene *scene, struct Object *ob, bool free_us)
Definition: rigidbody.c:2342
struct RigidBodyWorld * BKE_rigidbody_get_world(struct Scene *scene)
Definition: rigidbody.c:2324
struct RigidBodyCon * BKE_rigidbody_create_constraint(struct Scene *scene, struct Object *ob, short type)
Definition: rigidbody.c:2320
#define ELEM(...)
void DEG_id_tag_update(struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
@ ID_RECALC_TRANSFORM
Definition: DNA_ID.h:771
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:834
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:566
#define ID_IS_OVERRIDE_LIBRARY(_id)
Definition: DNA_ID.h:588
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
#define OBACT(_view_layer)
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
struct Object * ED_object_active_context(const struct bContext *C)
bool ED_operator_object_active_editable(struct bContext *C)
Definition: screen_ops.c:396
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
#define C
Definition: RandGen.cpp:25
@ OPTYPE_UNDO
Definition: WM_types.h:148
@ OPTYPE_REGISTER
Definition: WM_types.h:146
#define ND_TRANSFORM
Definition: WM_types.h:405
#define NC_OBJECT
Definition: WM_types.h:329
Scene scene
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)
Definition: rna_access.c:5004
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3783
const EnumPropertyItem rna_enum_rigidbody_constraint_type_items[]
Definition: rna_rigidbody.c:75
char name[66]
Definition: DNA_ID.h:378
Definition: BKE_main.h:121
struct RigidBodyCon * rigidbody_constraint
struct Collection * constraints
struct RigidBodyWorld * rigidbody_world
const char * name
Definition: WM_types.h:888
const char * idname
Definition: WM_types.h:890
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:943
struct StructRNA * srna
Definition: WM_types.h:969
const char * description
Definition: WM_types.h:893
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:903
PropertyRNA * prop
Definition: WM_types.h:981
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3479