Changeset View
Changeset View
Standalone View
Standalone View
source/blender/editors/physics/rigidbody_object.c
| Show First 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | |||||
| #include "BKE_context.h" | #include "BKE_context.h" | ||||
| #include "BKE_library.h" | #include "BKE_library.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 20 Lines • Show All 423 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) | ||||
| Show All 14 Lines | if (ob->rigidbody_object) { | ||||
| 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); | Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob); | ||||
| 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, ID_RECALC_TRANSFORM); | DEG_id_tag_update(&ob->id, ID_RECALC_TRANSFORM); | ||||
| Show All 24 Lines | void RIGIDBODY_OT_mass_calculate(wmOperatorType *ot) | ||||
| ot->description = "Automatically calculate mass values for Rigid Body Objects based on volume"; | ot->description = "Automatically calculate mass values for Rigid Body Objects based on volume"; | ||||
| /* callbacks */ | /* callbacks */ | ||||
| ot->invoke = WM_menu_invoke; // XXX | ot->invoke = WM_menu_invoke; // XXX | ||||
| ot->exec = rigidbody_objects_calc_mass_exec; | ot->exec = rigidbody_objects_calc_mass_exec; | ||||
| ot->poll = ED_operator_scene_editable; | ot->poll = ED_operator_scene_editable; | ||||
| /* flags */ | /* flags */ | ||||
| ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO; | ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_USE_EVAL_DATA; | ||||
| /* properties */ | /* properties */ | ||||
| ot->prop = prop = RNA_def_enum(ot->srna, "material", | ot->prop = prop = RNA_def_enum(ot->srna, "material", | ||||
| DummyRNA_DEFAULT_items, 0, | DummyRNA_DEFAULT_items, 0, | ||||
| "Material Preset", | "Material Preset", | ||||
| "Type of material that objects are made of (determines material density)"); | "Type of material that objects are made of (determines material density)"); | ||||
| RNA_def_enum_funcs(prop, rigidbody_materials_itemf); | RNA_def_enum_funcs(prop, rigidbody_materials_itemf); | ||||
| RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE); | RNA_def_property_flag(prop, PROP_ENUM_NO_TRANSLATE); | ||||
| RNA_def_float(ot->srna, "density", 1.0, FLT_MIN, FLT_MAX, | RNA_def_float(ot->srna, "density", 1.0, FLT_MIN, FLT_MAX, | ||||
| "Density", | "Density", | ||||
| "Custom density value (kg/m^3) to use instead of material preset", | "Custom density value (kg/m^3) to use instead of material preset", | ||||
| 1.0f, 2500.0f); | 1.0f, 2500.0f); | ||||
| } | } | ||||
| /* ********************************************** */ | /* ********************************************** */ | ||||