Blender  V2.93
rigidbody_world.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_object_types.h"
29 #include "DNA_rigidbody_types.h"
30 #include "DNA_scene_types.h"
31 
32 #ifdef WITH_BULLET
33 # include "RBI_api.h"
34 #endif
35 
36 #include "BKE_context.h"
37 #include "BKE_main.h"
38 #include "BKE_report.h"
39 #include "BKE_rigidbody.h"
40 
41 #include "DEG_depsgraph.h"
42 #include "DEG_depsgraph_build.h"
43 
44 #include "RNA_access.h"
45 
46 #include "WM_api.h"
47 #include "WM_types.h"
48 
49 #include "ED_screen.h"
50 
51 #include "physics_intern.h"
52 
53 /* ********************************************** */
54 /* API */
55 
56 /* check if there is an active rigid body world */
58 {
60  return (scene && scene->rigidbody_world);
61 }
63 {
65  return (scene && scene->rigidbody_world == NULL);
66 }
67 
68 /* ********************************************** */
69 /* OPERATORS - Management */
70 
71 /* ********** Add RigidBody World **************** */
72 
74 {
75  Main *bmain = CTX_data_main(C);
77  RigidBodyWorld *rbw;
78 
80  // BKE_rigidbody_validate_sim_world(scene, rbw, false);
81  scene->rigidbody_world = rbw;
82 
83  /* Full rebuild of DEG! */
86 
87  return OPERATOR_FINISHED;
88 }
89 
91 {
92  /* identifiers */
93  ot->idname = "RIGIDBODY_OT_world_add";
94  ot->name = "Add Rigid Body World";
95  ot->description = "Add Rigid Body simulation world to the current scene";
96 
97  /* callbacks */
100 
101  /* flags */
103 }
104 
105 /* ********** Remove RigidBody World ************* */
106 
108 {
109  Main *bmain = CTX_data_main(C);
112 
113  /* sanity checks */
114  if (ELEM(NULL, scene, rbw)) {
115  BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to remove");
116  return OPERATOR_CANCELLED;
117  }
118 
120 
121  /* Full rebuild of DEG! */
124 
125  /* done */
126  return OPERATOR_FINISHED;
127 }
128 
130 {
131  /* identifiers */
132  ot->idname = "RIGIDBODY_OT_world_remove";
133  ot->name = "Remove Rigid Body World";
134  ot->description = "Remove Rigid Body simulation world from the current scene";
135 
136  /* callbacks */
139 
140  /* flags */
142 }
143 
144 /* ********************************************** */
145 /* UTILITY OPERATORS */
146 
147 /* ********** Export RigidBody World ************* */
148 
150 {
153  char path[FILE_MAX];
154 
155  /* sanity checks */
156  if (ELEM(NULL, scene, rbw)) {
157  BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to export");
158  return OPERATOR_CANCELLED;
159  }
160  if (rbw->shared->physics_world == NULL) {
161  BKE_report(
162  op->reports, RPT_ERROR, "Rigid Body World has no associated physics data to export");
163  return OPERATOR_CANCELLED;
164  }
165 
166  RNA_string_get(op->ptr, "filepath", path);
167 #ifdef WITH_BULLET
169 #endif
170  return OPERATOR_FINISHED;
171 }
172 
174 {
175  if (!RNA_struct_property_is_set(op->ptr, "relative_path")) {
176  RNA_boolean_set(op->ptr, "relative_path", (U.flag & USER_RELPATHS) != 0);
177  }
178 
179  if (RNA_struct_property_is_set(op->ptr, "filepath")) {
180  return rigidbody_world_export_exec(C, op);
181  }
182 
183  /* TODO: use the actual rigidbody world's name + .bullet instead of this temp crap */
184  RNA_string_set(op->ptr, "filepath", "rigidbodyworld_export.bullet");
186 
187  return OPERATOR_RUNNING_MODAL;
188 }
189 
191 {
192  /* identifiers */
193  ot->idname = "RIGIDBODY_OT_world_export";
194  ot->name = "Export Rigid Body World";
195  ot->description =
196  "Export Rigid Body world to simulator's own fileformat (i.e. '.bullet' for Bullet Physics)";
197 
198  /* callbacks */
202 
203  /* flags */
205 
206  /* properties */
209  FILE_SPECIAL,
210  FILE_SAVE,
211  FILE_RELPATH,
214 }
struct Scene * CTX_data_scene(const bContext *C)
Definition: context.c:1034
struct Main * CTX_data_main(const bContext *C)
Definition: context.c:1018
void BKE_report(ReportList *reports, ReportType type, const char *message)
Definition: report.c:104
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_free_world(struct Scene *scene)
Definition: rigidbody.c:107
struct RigidBodyWorld * BKE_rigidbody_create_world(struct Scene *scene)
Definition: rigidbody.c:2283
#define FILE_MAX
#define UNUSED(x)
#define ELEM(...)
void DEG_id_tag_update_ex(struct Main *bmain, struct ID *id, int flag)
void DEG_relations_tag_update(struct Main *bmain)
@ ID_RECALC_ANIMATION
Definition: DNA_ID.h:614
Object is a sort of wrapper for general info.
Types and defines for representing Rigid Body entities.
@ FILE_SORT_DEFAULT
@ FILE_SPECIAL
@ FILE_TYPE_FOLDER
@ FILE_SAVE
@ FILE_DEFAULTDISPLAY
@ FILE_RELPATH
@ USER_RELPATHS
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
Rigid Body API for interfacing with external Physics Engines.
#define C
Definition: RandGen.cpp:39
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
unsigned int U
Definition: btGjkEpa3.h:78
Scene scene
void RB_dworld_export(rbDynamicsWorld *world, const char *filename)
void RIGIDBODY_OT_world_remove(wmOperatorType *ot)
static int rigidbody_world_add_exec(bContext *C, wmOperator *UNUSED(op))
static bool ED_rigidbody_world_active_poll(bContext *C)
static int rigidbody_world_export_exec(bContext *C, wmOperator *op)
static int rigidbody_world_remove_exec(bContext *C, wmOperator *op)
void RIGIDBODY_OT_world_export(wmOperatorType *ot)
void RIGIDBODY_OT_world_add(wmOperatorType *ot)
static int rigidbody_world_export_invoke(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))
static bool ED_rigidbody_world_add_poll(bContext *C)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
Definition: rna_access.c:6550
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
Definition: rna_access.c:6272
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
Definition: rna_access.c:6685
Definition: BKE_main.h:116
struct RigidBodyWorld_Shared * shared
struct RigidBodyWorld * rigidbody_world
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
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
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition: wm_files.c:3156
void WM_operator_properties_filesel(wmOperatorType *ot, int filter, short type, short action, short flag, short display, short sort)