Files
test/source/blender/editors/object/object_modifier.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

4301 lines
136 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup edobj
2018-09-02 18:28:27 +10:00
*/
2011-02-27 20:29:51 +00:00
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include "CLG_log.h"
#include "MEM_guardedalloc.h"
#include "DNA_anim_types.h"
#include "DNA_armature_types.h"
Geometry Nodes: support baking data block references With this patch, materials are kept intact in simulation zones and bake nodes without any additional user action. This implements the design proposed in #108410 to support referencing data-blocks (only materials for now) in the baked data. The task also describes why this is not a trivial issue. A previous attempt was implemented in #109703 but it didn't work well-enough. The solution is to have an explicit `name (+ library name) -> data-block` mapping that is stored in the modifier for each bake node and simulation zone. The `library name` is necessary for it to be unique within a .blend file. Note that this refers to the name of the `Library` data-block and not a file path. The baked data only contains the names of the used data-blocks. When the baked data is loaded, the correct material data-block is looked up from the mapping. ### Automatic Mapping Generation The most tricky aspect of this approach is to make it feel mostly automatic. From the user point-of-view, it should just work. Therefore, we don't want the user to have to create the mapping manually in the majority of cases. Creating the mapping automatically is difficult because the data-blocks that should become part of the mapping are only known during depsgraph evaluation. So we somehow have to gather the missing data blocks during evaluation and then write the new mappings back to the original data. While writing back to original data is something we do in some cases already, the situation here is different, because we are actually creating new relations between data-blocks. This also means that we'll have to do user-counting. Since user counts in data-blocks are *not* atomic, we can't do that from multiple threads at the same time. Also, under some circumstances, it may be necessary to trigger depsgraph evaluation again after the write-back because it actually affects the result. To solve this, a small new API is added in `DEG_depsgraph_writeback_sync.hh`. It allows gathering tasks which write back to original data in a synchronous way which may also require a reevaluation. ### Accessing the Mapping A new `BakeDataBlockMap` is passed to geometry nodes evaluation by the modifier. This map allows getting the `ID` pointer that should be used for a specific data-block name that is stored in baked data. It's also used to gather all the missing data mappings during evaluation. ### Weak ID References The baked/cached geometries may have references to other data-blocks (currently only materials, but in the future also e.g. instanced objects/collections). However, the pointers of these data-blocks are not stable over time. That is especially true when storing/loading the data from disk, but also just when playing back the animation. Therefore, the used data-blocks have to referenced in a different way at run-time. This is solved by adding `std::unique_ptr<bake::BakeMaterialsList>` to the run-time data of various geometry data-blocks. If the data-block is cached over a longer period of time (such that material pointers can't be used directly), it stores the material name (+ library name) used by each material slot. When the geometry is used again, the material pointers are restored using these weak name references and the `BakeDataBlockMap`. ### Manual Mapping Management There is a new `Data-Blocks` panel in the bake settings in the node editor sidebar that allows inspecting and modifying the data-blocks that are used when baking. The user can change what data-block a specific name is mapped to. Pull Request: https://projects.blender.org/blender/blender/pulls/117043
2024-02-01 09:21:55 +01:00
#include "DNA_array_utils.hh"
#include "DNA_curve_types.h"
#include "DNA_defaults.h"
#include "DNA_dynamicpaint_types.h"
#include "DNA_fluid_types.h"
#include "DNA_key_types.h"
#include "DNA_lattice_types.h"
#include "DNA_material_types.h"
#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"
#include "DNA_object_force_types.h"
#include "DNA_pointcloud_types.h"
#include "DNA_scene_types.h"
#include "DNA_space_types.h"
#include "BLI_bitmap.h"
#include "BLI_listbase.h"
#include "BLI_path_utils.hh"
#include "BLI_string.h"
#include "BLI_string_utf8.h"
#include "BLI_string_utils.hh"
#include "BLI_utildefines.h"
#include "BKE_animsys.h"
#include "BKE_armature.hh"
#include "BKE_context.hh"
#include "BKE_curve.hh"
#include "BKE_curves.h"
#include "BKE_curves.hh"
#include "BKE_displist.h"
#include "BKE_editmesh.hh"
#include "BKE_effect.h"
#include "BKE_geometry_set.hh"
#include "BKE_global.hh"
#include "BKE_grease_pencil.hh"
2024-03-26 12:57:30 -04:00
#include "BKE_idprop.hh"
2024-01-30 14:42:07 -05:00
#include "BKE_key.hh"
#include "BKE_lattice.hh"
2024-01-23 15:18:09 -05:00
#include "BKE_layer.hh"
2024-01-15 12:44:04 -05:00
#include "BKE_lib_id.hh"
#include "BKE_main.hh"
#include "BKE_material.h"
#include "BKE_mball.hh"
#include "BKE_mesh.hh"
#include "BKE_mesh_mapping.hh"
#include "BKE_mesh_runtime.hh"
2023-11-14 09:30:40 +01:00
#include "BKE_modifier.hh"
#include "BKE_multires.hh"
#include "BKE_object.hh"
#include "BKE_object_deform.h"
#include "BKE_object_types.hh"
#include "BKE_ocean.h"
#include "BKE_paint.hh"
#include "BKE_particle.h"
#include "BKE_pointcloud.hh"
#include "BKE_report.hh"
#include "BKE_scene.hh"
#include "BKE_softbody.h"
#include "BKE_volume.hh"
#include "BLT_translation.hh"
#include "DEG_depsgraph.hh"
#include "DEG_depsgraph_build.hh"
#include "DEG_depsgraph_query.hh"
#include "BLT_translation.hh"
#include "RNA_access.hh"
#include "RNA_define.hh"
#include "RNA_enum_types.hh"
#include "RNA_prototypes.hh"
#include "ED_armature.hh"
#include "ED_object.hh"
#include "ED_object_vgroup.hh"
#include "ED_screen.hh"
#include "ANIM_bone_collections.hh"
#include "GEO_merge_layers.hh"
#include "UI_interface.hh"
#include "WM_api.hh"
#include "WM_types.hh"
#include "object_intern.hh"
namespace blender::ed::object {
Mesh: Remove redundant custom data pointers For copy-on-write, we want to share attribute arrays between meshes where possible. Mutable pointers like `Mesh.mvert` make that difficult by making ownership vague. They also make code more complex by adding redundancy. The simplest solution is just removing them and retrieving layers from `CustomData` as needed. Similar changes have already been applied to curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of the pointers generally makes code more obvious and more reusable. Mesh data is now accessed with a C++ API (`Mesh::edges()` or `Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`). The CoW changes this commit makes possible are described in T95845 and T95842, and started in D14139 and D14140. The change also simplifies the ongoing mesh struct-of-array refactors from T95965. **RNA/Python Access Performance** Theoretically, accessing mesh elements with the RNA API may become slower, since the layer needs to be found on every random access. However, overhead is already high enough that this doesn't make a noticible differenc, and performance is actually improved in some cases. Random access can be up to 10% faster, but other situations might be a bit slower. Generally using `foreach_get/set` are the best way to improve performance. See the differential revision for more discussion about Python performance. Cycles has been updated to use raw pointers and the internal Blender mesh types, mostly because there is no sense in having this overhead when it's already compiled with Blender. In my tests this roughly halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million face grid). Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
static CLG_LogRef LOG = {"ed.object"};
static void modifier_skin_customdata_delete(Object *ob);
/* ------------------------------------------------------------------- */
/** \name Public Api
* \{ */
static void object_force_modifier_update_for_bind(Depsgraph *depsgraph, Object *ob)
{
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
BKE_object_eval_reset(ob_eval);
if (ob->type == OB_MESH) {
Mesh *mesh_eval = blender::bke::mesh_create_eval_final(
depsgraph, scene_eval, ob_eval, &CD_MASK_DERIVEDMESH);
BKE_id_free(nullptr, mesh_eval);
}
else if (ob->type == OB_LATTICE) {
BKE_lattice_modifiers_calc(depsgraph, scene_eval, ob_eval);
}
else if (ob->type == OB_MBALL) {
BKE_mball_data_update(depsgraph, scene_eval, ob_eval);
}
else if (ELEM(ob->type, OB_CURVES_LEGACY, OB_SURF, OB_FONT)) {
BKE_displist_make_curveTypes(depsgraph, scene_eval, ob_eval, false);
}
else if (ob->type == OB_CURVES) {
BKE_curves_data_update(depsgraph, scene_eval, ob);
}
else if (ob->type == OB_POINTCLOUD) {
BKE_pointcloud_data_update(depsgraph, scene_eval, ob);
}
else if (ob->type == OB_VOLUME) {
BKE_volume_data_update(depsgraph, scene_eval, ob);
}
}
static void object_force_modifier_bind_simple_options(Depsgraph *depsgraph,
Object *object,
ModifierData *md)
{
ModifierData *md_eval = (ModifierData *)BKE_modifier_get_evaluated(depsgraph, object, md);
const int mode = md_eval->mode;
md_eval->mode |= eModifierMode_Realtime;
object_force_modifier_update_for_bind(depsgraph, object);
md_eval->mode = mode;
}
ModifierData *modifier_add(
ReportList *reports, Main *bmain, Scene *scene, Object *ob, const char *name, int type)
{
ModifierData *new_md = nullptr;
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)type);
/* Check compatibility of modifier [#25291, #50373]. */
if (!BKE_object_support_modifier_type_check(ob, type)) {
BKE_reportf(reports, RPT_WARNING, "Modifiers cannot be added to object '%s'", ob->id.name + 2);
return nullptr;
}
2012-04-28 15:42:27 +00:00
if (mti->flags & eModifierTypeFlag_Single) {
if (BKE_modifiers_findby_type(ob, (ModifierType)type)) {
BKE_report(reports, RPT_WARNING, "Only one modifier of this type is allowed");
return nullptr;
}
}
if (type == eModifierType_ParticleSystem) {
/* don't need to worry about the new modifier's name, since that is set to the number
2018-09-02 18:28:27 +10:00
* of particle systems which shouldn't have too many duplicates
*/
new_md = object_add_particle_system(bmain, scene, ob, name);
}
else {
/* get new modifier data to add */
new_md = BKE_modifier_new(type);
ModifierData *next_md = nullptr;
LISTBASE_FOREACH_BACKWARD (ModifierData *, md, &ob->modifiers) {
if (md->flag & eModifierFlag_PinLast) {
next_md = md;
}
else {
break;
}
}
2012-04-28 15:42:27 +00:00
if (mti->flags & eModifierTypeFlag_RequiresOriginalData) {
next_md = static_cast<ModifierData *>(ob->modifiers.first);
while (next_md && BKE_modifier_get_info((ModifierType)next_md->type)->type ==
ModifierTypeType::OnlyDeform)
{
if (next_md->next && (next_md->next->flag & eModifierFlag_PinLast) != 0) {
break;
}
next_md = next_md->next;
2019-04-22 09:19:45 +10:00
}
}
BLI_insertlinkbefore(&ob->modifiers, next_md, new_md);
Modifiers: add unique modifier identifiers This adds a new `ModifierData.persistent_uid` integer property with the following properties: * It's unique within the object. * Match between the original and evaluated object. * Stable across Blender sessions. * Stable across renames and reorderings of modifiers. Potential use-cases: * Everywhere where we currently use the name as identifier. For example, `ModifierComputeContext` and `ModifierViewerPathElem`. * Can be used as part of a key in `IDCacheKey` to support caches that stay in-tact across undo steps. * Can be stored in the `SpaceNode` to identify the modifier whose geometry node tree is currently pinned (this could use the name currently, but that hasn't been implemented yet). This new identifier has some overlap with `ModifierData.session_uid`, but there are some differences: * `session_uid` is unique within the entire Blender session (except for duplicates between the original and evaluated data blocks). * `session_uid` is not stable across Blender sessions. Especially due to the first difference, it's not immediately obvious that the new `persistent_uid` can fulfill all use-cases of the existing `session_uid`. Nevertheless, this seems likely and will be cleaned up separately. Unfortunately, there is not a single place where modifiers are added to objects currently. Therefore, there are quite a few places that need to ensure valid identifiers. I tried to catch all the places, but it's hard to be sure. Therefore, I added an assert in `object_copy_data` that checks if all identifiers are valid. This way, we should be notified relatively quickly if issues are caused by invalid identifiers. Pull Request: https://projects.blender.org/blender/blender/pulls/117347
2024-02-06 17:10:40 +01:00
BKE_modifiers_persistent_uid_init(*ob, *new_md);
if (name) {
2023-05-09 12:50:37 +10:00
STRNCPY_UTF8(new_md->name, name);
}
/* make sure modifier data has unique name */
BKE_modifier_unique_name(&ob->modifiers, new_md);
/* special cases */
if (type == eModifierType_Softbody) {
if (!ob->soft) {
ob->soft = sbNew();
2012-04-28 15:42:27 +00:00
ob->softflag |= OB_SB_GOAL | OB_SB_EDGES;
}
}
else if (type == eModifierType_Collision) {
2019-04-22 09:19:45 +10:00
if (!ob->pd) {
2018-12-02 14:14:51 +11:00
ob->pd = BKE_partdeflect_new(0);
2019-04-22 09:19:45 +10:00
}
2012-04-28 15:42:27 +00:00
ob->pd->deflect = 1;
}
else if (type == eModifierType_Surface) {
/* pass */
}
else if (type == eModifierType_Multires) {
/* set totlvl from existing MDISPS layer if object already had it */
multiresModifier_set_levels_from_disps((MultiresModifierData *)new_md, ob);
if (ob->mode & OB_MODE_SCULPT) {
/* ensure that grid paint mask layer is created */
BKE_sculpt_mask_layers_ensure(nullptr, nullptr, ob, (MultiresModifierData *)new_md);
}
}
else if (type == eModifierType_Skin) {
/* ensure skin-node customdata exists */
BKE_mesh_ensure_skin_customdata(static_cast<Mesh *>(ob->data));
}
}
BKE_object_modifier_set_active(ob, new_md);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
DEG_relations_tag_update(bmain);
return new_md;
}
/* Return true if the object has a modifier of type 'type' other than
2018-09-02 18:28:27 +10:00
* the modifier pointed to be 'exclude', otherwise returns false. */
static bool object_has_modifier(const Object *ob, const ModifierData *exclude, ModifierType type)
{
LISTBASE_FOREACH (ModifierData *, md, &ob->modifiers) {
2019-04-22 09:19:45 +10:00
if ((md != exclude) && (md->type == type)) {
return true;
2019-04-22 09:19:45 +10:00
}
}
return false;
}
bool iter_other(Main *bmain,
Object *orig_ob,
const bool include_orig,
bool (*callback)(Object *ob, void *callback_data),
void *callback_data)
{
ID *ob_data_id = static_cast<ID *>(orig_ob->data);
int users = ob_data_id->us;
if (ob_data_id->flag & ID_FLAG_FAKEUSER) {
users--;
2019-04-22 09:19:45 +10:00
}
/* First check that the object's data has multiple users */
if (users > 1) {
Object *ob;
int totfound = include_orig ? 0 : 1;
for (ob = static_cast<Object *>(bmain->objects.first); ob && totfound < users;
ob = reinterpret_cast<Object *>(ob->id.next))
{
if (((ob != orig_ob) || include_orig) && (ob->data == orig_ob->data)) {
2019-04-22 09:19:45 +10:00
if (callback(ob, callback_data)) {
return true;
2019-04-22 09:19:45 +10:00
}
totfound++;
}
}
}
else if (include_orig) {
return callback(orig_ob, callback_data);
}
return false;
}
static bool object_has_modifier_cb(Object *ob, void *data)
{
2012-07-29 00:20:28 +00:00
ModifierType type = *((ModifierType *)data);
return object_has_modifier(ob, nullptr, type);
}
bool multires_update_totlevels(Object *ob, void *totlevel_v)
{
int totlevel = *((char *)totlevel_v);
LISTBASE_FOREACH (ModifierData *, md, &ob->modifiers) {
if (md->type == eModifierType_Multires) {
multires_set_tot_level(ob, (MultiresModifierData *)md, totlevel);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
}
}
return false;
}
/* Return true if no modifier of type 'type' other than 'exclude' */
static bool object_modifier_safe_to_delete(Main *bmain,
Object *ob,
ModifierData *exclude,
ModifierType type)
{
return (!object_has_modifier(ob, exclude, type) &&
!iter_other(bmain, ob, false, object_has_modifier_cb, &type));
}
static bool object_modifier_remove(
Main *bmain, Scene *scene, Object *ob, ModifierData *md, bool *r_sort_depsgraph)
{
/* It seems on rapid delete it is possible to
2018-09-02 18:28:27 +10:00
* get called twice on same modifier, so make
* sure it is in list. */
if (BLI_findindex(&ob->modifiers, md) == -1) {
2021-01-08 23:09:31 -06:00
return false;
}
/* special cases */
if (md->type == eModifierType_ParticleSystem) {
object_remove_particle_system(bmain, scene, ob, ((ParticleSystemModifierData *)md)->psys);
return true;
}
if (md->type == eModifierType_Softbody) {
if (ob->soft) {
sbFree(ob);
ob->softflag = 0; /* TODO(Sybren): this should probably be moved into sbFree() */
}
}
else if (md->type == eModifierType_Collision) {
2019-04-22 09:19:45 +10:00
if (ob->pd) {
2012-04-28 15:42:27 +00:00
ob->pd->deflect = 0;
2019-04-22 09:19:45 +10:00
}
*r_sort_depsgraph = true;
}
else if (md->type == eModifierType_Surface) {
*r_sort_depsgraph = true;
}
else if (md->type == eModifierType_Multires) {
/* Delete MDisps layer if not used by another multires modifier */
2019-04-22 09:19:45 +10:00
if (object_modifier_safe_to_delete(bmain, ob, md, eModifierType_Multires)) {
multires_customdata_delete(static_cast<Mesh *>(ob->data));
2019-04-22 09:19:45 +10:00
}
}
else if (md->type == eModifierType_Skin) {
/* Delete MVertSkin layer if not used by another skin modifier */
2019-04-22 09:19:45 +10:00
if (object_modifier_safe_to_delete(bmain, ob, md, eModifierType_Skin)) {
modifier_skin_customdata_delete(ob);
2019-04-22 09:19:45 +10:00
}
}
if (ELEM(md->type, eModifierType_Softbody, eModifierType_Cloth) &&
2018-07-31 20:11:55 +10:00
BLI_listbase_is_empty(&ob->particlesystem))
{
ob->mode &= ~OB_MODE_PARTICLE_EDIT;
}
BKE_modifier_remove_from_list(ob, md);
BKE_modifier_free(md);
BKE_object_free_derived_caches(ob);
2021-01-08 23:09:31 -06:00
return true;
}
bool modifier_remove(ReportList *reports, Main *bmain, Scene *scene, Object *ob, ModifierData *md)
{
bool sort_depsgraph = false;
bool ok = object_modifier_remove(bmain, scene, ob, md, &sort_depsgraph);
if (!ok) {
BKE_reportf(reports, RPT_ERROR, "Modifier '%s' not in object '%s'", md->name, ob->id.name);
return false;
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
DEG_relations_tag_update(bmain);
return true;
}
void modifiers_clear(Main *bmain, Scene *scene, Object *ob)
{
ModifierData *md = static_cast<ModifierData *>(ob->modifiers.first);
bool sort_depsgraph = false;
2019-04-22 09:19:45 +10:00
if (!md) {
return;
2019-04-22 09:19:45 +10:00
}
while (md) {
ModifierData *next_md = md->next;
object_modifier_remove(bmain, scene, ob, md, &sort_depsgraph);
2012-04-28 15:42:27 +00:00
md = next_md;
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
DEG_relations_tag_update(bmain);
}
static bool object_modifier_check_move_before(ReportList *reports,
eReportType error_type,
ModifierData *md,
ModifierData *md_prev)
{
if (md_prev) {
if (md->flag & eModifierFlag_PinLast && !(md_prev->flag & eModifierFlag_PinLast)) {
return false;
}
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md->type);
if (mti->type != ModifierTypeType::OnlyDeform) {
const ModifierTypeInfo *nmti = BKE_modifier_get_info((ModifierType)md_prev->type);
2012-04-28 15:42:27 +00:00
if (nmti->flags & eModifierTypeFlag_RequiresOriginalData) {
BKE_report(reports, error_type, "Cannot move above a modifier requiring original data");
return false;
}
}
}
else {
BKE_report(reports, error_type, "Cannot move modifier beyond the start of the list");
return false;
}
return true;
}
bool modifier_move_up(ReportList *reports, eReportType error_type, Object *ob, ModifierData *md)
{
if (object_modifier_check_move_before(reports, error_type, md, md->prev)) {
BLI_listbase_swaplinks(&ob->modifiers, md, md->prev);
return true;
}
return false;
}
static bool object_modifier_check_move_after(ReportList *reports,
eReportType error_type,
ModifierData *md,
ModifierData *md_next)
{
if (md_next) {
if (md_next->flag & eModifierFlag_PinLast && !(md->flag & eModifierFlag_PinLast)) {
return false;
}
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md->type);
2012-04-28 15:42:27 +00:00
if (mti->flags & eModifierTypeFlag_RequiresOriginalData) {
const ModifierTypeInfo *nmti = BKE_modifier_get_info((ModifierType)md_next->type);
if (nmti->type != ModifierTypeType::OnlyDeform) {
BKE_report(reports, error_type, "Cannot move beyond a non-deforming modifier");
return false;
}
}
}
else {
BKE_report(reports, error_type, "Cannot move modifier beyond the end of the list");
return false;
}
return true;
}
bool modifier_move_down(ReportList *reports, eReportType error_type, Object *ob, ModifierData *md)
{
if (object_modifier_check_move_after(reports, error_type, md, md->next)) {
BLI_listbase_swaplinks(&ob->modifiers, md, md->next);
return true;
}
return false;
}
bool modifier_move_to_index(ReportList *reports,
eReportType error_type,
Object *ob,
ModifierData *md,
const int index,
bool allow_partial)
{
BLI_assert(md != nullptr);
if (index < 0 || index >= BLI_listbase_count(&ob->modifiers)) {
BKE_report(reports, error_type, "Cannot move modifier beyond the end of the stack");
return false;
}
int md_index = BLI_findindex(&ob->modifiers, md);
BLI_assert(md_index != -1);
if (md_index < index) {
/* Move modifier down in list. */
ModifierData *md_target = md;
for (; md_index < index; md_index++, md_target = md_target->next) {
if (!object_modifier_check_move_after(reports, error_type, md, md_target->next)) {
if (!allow_partial || md == md_target) {
return false;
}
break;
}
}
BLI_assert(md != md_target && md_target);
BLI_remlink(&ob->modifiers, md);
BLI_insertlinkafter(&ob->modifiers, md_target, md);
}
else if (md_index > index) {
/* Move modifier up in list. */
ModifierData *md_target = md;
for (; md_index > index; md_index--, md_target = md_target->prev) {
if (!object_modifier_check_move_before(reports, error_type, md, md_target->prev)) {
if (!allow_partial || md == md_target) {
return false;
}
break;
}
}
BLI_assert(md != md_target && md_target);
BLI_remlink(&ob->modifiers, md);
BLI_insertlinkbefore(&ob->modifiers, md_target, md);
}
else {
return true;
}
/* NOTE: Dependency graph only uses modifier nodes for visibility updates, and exact order of
* modifier nodes in the graph does not matter. */
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_main_add_notifier(NC_OBJECT | ND_MODIFIER, ob);
return true;
}
void modifier_link(bContext *C, Object *ob_dst, Object *ob_src)
{
BKE_object_link_modifiers(ob_dst, ob_src);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob_dst);
DEG_id_tag_update(&ob_dst->id, ID_RECALC_TRANSFORM | ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
Main *bmain = CTX_data_main(C);
DEG_relations_tag_update(bmain);
}
bool modifier_copy_to_object(Main *bmain,
const Scene *scene,
const Object *ob_src,
const ModifierData *md,
Object *ob_dst,
ReportList *reports)
{
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md->type);
BLI_assert(ob_src != ob_dst);
/* Checked in #BKE_object_copy_modifier, but check here too so we can give a better message. */
if (!BKE_object_support_modifier_type_check(ob_dst, md->type)) {
BKE_reportf(reports,
RPT_WARNING,
"Object '%s' does not support %s modifiers",
ob_dst->id.name + 2,
RPT_(mti->name));
return false;
}
if (mti->flags & eModifierTypeFlag_Single) {
if (BKE_modifiers_findby_type(ob_dst, (ModifierType)md->type)) {
BKE_reportf(reports,
RPT_WARNING,
"Modifier can only be added once to object '%s'",
ob_dst->id.name + 2);
return false;
}
}
if (!BKE_object_copy_modifier(bmain, scene, ob_dst, ob_src, md)) {
BKE_reportf(reports,
RPT_ERROR,
"Copying modifier '%s' to object '%s' failed",
md->name,
ob_dst->id.name + 2);
return false;
}
WM_main_add_notifier(NC_OBJECT | ND_MODIFIER, ob_dst);
DEG_id_tag_update(&ob_dst->id, ID_RECALC_GEOMETRY | ID_RECALC_ANIMATION);
DEG_relations_tag_update(bmain);
return true;
}
bool convert_psys_to_mesh(ReportList * /*reports*/,
Main *bmain,
Depsgraph *depsgraph,
Scene *scene,
ViewLayer *view_layer,
Object *ob,
ModifierData *md)
{
int cvert = 0;
2019-04-22 09:19:45 +10:00
if (md->type != eModifierType_ParticleSystem) {
return false;
2019-04-22 09:19:45 +10:00
}
if (ob && ob->mode & OB_MODE_PARTICLE_EDIT) {
return false;
2019-04-22 09:19:45 +10:00
}
ParticleSystem *psys_orig = ((ParticleSystemModifierData *)md)->psys;
ParticleSettings *part = psys_orig->part;
if (part->ren_as != PART_DRAW_PATH) {
return false;
}
ParticleSystem *psys_eval = psys_eval_get(depsgraph, ob, psys_orig);
if (psys_eval->pathcache == nullptr) {
return false;
}
int part_num = psys_eval->totcached;
int child_num = psys_eval->totchildcache;
if (child_num && (part->draw & PART_DRAW_PARENT) == 0) {
part_num = 0;
2019-04-22 09:19:45 +10:00
}
/* count */
int verts_num = 0, edges_num = 0;
ParticleCacheKey **cache = psys_eval->pathcache;
for (int a = 0; a < part_num; a++) {
ParticleCacheKey *key = cache[a];
if (key->segments > 0) {
verts_num += key->segments + 1;
edges_num += key->segments;
}
}
cache = psys_eval->childcache;
for (int a = 0; a < child_num; a++) {
ParticleCacheKey *key = cache[a];
if (key->segments > 0) {
verts_num += key->segments + 1;
edges_num += key->segments;
}
}
if (verts_num == 0) {
return false;
2019-04-22 09:19:45 +10:00
}
/* add new mesh */
Object *obn = BKE_object_add(bmain, scene, view_layer, OB_MESH, nullptr);
Mesh *mesh = static_cast<Mesh *>(obn->data);
mesh->verts_num = verts_num;
mesh->edges_num = edges_num;
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
CustomData_add_layer_named(
&mesh->vert_data, CD_PROP_FLOAT3, CD_CONSTRUCT, verts_num, "position");
CustomData_add_layer_named(
&mesh->edge_data, CD_PROP_INT32_2D, CD_CONSTRUCT, mesh->edges_num, ".edge_verts");
CustomData_add_layer(&mesh->fdata_legacy, CD_MFACE, CD_SET_DEFAULT, 0);
MutableSpan<float3> positions = mesh->vert_positions_for_write();
MutableSpan<int2> edges = mesh->edges_for_write();
bke::MutableAttributeAccessor attributes = mesh->attributes_for_write();
2022-09-23 09:38:37 -05:00
bke::SpanAttributeWriter<bool> select_vert = attributes.lookup_or_add_for_write_span<bool>(
".select_vert", bke::AttrDomain::Point);
2022-09-23 09:38:37 -05:00
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
int edge_index = 0;
/* copy coordinates */
2022-09-23 09:38:37 -05:00
int vert_index = 0;
cache = psys_eval->pathcache;
for (int a = 0; a < part_num; a++) {
ParticleCacheKey *key = cache[a];
int kmax = key->segments;
2022-09-23 09:38:37 -05:00
for (int k = 0; k <= kmax; k++, key++, cvert++, vert_index++) {
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
positions[vert_index] = key->co;
if (k) {
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
edges[edge_index] = int2(cvert - 1, cvert);
edge_index++;
}
else {
/* cheap trick to select the roots */
2022-09-23 09:38:37 -05:00
select_vert.span[vert_index] = true;
}
}
}
cache = psys_eval->childcache;
for (int a = 0; a < child_num; a++) {
ParticleCacheKey *key = cache[a];
int kmax = key->segments;
2022-09-23 09:38:37 -05:00
for (int k = 0; k <= kmax; k++, key++, cvert++, vert_index++) {
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
copy_v3_v3(positions[vert_index], key->co);
if (k) {
Mesh: Move edges to a generic attribute Implements #95966, as the final step of #95965. This commit changes the storage of mesh edge vertex indices from the `MEdge` type to the generic `int2` attribute type. This follows the general design for geometry and the attribute system, where the data storage type and the usage semantics are separated. The main benefit of the change is reduced memory usage-- the requirements of storing mesh edges is reduced by 1/3. For example, this saves 8MB on a 1 million vertex grid. This also gives performance benefits to any memory-bound mesh processing algorithm that uses edges. Another benefit is that all of the edge's vertex indices are contiguous. In a few cases, it's helpful to process all of them as `Span<int>` rather than `Span<int2>`. Similarly, the type is more likely to match a generic format used by a library, or code that shouldn't know about specific Blender `Mesh` types. Various Notes: - The `.edge_verts` name is used to reflect a mapping between domains, similar to `.corner_verts`, etc. The period means that it the data shouldn't change arbitrarily by the user or procedural operations. - `edge[0]` is now used instead of `edge.v1` - Signed integers are used instead of unsigned to reduce the mixing of signed-ness, which can be error prone. - All of the previously used core mesh data types (`MVert`, `MEdge`, `MLoop`, `MPoly` are now deprecated. Only generic types are used). - The `vec2i` DNA type is used in the few C files where necessary. Pull Request: https://projects.blender.org/blender/blender/pulls/106638
2023-04-17 13:47:41 +02:00
edges[edge_index] = int2(cvert - 1, cvert);
edge_index++;
}
else {
/* cheap trick to select the roots */
2022-09-23 09:38:37 -05:00
select_vert.span[vert_index] = true;
}
}
}
2022-09-23 09:38:37 -05:00
select_vert.finish();
DEG_relations_tag_update(bmain);
return true;
}
static void add_shapekey_layers(Mesh &mesh_dest, const Mesh &mesh_src)
{
if (!mesh_src.key) {
return;
}
int i;
LISTBASE_FOREACH_INDEX (const KeyBlock *, kb, &mesh_src.key->block, i) {
void *array;
if (mesh_src.verts_num != kb->totelem) {
CLOG_ERROR(&LOG,
"vertex size mismatch (Mesh '%s':%d != KeyBlock '%s':%d)",
mesh_src.id.name + 2,
mesh_src.verts_num,
kb->name,
kb->totelem);
array = MEM_calloc_arrayN(size_t(mesh_src.verts_num), sizeof(float[3]), __func__);
}
else {
array = MEM_malloc_arrayN(size_t(mesh_src.verts_num), sizeof(float[3]), __func__);
memcpy(array, kb->data, sizeof(float[3]) * size_t(mesh_src.verts_num));
}
CustomData_add_layer_with_data(
&mesh_dest.vert_data, CD_SHAPEKEY, array, mesh_dest.verts_num, nullptr);
const int ci = CustomData_get_layer_index_n(&mesh_dest.vert_data, CD_SHAPEKEY, i);
mesh_dest.vert_data.layers[ci].uid = kb->uid;
}
}
/**
* \param use_virtual_modifiers: When enabled, calculate virtual-modifiers before applying
* `md_eval`. This is supported because virtual-modifiers are not modifiers from a user
* perspective, allowing shape keys to be included with the modifier being applied, see: #91923.
*/
static Mesh *create_applied_mesh_for_modifier(Depsgraph *depsgraph,
Scene *scene,
Object *ob_eval,
ModifierData *md_eval,
const bool use_virtual_modifiers,
const bool build_shapekey_layers,
ReportList *reports)
{
Mesh *mesh = ob_eval->runtime->data_orig ?
reinterpret_cast<Mesh *>(ob_eval->runtime->data_orig) :
reinterpret_cast<Mesh *>(ob_eval->data);
const ModifierTypeInfo *mti = BKE_modifier_get_info(ModifierType(md_eval->type));
const ModifierEvalContext mectx = {depsgraph, ob_eval, MOD_APPLY_TO_ORIGINAL};
if (!(md_eval->mode & eModifierMode_Realtime)) {
return nullptr;
}
if (mti->is_disabled && mti->is_disabled(scene, md_eval, false)) {
return nullptr;
}
if (build_shapekey_layers && mesh->key) {
if (KeyBlock *kb = static_cast<KeyBlock *>(
BLI_findlink(&mesh->key->block, ob_eval->shapenr - 1)))
{
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
BKE_keyblock_convert_to_mesh(
kb,
reinterpret_cast<float(*)[3]>(mesh->vert_positions_for_write().data()),
mesh->verts_num);
}
}
Mesh *mesh_temp = reinterpret_cast<Mesh *>(
BKE_id_copy_ex(nullptr, &mesh->id, nullptr, LIB_ID_COPY_LOCALIZE));
MutableSpan<float3> deformedVerts = mesh_temp->vert_positions_for_write();
if (use_virtual_modifiers) {
VirtualModifierData virtual_modifier_data;
for (ModifierData *md_eval_virt =
BKE_modifiers_get_virtual_modifierlist(ob_eval, &virtual_modifier_data);
md_eval_virt && (md_eval_virt != ob_eval->modifiers.first);
md_eval_virt = md_eval_virt->next)
{
if (!BKE_modifier_is_enabled(scene, md_eval_virt, eModifierMode_Realtime)) {
continue;
}
/* All virtual modifiers are deform modifiers. */
const ModifierTypeInfo *mti_virt = BKE_modifier_get_info(ModifierType(md_eval_virt->type));
BLI_assert(mti_virt->type == ModifierTypeType::OnlyDeform);
if (mti_virt->type != ModifierTypeType::OnlyDeform) {
continue;
}
mti_virt->deform_verts(md_eval_virt, &mectx, mesh_temp, deformedVerts);
}
}
Mesh *result = nullptr;
if (mti->type == ModifierTypeType::OnlyDeform) {
result = mesh_temp;
mti->deform_verts(md_eval, &mectx, result, deformedVerts);
result->tag_positions_changed();
if (build_shapekey_layers) {
add_shapekey_layers(*result, *mesh);
}
}
else {
if (build_shapekey_layers) {
add_shapekey_layers(*mesh_temp, *mesh);
}
if (mti->modify_geometry_set) {
bke::GeometrySet geometry_set = bke::GeometrySet::from_mesh(
mesh_temp, bke::GeometryOwnershipType::Owned);
mti->modify_geometry_set(md_eval, &mectx, &geometry_set);
if (!geometry_set.has_mesh()) {
BKE_report(reports, RPT_ERROR, "Evaluated geometry from modifier does not contain a mesh");
return nullptr;
}
result = geometry_set.get_component_for_write<bke::MeshComponent>().release();
}
else {
result = mti->modify_mesh(md_eval, &mectx, mesh_temp);
if (mesh_temp != result) {
BKE_id_free(nullptr, mesh_temp);
}
}
}
return result;
}
2021-01-08 23:09:31 -06:00
static bool modifier_apply_shape(Main *bmain,
ReportList *reports,
Depsgraph *depsgraph,
Scene *scene,
Object *ob,
ModifierData *md_eval)
{
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md_eval->type);
if (mti->is_disabled && mti->is_disabled(scene, md_eval, false)) {
BKE_report(reports, RPT_ERROR, "Modifier is disabled, skipping apply");
2021-01-08 23:09:31 -06:00
return false;
}
/* We could investigate using the #CD_ORIGINDEX layer
* to support other kinds of modifiers besides deforming modifiers.
* as this is done in many other places, see: #BKE_mesh_foreach_mapped_vert_coords_get.
*
* This isn't high priority in practice since most modifiers users
* want to apply as a shape are deforming modifiers.
*
* If a compelling use-case comes up where we want to support other kinds of modifiers
* we can look into supporting them. */
2012-04-28 15:42:27 +00:00
if (ob->type == OB_MESH) {
Mesh *mesh = static_cast<Mesh *>(ob->data);
Key *key = mesh->key;
if (!BKE_modifier_is_same_topology(md_eval) || mti->type == ModifierTypeType::NonGeometrical) {
BKE_report(reports, RPT_ERROR, "Only deforming modifiers can be applied to shapes");
2021-01-08 23:09:31 -06:00
return false;
}
Mesh *mesh_applied = create_applied_mesh_for_modifier(depsgraph,
DEG_get_evaluated_scene(depsgraph),
DEG_get_evaluated_object(depsgraph, ob),
md_eval,
true,
false,
reports);
if (!mesh_applied) {
BKE_report(reports, RPT_ERROR, "Modifier is disabled or returned error, skipping apply");
2021-01-08 23:09:31 -06:00
return false;
}
if (key == nullptr) {
key = mesh->key = BKE_key_add(bmain, (ID *)mesh);
2012-04-28 15:42:27 +00:00
key->type = KEY_RELATIVE;
/* if that was the first key block added, then it was the basis.
2018-09-02 18:28:27 +10:00
* Initialize it with the mesh, and add another for the modifier */
KeyBlock *kb = BKE_keyblock_add(key, nullptr);
BKE_keyblock_convert_from_mesh(mesh, key, kb);
}
KeyBlock *kb = BKE_keyblock_add(key, md_eval->name);
BKE_mesh_nomain_to_meshkey(mesh_applied, mesh, kb);
BKE_id_free(nullptr, mesh_applied);
}
else {
BKE_report(reports, RPT_ERROR, "Cannot apply modifier for this object type");
2021-01-08 23:09:31 -06:00
return false;
}
2021-01-08 23:09:31 -06:00
return true;
}
static bool meta_data_matches(const std::optional<bke::AttributeMetaData> meta_data,
const AttrDomainMask domains,
const eCustomDataMask types)
{
if (!meta_data) {
return false;
}
if (!(ATTR_DOMAIN_AS_MASK(meta_data->domain) & domains)) {
return false;
}
if (!(CD_TYPE_AS_MASK(meta_data->data_type) & types)) {
return false;
}
return true;
}
static void remove_invalid_attribute_strings(Mesh &mesh)
{
bke::AttributeAccessor attributes = mesh.attributes();
if (!meta_data_matches(attributes.lookup_meta_data(mesh.active_color_attribute),
ATTR_DOMAIN_MASK_COLOR,
CD_MASK_COLOR_ALL))
{
MEM_SAFE_FREE(mesh.active_color_attribute);
}
if (!meta_data_matches(attributes.lookup_meta_data(mesh.default_color_attribute),
ATTR_DOMAIN_MASK_COLOR,
CD_MASK_COLOR_ALL))
{
MEM_SAFE_FREE(mesh.default_color_attribute);
}
}
static void apply_eval_grease_pencil_data(const GreasePencil &src_grease_pencil,
const int eval_frame,
const IndexMask &orig_layers,
GreasePencil &orig_grease_pencil)
{
using namespace bke;
using namespace bke::greasepencil;
/* Build a set of pointers to the layers that we want to apply. */
Set<const Layer *> orig_layers_to_apply;
orig_layers.foreach_index([&](const int layer_i) {
const Layer &layer = orig_grease_pencil.layer(layer_i);
orig_layers_to_apply.add(&layer);
});
/* Ensure that the layer names are unique by merging layers with the same name. */
const int old_layers_num = src_grease_pencil.layers().size();
Vector<Vector<int>> layers_map;
Map<StringRef, int> new_layer_index_by_name;
for (const int layer_i : IndexRange(old_layers_num)) {
const Layer &layer = src_grease_pencil.layer(layer_i);
const int new_layer_index = new_layer_index_by_name.lookup_or_add_cb(
layer.name(), [&]() { return layers_map.append_and_get_index_as(); });
layers_map[new_layer_index].append(layer_i);
}
GreasePencil &merged_layers_grease_pencil = *geometry::merge_layers(
src_grease_pencil, layers_map, {});
Map<const Layer *, const Layer *> eval_to_orig_layer_map;
{
/* Keep track of the last layer in each group to ensure layers get added to the same groups in
* the same order as the original. This is better than using the layer cache since it avoids
* updating the cache every time a new layer is added. */
Map<const LayerGroup *, TreeNode *> last_node_by_group;
/* Set of orig layers that require the drawing on `eval_frame` to be cleared. These are layers
* that existed in original geometry but were removed during the modifier evaluation. */
Set<Layer *> orig_layers_to_clear(orig_grease_pencil.layers_for_write());
for (const TreeNode *node_eval : merged_layers_grease_pencil.nodes()) {
/* Check if the original geometry has a layer with the same name. */
TreeNode *node_orig = orig_grease_pencil.find_node_by_name(node_eval->name());
BLI_assert(node_eval != nullptr);
if (node_eval->is_layer()) {
/* If the orig layer isn't valid then a new layer with a unique name will be generated. */
const bool has_valid_orig_layer = (node_orig != nullptr && node_orig->is_layer());
if (!has_valid_orig_layer) {
/* Note: This name might be empty! This has to be resolved at a later stage! */
Layer &layer_orig = orig_grease_pencil.add_layer(node_eval->name(), true);
orig_layers_to_apply.add(&layer_orig);
/* Make sure to add a new keyframe with a new drawing. */
orig_grease_pencil.insert_frame(layer_orig, eval_frame);
node_orig = &layer_orig.as_node();
}
BLI_assert(node_orig != nullptr);
Layer &layer_orig = node_orig->as_layer();
/* This layer has a matching evaluated layer, so don't clear its keyframe. */
orig_layers_to_clear.remove(&layer_orig);
/* Only map layers in `eval_to_orig_layer_map` that we want to apply. */
if (orig_layers_to_apply.contains(&layer_orig)) {
/* Copy layer properties to original geometry. */
const Layer &layer_eval = node_eval->as_layer();
layer_orig.opacity = layer_eval.opacity;
layer_orig.set_local_transform(layer_eval.local_transform());
/* Add new mapping for layer_eval -> layer_orig*/
eval_to_orig_layer_map.add_new(&layer_eval, &layer_orig);
}
}
/* Insert the updated node after the last node in the same group.
* This keeps the layer order consistent. */
if (node_orig && node_orig->parent_group()) {
last_node_by_group.add_or_modify(
node_orig->parent_group(),
[&](TreeNode **node_ptr) {
/* First layer in the group, set the last-layer pointer. */
*node_ptr = node_orig;
},
[&](TreeNode **node_ptr) {
orig_grease_pencil.move_node_after(*node_orig, **node_ptr);
*node_ptr = node_orig;
});
}
}
/* Clear the keyframe of all the original layers that don't have a matching evaluated layer,
* e.g. the ones that were "deleted" in the modifier. */
for (Layer *layer_orig : orig_layers_to_clear) {
/* Try inserting a frame. */
Drawing *drawing_orig = orig_grease_pencil.insert_frame(*layer_orig, eval_frame);
if (drawing_orig == nullptr) {
/* If that fails, get the drawing for this frame. */
drawing_orig = orig_grease_pencil.get_drawing_at(*layer_orig, eval_frame);
}
/* Clear the existing drawing. */
drawing_orig->strokes_for_write() = {};
drawing_orig->tag_topology_changed();
}
}
/* Update the drawings. */
VectorSet<Drawing *> all_updated_drawings;
for (auto [layer_eval, layer_orig] : eval_to_orig_layer_map.items()) {
const Drawing *drawing_eval = merged_layers_grease_pencil.get_drawing_at(*layer_eval,
eval_frame);
Drawing *drawing_orig = orig_grease_pencil.get_drawing_at(*layer_orig, eval_frame);
if (drawing_orig && drawing_eval) {
/* Write the data to the original drawing. */
drawing_orig->strokes_for_write() = std::move(drawing_eval->strokes());
/* Anonymous attributes shouldn't be available on original geometry. */
drawing_orig->strokes_for_write().attributes_for_write().remove_anonymous();
drawing_orig->tag_topology_changed();
all_updated_drawings.add_new(drawing_orig);
}
}
/* Get the original material pointers from the result geometry. */
VectorSet<Material *> original_materials;
const Span<Material *> eval_materials = Span{merged_layers_grease_pencil.material_array,
merged_layers_grease_pencil.material_array_num};
for (Material *eval_material : eval_materials) {
if (eval_material != nullptr && eval_material->id.orig_id != nullptr) {
original_materials.add_new(reinterpret_cast<Material *>(eval_material->id.orig_id));
}
}
/* Build material indices mapping. This maps the materials indices on the original geometry to
* the material indices used in the result geometry. The material indices for the drawings in the
* result geometry are already correct, but this might not be the case for all drawings in the
* original geometry (like for drawings that are not visible on the frame that the modifier is
* being applied on). */
Array<int> material_indices_map(orig_grease_pencil.material_array_num);
for (const int mat_i : IndexRange(orig_grease_pencil.material_array_num)) {
Material *material = orig_grease_pencil.material_array[mat_i];
const int map_index = original_materials.index_of_try(material);
if (map_index != -1) {
material_indices_map[mat_i] = map_index;
}
}
/* Remap material indices for all other drawings. */
if (!material_indices_map.is_empty() &&
!array_utils::indices_are_range(material_indices_map,
IndexRange(orig_grease_pencil.material_array_num)))
{
for (GreasePencilDrawingBase *base : orig_grease_pencil.drawings()) {
if (base->type != GP_DRAWING) {
continue;
}
Drawing &drawing = reinterpret_cast<GreasePencilDrawing *>(base)->wrap();
if (all_updated_drawings.contains(&drawing)) {
/* Skip remapping drawings that already have been updated. */
continue;
}
MutableAttributeAccessor attributes = drawing.strokes_for_write().attributes_for_write();
if (!attributes.contains("material_index")) {
continue;
}
SpanAttributeWriter<int> material_indices = attributes.lookup_or_add_for_write_span<int>(
"material_index", AttrDomain::Curve);
for (int &material_index : material_indices.span) {
if (material_index >= 0 && material_index < material_indices_map.size()) {
material_index = material_indices_map[material_index];
}
}
material_indices.finish();
}
}
/* Convert the layer map into an index mapping. */
Map<int, int> eval_to_orig_layer_indices_map;
for (const int layer_eval_i : merged_layers_grease_pencil.layers().index_range()) {
const Layer *layer_eval = &merged_layers_grease_pencil.layer(layer_eval_i);
if (eval_to_orig_layer_map.contains(layer_eval)) {
const Layer *layer_orig = eval_to_orig_layer_map.lookup(layer_eval);
const int layer_orig_index = *orig_grease_pencil.get_layer_index(*layer_orig);
eval_to_orig_layer_indices_map.add(layer_eval_i, layer_orig_index);
}
}
/* Propagate layer attributes. */
AttributeAccessor src_attributes = merged_layers_grease_pencil.attributes();
MutableAttributeAccessor dst_attributes = orig_grease_pencil.attributes_for_write();
src_attributes.foreach_attribute([&](const bke::AttributeIter &iter) {
/* Anonymous attributes shouldn't be available on original geometry. */
if (attribute_name_is_anonymous(iter.name)) {
return;
}
if (iter.data_type == CD_PROP_STRING) {
return;
}
const GVArraySpan src = *iter.get(AttrDomain::Layer);
GSpanAttributeWriter dst = dst_attributes.lookup_or_add_for_write_only_span(
iter.name, AttrDomain::Layer, iter.data_type);
if (!dst) {
return;
}
attribute_math::convert_to_static_type(src.type(), [&](auto dummy) {
using T = decltype(dummy);
Span<T> src_span = src.typed<T>();
MutableSpan<T> dst_span = dst.span.typed<T>();
for (const auto [src_i, dst_i] : eval_to_orig_layer_indices_map.items()) {
dst_span[dst_i] = src_span[src_i];
}
});
dst.finish();
});
/* Free temporary grease pencil struct. */
BKE_id_free(nullptr, &merged_layers_grease_pencil);
}
static bool apply_grease_pencil_for_modifier(Depsgraph *depsgraph,
Object *ob,
GreasePencil &grease_pencil_orig,
ModifierData *md_eval)
{
using namespace bke;
using namespace bke::greasepencil;
const ModifierTypeInfo *mti = BKE_modifier_get_info(ModifierType(md_eval->type));
Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
GreasePencil *grease_pencil_for_eval = ob_eval->runtime->data_orig ?
reinterpret_cast<GreasePencil *>(
ob_eval->runtime->data_orig) :
&grease_pencil_orig;
const int eval_frame = int(DEG_get_ctime(depsgraph));
GreasePencil *grease_pencil_temp = reinterpret_cast<GreasePencil *>(
BKE_id_copy_ex(nullptr, &grease_pencil_for_eval->id, nullptr, LIB_ID_COPY_LOCALIZE));
grease_pencil_temp->runtime->eval_frame = eval_frame;
GeometrySet eval_geometry_set = GeometrySet::from_grease_pencil(grease_pencil_temp,
GeometryOwnershipType::Owned);
ModifierEvalContext mectx = {depsgraph, ob_eval, MOD_APPLY_TO_ORIGINAL};
mti->modify_geometry_set(md_eval, &mectx, &eval_geometry_set);
if (!eval_geometry_set.has_grease_pencil()) {
return false;
}
GreasePencil &grease_pencil_result =
*eval_geometry_set.get_component_for_write<GreasePencilComponent>().get_for_write();
apply_eval_grease_pencil_data(grease_pencil_result,
eval_frame,
grease_pencil_orig.layers().index_range(),
grease_pencil_orig);
Main *bmain = DEG_get_bmain(depsgraph);
/* There might be layers with empty names after evaluation. Make sure to rename them. */
for (Layer *layer : grease_pencil_orig.layers_for_write()) {
if (layer->name().is_empty()) {
grease_pencil_orig.rename_node(*bmain, layer->as_node(), DATA_("Layer"));
}
}
BKE_object_material_from_eval_data(bmain, ob, &grease_pencil_result.id);
return true;
}
static bool apply_grease_pencil_for_modifier_all_keyframes(Depsgraph *depsgraph,
Scene *scene,
Object *ob,
GreasePencil &grease_pencil_orig,
ModifierData *md)
{
using namespace bke;
using namespace bke::greasepencil;
Main *bmain = DEG_get_bmain(depsgraph);
const ModifierTypeInfo *mti = BKE_modifier_get_info(ModifierType(md->type));
WM_cursor_wait(true);
Map<int, Vector<int>> layer_indices_to_apply_per_frame;
{
for (const int layer_i : grease_pencil_orig.layers().index_range()) {
const Layer &layer = grease_pencil_orig.layer(layer_i);
for (const auto &[key, value] : layer.frames().items()) {
if (value.is_end()) {
continue;
}
layer_indices_to_apply_per_frame.lookup_or_add(key, {}).append(layer_i);
}
}
}
Array<int> sorted_frame_times(layer_indices_to_apply_per_frame.size());
int i = 0;
for (const int key : layer_indices_to_apply_per_frame.keys()) {
sorted_frame_times[i++] = key;
}
std::sort(sorted_frame_times.begin(), sorted_frame_times.end());
const int prev_frame = int(DEG_get_ctime(depsgraph));
bool changed = false;
for (const int eval_frame : sorted_frame_times) {
const Span<int> layer_indices = layer_indices_to_apply_per_frame.lookup(eval_frame).as_span();
scene->r.cfra = eval_frame;
BKE_scene_graph_update_for_newframe(depsgraph);
Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
GreasePencil *grease_pencil_for_eval = ob_eval->runtime->data_orig ?
reinterpret_cast<GreasePencil *>(
ob_eval->runtime->data_orig) :
&grease_pencil_orig;
GreasePencil *grease_pencil_temp = reinterpret_cast<GreasePencil *>(
BKE_id_copy_ex(nullptr, &grease_pencil_for_eval->id, nullptr, LIB_ID_COPY_LOCALIZE));
grease_pencil_temp->runtime->eval_frame = eval_frame;
GeometrySet eval_geometry_set = GeometrySet::from_grease_pencil(grease_pencil_temp,
GeometryOwnershipType::Owned);
ModifierData *md_eval = BKE_modifier_get_evaluated(depsgraph, ob, md);
ModifierEvalContext mectx = {depsgraph, ob_eval, MOD_APPLY_TO_ORIGINAL};
mti->modify_geometry_set(md_eval, &mectx, &eval_geometry_set);
if (!eval_geometry_set.has_grease_pencil()) {
continue;
}
GreasePencil &grease_pencil_result =
*eval_geometry_set.get_component_for_write<GreasePencilComponent>().get_for_write();
IndexMaskMemory memory;
const IndexMask orig_layers_to_apply = IndexMask::from_indices(layer_indices, memory);
apply_eval_grease_pencil_data(
grease_pencil_result, eval_frame, orig_layers_to_apply, grease_pencil_orig);
BKE_object_material_from_eval_data(bmain, ob, &grease_pencil_result.id);
changed = true;
}
scene->r.cfra = prev_frame;
BKE_scene_graph_update_for_newframe(depsgraph);
/* There might be layers with empty names after evaluation. Make sure to rename them. */
for (Layer *layer : grease_pencil_orig.layers_for_write()) {
if (layer->name().is_empty()) {
grease_pencil_orig.rename_node(*bmain, layer->as_node(), DATA_("Layer"));
}
}
WM_cursor_wait(false);
return changed;
}
static bool modifier_apply_obdata(ReportList *reports,
Depsgraph *depsgraph,
Scene *scene,
Object *ob,
ModifierData *md_eval,
const bool do_all_keyframes)
{
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md_eval->type);
if (mti->is_disabled && mti->is_disabled(scene, md_eval, false)) {
BKE_report(reports, RPT_ERROR, "Modifier is disabled, skipping apply");
2021-01-08 23:09:31 -06:00
return false;
}
2012-04-28 15:42:27 +00:00
if (ob->type == OB_MESH) {
Mesh *mesh = static_cast<Mesh *>(ob->data);
MultiresModifierData *mmd = find_multires_modifier_before(scene, md_eval);
if (mesh->key && mti->type != ModifierTypeType::NonGeometrical) {
BKE_report(reports, RPT_ERROR, "Modifier cannot be applied to a mesh with shape keys");
2021-01-08 23:09:31 -06:00
return false;
}
/* Multires: ensure that recent sculpting is applied */
if (md_eval->type == eModifierType_Multires) {
multires_force_sculpt_rebuild(ob);
2019-04-22 09:19:45 +10:00
}
if (mmd && mmd->totlvl && mti->type == ModifierTypeType::OnlyDeform) {
if (!multiresModifier_reshapeFromDeformModifier(depsgraph, ob, mmd, md_eval)) {
BKE_report(reports, RPT_ERROR, "Multires modifier returned error, skipping apply");
2021-01-08 23:09:31 -06:00
return false;
}
}
else {
Mesh *mesh_applied = create_applied_mesh_for_modifier(
depsgraph,
DEG_get_evaluated_scene(depsgraph),
DEG_get_evaluated_object(depsgraph, ob),
md_eval,
/* It's important not to apply virtual modifiers (e.g. shape-keys) because they're kept,
* causing them to be applied twice, see: #97758. */
false,
true,
reports);
if (!mesh_applied) {
2021-01-08 23:09:31 -06:00
return false;
}
Main *bmain = DEG_get_bmain(depsgraph);
BKE_object_material_from_eval_data(bmain, ob, &mesh_applied->id);
BKE_mesh_nomain_to_mesh(mesh_applied, mesh, ob);
/* Anonymous attributes shouldn't be available on the applied geometry. */
mesh->attributes_for_write().remove_anonymous();
/* Remove strings referring to attributes if they no longer exist. */
remove_invalid_attribute_strings(*mesh);
if (md_eval->type == eModifierType_Multires) {
multires_customdata_delete(mesh);
2019-04-22 09:19:45 +10:00
}
}
}
else if (ELEM(ob->type, OB_CURVES_LEGACY, OB_SURF)) {
Object *object_eval = DEG_get_evaluated_object(depsgraph, ob);
Curve *curve = static_cast<Curve *>(ob->data);
Curve *curve_eval = static_cast<Curve *>(object_eval->data);
ModifierEvalContext mectx = {depsgraph, object_eval, MOD_APPLY_TO_ORIGINAL};
if (ELEM(mti->type, ModifierTypeType::Constructive, ModifierTypeType::Nonconstructive)) {
BKE_report(
reports,
RPT_ERROR,
"Cannot apply constructive modifiers on curve. Convert curve to mesh in order to apply");
2021-01-08 23:09:31 -06:00
return false;
}
2012-03-18 07:38:51 +00:00
BKE_report(reports,
RPT_INFO,
"Applied modifier only changed CV points, not tessellated/bevel vertices");
int verts_num;
float(*vertexCos)[3] = BKE_curve_nurbs_vert_coords_alloc(&curve_eval->nurb, &verts_num);
mti->deform_verts(
md_eval, &mectx, nullptr, {reinterpret_cast<float3 *>(vertexCos), verts_num});
BKE_curve_nurbs_vert_coords_apply(&curve->nurb, vertexCos, false);
MEM_freeN(vertexCos);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
}
else if (ob->type == OB_LATTICE) {
Object *object_eval = DEG_get_evaluated_object(depsgraph, ob);
Lattice *lattice = static_cast<Lattice *>(ob->data);
ModifierEvalContext mectx = {depsgraph, object_eval, MOD_APPLY_TO_ORIGINAL};
if (ELEM(mti->type, ModifierTypeType::Constructive, ModifierTypeType::Nonconstructive)) {
BKE_report(reports, RPT_ERROR, "Constructive modifiers cannot be applied");
2021-01-08 23:09:31 -06:00
return false;
}
int verts_num;
float(*vertexCos)[3] = BKE_lattice_vert_coords_alloc(lattice, &verts_num);
mti->deform_verts(
md_eval, &mectx, nullptr, {reinterpret_cast<float3 *>(vertexCos), verts_num});
BKE_lattice_vert_coords_apply(lattice, vertexCos);
MEM_freeN(vertexCos);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
}
else if (ob->type == OB_CURVES) {
Curves &curves = *static_cast<Curves *>(ob->data);
if (mti->modify_geometry_set == nullptr) {
BLI_assert_unreachable();
return false;
}
bke::GeometrySet geometry_set = bke::GeometrySet::from_curves(
&curves, bke::GeometryOwnershipType::ReadOnly);
ModifierEvalContext mectx = {depsgraph, ob, MOD_APPLY_TO_ORIGINAL};
mti->modify_geometry_set(md_eval, &mectx, &geometry_set);
if (!geometry_set.has_curves()) {
BKE_report(reports, RPT_ERROR, "Evaluated geometry from modifier does not contain curves");
return false;
}
Curves &curves_eval = *geometry_set.get_curves_for_write();
/* Anonymous attributes shouldn't be available on original geometry. */
curves_eval.geometry.wrap().attributes_for_write().remove_anonymous();
curves.geometry.wrap() = std::move(curves_eval.geometry.wrap());
Main *bmain = DEG_get_bmain(depsgraph);
BKE_object_material_from_eval_data(bmain, ob, &curves_eval.id);
}
else if (ob->type == OB_POINTCLOUD) {
PointCloud &points = *static_cast<PointCloud *>(ob->data);
if (mti->modify_geometry_set == nullptr) {
BLI_assert_unreachable();
return false;
}
bke::GeometrySet geometry_set = bke::GeometrySet::from_pointcloud(
&points, bke::GeometryOwnershipType::ReadOnly);
ModifierEvalContext mectx = {depsgraph, ob, MOD_APPLY_TO_ORIGINAL};
mti->modify_geometry_set(md_eval, &mectx, &geometry_set);
if (!geometry_set.has_pointcloud()) {
BKE_report(
reports, RPT_ERROR, "Evaluated geometry from modifier does not contain a point cloud");
return false;
}
PointCloud *pointcloud_eval =
geometry_set.get_component_for_write<bke::PointCloudComponent>().release();
/* Anonymous attributes shouldn't be available on original geometry. */
pointcloud_eval->attributes_for_write().remove_anonymous();
Main *bmain = DEG_get_bmain(depsgraph);
BKE_object_material_from_eval_data(bmain, ob, &pointcloud_eval->id);
BKE_pointcloud_nomain_to_pointcloud(pointcloud_eval, &points);
}
else if (ob->type == OB_GREASE_PENCIL) {
if (mti->modify_geometry_set == nullptr) {
BKE_report(reports, RPT_ERROR, "Cannot apply this modifier to Grease Pencil geometry");
return false;
}
GreasePencil &grease_pencil_orig = *static_cast<GreasePencil *>(ob->data);
bool success = false;
if (do_all_keyframes) {
/* The function #apply_grease_pencil_for_modifier_all_keyframes will retrieve
* the evaluated modifier for each keyframe. The original modifier is passed
* to ensure the evaluated modifier is not used, as it will be invalid when
* the scene graph is updated for the next keyframe. */
ModifierData *md = BKE_modifier_get_original(ob, md_eval);
success = apply_grease_pencil_for_modifier_all_keyframes(
depsgraph, scene, ob, grease_pencil_orig, md);
}
else {
success = apply_grease_pencil_for_modifier(depsgraph, ob, grease_pencil_orig, md_eval);
}
if (!success) {
BKE_report(reports,
RPT_ERROR,
"Evaluated geometry from modifier does not contain Grease Pencil geometry");
return false;
}
}
else {
/* TODO: implement for volumes. */
BKE_report(reports, RPT_ERROR, "Cannot apply modifier for this object type");
2021-01-08 23:09:31 -06:00
return false;
}
/* lattice modifier can be applied to particle system too */
if (ob->particlesystem.first) {
LISTBASE_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
2019-04-22 09:19:45 +10:00
if (psys->part->type != PART_HAIR) {
continue;
2019-04-22 09:19:45 +10:00
}
psys_apply_hair_lattice(depsgraph, scene, ob, psys);
}
}
2021-01-08 23:09:31 -06:00
return true;
}
bool modifier_apply(Main *bmain,
ReportList *reports,
Depsgraph *depsgraph,
Scene *scene,
Object *ob,
ModifierData *md,
int mode,
bool keep_modifier,
const bool do_all_keyframes)
{
if (BKE_object_is_in_editmode(ob)) {
BKE_report(reports, RPT_ERROR, "Modifiers cannot be applied in edit mode");
return false;
}
if (mode != MODIFIER_APPLY_SHAPE && ID_REAL_USERS(ob->data) > 1) {
BKE_report(reports, RPT_ERROR, "Modifiers cannot be applied to multi-user data");
return false;
}
2022-10-07 22:52:53 +11:00
if ((ob->mode & OB_MODE_SCULPT) && find_multires_modifier_before(scene, md) &&
(BKE_modifier_is_same_topology(md) == false))
{
BKE_report(reports,
RPT_ERROR,
"Constructive modifier cannot be applied to multi-res data in sculpt mode");
return false;
}
2019-04-22 09:19:45 +10:00
if (md != ob->modifiers.first) {
BKE_report(reports, RPT_INFO, "Applied modifier was not first, result may not be as expected");
2019-04-22 09:19:45 +10:00
}
/* Get evaluated modifier, so object links pointer to evaluated data,
2018-09-02 18:28:27 +10:00
* but still use original object it is applied to the original mesh. */
Object *ob_eval = DEG_get_evaluated_object(depsgraph, ob);
2020-05-25 11:39:52 +02:00
ModifierData *md_eval = (ob_eval) ? BKE_modifiers_findby_name(ob_eval, md->name) : md;
Depsgraph *apply_depsgraph = depsgraph;
Depsgraph *local_depsgraph = nullptr;
/* If the object is hidden or the modifier is not enabled for the viewport is disabled a special
* handling is required. This is because the viewport dependency graph optimizes out evaluation
* of objects which are used by hidden objects and disabled modifiers.
*
* The idea is to create a dependency graph which does not perform those optimizations. */
if ((ob_eval->base_flag & BASE_ENABLED_VIEWPORT) == 0 ||
(md_eval->mode & eModifierMode_Realtime) == 0)
{
ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph);
local_depsgraph = DEG_graph_new(bmain, scene, view_layer, DAG_EVAL_VIEWPORT);
DEG_disable_visibility_optimization(local_depsgraph);
DEG_graph_build_from_ids(local_depsgraph, {&ob->id});
DEG_evaluate_on_refresh(local_depsgraph);
apply_depsgraph = local_depsgraph;
/* The evaluated object and modifier are now from the different dependency graph. */
ob_eval = DEG_get_evaluated_object(local_depsgraph, ob);
md_eval = BKE_modifiers_findby_name(ob_eval, md->name);
/* Force mode on the evaluated modifier, enforcing the modifier evaluation in the apply()
* functions. */
md_eval->mode |= eModifierMode_Realtime;
}
bool did_apply = false;
if (mode == MODIFIER_APPLY_SHAPE) {
did_apply = modifier_apply_shape(bmain, reports, apply_depsgraph, scene, ob, md_eval);
}
else {
did_apply = modifier_apply_obdata(
reports, apply_depsgraph, scene, ob, md_eval, do_all_keyframes);
}
if (did_apply) {
if (!keep_modifier) {
BKE_modifier_remove_from_list(ob, md);
BKE_modifier_free(md);
}
BKE_object_free_derived_caches(ob);
}
if (local_depsgraph != nullptr) {
DEG_graph_free(local_depsgraph);
}
return true;
}
bool modifier_copy(
ReportList * /*reports*/, Main *bmain, Scene *scene, Object *ob, ModifierData *md)
{
if (md->type == eModifierType_ParticleSystem) {
2021-01-08 23:10:58 -06:00
ModifierData *nmd = object_copy_particle_system(
bmain, scene, ob, ((ParticleSystemModifierData *)md)->psys);
BLI_remlink(&ob->modifiers, nmd);
BLI_insertlinkafter(&ob->modifiers, md, nmd);
BKE_object_modifier_set_active(ob, nmd);
return true;
}
2021-01-08 23:09:31 -06:00
ModifierData *nmd = BKE_modifier_new(md->type);
BKE_modifier_copydata(md, nmd);
BLI_insertlinkafter(&ob->modifiers, md, nmd);
BKE_modifier_unique_name(&ob->modifiers, nmd);
Modifiers: add unique modifier identifiers This adds a new `ModifierData.persistent_uid` integer property with the following properties: * It's unique within the object. * Match between the original and evaluated object. * Stable across Blender sessions. * Stable across renames and reorderings of modifiers. Potential use-cases: * Everywhere where we currently use the name as identifier. For example, `ModifierComputeContext` and `ModifierViewerPathElem`. * Can be used as part of a key in `IDCacheKey` to support caches that stay in-tact across undo steps. * Can be stored in the `SpaceNode` to identify the modifier whose geometry node tree is currently pinned (this could use the name currently, but that hasn't been implemented yet). This new identifier has some overlap with `ModifierData.session_uid`, but there are some differences: * `session_uid` is unique within the entire Blender session (except for duplicates between the original and evaluated data blocks). * `session_uid` is not stable across Blender sessions. Especially due to the first difference, it's not immediately obvious that the new `persistent_uid` can fulfill all use-cases of the existing `session_uid`. Nevertheless, this seems likely and will be cleaned up separately. Unfortunately, there is not a single place where modifiers are added to objects currently. Therefore, there are quite a few places that need to ensure valid identifiers. I tried to catch all the places, but it's hard to be sure. Therefore, I added an assert in `object_copy_data` that checks if all identifiers are valid. This way, we should be notified relatively quickly if issues are caused by invalid identifiers. Pull Request: https://projects.blender.org/blender/blender/pulls/117347
2024-02-06 17:10:40 +01:00
BKE_modifiers_persistent_uid_init(*ob, *nmd);
BKE_object_modifier_set_active(ob, nmd);
nmd->flag |= eModifierFlag_OverrideLibrary_Local;
2021-01-08 23:09:31 -06:00
return true;
}
/** \} */
Vector<PointerRNA> modifier_get_edit_objects(const bContext &C, const wmOperator &op)
{
Vector<PointerRNA> objects;
if (RNA_boolean_get(op.ptr, "use_selected_objects")) {
CTX_data_selected_editable_objects(&C, &objects);
}
else {
if (Object *object = context_active_object(&C)) {
objects.append(RNA_id_pointer_create(&object->id));
}
}
return objects;
}
void modifier_register_use_selected_objects_prop(wmOperatorType *ot)
{
PropertyRNA *prop = RNA_def_boolean(
ot->srna,
"use_selected_objects",
false,
"Selected Objects",
"Affect all selected objects instead of just the active object");
RNA_def_property_flag(prop, PROP_SKIP_SAVE | PROP_HIDDEN);
}
/* ------------------------------------------------------------------- */
/** \name Add Modifier Operator
* \{ */
static int modifier_add_exec(bContext *C, wmOperator *op)
{
2012-04-28 15:42:27 +00:00
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
int type = RNA_enum_get(op->ptr, "type");
bool changed = false;
for (const PointerRNA &ptr : modifier_get_edit_objects(*C, *op)) {
Object *ob = static_cast<Object *>(ptr.data);
if (!modifier_add(op->reports, bmain, scene, ob, nullptr, type)) {
continue;
}
changed = true;
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
}
if (!changed) {
return OPERATOR_CANCELLED;
2019-04-22 09:19:45 +10:00
}
return OPERATOR_FINISHED;
}
static int modifier_add_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
if (event->modifier & KM_ALT || CTX_wm_view3d(C)) {
RNA_boolean_set(op->ptr, "use_selected_objects", true);
}
if (!RNA_struct_property_is_set(op->ptr, "type")) {
return WM_menu_invoke(C, op, event);
}
return modifier_add_exec(C, op);
}
static const EnumPropertyItem *modifier_add_itemf(bContext *C,
PointerRNA * /*ptr*/,
PropertyRNA * /*prop*/,
bool *r_free)
{
Object *ob = context_active_object(C);
2019-04-22 09:19:45 +10:00
if (!ob) {
return rna_enum_object_modifier_type_items;
2019-04-22 09:19:45 +10:00
}
EnumPropertyItem *items = nullptr;
int totitem = 0;
const EnumPropertyItem *group_item = nullptr;
for (int a = 0; rna_enum_object_modifier_type_items[a].identifier; a++) {
const EnumPropertyItem *md_item = &rna_enum_object_modifier_type_items[a];
if (md_item->identifier[0]) {
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md_item->value);
2019-04-22 09:19:45 +10:00
if (mti->flags & eModifierTypeFlag_NoUserAdd) {
continue;
2019-04-22 09:19:45 +10:00
}
2019-04-22 09:19:45 +10:00
if (!BKE_object_support_modifier_type_check(ob, md_item->value)) {
continue;
2019-04-22 09:19:45 +10:00
}
}
else {
2012-04-28 15:42:27 +00:00
group_item = md_item;
continue;
}
if (group_item) {
RNA_enum_item_add(&items, &totitem, group_item);
group_item = nullptr;
}
RNA_enum_item_add(&items, &totitem, md_item);
}
RNA_enum_item_end(&items, &totitem);
*r_free = true;
return items;
}
void OBJECT_OT_modifier_add(wmOperatorType *ot)
{
PropertyRNA *prop;
/* identifiers */
ot->name = "Add Modifier";
ot->description = "Add a procedural operation/effect to the active object";
ot->idname = "OBJECT_OT_modifier_add";
/* api callbacks */
ot->invoke = modifier_add_invoke;
ot->exec = modifier_add_exec;
ot->poll = ED_operator_object_active_editable;
/* flags */
2012-04-28 15:42:27 +00:00
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
/* properties */
prop = RNA_def_enum(
ot->srna, "type", rna_enum_object_modifier_type_items, eModifierType_Subsurf, "Type", "");
RNA_def_enum_funcs(prop, modifier_add_itemf);
ot->prop = prop;
modifier_register_use_selected_objects_prop(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Generic Poll Function and Properties
*
* Using modifier names and data context.
* \{ */
bool edit_modifier_poll_generic(bContext *C,
StructRNA *rna_type,
int obtype_flag,
const bool is_editmode_allowed,
const bool is_liboverride_allowed)
{
Main *bmain = CTX_data_main(C);
2012-04-28 15:42:27 +00:00
PointerRNA ptr = CTX_data_pointer_get_type(C, "modifier", rna_type);
Object *ob = (ptr.owner_id) ? (Object *)ptr.owner_id : context_active_object(C);
ModifierData *mod = static_cast<ModifierData *>(ptr.data); /* May be nullptr. */
if (mod == nullptr && ob != nullptr) {
mod = BKE_object_active_modifier(ob);
}
if (!ob || !BKE_id_is_editable(bmain, &ob->id)) {
return false;
2019-04-22 09:19:45 +10:00
}
if (obtype_flag && ((1 << ob->type) & obtype_flag) == 0) {
return false;
2019-04-22 09:19:45 +10:00
}
if (ptr.owner_id && !BKE_id_is_editable(bmain, ptr.owner_id)) {
return false;
2019-04-22 09:19:45 +10:00
}
if (!is_liboverride_allowed && BKE_modifier_is_nonlocal_in_liboverride(ob, mod)) {
CTX_wm_operator_poll_msg_set(
C, "Cannot edit modifiers coming from linked data in a library override");
return false;
}
2018-06-04 09:39:04 +02:00
if (!is_editmode_allowed && CTX_data_edit_object(C) != nullptr) {
CTX_wm_operator_poll_msg_set(C, "This modifier operation is not allowed from Edit mode");
return false;
}
return true;
}
static bool edit_modifier_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_Modifier, 0, true, false);
}
/* Used by operators performing actions allowed also on modifiers from the overridden linked object
* (not only from added 'local' ones). */
static bool edit_modifier_liboverride_allowed_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_Modifier, 0, true, true);
}
void edit_modifier_properties(wmOperatorType *ot)
{
PropertyRNA *prop = RNA_def_string(
ot->srna, "modifier", nullptr, MAX_NAME, "Modifier", "Name of the modifier to edit");
RNA_def_property_flag(prop, PROP_HIDDEN);
}
static void edit_modifier_report_property(wmOperatorType *ot)
{
PropertyRNA *prop = RNA_def_boolean(
ot->srna, "report", false, "Report", "Create a notification after the operation");
RNA_def_property_flag(prop, PROP_HIDDEN);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Generic Invoke Functions
*
* Using modifier names and data context.
* \{ */
bool edit_modifier_invoke_properties(bContext *C, wmOperator *op)
{
if (RNA_struct_property_is_set(op->ptr, "modifier")) {
return true;
}
PointerRNA ctx_ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_Modifier);
if (ctx_ptr.data != nullptr) {
ModifierData *md = static_cast<ModifierData *>(ctx_ptr.data);
RNA_string_set(op->ptr, "modifier", md->name);
return true;
}
return false;
}
/**
* If the "modifier" property is not set, fill the modifier property with the name of the modifier
* with a UI panel below the mouse cursor, unless a specific modifier is set with a context
* pointer. Used in order to apply modifier operators on hover over their panels.
*/
2021-04-26 22:19:39 -05:00
static bool edit_modifier_invoke_properties_with_hover(bContext *C,
wmOperator *op,
const wmEvent *event,
int *r_retval)
{
if (RNA_struct_find_property(op->ptr, "use_selected_objects")) {
if (event->modifier & KM_ALT) {
RNA_boolean_set(op->ptr, "use_selected_objects", true);
}
}
if (RNA_struct_property_is_set(op->ptr, "modifier")) {
return true;
}
/* Note that the context pointer is *not* the active modifier, it is set in UI layouts. */
PointerRNA ctx_ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_Modifier);
if (ctx_ptr.data != nullptr) {
ModifierData *md = static_cast<ModifierData *>(ctx_ptr.data);
RNA_string_set(op->ptr, "modifier", md->name);
return true;
}
PointerRNA *panel_ptr = UI_region_panel_custom_data_under_cursor(C, event);
if (panel_ptr == nullptr || RNA_pointer_is_null(panel_ptr)) {
*r_retval = OPERATOR_CANCELLED;
return false;
}
if (!RNA_struct_is_a(panel_ptr->type, &RNA_Modifier)) {
2021-03-30 10:03:11 -05:00
/* Work around multiple operators using the same shortcut. The operators for the other
* stacks in the property editor use the same key, and will not run after these return
* OPERATOR_CANCELLED. */
*r_retval = (OPERATOR_PASS_THROUGH | OPERATOR_CANCELLED);
return false;
}
const ModifierData *md = static_cast<const ModifierData *>(panel_ptr->data);
RNA_string_set(op->ptr, "modifier", md->name);
return true;
}
ModifierData *edit_modifier_property_get(wmOperator *op, Object *ob, int type)
{
char modifier_name[MAX_NAME];
RNA_string_get(op->ptr, "modifier", modifier_name);
ModifierData *md = BKE_modifiers_findby_name(ob, modifier_name);
2019-04-22 09:19:45 +10:00
if (md && type != 0 && md->type != type) {
md = nullptr;
2019-04-22 09:19:45 +10:00
}
return md;
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Remove Modifier Operator
* \{ */
static int modifier_remove_exec(bContext *C, wmOperator *op)
{
2012-04-28 15:42:27 +00:00
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
char name[MAX_NAME];
RNA_string_get(op->ptr, "modifier", name);
bool changed = false;
for (const PointerRNA &ptr : modifier_get_edit_objects(*C, *op)) {
Object *ob = static_cast<Object *>(ptr.data);
ModifierData *md = BKE_modifiers_findby_name(ob, name);
if (md == nullptr) {
continue;
}
int mode_orig = ob->mode;
if (!modifier_remove(op->reports, bmain, scene, ob, md)) {
continue;
}
changed = true;
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
/* if cloth/softbody was removed, particle mode could be cleared */
if (mode_orig & OB_MODE_PARTICLE_EDIT) {
if ((ob->mode & OB_MODE_PARTICLE_EDIT) == 0) {
BKE_view_layer_synced_ensure(scene, view_layer);
if (ob == BKE_view_layer_active_object_get(view_layer)) {
WM_event_add_notifier(C, NC_SCENE | ND_MODE | NS_MODE_OBJECT, nullptr);
}
}
}
}
if (!changed) {
return OPERATOR_CANCELLED;
}
if (RNA_boolean_get(op->ptr, "report")) {
BKE_reportf(op->reports, RPT_INFO, "Removed modifier: %s", name);
}
return OPERATOR_FINISHED;
}
static int modifier_remove_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_remove_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return retval;
}
void OBJECT_OT_modifier_remove(wmOperatorType *ot)
{
ot->name = "Remove Modifier";
ot->description = "Remove a modifier from the active object";
ot->idname = "OBJECT_OT_modifier_remove";
ot->invoke = modifier_remove_invoke;
ot->exec = modifier_remove_exec;
ot->poll = edit_modifier_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
edit_modifier_report_property(ot);
modifier_register_use_selected_objects_prop(ot);
}
static int modifiers_clear_exec(bContext *C, wmOperator * /*op*/)
{
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
CTX_DATA_BEGIN (C, Object *, object, selected_editable_objects) {
modifiers_clear(bmain, scene, object);
WM_main_add_notifier(NC_OBJECT | ND_MODIFIER | NA_REMOVED, object);
}
CTX_DATA_END;
return OPERATOR_FINISHED;
}
static bool modifiers_clear_poll(bContext *C)
{
if (!ED_operator_object_active_local_editable(C)) {
return false;
}
const Object *object = context_active_object(C);
if (!BKE_object_supports_modifiers(object)) {
return false;
}
return true;
}
void OBJECT_OT_modifiers_clear(wmOperatorType *ot)
{
ot->name = "Clear Object Modifiers";
ot->description = "Clear all modifiers from the selected objects";
ot->idname = "OBJECT_OT_modifiers_clear";
ot->exec = modifiers_clear_exec;
ot->poll = modifiers_clear_poll;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Move Up Modifier Operator
* \{ */
static int modifier_move_up_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
ModifierData *md = edit_modifier_property_get(op, ob, 0);
if (!md || !modifier_move_up(op->reports, RPT_WARNING, ob, md)) {
return OPERATOR_CANCELLED;
2019-04-22 09:19:45 +10:00
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2012-04-28 15:42:27 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int modifier_move_up_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_move_up_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return retval;
}
void OBJECT_OT_modifier_move_up(wmOperatorType *ot)
{
ot->name = "Move Up Modifier";
ot->description = "Move modifier up in the stack";
ot->idname = "OBJECT_OT_modifier_move_up";
ot->invoke = modifier_move_up_invoke;
ot->exec = modifier_move_up_exec;
ot->poll = edit_modifier_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Move Down Modifier Operator
* \{ */
static int modifier_move_down_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
ModifierData *md = edit_modifier_property_get(op, ob, 0);
if (!md || !modifier_move_down(op->reports, RPT_WARNING, ob, md)) {
return OPERATOR_CANCELLED;
2019-04-22 09:19:45 +10:00
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2012-04-28 15:42:27 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int modifier_move_down_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_move_down_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return retval;
}
void OBJECT_OT_modifier_move_down(wmOperatorType *ot)
{
ot->name = "Move Down Modifier";
ot->description = "Move modifier down in the stack";
ot->idname = "OBJECT_OT_modifier_move_down";
ot->invoke = modifier_move_down_invoke;
ot->exec = modifier_move_down_exec;
ot->poll = edit_modifier_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Move to Index Modifier Operator
* \{ */
static int modifier_move_to_index_exec(bContext *C, wmOperator *op)
{
char name[MAX_NAME];
RNA_string_get(op->ptr, "modifier", name);
const int index = RNA_int_get(op->ptr, "index");
bool changed = false;
for (const PointerRNA &ptr : modifier_get_edit_objects(*C, *op)) {
Object *ob = static_cast<Object *>(ptr.data);
ModifierData *md = BKE_modifiers_findby_name(ob, name);
if (!md) {
continue;
}
if (!modifier_move_to_index(op->reports, RPT_WARNING, ob, md, index, true)) {
continue;
}
changed = true;
}
if (!changed) {
return OPERATOR_CANCELLED;
}
return OPERATOR_FINISHED;
}
static int modifier_move_to_index_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_move_to_index_exec(C, op);
}
return retval;
}
void OBJECT_OT_modifier_move_to_index(wmOperatorType *ot)
{
ot->name = "Move Active Modifier to Index";
ot->description =
"Change the modifier's index in the stack so it evaluates after the set number of others";
ot->idname = "OBJECT_OT_modifier_move_to_index";
ot->invoke = modifier_move_to_index_invoke;
ot->exec = modifier_move_to_index_exec;
ot->poll = edit_modifier_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
RNA_def_int(
ot->srna, "index", 0, 0, INT_MAX, "Index", "The index to move the modifier to", 0, INT_MAX);
modifier_register_use_selected_objects_prop(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Apply Modifier Operator
* \{ */
static bool modifier_apply_poll(bContext *C)
{
if (!edit_modifier_poll_generic(C, &RNA_Modifier, 0, false, false)) {
return false;
}
Scene *scene = CTX_data_scene(C);
PointerRNA ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_Modifier);
Object *ob = (ptr.owner_id != nullptr) ? (Object *)ptr.owner_id : context_active_object(C);
ModifierData *md = static_cast<ModifierData *>(ptr.data); /* May be nullptr. */
if (ID_IS_OVERRIDE_LIBRARY(ob) || ((ob->data != nullptr) && ID_IS_OVERRIDE_LIBRARY(ob->data))) {
CTX_wm_operator_poll_msg_set(C, "Modifiers cannot be applied on override data");
return false;
}
if (md != nullptr) {
2022-10-07 22:52:53 +11:00
if ((ob->mode & OB_MODE_SCULPT) && find_multires_modifier_before(scene, md) &&
(BKE_modifier_is_same_topology(md) == false))
{
CTX_wm_operator_poll_msg_set(
C, "Constructive modifier cannot be applied to multi-res data in sculpt mode");
return false;
}
}
return true;
}
static int modifier_apply_exec_ex(bContext *C, wmOperator *op, int apply_as, bool keep_modifier)
{
Main *bmain = CTX_data_main(C);
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
2012-04-28 15:42:27 +00:00
Scene *scene = CTX_data_scene(C);
Vector<PointerRNA> objects = modifier_get_edit_objects(*C, *op);
char name[MAX_NAME];
RNA_string_get(op->ptr, "modifier", name);
const bool do_report = RNA_boolean_get(op->ptr, "report");
const int reports_len = do_report ? BLI_listbase_count(&op->reports->list) : 0;
const bool do_single_user = (apply_as == MODIFIER_APPLY_DATA) ?
RNA_boolean_get(op->ptr, "single_user") :
false;
const bool do_merge_customdata = (apply_as == MODIFIER_APPLY_DATA) ?
RNA_boolean_get(op->ptr, "merge_customdata") :
false;
const bool do_all_keyframes = (apply_as == MODIFIER_APPLY_DATA) ?
RNA_boolean_get(op->ptr, "all_keyframes") :
false;
bool changed = false;
for (const PointerRNA &ptr : objects) {
Object *ob = static_cast<Object *>(ptr.data);
ModifierData *md = BKE_modifiers_findby_name(ob, name);
if (md == nullptr) {
continue;
}
const ModifierTypeInfo *mti = BKE_modifier_get_info((ModifierType)md->type);
if (do_single_user && ID_REAL_USERS(ob->data) > 1) {
single_obdata_user_make(bmain, scene, ob);
BKE_main_id_newptr_and_tag_clear(bmain);
WM_event_add_notifier(C, NC_WINDOW, nullptr);
DEG_relations_tag_update(bmain);
}
if (!modifier_apply(bmain,
op->reports,
depsgraph,
scene,
ob,
md,
apply_as,
keep_modifier,
do_all_keyframes))
{
continue;
}
changed = true;
if (ob->type == OB_MESH && do_merge_customdata &&
ELEM(mti->type, ModifierTypeType::Constructive, ModifierTypeType::Nonconstructive))
{
BKE_mesh_merge_customdata_for_apply_modifier((Mesh *)ob->data);
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
DEG_relations_tag_update(bmain);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
}
if (!changed) {
return OPERATOR_CANCELLED;
}
if (do_report) {
/* Only add this report if the operator didn't cause another one. The purpose here is
* to alert that something happened, and the previous report will do that anyway. */
if (BLI_listbase_count(&op->reports->list) == reports_len) {
BKE_reportf(op->reports, RPT_INFO, "Applied modifier: %s", name);
}
}
return OPERATOR_FINISHED;
}
static int modifier_apply_exec(bContext *C, wmOperator *op)
{
return modifier_apply_exec_ex(C, op, MODIFIER_APPLY_DATA, false);
}
static int modifier_apply_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
PointerRNA ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_Modifier);
Object *ob = (ptr.owner_id != nullptr) ? (Object *)ptr.owner_id : context_active_object(C);
if ((ob->data != nullptr) && ID_REAL_USERS(ob->data) > 1) {
PropertyRNA *prop = RNA_struct_find_property(op->ptr, "single_user");
if (!RNA_property_is_set(op->ptr, prop)) {
RNA_property_boolean_set(op->ptr, prop, true);
}
if (RNA_property_boolean_get(op->ptr, prop)) {
return WM_operator_confirm_ex(
C,
op,
IFACE_("Apply Modifier"),
IFACE_("Make data single-user, apply modifier, and remove it from the list."),
IFACE_("Apply"),
ALERT_ICON_WARNING,
false);
}
}
return modifier_apply_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return retval;
}
void OBJECT_OT_modifier_apply(wmOperatorType *ot)
{
PropertyRNA *prop;
ot->name = "Apply Modifier";
ot->description = "Apply modifier and remove from the stack";
ot->idname = "OBJECT_OT_modifier_apply";
ot->invoke = modifier_apply_invoke;
ot->exec = modifier_apply_exec;
ot->poll = modifier_apply_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
edit_modifier_report_property(ot);
RNA_def_boolean(ot->srna,
"merge_customdata",
true,
"Merge UVs",
"For mesh objects, merge UV coordinates that share a vertex to account for "
"imprecision in some modifiers");
prop = RNA_def_boolean(ot->srna,
"single_user",
false,
"Make Data Single User",
"Make the object's data single user if needed");
RNA_def_property_flag(prop, (PropertyFlag)(PROP_HIDDEN | PROP_SKIP_SAVE));
prop = RNA_def_boolean(ot->srna,
"all_keyframes",
false,
"Apply to all keyframes",
"For Grease Pencil objects, apply the modifier to all the keyframes");
RNA_def_property_flag(prop, (PropertyFlag)(PROP_HIDDEN | PROP_SKIP_SAVE));
modifier_register_use_selected_objects_prop(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
2022-09-16 18:13:19 +10:00
/** \name Apply Modifier As Shape-Key Operator
* \{ */
static bool modifier_apply_as_shapekey_poll(bContext *C)
{
return modifier_apply_poll(C);
}
static int modifier_apply_as_shapekey_exec(bContext *C, wmOperator *op)
{
bool keep = RNA_boolean_get(op->ptr, "keep_modifier");
return modifier_apply_exec_ex(C, op, MODIFIER_APPLY_SHAPE, keep);
}
static int modifier_apply_as_shapekey_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_apply_as_shapekey_exec(C, op);
}
return retval;
}
static std::string modifier_apply_as_shapekey_get_description(bContext * /*C*/,
wmOperatorType * /*ot*/,
PointerRNA *ptr)
{
bool keep = RNA_boolean_get(ptr, "keep_modifier");
if (keep) {
return TIP_("Apply modifier as a new shapekey and keep it in the stack");
}
return "";
}
void OBJECT_OT_modifier_apply_as_shapekey(wmOperatorType *ot)
{
ot->name = "Apply Modifier as Shape Key";
ot->description = "Apply modifier as a new shape key and remove from the stack";
ot->idname = "OBJECT_OT_modifier_apply_as_shapekey";
ot->invoke = modifier_apply_as_shapekey_invoke;
ot->exec = modifier_apply_as_shapekey_exec;
ot->poll = modifier_apply_as_shapekey_poll;
ot->get_description = modifier_apply_as_shapekey_get_description;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
RNA_def_boolean(
ot->srna, "keep_modifier", false, "Keep Modifier", "Do not remove the modifier from stack");
edit_modifier_properties(ot);
edit_modifier_report_property(ot);
modifier_register_use_selected_objects_prop(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Convert Particle System Modifier to Mesh Operator
* \{ */
static int modifier_convert_exec(bContext *C, wmOperator *op)
{
2012-04-28 15:42:27 +00:00
Main *bmain = CTX_data_main(C);
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Scene *scene = CTX_data_scene(C);
ViewLayer *view_layer = CTX_data_view_layer(C);
Object *ob = context_active_object(C);
ModifierData *md = edit_modifier_property_get(op, ob, 0);
if (!md || !convert_psys_to_mesh(op->reports, bmain, depsgraph, scene, view_layer, ob, md)) {
return OPERATOR_CANCELLED;
2019-04-22 09:19:45 +10:00
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2012-04-28 15:42:27 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int modifier_convert_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return modifier_convert_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_modifier_convert(wmOperatorType *ot)
{
ot->name = "Convert Particles to Mesh";
ot->description = "Convert particles to a mesh object";
ot->idname = "OBJECT_OT_modifier_convert";
ot->invoke = modifier_convert_invoke;
ot->exec = modifier_convert_exec;
ot->poll = edit_modifier_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Copy Modifier Operator
* \{ */
static int modifier_copy_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
Scene *scene = CTX_data_scene(C);
char name[MAX_NAME];
RNA_string_get(op->ptr, "modifier", name);
bool changed = false;
for (const PointerRNA &ptr : modifier_get_edit_objects(*C, *op)) {
Object *ob = static_cast<Object *>(ptr.data);
ModifierData *md = BKE_modifiers_findby_name(ob, name);
if (!md) {
continue;
}
if (!modifier_copy(op->reports, bmain, scene, ob, md)) {
continue;
}
changed = true;
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
DEG_relations_tag_update(bmain);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
2019-04-22 09:19:45 +10:00
}
if (!changed) {
return OPERATOR_CANCELLED;
}
return OPERATOR_FINISHED;
}
static int modifier_copy_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_copy_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return retval;
}
void OBJECT_OT_modifier_copy(wmOperatorType *ot)
{
ot->name = "Copy Modifier";
ot->description = "Duplicate modifier at the same position in the stack";
ot->idname = "OBJECT_OT_modifier_copy";
ot->invoke = modifier_copy_invoke;
ot->exec = modifier_copy_exec;
ot->poll = edit_modifier_liboverride_allowed_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
modifier_register_use_selected_objects_prop(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Set Active Modifier Operator
* \{ */
static int modifier_set_active_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
ModifierData *md = edit_modifier_property_get(op, ob, 0);
/* If there is no modifier set for this operator, clear the active modifier field. */
BKE_object_modifier_set_active(ob, md);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int modifier_set_active_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_set_active_exec(C, op);
}
return retval;
}
void OBJECT_OT_modifier_set_active(wmOperatorType *ot)
{
ot->name = "Set Active Modifier";
ot->description = "Activate the modifier to use as the context";
ot->idname = "OBJECT_OT_modifier_set_active";
ot->invoke = modifier_set_active_invoke;
ot->exec = modifier_set_active_exec;
ot->poll = edit_modifier_liboverride_allowed_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Copy Modifier To Selected Operator
* \{ */
static int modifier_copy_to_selected_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
const Scene *scene = CTX_data_scene(C);
Object *obact = context_active_object(C);
ModifierData *md = edit_modifier_property_get(op, obact, 0);
if (!md) {
return OPERATOR_CANCELLED;
}
int num_copied = 0;
Vector<PointerRNA> selected_objects;
CTX_data_selected_objects(C, &selected_objects);
CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
if (ob == obact) {
continue;
}
if (!ID_IS_EDITABLE(ob)) {
continue;
}
if (modifier_copy_to_object(bmain, scene, obact, md, ob, op->reports)) {
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER | NA_ADDED, ob);
num_copied++;
}
}
CTX_DATA_END;
if (num_copied > 0) {
DEG_relations_tag_update(bmain);
}
else {
BKE_reportf(op->reports, RPT_ERROR, "Modifier '%s' was not copied to any objects", md->name);
return OPERATOR_CANCELLED;
}
return OPERATOR_FINISHED;
}
static int modifier_copy_to_selected_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
int retval;
if (edit_modifier_invoke_properties_with_hover(C, op, event, &retval)) {
return modifier_copy_to_selected_exec(C, op);
}
return retval;
}
static bool modifier_copy_to_selected_poll(bContext *C)
{
PointerRNA ptr = CTX_data_pointer_get_type(C, "modifier", &RNA_Modifier);
Object *obact = (ptr.owner_id) ? (Object *)ptr.owner_id : context_active_object(C);
ModifierData *md = static_cast<ModifierData *>(ptr.data);
/* This just mirrors the check in #BKE_object_copy_modifier,
* but there is no reasoning for it there. */
if (md && ELEM(md->type, eModifierType_Hook, eModifierType_Collision)) {
CTX_wm_operator_poll_msg_set(C, R"(Not supported for "Collision" or "Hook" modifiers)");
return false;
}
if (!obact) {
CTX_wm_operator_poll_msg_set(C, "No selected object is active");
return false;
}
if (!BKE_object_supports_modifiers(obact)) {
CTX_wm_operator_poll_msg_set(C, "Object type of source object is not supported");
return false;
}
/* This could have a performance impact in the worst case, where there are many objects selected
* and none of them pass either of the checks. But that should be uncommon, and this operator is
* only exposed in a drop-down menu anyway. */
bool found_supported_objects = false;
CTX_DATA_BEGIN (C, Object *, ob, selected_objects) {
if (ob == obact) {
continue;
}
if (!md && BKE_object_supports_modifiers(ob)) {
/* Skip type check if modifier could not be found ("modifier" context variable not set). */
found_supported_objects = true;
break;
}
2021-01-05 17:04:02 +01:00
if (BKE_object_support_modifier_type_check(ob, md->type)) {
found_supported_objects = true;
break;
}
}
CTX_DATA_END;
if (!found_supported_objects) {
CTX_wm_operator_poll_msg_set(C, "No supported objects were selected");
return false;
}
return true;
}
void OBJECT_OT_modifier_copy_to_selected(wmOperatorType *ot)
{
ot->name = "Copy Modifier to Selected";
ot->description = "Copy the modifier from the active object to all selected objects";
ot->idname = "OBJECT_OT_modifier_copy_to_selected";
ot->invoke = modifier_copy_to_selected_invoke;
ot->exec = modifier_copy_to_selected_exec;
ot->poll = modifier_copy_to_selected_poll;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
static int object_modifiers_copy_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
const Scene *scene = CTX_data_scene(C);
Object *active_object = context_active_object(C);
Vector<PointerRNA> selected_objects;
CTX_data_selected_objects(C, &selected_objects);
CTX_DATA_BEGIN (C, Object *, object, selected_objects) {
if (object == active_object) {
continue;
}
LISTBASE_FOREACH (const ModifierData *, md, &active_object->modifiers) {
if (modifier_copy_to_object(bmain, scene, active_object, md, object, op->reports)) {
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER | NA_ADDED, object);
}
}
}
CTX_DATA_END;
return OPERATOR_FINISHED;
DEG_relations_tag_update(bmain);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER | NA_ADDED, nullptr);
return OPERATOR_FINISHED;
}
static bool modifiers_copy_to_selected_poll(bContext *C)
{
if (!ED_operator_object_active_editable(C)) {
return false;
}
const Object *active_object = context_active_object(C);
if (!BKE_object_supports_modifiers(active_object)) {
return false;
}
if (BLI_listbase_is_empty(&active_object->modifiers)) {
CTX_wm_operator_poll_msg_set(C, "Active object has no modifiers");
return false;
}
return true;
}
void OBJECT_OT_modifiers_copy_to_selected(wmOperatorType *ot)
{
ot->name = "Copy Modifiers to Selected Objects";
ot->idname = "OBJECT_OT_modifiers_copy_to_selected";
ot->description = "Copy modifiers to other selected objects";
ot->exec = object_modifiers_copy_exec;
ot->poll = modifiers_copy_to_selected_poll;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Skin Modifier
* \{ */
static void modifier_skin_customdata_delete(Object *ob)
{
Mesh *mesh = static_cast<Mesh *>(ob->data);
if (BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
BM_data_layer_free(em->bm, &em->bm->vdata, CD_MVERT_SKIN);
2019-04-22 09:19:45 +10:00
}
else {
CustomData_free_layer_active(&mesh->vert_data, CD_MVERT_SKIN, mesh->verts_num);
2019-04-22 09:19:45 +10:00
}
}
2018-07-02 11:47:00 +02:00
static bool skin_poll(bContext *C)
{
2022-10-07 22:52:53 +11:00
return edit_modifier_poll_generic(C, &RNA_SkinModifier, (1 << OB_MESH), false, false);
}
2018-07-02 11:47:00 +02:00
static bool skin_edit_poll(bContext *C)
{
Object *ob = CTX_data_edit_object(C);
return (ob != nullptr &&
edit_modifier_poll_generic(C, &RNA_SkinModifier, (1 << OB_MESH), true, false) &&
!ID_IS_OVERRIDE_LIBRARY(ob) && !ID_IS_OVERRIDE_LIBRARY(ob->data));
}
static void skin_root_clear(BMVert *bm_vert, GSet *visited, const int cd_vert_skin_offset)
{
BMEdge *bm_edge;
BMIter bm_iter;
BM_ITER_ELEM (bm_edge, &bm_iter, bm_vert, BM_EDGES_OF_VERT) {
BMVert *v2 = BM_edge_other_vert(bm_edge, bm_vert);
if (BLI_gset_add(visited, v2)) {
MVertSkin *vs = static_cast<MVertSkin *>(BM_ELEM_CD_GET_VOID_P(v2, cd_vert_skin_offset));
/* clear vertex root flag and add to visited set */
vs->flag &= ~MVERT_SKIN_ROOT;
skin_root_clear(v2, visited, cd_vert_skin_offset);
}
}
}
static int skin_root_mark_exec(bContext *C, wmOperator * /*op*/)
{
Object *ob = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(ob);
BMesh *bm = em->bm;
GSet *visited = BLI_gset_ptr_new(__func__);
BKE_mesh_ensure_skin_customdata(static_cast<Mesh *>(ob->data));
const int cd_vert_skin_offset = CustomData_get_offset(&bm->vdata, CD_MVERT_SKIN);
BMVert *bm_vert;
BMIter bm_iter;
BM_ITER_MESH (bm_vert, &bm_iter, bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(bm_vert, BM_ELEM_SELECT) && BLI_gset_add(visited, bm_vert)) {
MVertSkin *vs = static_cast<MVertSkin *>(
BM_ELEM_CD_GET_VOID_P(bm_vert, cd_vert_skin_offset));
/* mark vertex as root and add to visited set */
vs->flag |= MVERT_SKIN_ROOT;
/* clear root flag from all connected vertices (recursively) */
skin_root_clear(bm_vert, visited, cd_vert_skin_offset);
}
}
BLI_gset_free(visited, nullptr);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_skin_root_mark(wmOperatorType *ot)
{
ot->name = "Skin Root Mark";
ot->description = "Mark selected vertices as roots";
ot->idname = "OBJECT_OT_skin_root_mark";
ot->poll = skin_edit_poll;
ot->exec = skin_root_mark_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
enum SkinLooseAction {
SKIN_LOOSE_MARK,
2019-04-16 16:40:47 +02:00
SKIN_LOOSE_CLEAR,
};
static int skin_loose_mark_clear_exec(bContext *C, wmOperator *op)
{
Object *ob = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(ob);
BMesh *bm = em->bm;
SkinLooseAction action = static_cast<SkinLooseAction>(RNA_enum_get(op->ptr, "action"));
if (!CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN)) {
return OPERATOR_CANCELLED;
}
BMVert *bm_vert;
BMIter bm_iter;
BM_ITER_MESH (bm_vert, &bm_iter, bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(bm_vert, BM_ELEM_SELECT)) {
MVertSkin *vs = static_cast<MVertSkin *>(
CustomData_bmesh_get(&bm->vdata, bm_vert->head.data, CD_MVERT_SKIN));
switch (action) {
case SKIN_LOOSE_MARK:
vs->flag |= MVERT_SKIN_LOOSE;
break;
case SKIN_LOOSE_CLEAR:
vs->flag &= ~MVERT_SKIN_LOOSE;
break;
}
}
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_skin_loose_mark_clear(wmOperatorType *ot)
{
static const EnumPropertyItem action_items[] = {
{SKIN_LOOSE_MARK, "MARK", 0, "Mark", "Mark selected vertices as loose"},
{SKIN_LOOSE_CLEAR, "CLEAR", 0, "Clear", "Set selected vertices as not loose"},
{0, nullptr, 0, nullptr, nullptr},
};
ot->name = "Skin Mark/Clear Loose";
ot->description = "Mark/clear selected vertices as loose";
ot->idname = "OBJECT_OT_skin_loose_mark_clear";
ot->poll = skin_edit_poll;
ot->exec = skin_loose_mark_clear_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
RNA_def_enum(ot->srna, "action", action_items, SKIN_LOOSE_MARK, "Action", nullptr);
}
static int skin_radii_equalize_exec(bContext *C, wmOperator * /*op*/)
{
Object *ob = CTX_data_edit_object(C);
BMEditMesh *em = BKE_editmesh_from_object(ob);
BMesh *bm = em->bm;
if (!CustomData_has_layer(&bm->vdata, CD_MVERT_SKIN)) {
return OPERATOR_CANCELLED;
}
BMVert *bm_vert;
BMIter bm_iter;
BM_ITER_MESH (bm_vert, &bm_iter, bm, BM_VERTS_OF_MESH) {
if (BM_elem_flag_test(bm_vert, BM_ELEM_SELECT)) {
MVertSkin *vs = static_cast<MVertSkin *>(
CustomData_bmesh_get(&bm->vdata, bm_vert->head.data, CD_MVERT_SKIN));
float avg = (vs->radius[0] + vs->radius[1]) * 0.5f;
vs->radius[0] = vs->radius[1] = avg;
}
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_skin_radii_equalize(wmOperatorType *ot)
{
ot->name = "Skin Radii Equalize";
ot->description = "Make skin radii of selected vertices equal on each axis";
ot->idname = "OBJECT_OT_skin_radii_equalize";
ot->poll = skin_edit_poll;
ot->exec = skin_radii_equalize_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
static void skin_armature_bone_create(Object *skin_ob,
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
const Span<float3> positions,
const int2 *edges,
bArmature *arm,
BLI_bitmap *edges_visited,
const GroupedSpan<int> emap,
EditBone *parent_bone,
int parent_v)
{
for (int i = 0; i < emap[parent_v].size(); i++) {
int endx = emap[parent_v][i];
const int2 &edge = edges[endx];
/* ignore edge if already visited */
2019-04-22 09:19:45 +10:00
if (BLI_BITMAP_TEST(edges_visited, endx)) {
continue;
2019-04-22 09:19:45 +10:00
}
BLI_BITMAP_ENABLE(edges_visited, endx);
int v = bke::mesh::edge_other_vert(edge, parent_v);
EditBone *bone = ED_armature_ebone_add(arm, "Bone");
bone->parent = parent_bone;
if (parent_bone != nullptr) {
bone->flag |= BONE_CONNECTED;
}
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
copy_v3_v3(bone->head, positions[parent_v]);
copy_v3_v3(bone->tail, positions[v]);
bone->rad_head = bone->rad_tail = 0.25;
2023-05-09 12:50:37 +10:00
SNPRINTF(bone->name, "Bone.%.2d", endx);
/* add bDeformGroup */
bDeformGroup *dg = BKE_object_defgroup_add_name(skin_ob, bone->name);
if (dg != nullptr) {
blender::ed::object::vgroup_vert_add(skin_ob, dg, parent_v, 1, WEIGHT_REPLACE);
blender::ed::object::vgroup_vert_add(skin_ob, dg, v, 1, WEIGHT_REPLACE);
}
skin_armature_bone_create(skin_ob, positions, edges, arm, edges_visited, emap, bone, v);
}
}
static Object *modifier_skin_armature_create(Depsgraph *depsgraph, Main *bmain, Object *skin_ob)
{
Mesh *mesh = static_cast<Mesh *>(skin_ob->data);
const Span<float3> me_positions = mesh->vert_positions();
const Span<int2> me_edges = mesh->edges();
Scene *scene_eval = DEG_get_evaluated_scene(depsgraph);
Object *ob_eval = DEG_get_evaluated_object(depsgraph, skin_ob);
const Mesh *me_eval_deform = blender::bke::mesh_get_eval_deform(
Mesh: Remove redundant custom data pointers For copy-on-write, we want to share attribute arrays between meshes where possible. Mutable pointers like `Mesh.mvert` make that difficult by making ownership vague. They also make code more complex by adding redundancy. The simplest solution is just removing them and retrieving layers from `CustomData` as needed. Similar changes have already been applied to curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of the pointers generally makes code more obvious and more reusable. Mesh data is now accessed with a C++ API (`Mesh::edges()` or `Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`). The CoW changes this commit makes possible are described in T95845 and T95842, and started in D14139 and D14140. The change also simplifies the ongoing mesh struct-of-array refactors from T95965. **RNA/Python Access Performance** Theoretically, accessing mesh elements with the RNA API may become slower, since the layer needs to be found on every random access. However, overhead is already high enough that this doesn't make a noticible differenc, and performance is actually improved in some cases. Random access can be up to 10% faster, but other situations might be a bit slower. Generally using `foreach_get/set` are the best way to improve performance. See the differential revision for more discussion about Python performance. Cycles has been updated to use raw pointers and the internal Blender mesh types, mostly because there is no sense in having this overhead when it's already compiled with Blender. In my tests this roughly halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million face grid). Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
depsgraph, scene_eval, ob_eval, &CD_MASK_BAREMESH);
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
const Span<float3> positions_eval = me_eval_deform->vert_positions();
/* add vertex weights to original mesh */
CustomData_add_layer(&mesh->vert_data, CD_MDEFORMVERT, CD_SET_DEFAULT, mesh->verts_num);
Scene *scene = DEG_get_input_scene(depsgraph);
ViewLayer *view_layer = DEG_get_input_view_layer(depsgraph);
Object *arm_ob = BKE_object_add(bmain, scene, view_layer, OB_ARMATURE, nullptr);
BKE_object_transform_copy(arm_ob, skin_ob);
bArmature *arm = static_cast<bArmature *>(arm_ob->data);
Anim: replace Bone Groups & Armature Layers with Bone Collections Armature layers (the 32 little dots) and bone groups are replaced with Bone Collections: - Bone collections are stored on the armature, and have a name that is unique within that armature. - An armature can have an arbitrary number of bone collections (instead of the fixed 32 layers). - Bones can be assigned to zero or more bone collections. - Bone collections have a visibility setting, just like objects in scene collections. - When a bone is in at least one collection, and all its collections in are hidden, the bone is hidden. In other cases (in any visible collection, or in no collection at all), the bone visibility is determined by its own 'hidden' flag. - For now, bone collections cannot be nested; they are a flat list just like bone groups were. Nestability of bone collections is intended to be implemented in a later 4.x release. - Since bone collections are defined on the armature, they can be used from both pose mode and edit mode. Versioning converts bone groups and armature layers to new bone collections. Layers that do not contain any bones are skipped. The old data structures remain in DNA and are unaltered, for limited forward compatibility. That way at least a save with Blender 4.0 will not immediately erase the bone group and armature layers and their bone assignments. Shortcuts: - M/Shift+M in pose/edit mode: move to collection (M) and add to collection (shift+M). This works similar to the M/Shift+M menus for objects & scene collections. - Ctrl+G in pose mode shows a port of the old 'bone groups' menu. This is likely to be removed in the near future, as the functionality overlaps with the M/Shift+M menus. This is the first commit of a series; the bone collections feature will be improved before the Blender 4.0 release. See #108941 for more info. Pull request: https://projects.blender.org/blender/blender/pulls/109976
2023-08-22 12:15:54 +02:00
ANIM_armature_bonecoll_show_all(arm);
arm_ob->dtx |= OB_DRAW_IN_FRONT;
arm->drawtype = ARM_LINE;
arm->edbo = MEM_cnew<ListBase>("edbo armature");
MVertSkin *mvert_skin = static_cast<MVertSkin *>(
CustomData_get_layer_for_write(&mesh->vert_data, CD_MVERT_SKIN, mesh->verts_num));
Array<int> vert_to_edge_offsets;
Array<int> vert_to_edge_indices;
const GroupedSpan<int> emap = bke::mesh::build_vert_to_edge_map(
me_edges, mesh->verts_num, vert_to_edge_offsets, vert_to_edge_indices);
BLI_bitmap *edges_visited = BLI_BITMAP_NEW(mesh->edges_num, "edge_visited");
/* NOTE: we use EditBones here, easier to set them up and use
2018-09-02 18:28:27 +10:00
* edit-armature functions to convert back to regular bones */
for (int v = 0; v < mesh->verts_num; v++) {
if (mvert_skin[v].flag & MVERT_SKIN_ROOT) {
EditBone *bone = nullptr;
/* Unless the skin root has just one adjacent edge, create
2018-09-02 18:28:27 +10:00
* a fake root bone (have it going off in the Y direction
* (arbitrary) */
if (emap[v].size() > 1) {
bone = ED_armature_ebone_add(arm, "Bone");
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
copy_v3_v3(bone->head, me_positions[v]);
copy_v3_v3(bone->tail, me_positions[v]);
bone->head[1] = 1.0f;
bone->rad_head = bone->rad_tail = 0.25;
}
if (emap[v].size() >= 1) {
Mesh: Remove redundant custom data pointers For copy-on-write, we want to share attribute arrays between meshes where possible. Mutable pointers like `Mesh.mvert` make that difficult by making ownership vague. They also make code more complex by adding redundancy. The simplest solution is just removing them and retrieving layers from `CustomData` as needed. Similar changes have already been applied to curves and point clouds (e9f82d3dc7ee, 410a6efb747f). Removing use of the pointers generally makes code more obvious and more reusable. Mesh data is now accessed with a C++ API (`Mesh::edges()` or `Mesh::edges_for_write()`), and a C API (`BKE_mesh_edges(mesh)`). The CoW changes this commit makes possible are described in T95845 and T95842, and started in D14139 and D14140. The change also simplifies the ongoing mesh struct-of-array refactors from T95965. **RNA/Python Access Performance** Theoretically, accessing mesh elements with the RNA API may become slower, since the layer needs to be found on every random access. However, overhead is already high enough that this doesn't make a noticible differenc, and performance is actually improved in some cases. Random access can be up to 10% faster, but other situations might be a bit slower. Generally using `foreach_get/set` are the best way to improve performance. See the differential revision for more discussion about Python performance. Cycles has been updated to use raw pointers and the internal Blender mesh types, mostly because there is no sense in having this overhead when it's already compiled with Blender. In my tests this roughly halves the Cycles mesh creation time (0.19s to 0.10s for a 1 million face grid). Differential Revision: https://developer.blender.org/D15488
2022-09-05 11:56:34 -05:00
skin_armature_bone_create(
Mesh: Move positions to a generic attribute **Changes** As described in T93602, this patch removes all use of the `MVert` struct, replacing it with a generic named attribute with the name `"position"`, consistent with other geometry types. Variable names have been changed from `verts` to `positions`, to align with the attribute name and the more generic design (positions are not vertices, they are just an attribute stored on the point domain). This change is made possible by previous commits that moved all other data out of `MVert` to runtime data or other generic attributes. What remains is mostly a simple type change. Though, the type still shows up 859 times, so the patch is quite large. One compromise is that now `CD_MASK_BAREMESH` now contains `CD_PROP_FLOAT3`. With the general move towards generic attributes over custom data types, we are removing use of these type masks anyway. **Benefits** The most obvious benefit is reduced memory usage and the benefits that brings in memory-bound situations. `float3` is only 3 bytes, in comparison to `MVert` which was 4. When there are millions of vertices this starts to matter more. The other benefits come from using a more generic type. Instead of writing algorithms specifically for `MVert`, code can just use arrays of vectors. This will allow eliminating many temporary arrays or wrappers used to extract positions. Many possible improvements aren't implemented in this patch, though I did switch simplify or remove the process of creating temporary position arrays in a few places. The design clarity that "positions are just another attribute" brings allows removing explicit copying of vertices in some procedural operations-- they are just processed like most other attributes. **Performance** This touches so many areas that it's hard to benchmark exhaustively, but I observed some areas as examples. * The mesh line node with 4 million count was 1.5x (8ms to 12ms) faster. * The Spring splash screen went from ~4.3 to ~4.5 fps. * The subdivision surface modifier/node was slightly faster RNA access through Python may be slightly slower, since now we need a name lookup instead of just a custom data type lookup for each index. **Future Improvements** * Remove uses of "vert_coords" functions: * `BKE_mesh_vert_coords_alloc` * `BKE_mesh_vert_coords_get` * `BKE_mesh_vert_coords_apply{_with_mat4}` * Remove more hidden copying of positions * General simplification now possible in many areas * Convert more code to C++ to use `float3` instead of `float[3]` * Currently `reinterpret_cast` is used for those C-API functions Differential Revision: https://developer.blender.org/D15982
2023-01-10 00:10:43 -05:00
skin_ob, positions_eval, me_edges.data(), arm, edges_visited, emap, bone, v);
}
}
}
MEM_freeN(edges_visited);
2018-06-06 15:50:24 +02:00
ED_armature_from_edit(bmain, arm);
ED_armature_edit_free(arm);
return arm_ob;
}
static int skin_armature_create_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Object *ob = CTX_data_active_object(C);
Mesh *mesh = static_cast<Mesh *>(ob->data);
ModifierData *skin_md;
if (!CustomData_has_layer(&mesh->vert_data, CD_MVERT_SKIN)) {
BKE_reportf(op->reports, RPT_WARNING, "Mesh '%s' has no skin vertex data", mesh->id.name + 2);
return OPERATOR_CANCELLED;
}
/* create new armature */
Object *arm_ob = modifier_skin_armature_create(depsgraph, bmain, ob);
/* add a modifier to connect the new armature to the mesh */
ArmatureModifierData *arm_md = (ArmatureModifierData *)BKE_modifier_new(eModifierType_Armature);
if (arm_md) {
skin_md = edit_modifier_property_get(op, ob, eModifierType_Skin);
BLI_insertlinkafter(&ob->modifiers, skin_md, arm_md);
Modifiers: add unique modifier identifiers This adds a new `ModifierData.persistent_uid` integer property with the following properties: * It's unique within the object. * Match between the original and evaluated object. * Stable across Blender sessions. * Stable across renames and reorderings of modifiers. Potential use-cases: * Everywhere where we currently use the name as identifier. For example, `ModifierComputeContext` and `ModifierViewerPathElem`. * Can be used as part of a key in `IDCacheKey` to support caches that stay in-tact across undo steps. * Can be stored in the `SpaceNode` to identify the modifier whose geometry node tree is currently pinned (this could use the name currently, but that hasn't been implemented yet). This new identifier has some overlap with `ModifierData.session_uid`, but there are some differences: * `session_uid` is unique within the entire Blender session (except for duplicates between the original and evaluated data blocks). * `session_uid` is not stable across Blender sessions. Especially due to the first difference, it's not immediately obvious that the new `persistent_uid` can fulfill all use-cases of the existing `session_uid`. Nevertheless, this seems likely and will be cleaned up separately. Unfortunately, there is not a single place where modifiers are added to objects currently. Therefore, there are quite a few places that need to ensure valid identifiers. I tried to catch all the places, but it's hard to be sure. Therefore, I added an assert in `object_copy_data` that checks if all identifiers are valid. This way, we should be notified relatively quickly if issues are caused by invalid identifiers. Pull Request: https://projects.blender.org/blender/blender/pulls/117347
2024-02-06 17:10:40 +01:00
BKE_modifiers_persistent_uid_init(*arm_ob, arm_md->modifier);
arm_md->object = arm_ob;
arm_md->deformflag = ARM_DEF_VGROUP | ARM_DEF_QUATERNION;
DEG_relations_tag_update(bmain);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
}
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int skin_armature_create_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return skin_armature_create_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_skin_armature_create(wmOperatorType *ot)
{
ot->name = "Skin Armature Create";
ot->description = "Create an armature that parallels the skin layout";
ot->idname = "OBJECT_OT_skin_armature_create";
ot->poll = skin_poll;
ot->invoke = skin_armature_create_invoke;
ot->exec = skin_armature_create_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Delta Mesh Bind Operator
* \{ */
2018-07-02 11:47:00 +02:00
static bool correctivesmooth_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_CorrectiveSmoothModifier, 0, true, false);
}
static int correctivesmooth_bind_exec(bContext *C, wmOperator *op)
{
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Scene *scene = CTX_data_scene(C);
Object *ob = context_active_object(C);
CorrectiveSmoothModifierData *csmd = (CorrectiveSmoothModifierData *)edit_modifier_property_get(
op, ob, eModifierType_CorrectiveSmooth);
if (!csmd) {
return OPERATOR_CANCELLED;
}
if (!BKE_modifier_is_enabled(scene, &csmd->modifier, eModifierMode_Realtime)) {
BKE_report(op->reports, RPT_ERROR, "Modifier is disabled");
return OPERATOR_CANCELLED;
}
const bool is_bind = (csmd->bind_coords != nullptr);
MEM_SAFE_FREE(csmd->bind_coords);
MEM_SAFE_FREE(csmd->delta_cache.deltas);
if (is_bind) {
/* toggle off */
csmd->bind_coords_num = 0;
}
else {
/* Signal to modifier to recalculate. */
CorrectiveSmoothModifierData *csmd_eval = (CorrectiveSmoothModifierData *)
BKE_modifier_get_evaluated(depsgraph, ob, &csmd->modifier);
csmd_eval->bind_coords_num = uint(-1);
/* Force modifier to run, it will call binding routine
* (this has to happen outside of depsgraph evaluation). */
object_force_modifier_bind_simple_options(depsgraph, ob, &csmd->modifier);
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int correctivesmooth_bind_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return correctivesmooth_bind_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_correctivesmooth_bind(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Corrective Smooth Bind";
ot->description = "Bind base pose in Corrective Smooth modifier";
ot->idname = "OBJECT_OT_correctivesmooth_bind";
/* api callbacks */
ot->poll = correctivesmooth_poll;
ot->invoke = correctivesmooth_bind_invoke;
ot->exec = correctivesmooth_bind_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Mesh Deform Bind Operator
* \{ */
2018-07-02 11:47:00 +02:00
static bool meshdeform_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_MeshDeformModifier, 0, true, false);
}
static int meshdeform_bind_exec(bContext *C, wmOperator *op)
{
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
Object *ob = context_active_object(C);
MeshDeformModifierData *mmd = (MeshDeformModifierData *)edit_modifier_property_get(
op, ob, eModifierType_MeshDeform);
if (mmd == nullptr) {
return OPERATOR_CANCELLED;
}
if (mmd->bindcagecos != nullptr) {
MEM_SAFE_FREE(mmd->bindcagecos);
MEM_SAFE_FREE(mmd->dyngrid);
MEM_SAFE_FREE(mmd->dyninfluences);
MEM_SAFE_FREE(mmd->bindinfluences);
MEM_SAFE_FREE(mmd->bindoffsets);
MEM_SAFE_FREE(mmd->dynverts);
MEM_SAFE_FREE(mmd->bindweights); /* Deprecated */
MEM_SAFE_FREE(mmd->bindcos); /* Deprecated */
mmd->verts_num = 0;
mmd->cage_verts_num = 0;
mmd->influences_num = 0;
}
else {
/* Force modifier to run, it will call binding routine
* (this has to happen outside of depsgraph evaluation). */
MeshDeformModifierData *mmd_eval = (MeshDeformModifierData *)BKE_modifier_get_evaluated(
depsgraph, ob, &mmd->modifier);
mmd_eval->bindfunc = ED_mesh_deform_bind_callback;
object_force_modifier_bind_simple_options(depsgraph, ob, &mmd->modifier);
mmd_eval->bindfunc = nullptr;
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int meshdeform_bind_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return meshdeform_bind_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_meshdeform_bind(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Mesh Deform Bind";
ot->description = "Bind mesh to cage in mesh deform modifier";
ot->idname = "OBJECT_OT_meshdeform_bind";
/* api callbacks */
ot->poll = meshdeform_poll;
ot->invoke = meshdeform_bind_invoke;
ot->exec = meshdeform_bind_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Explode Refresh Operator
* \{ */
2018-07-02 11:47:00 +02:00
static bool explode_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_ExplodeModifier, 0, true, false);
}
static int explode_refresh_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
ExplodeModifierData *emd = (ExplodeModifierData *)edit_modifier_property_get(
op, ob, eModifierType_Explode);
2019-04-22 09:19:45 +10:00
if (!emd) {
return OPERATOR_CANCELLED;
2019-04-22 09:19:45 +10:00
}
emd->flag |= eExplodeFlag_CalcFaces;
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2012-04-28 15:42:27 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int explode_refresh_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return explode_refresh_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_explode_refresh(wmOperatorType *ot)
{
ot->name = "Explode Refresh";
ot->description = "Refresh data in the Explode modifier";
ot->idname = "OBJECT_OT_explode_refresh";
ot->poll = explode_poll;
ot->invoke = explode_refresh_invoke;
ot->exec = explode_refresh_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Ocean Bake Operator
* \{ */
2018-07-02 11:47:00 +02:00
static bool ocean_bake_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_OceanModifier, 0, true, false);
}
struct OceanBakeJob {
/* from wmJob */
Object *owner;
bool *stop, *do_update;
float *progress;
int current_frame;
OceanCache *och;
Ocean *ocean;
OceanModifierData *omd;
};
static void oceanbake_free(void *customdata)
{
OceanBakeJob *oj = static_cast<OceanBakeJob *>(customdata);
MEM_delete(oj);
}
/* called by oceanbake, only to check job 'stop' value */
static int oceanbake_breakjob(void * /*customdata*/)
{
// OceanBakeJob *ob = (OceanBakeJob *)customdata;
// return *(ob->stop);
/* this is not nice yet, need to make the jobs list template better
2018-09-02 18:28:27 +10:00
* for identifying/acting upon various different jobs */
/* but for now we'll reuse the render break... */
return (G.is_break);
}
/* called by oceanbake, wmJob sends notifier */
static void oceanbake_update(void *customdata, float progress, int *cancel)
{
OceanBakeJob *oj = static_cast<OceanBakeJob *>(customdata);
2019-04-22 09:19:45 +10:00
if (oceanbake_breakjob(oj)) {
*cancel = 1;
2019-04-22 09:19:45 +10:00
}
*(oj->do_update) = true;
2012-04-28 15:42:27 +00:00
*(oj->progress) = progress;
}
static void oceanbake_startjob(void *customdata, wmJobWorkerStatus *worker_status)
{
OceanBakeJob *oj = static_cast<OceanBakeJob *>(customdata);
oj->stop = &worker_status->stop;
oj->do_update = &worker_status->do_update;
oj->progress = &worker_status->progress;
G.is_break = false; /* XXX shared with render - replace with job 'stop' switch */
2015-04-04 15:18:52 +11:00
BKE_ocean_bake(oj->ocean, oj->och, oceanbake_update, (void *)oj);
worker_status->do_update = true;
worker_status->stop = false;
}
static void oceanbake_endjob(void *customdata)
{
OceanBakeJob *oj = static_cast<OceanBakeJob *>(customdata);
if (oj->ocean) {
2015-04-04 15:18:52 +11:00
BKE_ocean_free(oj->ocean);
oj->ocean = nullptr;
}
oj->omd->oceancache = oj->och;
oj->omd->cached = true;
Object *ob = oj->owner;
DEG_id_tag_update(&ob->id, ID_RECALC_SYNC_TO_EVAL);
}
static int ocean_bake_exec(bContext *C, wmOperator *op)
{
Main *bmain = CTX_data_main(C);
Object *ob = context_active_object(C);
OceanModifierData *omd = (OceanModifierData *)edit_modifier_property_get(
op, ob, eModifierType_Ocean);
Scene *scene = CTX_data_scene(C);
2014-02-03 18:55:59 +11:00
const bool free = RNA_boolean_get(op->ptr, "free");
2019-04-22 09:19:45 +10:00
if (!omd) {
return OPERATOR_CANCELLED;
2019-04-22 09:19:45 +10:00
}
if (free) {
BKE_ocean_free_modifier_cache(omd);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2012-04-28 15:42:27 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
OceanCache *och = BKE_ocean_init_cache(omd->cachepath,
BKE_modifier_path_relbase(bmain, ob),
omd->bakestart,
omd->bakeend,
omd->wave_scale,
omd->chop_amount,
omd->foam_coverage,
omd->foam_fade,
omd->resolution);
och->time = static_cast<float *>(MEM_mallocN(och->duration * sizeof(float), "foam bake time"));
int cfra = scene->r.cfra;
/* precalculate time variable before baking */
int i = 0;
T77086 Animation: Passing Dependency Graph to Drivers Custom driver functions need access to the dependency graph that is triggering the evaluation of the driver. This patch passes the dependency graph pointer through all the animation-related calls. Instead of passing the evaluation time to functions, the code now passes an `AnimationEvalContext` pointer: ``` typedef struct AnimationEvalContext { struct Depsgraph *const depsgraph; const float eval_time; } AnimationEvalContext; ``` These structs are read-only, meaning that the code cannot change the evaluation time. Note that the `depsgraph` pointer itself is const, but it points to a non-const depsgraph. FCurves and Drivers can be evaluated at a different time than the current scene time, for example when evaluating NLA strips. This means that, even though the current time is stored in the dependency graph, we need an explicit evaluation time. There are two functions that allow creation of `AnimationEvalContext` objects: - `BKE_animsys_eval_context_construct(Depsgraph *depsgraph, float eval_time)`, which creates a new context object from scratch, and - `BKE_animsys_eval_context_construct_at(AnimationEvalContext *anim_eval_context, float eval_time)`, which can be used to create a `AnimationEvalContext` with the same depsgraph, but at a different time. This makes it possible to later add fields without changing any of the code that just want to change the eval time. This also provides a fix for T75553, although it does require a change to the custom driver function. The driver should call `custom_function(depsgraph)`, and the function should use that depsgraph instead of information from `bpy.context`. Reviewed By: brecht, sergey Differential Revision: https://developer.blender.org/D8047
2020-07-17 17:38:09 +02:00
Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
for (int f = omd->bakestart; f <= omd->bakeend; f++) {
/* For now only simple animation of time value is supported, nothing else.
* No drivers or other modifier parameters. */
/* TODO(sergey): This operates on an original data, so no flush is needed. However, baking
* usually should happen on an evaluated objects, so this seems to be deeper issue here. */
T77086 Animation: Passing Dependency Graph to Drivers Custom driver functions need access to the dependency graph that is triggering the evaluation of the driver. This patch passes the dependency graph pointer through all the animation-related calls. Instead of passing the evaluation time to functions, the code now passes an `AnimationEvalContext` pointer: ``` typedef struct AnimationEvalContext { struct Depsgraph *const depsgraph; const float eval_time; } AnimationEvalContext; ``` These structs are read-only, meaning that the code cannot change the evaluation time. Note that the `depsgraph` pointer itself is const, but it points to a non-const depsgraph. FCurves and Drivers can be evaluated at a different time than the current scene time, for example when evaluating NLA strips. This means that, even though the current time is stored in the dependency graph, we need an explicit evaluation time. There are two functions that allow creation of `AnimationEvalContext` objects: - `BKE_animsys_eval_context_construct(Depsgraph *depsgraph, float eval_time)`, which creates a new context object from scratch, and - `BKE_animsys_eval_context_construct_at(AnimationEvalContext *anim_eval_context, float eval_time)`, which can be used to create a `AnimationEvalContext` with the same depsgraph, but at a different time. This makes it possible to later add fields without changing any of the code that just want to change the eval time. This also provides a fix for T75553, although it does require a change to the custom driver function. The driver should call `custom_function(depsgraph)`, and the function should use that depsgraph instead of information from `bpy.context`. Reviewed By: brecht, sergey Differential Revision: https://developer.blender.org/D8047
2020-07-17 17:38:09 +02:00
const AnimationEvalContext anim_eval_context = BKE_animsys_eval_context_construct(depsgraph,
f);
BKE_animsys_evaluate_animdata((ID *)ob, ob->adt, &anim_eval_context, ADT_RECALC_ANIM, false);
och->time[i] = omd->time;
i++;
}
2021-02-05 16:23:34 +11:00
/* Make a copy of ocean to use for baking - thread-safety. */
Ocean *ocean = BKE_ocean_add();
BKE_ocean_init_from_modifier(ocean, omd, omd->resolution);
#if 0
2015-04-04 15:18:52 +11:00
BKE_ocean_bake(ocean, och);
omd->oceancache = och;
omd->cached = true;
scene->r.cfra = cfra;
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
2012-04-28 15:42:27 +00:00
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
#endif
/* job stuff */
scene->r.cfra = cfra;
/* setup job */
wmJob *wm_job = WM_jobs_get(CTX_wm_manager(C),
CTX_wm_window(C),
scene,
"Ocean Simulation",
WM_JOB_PROGRESS,
WM_JOB_TYPE_OBJECT_SIM_OCEAN);
OceanBakeJob *oj = MEM_cnew<OceanBakeJob>("ocean bake job");
oj->owner = ob;
oj->ocean = ocean;
oj->och = och;
oj->omd = omd;
WM_jobs_customdata_set(wm_job, oj, oceanbake_free);
WM_jobs_timer(wm_job, 0.1, NC_OBJECT | ND_MODIFIER, NC_OBJECT | ND_MODIFIER);
WM_jobs_callbacks(wm_job, oceanbake_startjob, nullptr, nullptr, oceanbake_endjob);
WM_jobs_start(CTX_wm_manager(C), wm_job);
return OPERATOR_FINISHED;
}
static int ocean_bake_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return ocean_bake_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_ocean_bake(wmOperatorType *ot)
{
ot->name = "Bake Ocean";
ot->description = "Bake an image sequence of ocean data";
ot->idname = "OBJECT_OT_ocean_bake";
ot->poll = ocean_bake_poll;
ot->invoke = ocean_bake_invoke;
ot->exec = ocean_bake_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
RNA_def_boolean(ot->srna, "free", false, "Free", "Free the bake, rather than generating it");
}
/** \} */
/* ------------------------------------------------------------------- */
2021-09-01 21:41:23 +10:00
/** \name Laplacian-Deform Bind Operator
* \{ */
2018-07-02 11:47:00 +02:00
static bool laplaciandeform_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_LaplacianDeformModifier, 0, false, false);
}
static int laplaciandeform_bind_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
LaplacianDeformModifierData *lmd = (LaplacianDeformModifierData *)edit_modifier_property_get(
op, ob, eModifierType_LaplacianDeform);
if (lmd == nullptr) {
return OPERATOR_CANCELLED;
}
if (lmd->flag & MOD_LAPLACIANDEFORM_BIND) {
lmd->flag &= ~MOD_LAPLACIANDEFORM_BIND;
}
else {
lmd->flag |= MOD_LAPLACIANDEFORM_BIND;
}
2020-05-08 19:02:03 +10:00
LaplacianDeformModifierData *lmd_eval = (LaplacianDeformModifierData *)
BKE_modifier_get_evaluated(depsgraph, ob, &lmd->modifier);
lmd_eval->flag = lmd->flag;
/* Force modifier to run, it will call binding routine
* (this has to happen outside of depsgraph evaluation). */
object_force_modifier_bind_simple_options(depsgraph, ob, &lmd->modifier);
/* This is hard to know from the modifier itself whether the evaluation is
* happening for binding or not. So we copy all the required data here. */
lmd->verts_num = lmd_eval->verts_num;
if (lmd_eval->vertexco == nullptr) {
MEM_SAFE_FREE(lmd->vertexco);
}
else {
lmd->vertexco = static_cast<float *>(MEM_dupallocN(lmd_eval->vertexco));
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int laplaciandeform_bind_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return laplaciandeform_bind_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_laplaciandeform_bind(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Laplacian Deform Bind";
ot->description = "Bind mesh to system in laplacian deform modifier";
ot->idname = "OBJECT_OT_laplaciandeform_bind";
/* api callbacks */
ot->poll = laplaciandeform_poll;
ot->invoke = laplaciandeform_bind_invoke;
ot->exec = laplaciandeform_bind_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Surface Deform Bind Operator
* \{ */
2018-07-02 11:47:00 +02:00
static bool surfacedeform_bind_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_SurfaceDeformModifier, 0, true, false);
}
static int surfacedeform_bind_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
Refactor access to dependency graph This change ensures that operators which needs access to evaluated data first makes sure there is a dependency graph. Other accesses to the dependency graph made it more explicit about whether they just need a valid dependency graph pointer or whether they expect the graph to be already evaluated. This replaces OPTYPE_USE_EVAL_DATA which is now removed. Some general rules about usage of accessors: - Drawing is expected to happen from a fully evaluated dependency graph. There is now a function to access it, which will in the future control that dependency graph is actually evaluated. This check is not yet done because there are some things to be taken care about first: for example, post-update hooks might leave scene in a state where something is still tagged for update. - All operators which needs to access evaluated state must use CTX_data_ensure_evaluated_depsgraph(). This function replaces OPTYPE_USE_EVAL_DATA. The call is generally to be done in the very beginning of the operator, prior other logic (unless this is some comprehensive operator which might or might not need access to an evaluated state). This call is never to be used from a loop. If some utility function requires evaluated state of dependency graph the graph is to be passed as an explicit argument. This way it is clear that no evaluation happens in a loop or something like this. - All cases which needs to know dependency graph pointer, but which doesn't want to actually evaluate it can use old-style function CTX_data_depsgraph_pointer(), assuming that underlying code will ensure dependency graph is evaluated prior to accessing it. - The new functions are replacing OPTYPE_USE_EVAL_DATA, so now it is explicit and local about where dependency graph is being ensured. This commit also contains some fixes of wrong usage of evaluation functions on original objects. Ideally should be split out, but in reality with all the APIs being renamed is quite tricky. Fixes T67454: Blender crash on rapid undo and select Speculation here is that sometimes undo and selection operators are sometimes handled in the same event loop iteration, which leaves non-evaluated dependency graph. Fixes T67973: Crash on Fix Deforms operator Fixes T67902: Crash when undo a loop cut Reviewers: brecht Reviewed By: brecht Subscribers: lichtwerk Maniphest Tasks: T67454 Differential Revision: https://developer.blender.org/D5343
2019-07-25 16:36:22 +02:00
Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
SurfaceDeformModifierData *smd = (SurfaceDeformModifierData *)edit_modifier_property_get(
op, ob, eModifierType_SurfaceDeform);
if (smd == nullptr) {
return OPERATOR_CANCELLED;
}
if (smd->flags & MOD_SDEF_BIND) {
smd->flags &= ~MOD_SDEF_BIND;
}
else if (smd->target) {
smd->flags |= MOD_SDEF_BIND;
}
SurfaceDeformModifierData *smd_eval = (SurfaceDeformModifierData *)BKE_modifier_get_evaluated(
depsgraph, ob, &smd->modifier);
smd_eval->flags = smd->flags;
/* Force modifier to run, it will call binding routine
* (this has to happen outside of depsgraph evaluation). */
object_force_modifier_bind_simple_options(depsgraph, ob, &smd->modifier);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int surfacedeform_bind_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return surfacedeform_bind_exec(C, op);
2019-04-22 09:19:45 +10:00
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_surfacedeform_bind(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Surface Deform Bind";
ot->description = "Bind mesh to target in surface deform modifier";
ot->idname = "OBJECT_OT_surfacedeform_bind";
/* api callbacks */
ot->poll = surfacedeform_bind_poll;
ot->invoke = surfacedeform_bind_invoke;
ot->exec = surfacedeform_bind_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Toggle Value or Attribute Operator
*
* \note This operator basically only exists to provide a better tooltip for the toggle button,
* since it is stored as an IDProperty. It also stops the button from being highlighted when
* "use_attribute" is on, which isn't expected.
* \{ */
static int geometry_nodes_input_attribute_toggle_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
char modifier_name[MAX_NAME];
RNA_string_get(op->ptr, "modifier_name", modifier_name);
NodesModifierData *nmd = (NodesModifierData *)BKE_modifiers_findby_name(ob, modifier_name);
if (nmd == nullptr) {
return OPERATOR_CANCELLED;
}
char input_name[MAX_NAME];
RNA_string_get(op->ptr, "input_name", input_name);
IDProperty *use_attribute = IDP_GetPropertyFromGroup(
nmd->settings.properties, std::string(input_name + std::string("_use_attribute")).c_str());
if (!use_attribute) {
return OPERATOR_CANCELLED;
}
if (use_attribute->type == IDP_INT) {
IDP_Int(use_attribute) = !IDP_Int(use_attribute);
}
else if (use_attribute->type == IDP_BOOLEAN) {
IDP_Bool(use_attribute) = !IDP_Bool(use_attribute);
}
else {
return OPERATOR_CANCELLED;
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_geometry_nodes_input_attribute_toggle(wmOperatorType *ot)
{
ot->name = "Input Attribute Toggle";
ot->description =
"Switch between an attribute and a single value to define the data for every element";
ot->idname = "OBJECT_OT_geometry_nodes_input_attribute_toggle";
ot->exec = geometry_nodes_input_attribute_toggle_exec;
ot->poll = ED_operator_object_active;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
RNA_def_string(ot->srna, "input_name", nullptr, 0, "Input Name", "");
RNA_def_string(ot->srna, "modifier_name", nullptr, MAX_NAME, "Modifier Name", "");
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Copy and Assign Geometry Node Group operator
* \{ */
static int geometry_node_tree_copy_assign_exec(bContext *C, wmOperator * /*op*/)
{
Main *bmain = CTX_data_main(C);
Object *ob = context_active_object(C);
ModifierData *md = BKE_object_active_modifier(ob);
if (!(md && md->type == eModifierType_Nodes)) {
return OPERATOR_CANCELLED;
}
NodesModifierData *nmd = (NodesModifierData *)md;
bNodeTree *tree = nmd->node_group;
if (tree == nullptr) {
return OPERATOR_CANCELLED;
}
bNodeTree *new_tree = (bNodeTree *)BKE_id_copy_ex(
bmain, &tree->id, nullptr, LIB_ID_COPY_ACTIONS | LIB_ID_COPY_DEFAULT);
nmd->flag &= ~NODES_MODIFIER_HIDE_DATABLOCK_SELECTOR;
if (new_tree == nullptr) {
return OPERATOR_CANCELLED;
}
nmd->node_group = new_tree;
id_us_min(&tree->id);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY);
DEG_relations_tag_update(bmain);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
void OBJECT_OT_geometry_node_tree_copy_assign(wmOperatorType *ot)
{
ot->name = "Copy Geometry Node Group";
ot->description = "Copy the active geometry node group and assign it to the active modifier";
ot->idname = "OBJECT_OT_geometry_node_tree_copy_assign";
ot->exec = geometry_node_tree_copy_assign_exec;
ot->poll = ED_operator_object_active;
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO;
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Dash Modifier
* \{ */
static bool dash_modifier_segment_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_GreasePencilDashModifierData, 0, false, false);
}
static int dash_modifier_segment_add_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
auto *dmd = reinterpret_cast<GreasePencilDashModifierData *>(
edit_modifier_property_get(op, ob, eModifierType_GreasePencilDash));
if (dmd == nullptr) {
return OPERATOR_CANCELLED;
}
GreasePencilDashModifierSegment *new_segments = static_cast<GreasePencilDashModifierSegment *>(
MEM_malloc_arrayN(dmd->segments_num + 1, sizeof(GreasePencilDashModifierSegment), __func__));
const int new_active_index = std::clamp(dmd->segment_active_index + 1, 0, dmd->segments_num);
if (dmd->segments_num != 0) {
/* Copy the segments before the new segment. */
memcpy(new_segments,
dmd->segments_array,
sizeof(GreasePencilDashModifierSegment) * new_active_index);
/* Copy the segments after the new segment. */
memcpy(new_segments + new_active_index + 1,
dmd->segments_array + new_active_index,
sizeof(GreasePencilDashModifierSegment) * (dmd->segments_num - new_active_index));
}
/* Create the new segment. */
GreasePencilDashModifierSegment *ds = &new_segments[new_active_index];
memcpy(ds,
DNA_struct_default_get(GreasePencilDashModifierSegment),
sizeof(GreasePencilDashModifierSegment));
BLI_uniquename_cb(
[&](const StringRef name) {
for (const GreasePencilDashModifierSegment &ds : dmd->segments()) {
if (STREQ(ds.name, name.data())) {
return true;
}
}
return false;
},
'.',
ds->name);
MEM_SAFE_FREE(dmd->segments_array);
dmd->segments_array = new_segments;
dmd->segments_num++;
dmd->segment_active_index = new_active_index;
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY | ID_RECALC_SYNC_TO_EVAL);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int dash_modifier_segment_add_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return dash_modifier_segment_add_exec(C, op);
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_grease_pencil_dash_modifier_segment_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Segment";
ot->description = "Add a segment to the dash modifier";
ot->idname = "OBJECT_OT_grease_pencil_dash_modifier_segment_add";
/* api callbacks */
ot->poll = dash_modifier_segment_poll;
ot->invoke = dash_modifier_segment_add_invoke;
ot->exec = dash_modifier_segment_add_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
static void dash_modifier_segment_free(GreasePencilDashModifierSegment * /*ds*/) {}
static int dash_modifier_segment_remove_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
auto *dmd = reinterpret_cast<GreasePencilDashModifierData *>(
edit_modifier_property_get(op, ob, eModifierType_GreasePencilDash));
if (dmd == nullptr) {
return OPERATOR_CANCELLED;
}
if (!dmd->segments().index_range().contains(dmd->segment_active_index)) {
return OPERATOR_CANCELLED;
}
dna::array::remove_index(&dmd->segments_array,
&dmd->segments_num,
&dmd->segment_active_index,
dmd->segment_active_index,
dash_modifier_segment_free);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY | ID_RECALC_SYNC_TO_EVAL);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int dash_modifier_segment_remove_invoke(bContext *C,
wmOperator *op,
const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return dash_modifier_segment_remove_exec(C, op);
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_grease_pencil_dash_modifier_segment_remove(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove Dash Segment";
ot->description = "Remove the active segment from the dash modifier";
ot->idname = "OBJECT_OT_grease_pencil_dash_modifier_segment_remove";
/* api callbacks */
ot->poll = dash_modifier_segment_poll;
ot->invoke = dash_modifier_segment_remove_invoke;
ot->exec = dash_modifier_segment_remove_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
RNA_def_int(
ot->srna, "index", 0, 0, INT_MAX, "Index", "Index of the segment to remove", 0, INT_MAX);
}
enum class DashSegmentMoveDirection {
Up = -1,
Down = 1,
};
static int dash_modifier_segment_move_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
auto *dmd = reinterpret_cast<GreasePencilDashModifierData *>(
edit_modifier_property_get(op, ob, eModifierType_GreasePencilDash));
if (dmd == nullptr) {
return OPERATOR_CANCELLED;
}
if (dmd->segments_num < 2) {
return OPERATOR_CANCELLED;
}
const DashSegmentMoveDirection direction = DashSegmentMoveDirection(
RNA_enum_get(op->ptr, "type"));
switch (direction) {
case DashSegmentMoveDirection::Up:
if (dmd->segment_active_index == 0) {
return OPERATOR_CANCELLED;
}
std::swap(dmd->segments_array[dmd->segment_active_index],
dmd->segments_array[dmd->segment_active_index - 1]);
dmd->segment_active_index--;
break;
case DashSegmentMoveDirection::Down:
if (dmd->segment_active_index == dmd->segments_num - 1) {
return OPERATOR_CANCELLED;
}
std::swap(dmd->segments_array[dmd->segment_active_index],
dmd->segments_array[dmd->segment_active_index + 1]);
dmd->segment_active_index++;
break;
default:
return OPERATOR_CANCELLED;
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY | ID_RECALC_SYNC_TO_EVAL);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int dash_modifier_segment_move_invoke(bContext *C,
wmOperator *op,
const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return dash_modifier_segment_move_exec(C, op);
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_grease_pencil_dash_modifier_segment_move(wmOperatorType *ot)
{
static const EnumPropertyItem segment_move[] = {
{int(DashSegmentMoveDirection::Up), "UP", 0, "Up", ""},
{int(DashSegmentMoveDirection::Down), "DOWN", 0, "Down", ""},
{0, nullptr, 0, nullptr, nullptr},
};
/* identifiers */
ot->name = "Move Dash Segment";
ot->description = "Move the active dash segment up or down";
ot->idname = "OBJECT_OT_grease_pencil_dash_modifier_segment_move";
/* api callbacks */
ot->poll = dash_modifier_segment_poll;
ot->invoke = dash_modifier_segment_move_invoke;
ot->exec = dash_modifier_segment_move_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
ot->prop = RNA_def_enum(ot->srna, "type", segment_move, 0, "Type", "");
}
/** \} */
/* ------------------------------------------------------------------- */
/** \name Time Modifier
* \{ */
static bool time_modifier_segment_poll(bContext *C)
{
return edit_modifier_poll_generic(C, &RNA_GreasePencilTimeModifier, 0, false, false);
}
static int time_modifier_segment_add_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
auto *tmd = reinterpret_cast<GreasePencilTimeModifierData *>(
edit_modifier_property_get(op, ob, eModifierType_GreasePencilTime));
if (tmd == nullptr) {
return OPERATOR_CANCELLED;
}
GreasePencilTimeModifierSegment *new_segments = static_cast<GreasePencilTimeModifierSegment *>(
MEM_malloc_arrayN(tmd->segments_num + 1, sizeof(GreasePencilTimeModifierSegment), __func__));
const int new_active_index = std::clamp(tmd->segment_active_index + 1, 0, tmd->segments_num);
if (tmd->segments_num != 0) {
/* Copy the segments before the new segment. */
memcpy(new_segments,
tmd->segments_array,
sizeof(GreasePencilTimeModifierSegment) * new_active_index);
/* Copy the segments after the new segment. */
memcpy(new_segments + new_active_index + 1,
tmd->segments_array + new_active_index,
sizeof(GreasePencilTimeModifierSegment) * (tmd->segments_num - new_active_index));
}
/* Create the new segment. */
GreasePencilTimeModifierSegment *segment = &new_segments[new_active_index];
memcpy(segment,
DNA_struct_default_get(GreasePencilTimeModifierSegment),
sizeof(GreasePencilTimeModifierSegment));
BLI_uniquename_cb(
[&](const StringRef name) {
for (const GreasePencilTimeModifierSegment &segment : tmd->segments()) {
if (STREQ(segment.name, name.data())) {
return true;
}
}
return false;
},
'.',
segment->name);
MEM_SAFE_FREE(tmd->segments_array);
tmd->segments_array = new_segments;
tmd->segments_num++;
tmd->segment_active_index++;
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY | ID_RECALC_SYNC_TO_EVAL);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int time_modifier_segment_add_invoke(bContext *C, wmOperator *op, const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return time_modifier_segment_add_exec(C, op);
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_grease_pencil_time_modifier_segment_add(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Add Segment";
ot->description = "Add a segment to the time modifier";
ot->idname = "OBJECT_OT_grease_pencil_time_modifier_segment_add";
/* api callbacks */
ot->poll = time_modifier_segment_poll;
ot->invoke = time_modifier_segment_add_invoke;
ot->exec = time_modifier_segment_add_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
}
static void time_modifier_segment_free(GreasePencilTimeModifierSegment * /*ds*/) {}
static int time_modifier_segment_remove_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
auto *tmd = reinterpret_cast<GreasePencilTimeModifierData *>(
edit_modifier_property_get(op, ob, eModifierType_GreasePencilTime));
if (tmd == nullptr) {
return OPERATOR_CANCELLED;
}
if (!tmd->segments().index_range().contains(tmd->segment_active_index)) {
return OPERATOR_CANCELLED;
}
dna::array::remove_index(&tmd->segments_array,
&tmd->segments_num,
&tmd->segment_active_index,
tmd->segment_active_index,
time_modifier_segment_free);
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY | ID_RECALC_SYNC_TO_EVAL);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int time_modifier_segment_remove_invoke(bContext *C,
wmOperator *op,
const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return time_modifier_segment_remove_exec(C, op);
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_grease_pencil_time_modifier_segment_remove(wmOperatorType *ot)
{
/* identifiers */
ot->name = "Remove Segment";
ot->description = "Remove the active segment from the time modifier";
ot->idname = "OBJECT_OT_grease_pencil_time_modifier_segment_remove";
/* api callbacks */
ot->poll = time_modifier_segment_poll;
ot->invoke = time_modifier_segment_remove_invoke;
ot->exec = time_modifier_segment_remove_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
RNA_def_int(
ot->srna, "index", 0, 0, INT_MAX, "Index", "Index of the segment to remove", 0, INT_MAX);
}
enum class TimeSegmentMoveDirection {
Up = -1,
Down = 1,
};
static int time_modifier_segment_move_exec(bContext *C, wmOperator *op)
{
Object *ob = context_active_object(C);
auto *tmd = reinterpret_cast<GreasePencilTimeModifierData *>(
edit_modifier_property_get(op, ob, eModifierType_GreasePencilTime));
if (tmd == nullptr) {
return OPERATOR_CANCELLED;
}
if (tmd->segments_num < 2) {
return OPERATOR_CANCELLED;
}
const TimeSegmentMoveDirection direction = TimeSegmentMoveDirection(
RNA_enum_get(op->ptr, "type"));
switch (direction) {
case TimeSegmentMoveDirection::Up:
if (tmd->segment_active_index == 0) {
return OPERATOR_CANCELLED;
}
std::swap(tmd->segments_array[tmd->segment_active_index],
tmd->segments_array[tmd->segment_active_index - 1]);
tmd->segment_active_index--;
break;
case TimeSegmentMoveDirection::Down:
if (tmd->segment_active_index == tmd->segments_num - 1) {
return OPERATOR_CANCELLED;
}
std::swap(tmd->segments_array[tmd->segment_active_index],
tmd->segments_array[tmd->segment_active_index + 1]);
tmd->segment_active_index++;
break;
default:
return OPERATOR_CANCELLED;
}
DEG_id_tag_update(&ob->id, ID_RECALC_GEOMETRY | ID_RECALC_SYNC_TO_EVAL);
WM_event_add_notifier(C, NC_OBJECT | ND_MODIFIER, ob);
return OPERATOR_FINISHED;
}
static int time_modifier_segment_move_invoke(bContext *C,
wmOperator *op,
const wmEvent * /*event*/)
{
if (edit_modifier_invoke_properties(C, op)) {
return time_modifier_segment_move_exec(C, op);
}
return OPERATOR_CANCELLED;
}
void OBJECT_OT_grease_pencil_time_modifier_segment_move(wmOperatorType *ot)
{
static const EnumPropertyItem segment_move[] = {
{int(TimeSegmentMoveDirection::Up), "UP", 0, "Up", ""},
{int(TimeSegmentMoveDirection::Down), "DOWN", 0, "Down", ""},
{0, nullptr, 0, nullptr, nullptr},
};
/* identifiers */
ot->name = "Move Segment";
ot->description = "Move the active time segment up or down";
ot->idname = "OBJECT_OT_grease_pencil_time_modifier_segment_move";
/* api callbacks */
ot->poll = time_modifier_segment_poll;
ot->invoke = time_modifier_segment_move_invoke;
ot->exec = time_modifier_segment_move_exec;
/* flags */
ot->flag = OPTYPE_REGISTER | OPTYPE_UNDO | OPTYPE_INTERNAL;
edit_modifier_properties(ot);
ot->prop = RNA_def_enum(ot->srna, "type", segment_move, 0, "Type", "");
}
/** \} */
} // namespace blender::ed::object