Blender V5.3
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
36namespace blender {
37
38/* ********************************************** */
39/* API */
40
41/* check if there is an active rigid body world */
43{
44 Scene *scene = CTX_data_scene(C);
45 return (scene && scene->rigidbody_world);
46}
48{
49 Scene *scene = CTX_data_scene(C);
50 return (scene && scene->rigidbody_world == nullptr);
51}
52
53/* ********************************************** */
54/* OPERATORS - Management */
55
56/* ********** Add RigidBody World **************** */
57
59{
60 Main *bmain = CTX_data_main(C);
61 Scene *scene = CTX_data_scene(C);
62 RigidBodyWorld *rbw;
63
64 rbw = BKE_rigidbody_create_world(scene);
65 // BKE_rigidbody_validate_sim_world(scene, rbw, false);
66 scene->rigidbody_world = rbw;
67
68 /* Full rebuild of DEG! */
71
72 return OPERATOR_FINISHED;
73}
74
76{
77 /* identifiers */
78 ot->idname = "RIGIDBODY_OT_world_add";
79 ot->name = "Add Rigid Body World";
80 ot->description = "Add Rigid Body simulation world to the current scene";
81
82 /* callbacks */
85
86 /* flags */
88}
89
90/* ********** Remove RigidBody World ************* */
91
93{
94 Main *bmain = CTX_data_main(C);
95 Scene *scene = CTX_data_scene(C);
96 RigidBodyWorld *rbw = scene->rigidbody_world;
97
98 /* sanity checks */
99 if (ELEM(nullptr, scene, rbw)) {
100 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to remove");
101 return OPERATOR_CANCELLED;
102 }
103
105
106 /* Full rebuild of DEG! */
109
110 /* done */
111 return OPERATOR_FINISHED;
112}
113
115{
116 /* identifiers */
117 ot->idname = "RIGIDBODY_OT_world_remove";
118 ot->name = "Remove Rigid Body World";
119 ot->description = "Remove Rigid Body simulation world from the current scene";
120
121 /* callbacks */
124
125 /* flags */
127}
128
129/* ********************************************** */
130/* UTILITY OPERATORS */
131
132/* ********** Export RigidBody World ************* */
133
135{
136 Scene *scene = CTX_data_scene(C);
137 RigidBodyWorld *rbw = scene->rigidbody_world;
138 char filepath[FILE_MAX];
139
140 /* sanity checks */
141 if (ELEM(nullptr, scene, rbw)) {
142 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to export");
143 return OPERATOR_CANCELLED;
144 }
145 rbDynamicsWorld *physics_world = BKE_rigidbody_world_physics(rbw);
146 if (physics_world == nullptr) {
148 op->reports, RPT_ERROR, "Rigid Body World has no associated physics data to export");
149 return OPERATOR_CANCELLED;
150 }
151
152 RNA_string_get(op->ptr, "filepath", filepath);
153#ifdef WITH_BULLET
154 RB_dworld_export(physics_world, filepath);
155#endif
156 return OPERATOR_FINISHED;
157}
158
160 wmOperator *op,
161 const wmEvent * /*event*/)
162{
163 if (!RNA_struct_property_is_set(op->ptr, "relative_path")) {
164 RNA_boolean_set(op->ptr, "relative_path", (U.flag & USER_RELPATHS) != 0);
165 }
166
167 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
168 return rigidbody_world_export_exec(C, op);
169 }
170
171 /* TODO: use the actual rigidbody world's name + .bullet instead of this temp crap */
172 RNA_string_set(op->ptr, "filepath", "rigidbodyworld_export.bullet");
174
176}
177
179{
180 /* identifiers */
181 ot->idname = "RIGIDBODY_OT_world_export";
182 ot->name = "Export Rigid Body World";
183 ot->description =
184 "Export Rigid Body world to the simulator's own file-format "
185 "(i.e. '.bullet' for Bullet Physics)";
186
187 /* callbacks */
191
192 /* flags */
194
195 /* properties */
199 FILE_SAVE,
203}
204
205} // namespace blender
API for Blender-side Rigid Body stuff.
#define FILE_MAX
Types and defines for representing Rigid Body entities.
Rigid Body API for interfacing with external Physics Engines.
#define C
Definition RandGen.cpp:29
#define ELEM(...)
BMesh Iterators.
UserDef U
Definition blender.cc:58
void RIGIDBODY_OT_world_add(wmOperatorType *ot)
rbDynamicsWorld * BKE_rigidbody_world_physics(struct RigidBodyWorld *rbw)
Definition rigidbody.cc:105
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
static wmOperatorStatus rigidbody_world_add_exec(bContext *C, wmOperator *)
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)
static wmOperatorStatus rigidbody_world_remove_exec(bContext *C, wmOperator *op)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
static wmOperatorStatus rigidbody_world_export_invoke(bContext *C, wmOperator *op, const wmEvent *)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:162
@ WM_FILESEL_RELPATH
Definition WM_api.hh:1329
static wmOperatorStatus rigidbody_world_export_exec(bContext *C, wmOperator *op)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
static bool rigidbody_world_add_poll(bContext *C)
void RIGIDBODY_OT_world_remove(wmOperatorType *ot)
static bool rigidbody_world_active_poll(bContext *C)
struct RigidBodyWorld * BKE_rigidbody_create_world(struct Scene *scene)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
@ FILE_SAVE
Definition WM_api.hh:1342
void BKE_rigidbody_free_world(struct Scene *scene)
Definition rigidbody.cc:110
Main * CTX_data_main(const bContext *C)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1077
void DEG_relations_tag_update(Main *bmain)
Scene * CTX_data_scene(const bContext *C)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
std::string RNA_string_get(PointerRNA *ptr, const char *name)
void WM_event_add_fileselect(bContext *C, wmOperator *op)
void RIGIDBODY_OT_world_export(wmOperatorType *ot)
void RB_dworld_export(rbDynamicsWorld *world, const char *filename)
struct RigidBodyWorld * rigidbody_world
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
wmOperatorStatus(*) invoke(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1141
struct ReportList * reports