Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/physics/rigidbody_object.c
| Show First 20 Lines • Show All 44 Lines • ▼ Show 20 Lines | |||||
| #include "BKE_context.h" | #include "BKE_context.h" | ||||
| #include "BKE_group.h" | #include "BKE_group.h" | ||||
| #include "BKE_main.h" | #include "BKE_main.h" | ||||
| #include "BKE_report.h" | #include "BKE_report.h" | ||||
| #include "BKE_rigidbody.h" | #include "BKE_rigidbody.h" | ||||
| #include "DEG_depsgraph.h" | #include "DEG_depsgraph.h" | ||||
| #include "DEG_depsgraph_build.h" | #include "DEG_depsgraph_build.h" | ||||
| #include "DEG_depsgraph_query.h" | |||||
| #include "RNA_access.h" | #include "RNA_access.h" | ||||
| #include "RNA_define.h" | #include "RNA_define.h" | ||||
| #include "RNA_enum_types.h" | #include "RNA_enum_types.h" | ||||
| #include "WM_api.h" | #include "WM_api.h" | ||||
| #include "WM_types.h" | #include "WM_types.h" | ||||
| Show All 23 Lines | if (ED_operator_object_active_editable(C)) { | ||||
| return (ob && ob->type == OB_MESH); | return (ob && ob->type == OB_MESH); | ||||
| } | } | ||||
| else | else | ||||
| return 0; | return 0; | ||||
| } | } | ||||
| /* ----------------- */ | /* ----------------- */ | ||||
| bool ED_rigidbody_object_add(Main *bmain, Scene *scene, Object *ob, int type, ReportList *reports) | bool ED_rigidbody_object_add(Depsgraph *depsgraph, Main *bmain, Scene *scene, Object *ob, int type, ReportList *reports) | ||||
| { | { | ||||
| RigidBodyWorld *rbw = BKE_rigidbody_get_world(scene); | RigidBodyWorld *rbw = BKE_rigidbody_get_world(scene); | ||||
| if (ob->type != OB_MESH) { | if (ob->type != OB_MESH) { | ||||
| BKE_report(reports, RPT_ERROR, "Can't add Rigid Body to non mesh object"); | BKE_report(reports, RPT_ERROR, "Can't add Rigid Body to non mesh object"); | ||||
| return false; | return false; | ||||
| } | } | ||||
| /* Add rigid body world and group if they don't exist for convenience */ | /* Add rigid body world and group if they don't exist for convenience */ | ||||
| if (rbw == NULL) { | if (rbw == NULL) { | ||||
| rbw = BKE_rigidbody_create_world(scene); | rbw = BKE_rigidbody_create_world(scene); | ||||
| if (rbw == NULL) { | if (rbw == NULL) { | ||||
| BKE_report(reports, RPT_ERROR, "Can't create Rigid Body world"); | BKE_report(reports, RPT_ERROR, "Can't create Rigid Body world"); | ||||
| return false; | return false; | ||||
| } | } | ||||
| BKE_rigidbody_validate_sim_world(scene, rbw, false); | BKE_rigidbody_validate_sim_world(scene, rbw, false); | ||||
| scene->rigidbody_world = rbw; | scene->rigidbody_world = rbw; | ||||
| } | } | ||||
| if (rbw->group == NULL) { | if (rbw->group == NULL) { | ||||
| rbw->group = BKE_group_add(bmain, "RigidBodyWorld"); | rbw->group = BKE_group_add(bmain, "RigidBodyWorld"); | ||||
| } | } | ||||
| /* make rigidbody object settings */ | /* make rigidbody object settings */ | ||||
| if (ob->rigidbody_object == NULL) { | if (ob->rigidbody_object == NULL) { | ||||
| ob->rigidbody_object = BKE_rigidbody_create_object(scene, ob, type); | ob->rigidbody_object = BKE_rigidbody_create_object(depsgraph, scene, ob, type); | ||||
| } | } | ||||
| ob->rigidbody_object->type = type; | ob->rigidbody_object->type = type; | ||||
| ob->rigidbody_object->flag |= RBO_FLAG_NEEDS_VALIDATE; | ob->rigidbody_object->flag |= RBO_FLAG_NEEDS_VALIDATE; | ||||
| /* add object to rigid body group */ | /* add object to rigid body group */ | ||||
| BKE_group_object_add(rbw->group, ob); | BKE_group_object_add(rbw->group, ob); | ||||
| DEG_relations_tag_update(bmain); | DEG_relations_tag_update(bmain); | ||||
| Show All 16 Lines | |||||
| /* ********************************************** */ | /* ********************************************** */ | ||||
| /* Active Object Add/Remove Operators */ | /* Active Object Add/Remove Operators */ | ||||
| /* ************ Add Rigid Body ************** */ | /* ************ Add Rigid Body ************** */ | ||||
| static int rigidbody_object_add_exec(bContext *C, wmOperator *op) | static int rigidbody_object_add_exec(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| Depsgraph *depsgraph = CTX_data_depsgraph(C); | |||||
| Main *bmain = CTX_data_main(C); | Main *bmain = CTX_data_main(C); | ||||
| Scene *scene = CTX_data_scene(C); | Scene *scene = CTX_data_scene(C); | ||||
| Object *ob = ED_object_active_context(C); | Object *ob = ED_object_active_context(C); | ||||
| int type = RNA_enum_get(op->ptr, "type"); | int type = RNA_enum_get(op->ptr, "type"); | ||||
| bool changed; | bool changed; | ||||
| /* apply to active object */ | /* apply to active object */ | ||||
| changed = ED_rigidbody_object_add(bmain, scene, ob, type, op->reports); | changed = ED_rigidbody_object_add(depsgraph, bmain, scene, ob, type, op->reports); | ||||
| if (changed) { | if (changed) { | ||||
| /* send updates */ | /* send updates */ | ||||
| WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL); | WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL); | ||||
| WM_event_add_notifier(C, NC_OBJECT | ND_POINTCACHE, NULL); | WM_event_add_notifier(C, NC_OBJECT | ND_POINTCACHE, NULL); | ||||
| /* done */ | /* done */ | ||||
| return OPERATOR_FINISHED; | return OPERATOR_FINISHED; | ||||
| ▲ Show 20 Lines • Show All 67 Lines • ▼ Show 20 Lines | |||||
| /* ********************************************** */ | /* ********************************************** */ | ||||
| /* Selected Object Add/Remove Operators */ | /* Selected Object Add/Remove Operators */ | ||||
| /* ************ Add Rigid Bodies ************** */ | /* ************ Add Rigid Bodies ************** */ | ||||
| static int rigidbody_objects_add_exec(bContext *C, wmOperator *op) | static int rigidbody_objects_add_exec(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| Depsgraph *depsgraph = CTX_data_depsgraph(C); | |||||
| Main *bmain = CTX_data_main(C); | Main *bmain = CTX_data_main(C); | ||||
| Scene *scene = CTX_data_scene(C); | Scene *scene = CTX_data_scene(C); | ||||
| int type = RNA_enum_get(op->ptr, "type"); | int type = RNA_enum_get(op->ptr, "type"); | ||||
| bool changed = false; | bool changed = false; | ||||
| /* create rigid body objects and add them to the world's group */ | /* create rigid body objects and add them to the world's group */ | ||||
| CTX_DATA_BEGIN(C, Object *, ob, selected_objects) { | CTX_DATA_BEGIN(C, Object *, ob, selected_objects) { | ||||
| changed |= ED_rigidbody_object_add(bmain, scene, ob, type, op->reports); | changed |= ED_rigidbody_object_add(depsgraph, bmain, scene, ob, type, op->reports); | ||||
| } | } | ||||
| CTX_DATA_END; | CTX_DATA_END; | ||||
| if (changed) { | if (changed) { | ||||
| /* send updates */ | /* send updates */ | ||||
| WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL); | WM_event_add_notifier(C, NC_OBJECT | ND_TRANSFORM, NULL); | ||||
| WM_event_add_notifier(C, NC_OBJECT | ND_POINTCACHE, NULL); | WM_event_add_notifier(C, NC_OBJECT | ND_POINTCACHE, NULL); | ||||
| ▲ Show 20 Lines • Show All 229 Lines • ▼ Show 20 Lines | static const EnumPropertyItem *rigidbody_materials_itemf(bContext *UNUSED(C), PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free) | ||||
| return item; | return item; | ||||
| } | } | ||||
| /* ------------------------------------------ */ | /* ------------------------------------------ */ | ||||
| static int rigidbody_objects_calc_mass_exec(bContext *C, wmOperator *op) | static int rigidbody_objects_calc_mass_exec(bContext *C, wmOperator *op) | ||||
| { | { | ||||
| Depsgraph *depsgraph = CTX_data_depsgraph(C); | |||||
| int material = RNA_enum_get(op->ptr, "material"); | int material = RNA_enum_get(op->ptr, "material"); | ||||
| float density; | float density; | ||||
| bool changed = false; | bool changed = false; | ||||
| /* get density (kg/m^3) to apply */ | /* get density (kg/m^3) to apply */ | ||||
| if (material >= 0) { | if (material >= 0) { | ||||
| /* get density from table, and store in props for later repeating */ | /* get density from table, and store in props for later repeating */ | ||||
| if (material >= NUM_RB_MATERIAL_PRESETS) | if (material >= NUM_RB_MATERIAL_PRESETS) | ||||
| material = 0; | material = 0; | ||||
| density = RB_MATERIAL_DENSITY_TABLE[material].density; | density = RB_MATERIAL_DENSITY_TABLE[material].density; | ||||
| RNA_float_set(op->ptr, "density", density); | RNA_float_set(op->ptr, "density", density); | ||||
| } | } | ||||
| else { | else { | ||||
| /* custom - grab from whatever value is set */ | /* custom - grab from whatever value is set */ | ||||
| density = RNA_float_get(op->ptr, "density"); | density = RNA_float_get(op->ptr, "density"); | ||||
| } | } | ||||
| /* apply this to all selected objects (with rigidbodies)... */ | /* apply this to all selected objects (with rigidbodies)... */ | ||||
| CTX_DATA_BEGIN(C, Object *, ob, selected_objects) | CTX_DATA_BEGIN(C, Object *, ob, selected_objects) | ||||
| { | { | ||||
| if (ob->rigidbody_object) { | if (ob->rigidbody_object) { | ||||
| Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob); | |||||
| PointerRNA ptr; | PointerRNA ptr; | ||||
| float volume; /* m^3 */ | float volume; /* m^3 */ | ||||
| float mass; /* kg */ | float mass; /* kg */ | ||||
| /* mass is calculated from the approximate volume of the object, | /* mass is calculated from the approximate volume of the object, | ||||
| * and the density of the material we're simulating | * and the density of the material we're simulating | ||||
| */ | */ | ||||
| BKE_rigidbody_calc_volume(ob, &volume); | BKE_rigidbody_calc_volume(ob_eval, &volume); | ||||
| mass = volume * density; | mass = volume * density; | ||||
| /* use RNA-system to change the property and perform all necessary changes */ | /* use RNA-system to change the property and perform all necessary changes */ | ||||
| RNA_pointer_create(&ob->id, &RNA_RigidBodyObject, ob->rigidbody_object, &ptr); | RNA_pointer_create(&ob->id, &RNA_RigidBodyObject, ob->rigidbody_object, &ptr); | ||||
| RNA_float_set(&ptr, "mass", mass); | RNA_float_set(&ptr, "mass", mass); | ||||
| DEG_id_tag_update(&ob->id, OB_RECALC_OB); | DEG_id_tag_update(&ob->id, OB_RECALC_OB); | ||||
| ▲ Show 20 Lines • Show All 49 Lines • Show Last 20 Lines | |||||