Blender V5.0
rigidbody_world.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
13#include "DNA_rigidbody_types.h"
14#include "DNA_scene_types.h"
15
16#ifdef WITH_BULLET
17# include "RBI_api.h"
18#endif
19
20#include "BKE_context.hh"
21#include "BKE_report.hh"
22#include "BKE_rigidbody.h"
23
24#include "DEG_depsgraph.hh"
26
27#include "RNA_access.hh"
28
29#include "WM_api.hh"
30#include "WM_types.hh"
31
32#include "ED_screen.hh"
33
34#include "physics_intern.hh"
35
36/* ********************************************** */
37/* API */
38
39/* check if there is an active rigid body world */
41{
42 Scene *scene = CTX_data_scene(C);
43 return (scene && scene->rigidbody_world);
44}
46{
47 Scene *scene = CTX_data_scene(C);
48 return (scene && scene->rigidbody_world == nullptr);
49}
50
51/* ********************************************** */
52/* OPERATORS - Management */
53
54/* ********** Add RigidBody World **************** */
55
57{
58 Main *bmain = CTX_data_main(C);
59 Scene *scene = CTX_data_scene(C);
60 RigidBodyWorld *rbw;
61
62 rbw = BKE_rigidbody_create_world(scene);
63 // BKE_rigidbody_validate_sim_world(scene, rbw, false);
64 scene->rigidbody_world = rbw;
65
66 /* Full rebuild of DEG! */
69
70 return OPERATOR_FINISHED;
71}
72
74{
75 /* identifiers */
76 ot->idname = "RIGIDBODY_OT_world_add";
77 ot->name = "Add Rigid Body World";
78 ot->description = "Add Rigid Body simulation world to the current scene";
79
80 /* callbacks */
83
84 /* flags */
86}
87
88/* ********** Remove RigidBody World ************* */
89
91{
92 Main *bmain = CTX_data_main(C);
93 Scene *scene = CTX_data_scene(C);
94 RigidBodyWorld *rbw = scene->rigidbody_world;
95
96 /* sanity checks */
97 if (ELEM(nullptr, scene, rbw)) {
98 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to remove");
99 return OPERATOR_CANCELLED;
100 }
101
103
104 /* Full rebuild of DEG! */
107
108 /* done */
109 return OPERATOR_FINISHED;
110}
111
113{
114 /* identifiers */
115 ot->idname = "RIGIDBODY_OT_world_remove";
116 ot->name = "Remove Rigid Body World";
117 ot->description = "Remove Rigid Body simulation world from the current scene";
118
119 /* callbacks */
122
123 /* flags */
125}
126
127/* ********************************************** */
128/* UTILITY OPERATORS */
129
130/* ********** Export RigidBody World ************* */
131
133{
134 Scene *scene = CTX_data_scene(C);
135 RigidBodyWorld *rbw = scene->rigidbody_world;
136 char filepath[FILE_MAX];
137
138 /* sanity checks */
139 if (ELEM(nullptr, scene, rbw)) {
140 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to export");
141 return OPERATOR_CANCELLED;
142 }
143 rbDynamicsWorld *physics_world = BKE_rigidbody_world_physics(rbw);
144 if (physics_world == nullptr) {
146 op->reports, RPT_ERROR, "Rigid Body World has no associated physics data to export");
147 return OPERATOR_CANCELLED;
148 }
149
150 RNA_string_get(op->ptr, "filepath", filepath);
151#ifdef WITH_BULLET
152 RB_dworld_export(physics_world, filepath);
153#endif
154 return OPERATOR_FINISHED;
155}
156
158 wmOperator *op,
159 const wmEvent * /*event*/)
160{
161 if (!RNA_struct_property_is_set(op->ptr, "relative_path")) {
162 RNA_boolean_set(op->ptr, "relative_path", (U.flag & USER_RELPATHS) != 0);
163 }
164
165 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
166 return rigidbody_world_export_exec(C, op);
167 }
168
169 /* TODO: use the actual rigidbody world's name + .bullet instead of this temp crap */
170 RNA_string_set(op->ptr, "filepath", "rigidbodyworld_export.bullet");
172
174}
175
177{
178 /* identifiers */
179 ot->idname = "RIGIDBODY_OT_world_export";
180 ot->name = "Export Rigid Body World";
181 ot->description =
182 "Export Rigid Body world to the simulator's own file-format "
183 "(i.e. '.bullet' for Bullet Physics)";
184
185 /* callbacks */
189
190 /* flags */
192
193 /* properties */
197 FILE_SAVE,
201}
Scene * CTX_data_scene(const bContext *C)
Main * CTX_data_main(const bContext *C)
@ 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.
struct rbDynamicsWorld * BKE_rigidbody_world_physics(struct RigidBodyWorld *rbw)
Definition rigidbody.cc:103
void BKE_rigidbody_free_world(struct Scene *scene)
Definition rigidbody.cc:108
struct RigidBodyWorld * BKE_rigidbody_create_world(struct Scene *scene)
#define FILE_MAX
#define ELEM(...)
void DEG_relations_tag_update(Main *bmain)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
Types and defines for representing Rigid Body entities.
@ FILE_SORT_DEFAULT
@ FILE_SPECIAL
@ FILE_TYPE_FOLDER
@ FILE_DEFAULTDISPLAY
@ USER_RELPATHS
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
@ OPERATOR_RUNNING_MODAL
Rigid Body API for interfacing with external Physics Engines.
#define C
Definition RandGen.cpp:29
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1121
@ FILE_SAVE
Definition WM_api.hh:1134
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define U
DEG_id_tag_update_ex(cb_data->bmain, cb_data->owner_id, ID_RECALC_TAG_FOR_UNDO|ID_RECALC_SYNC_TO_EVAL)
void RB_dworld_export(rbDynamicsWorld *world, const char *filename)
void RIGIDBODY_OT_world_remove(wmOperatorType *ot)
static wmOperatorStatus rigidbody_world_export_invoke(bContext *C, wmOperator *op, const wmEvent *)
static wmOperatorStatus rigidbody_world_add_exec(bContext *C, wmOperator *)
static bool rigidbody_world_active_poll(bContext *C)
static wmOperatorStatus rigidbody_world_remove_exec(bContext *C, wmOperator *op)
void RIGIDBODY_OT_world_export(wmOperatorType *ot)
void RIGIDBODY_OT_world_add(wmOperatorType *ot)
static bool rigidbody_world_add_poll(bContext *C)
static wmOperatorStatus rigidbody_world_export_exec(bContext *C, wmOperator *op)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
std::string RNA_string_get(PointerRNA *ptr, const char *name)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
struct RigidBodyWorld * rigidbody_world
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition wm_files.cc:4237
void WM_operator_properties_filesel(wmOperatorType *ot, const int filter, const short type, const eFileSel_Action action, const eFileSel_Flag flag, const short display, const short sort)