Blender V4.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
10#include <cstdlib>
11#include <cstring>
12
13#include "DNA_object_types.h"
14#include "DNA_rigidbody_types.h"
15#include "DNA_scene_types.h"
16
17#ifdef WITH_BULLET
18# include "RBI_api.h"
19#endif
20
21#include "BKE_context.hh"
22#include "BKE_report.hh"
23#include "BKE_rigidbody.h"
24
25#include "DEG_depsgraph.hh"
27
28#include "RNA_access.hh"
29
30#include "WM_api.hh"
31#include "WM_types.hh"
32
33#include "ED_screen.hh"
34
35#include "physics_intern.hh"
36
37/* ********************************************** */
38/* API */
39
40/* check if there is an active rigid body world */
42{
43 Scene *scene = CTX_data_scene(C);
44 return (scene && scene->rigidbody_world);
45}
47{
48 Scene *scene = CTX_data_scene(C);
49 return (scene && scene->rigidbody_world == nullptr);
50}
51
52/* ********************************************** */
53/* OPERATORS - Management */
54
55/* ********** Add RigidBody World **************** */
56
58{
59 Main *bmain = CTX_data_main(C);
60 Scene *scene = CTX_data_scene(C);
61 RigidBodyWorld *rbw;
62
63 rbw = BKE_rigidbody_create_world(scene);
64 // BKE_rigidbody_validate_sim_world(scene, rbw, false);
65 scene->rigidbody_world = rbw;
66
67 /* Full rebuild of DEG! */
69 DEG_id_tag_update_ex(bmain, &scene->id, ID_RECALC_ANIMATION);
70
71 return OPERATOR_FINISHED;
72}
73
75{
76 /* identifiers */
77 ot->idname = "RIGIDBODY_OT_world_add";
78 ot->name = "Add Rigid Body World";
79 ot->description = "Add Rigid Body simulation world to the current scene";
80
81 /* callbacks */
84
85 /* flags */
87}
88
89/* ********** Remove RigidBody World ************* */
90
92{
93 Main *bmain = CTX_data_main(C);
94 Scene *scene = CTX_data_scene(C);
95 RigidBodyWorld *rbw = scene->rigidbody_world;
96
97 /* sanity checks */
98 if (ELEM(nullptr, scene, rbw)) {
99 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to remove");
100 return OPERATOR_CANCELLED;
101 }
102
104
105 /* Full rebuild of DEG! */
107 DEG_id_tag_update_ex(bmain, &scene->id, ID_RECALC_ANIMATION);
108
109 /* done */
110 return OPERATOR_FINISHED;
111}
112
114{
115 /* identifiers */
116 ot->idname = "RIGIDBODY_OT_world_remove";
117 ot->name = "Remove Rigid Body World";
118 ot->description = "Remove Rigid Body simulation world from the current scene";
119
120 /* callbacks */
123
124 /* flags */
126}
127
128/* ********************************************** */
129/* UTILITY OPERATORS */
130
131/* ********** Export RigidBody World ************* */
132
134{
135 Scene *scene = CTX_data_scene(C);
136 RigidBodyWorld *rbw = scene->rigidbody_world;
137 char filepath[FILE_MAX];
138
139 /* sanity checks */
140 if (ELEM(nullptr, scene, rbw)) {
141 BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to export");
142 return OPERATOR_CANCELLED;
143 }
144 if (rbw->shared->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(static_cast<rbDynamicsWorld *>(rbw->shared->physics_world), filepath);
153#endif
154 return OPERATOR_FINISHED;
155}
156
157static int rigidbody_world_export_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
158{
159 if (!RNA_struct_property_is_set(op->ptr, "relative_path")) {
160 RNA_boolean_set(op->ptr, "relative_path", (U.flag & USER_RELPATHS) != 0);
161 }
162
163 if (RNA_struct_property_is_set(op->ptr, "filepath")) {
164 return rigidbody_world_export_exec(C, op);
165 }
166
167 /* TODO: use the actual rigidbody world's name + .bullet instead of this temp crap */
168 RNA_string_set(op->ptr, "filepath", "rigidbodyworld_export.bullet");
170
172}
173
175{
176 /* identifiers */
177 ot->idname = "RIGIDBODY_OT_world_export";
178 ot->name = "Export Rigid Body World";
179 ot->description =
180 "Export Rigid Body world to the simulator's own fileformat (i.e. '.bullet' for Bullet "
181 "Physics)";
182
183 /* callbacks */
187
188 /* flags */
190
191 /* properties */
195 FILE_SAVE,
199}
Scene * CTX_data_scene(const bContext *C)
Main * CTX_data_main(const bContext *C)
void BKE_report(ReportList *reports, eReportType type, const char *message)
Definition report.cc:125
API for Blender-side Rigid Body stuff.
void BKE_rigidbody_free_world(struct Scene *scene)
Definition rigidbody.cc:89
struct RigidBodyWorld * BKE_rigidbody_create_world(struct Scene *scene)
#define FILE_MAX
#define ELEM(...)
void DEG_id_tag_update_ex(Main *bmain, ID *id, unsigned int flags)
void DEG_relations_tag_update(Main *bmain)
@ ID_RECALC_ANIMATION
Definition DNA_ID.h:1044
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_DEFAULTDISPLAY
@ USER_RELPATHS
@ OPERATOR_RUNNING_MODAL
Rigid Body API for interfacing with external Physics Engines.
@ WM_FILESEL_RELPATH
Definition WM_api.hh:933
@ FILE_SAVE
Definition WM_api.hh:946
@ OPTYPE_UNDO
Definition WM_types.hh:162
@ OPTYPE_REGISTER
Definition WM_types.hh:160
unsigned int U
Definition btGjkEpa3.h:78
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 *)
static bool ED_rigidbody_world_active_poll(bContext *C)
static int rigidbody_world_export_exec(bContext *C, wmOperator *op)
static int rigidbody_world_export_invoke(bContext *C, wmOperator *op, const wmEvent *)
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 bool ED_rigidbody_world_add_poll(bContext *C)
void RNA_string_set(PointerRNA *ptr, const char *name, const char *value)
void RNA_boolean_set(PointerRNA *ptr, const char *name, bool value)
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
bool RNA_struct_property_is_set(PointerRNA *ptr, const char *identifier)
struct RigidBodyWorld_Shared * shared
const char * name
Definition WM_types.hh:990
bool(* poll)(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1042
const char * idname
Definition WM_types.hh:992
int(* invoke)(bContext *C, wmOperator *op, const wmEvent *event) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1022
int(* exec)(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1006
const char * description
Definition WM_types.hh:996
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_fileselect(bContext *C, wmOperator *op)
wmOperatorType * ot
Definition wm_files.cc:4125
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)