Welcome to mirror list, hosted at ThFree Co, Russian Federation.

rigidbody_constraint.c « physics « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cb7ca5bd5d1e8d9292d58f858136f7ecc8567193 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2013 Blender Foundation
 * All rights reserved.
 */

/** \file
 * \ingroup editor_physics
 * \brief Rigid Body constraint editing operators
 */

#include <stdlib.h>
#include <string.h>

#include "DNA_collection_types.h"
#include "DNA_object_types.h"
#include "DNA_rigidbody_types.h"
#include "DNA_scene_types.h"

#include "BKE_collection.h"
#include "BKE_context.h"
#include "BKE_lib_id.h"
#include "BKE_main.h"
#include "BKE_report.h"
#include "BKE_rigidbody.h"

#include "DEG_depsgraph.h"
#include "DEG_depsgraph_build.h"

#include "RNA_access.h"
#include "RNA_define.h"
#include "RNA_enum_types.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_object.h"
#include "ED_physics.h"
#include "ED_screen.h"

#include "physics_intern.h"

/* ********************************************** */
/* Helper API's for RigidBody Constraint Editing */

static bool ED_operator_rigidbody_con_active_poll(bContext *C)
{
  Scene *scene = CTX_data_scene(C);
  if (scene == NULL || ID_IS_LINKED(&scene->id) ||
      (scene->rigidbody_world != NULL && scene->rigidbody_world->constraints != NULL &&
       ID_IS_LINKED(&scene->rigidbody_world->constraints->id))) {
    return false;
  }

  if (ED_operator_object_active_editable(C)) {
    Object *ob = ED_object_active_context(C);
    return (ob && ob->rigidbody_constraint);
  }
  return false;
}

static bool ED_operator_rigidbody_con_add_poll(bContext *C)
{
  Scene *scene = CTX_data_scene(C);
  if (scene == NULL || ID_IS_LINKED(&scene->id) ||
      (scene->rigidbody_world != NULL && scene->rigidbody_world->constraints != NULL &&
       ID_IS_LINKED(&scene->rigidbody_world->constraints->id))) {
    return false;
  }
  return ED_operator_object_active_editable(C);
}

bool ED_rigidbody_constraint_add(
    Main *bmain, Scene *scene, Object *ob, int type, ReportList *reports)
{
  RigidBodyWorld *rbw = BKE_rigidbody_get_world(scene);

  /* check that object doesn't already have a constraint */
  if (ob->rigidbody_constraint) {
    BKE_reportf(
        reports, RPT_INFO, "Object '%s' already has a Rigid Body Constraint", ob->id.name + 2);
    return false;
  }
  /* create constraint group if it doesn't already exits */
  if (rbw->constraints == NULL) {
    rbw->constraints = BKE_collection_add(bmain, NULL, "RigidBodyConstraints");
    id_fake_user_set(&rbw->constraints->id);
  }
  /* make rigidbody constraint settings */
  ob->rigidbody_constraint = BKE_rigidbody_create_constraint(scene, ob, type);

  /* add constraint to rigid body constraint group */
  BKE_collection_object_add(bmain, rbw->constraints, ob);

  DEG_relations_tag_update(bmain);
  DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
  DEG_id_tag_update(&rbw->constraints->id, ID_RECALC_COPY_ON_WRITE);

  return true;
}

void ED_rigidbody_constraint_remove(Main *bmain, Scene *scene, Object *ob)
{
  BKE_rigidbody_remove_constraint(bmain, scene, ob, false);

  DEG_relations_tag_update(bmain);
  DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM);
}

/* ********************************************** */
/* Active Object Add/Remove Operators */

/* ************ Add Rigid Body Constraint ************** */

static int rigidbody_con_add_exec(bContext *C, wmOperator *op)
{
  Main *bmain = CTX_data_main(C);
  Scene *scene = CTX_data_scene(C);
  ViewLayer *view_layer = CTX_data_view_layer(C);
  RigidBodyWorld *rbw = BKE_rigidbody_get_world(scene);
  Object *ob = OBACT(view_layer);
  int type = RNA_enum_get(op->ptr, "type");
  bool changed;

  /* sanity checks */
  if (ELEM(NULL, scene, rbw)) {
    BKE_report(op->reports, RPT_ERROR, "No Rigid Body World to add Rigid Body Constraint to");
    return OPERATOR_CANCELLED;
  }
  /* apply to active object */
  changed = ED_rigidbody_constraint_add(bmain, scene, ob, type, op->reports);

  if (changed) {
    /* send updates */
    WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);

    /* done */
    return OPERATOR_FINISHED;
  }
  return OPERATOR_CANCELLED;
}

void RIGIDBODY_OT_constraint_add(wmOperatorType *ot)
{
  /* identifiers */
  ot->idname = "RIGIDBODY_OT_constraint_add";
  ot->name = "Add Rigid Body Constraint";
  ot->description = "Add Rigid Body Constraint to active object";

  /* callbacks */
  ot->exec = rigidbody_con_add_exec;
  ot->poll = ED_operator_rigidbody_con_add_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;

  /* properties */
  ot->prop = RNA_def_enum(ot->srna,
                          "type",
                          rna_enum_rigidbody_constraint_type_items,
                          RBC_TYPE_FIXED,
                          "Rigid Body Constraint Type",
                          "");
}

/* ************ Remove Rigid Body Constraint ************** */

static int rigidbody_con_remove_exec(bContext *C, wmOperator *op)
{
  Main *bmain = CTX_data_main(C);
  Scene *scene = CTX_data_scene(C);
  ViewLayer *view_layer = CTX_data_view_layer(C);
  Object *ob = OBACT(view_layer);

  /* apply to active object */
  if (ELEM(NULL, ob, ob->rigidbody_constraint)) {
    BKE_report(op->reports, RPT_ERROR, "Object has no Rigid Body Constraint to remove");
    return OPERATOR_CANCELLED;
  }
  ED_rigidbody_constraint_remove(bmain, scene, ob);

  /* send updates */
  WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL);

  /* done */
  return OPERATOR_FINISHED;
}

void RIGIDBODY_OT_constraint_remove(wmOperatorType *ot)
{
  /* identifiers */
  ot->idname = "RIGIDBODY_OT_constraint_remove";
  ot->name = "Remove Rigid Body Constraint";
  ot->description = "Remove Rigid Body Constraint from Object";

  /* callbacks */
  ot->exec = rigidbody_con_remove_exec;
  ot->poll = ED_operator_rigidbody_con_active_poll;

  /* flags */
  ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}