Blender  V2.93
rigidbody_constraint.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation
17  * All rights reserved.
18  */
19 
25 #include <stdlib.h>
26 #include <string.h>
27 
28 #include "DNA_collection_types.h"
29 #include "DNA_object_types.h"
30 #include "DNA_rigidbody_types.h"
31 #include "DNA_scene_types.h"
32 
33 #include "BKE_collection.h"
34 #include "BKE_context.h"
35 #include "BKE_lib_id.h"
36 #include "BKE_main.h"
37 #include "BKE_report.h"
38 #include "BKE_rigidbody.h"
39 
40 #include "DEG_depsgraph.h"
41 #include "DEG_depsgraph_build.h"
42 
43 #include "RNA_access.h"
44 #include "RNA_define.h"
45 #include "RNA_enum_types.h"
46 
47 #include "WM_api.h"
48 #include "WM_types.h"
49 
50 #include "ED_object.h"
51 #include "ED_physics.h"
52 #include "ED_screen.h"
53 
54 #include "physics_intern.h"
55 
56 /* ********************************************** */
57 /* Helper API's for RigidBody Constraint Editing */
58 
60 {
62  if (scene == NULL || ID_IS_LINKED(&scene->id) ||
65  return false;
66  }
67 
70  return (ob && ob->rigidbody_constraint);
71  }
72  return false;
73 }
74 
76 {
78  if (scene == NULL || ID_IS_LINKED(&scene->id) ||
81  return false;
82  }
84 }
85 
87  Main *bmain, Scene *scene, Object *ob, int type, ReportList *reports)
88 {
90 
91  /* check that object doesn't already have a constraint */
92  if (ob->rigidbody_constraint) {
94  reports, RPT_INFO, "Object '%s' already has a Rigid Body Constraint", ob->id.name + 2);
95  return false;
96  }
97  /* create constraint group if it doesn't already exits */
98  if (rbw->constraints == NULL) {
99  rbw->constraints = BKE_collection_add(bmain, NULL, "RigidBodyConstraints");
101  }
102  /* make rigidbody constraint settings */
104 
105  /* add constraint to rigid body constraint group */
106  BKE_collection_object_add(bmain, rbw->constraints, ob);
107 
111 
112  return true;
113 }
114 
116 {
117  BKE_rigidbody_remove_constraint(bmain, scene, ob, false);
118 
121 }
122 
123 /* ********************************************** */
124 /* Active Object Add/Remove Operators */
125 
126 /* ************ Add Rigid Body Constraint ************** */
127 
129 {
130  Main *bmain = CTX_data_main(C);
132  ViewLayer *view_layer = CTX_data_view_layer(C);
134  Object *ob = OBACT(view_layer);
135  int type = RNA_enum_get(op->ptr, "type");
136  bool changed;
137 
138  /* sanity checks */
139  if (ELEM(NULL, scene, rbw)) {
140  BKE_report(op->reports, RPT_ERROR, "No Rigid Body World 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);
185  ViewLayer *view_layer = CTX_data_view_layer(C);
186  Object *ob = OBACT(view_layer);
187 
188  /* apply to active object */
189  if (ELEM(NULL, ob, ob->rigidbody_constraint)) {
190  BKE_report(op->reports, RPT_ERROR, "Object has no Rigid Body Constraint to remove");
191  return OPERATOR_CANCELLED;
192  }
194 
195  /* send updates */
197 
198  /* done */
199  return OPERATOR_FINISHED;
200 }
201 
203 {
204  /* identifiers */
205  ot->idname = "RIGIDBODY_OT_constraint_remove";
206  ot->name = "Remove Rigid Body Constraint";
207  ot->description = "Remove Rigid Body Constraint from Object";
208 
209  /* callbacks */
212 
213  /* flags */
215 }
struct Collection * BKE_collection_add(struct Main *bmain, struct Collection *parent, const char *name)
Definition: collection.c:435
bool BKE_collection_object_add(struct Main *bmain, struct Collection *collection, struct Object *ob)
Definition: collection.c:1134
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct ViewLayer * CTX_data_view_layer(const bContext *C)
Definition: context.c:1044
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void id_fake_user_set(struct ID *id)
Definition: lib_id.c:328
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
void BKE_reportf(ReportList *reports, ReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_remove_constraint(struct Main *bmain, struct Scene *scene, struct Object *ob, const bool free_us)
Definition: rigidbody.c:2325
struct RigidBodyWorld * BKE_rigidbody_get_world(struct Scene *scene)
Definition: rigidbody.c:2307
struct RigidBodyCon * BKE_rigidbody_create_constraint(struct Scene *scene, struct Object *ob, short type)
Definition: rigidbody.c:2303
#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:599
@ ID_RECALC_COPY_ON_WRITE
Definition: DNA_ID.h:654
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:426
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:366
_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:39
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define ND_TRANSFORM
Definition: WM_types.h:357
#define NC_OBJECT
Definition: WM_types.h:280
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)
void RIGIDBODY_OT_constraint_add(wmOperatorType *ot)
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
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:3771
const EnumPropertyItem rna_enum_rigidbody_constraint_type_items[]
Definition: rna_rigidbody.c:89
char name[66]
Definition: DNA_ID.h:283
Definition: BKE_main.h:116
struct RigidBodyCon * rigidbody_constraint
struct Collection * constraints
struct RigidBodyWorld * rigidbody_world
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
PropertyRNA * prop
Definition: WM_types.h:814
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition: wm_files.c:3156