Files
test2/source/blender/blenkernel/intern/layer_utils.cc

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

291 lines
8.7 KiB
C++
Raw Normal View History

/* SPDX-FileCopyrightText: 2023 Blender Authors
*
* SPDX-License-Identifier: GPL-2.0-or-later */
/** \file
* \ingroup bke
*/
#include <cstring>
#include "BKE_collection.hh"
#include "BKE_customdata.hh"
#include "BKE_editmesh.hh"
2024-01-23 15:18:09 -05:00
#include "BKE_layer.hh"
#include "BKE_mesh_types.hh"
#include "DNA_ID.h"
#include "DNA_layer_types.h"
#include "DNA_mesh_types.h"
#include "DNA_object_types.h"
/* -------------------------------------------------------------------- */
/** \name Selected Object Array
* \{ */
using blender::Vector;
Vector<Object *> BKE_view_layer_array_selected_objects_params(
ViewLayer *view_layer, const View3D *v3d, const ObjectsInViewLayerParams *params)
{
if (params->no_dup_data) {
FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob_iter) {
ID *id = static_cast<ID *>(ob_iter->data);
if (id) {
id->tag |= ID_TAG_DOIT;
}
}
FOREACH_SELECTED_OBJECT_END;
}
Vector<Object *> objects;
FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob_iter) {
if (params->filter_fn) {
if (!params->filter_fn(ob_iter, params->filter_userdata)) {
continue;
}
}
if (params->no_dup_data) {
ID *id = static_cast<ID *>(ob_iter->data);
if (id) {
if (id->tag & ID_TAG_DOIT) {
id->tag &= ~ID_TAG_DOIT;
}
else {
continue;
}
}
}
objects.append(ob_iter);
}
FOREACH_SELECTED_OBJECT_END;
return objects;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Objects in Mode Array
* \{ */
Vector<Base *> BKE_view_layer_array_from_bases_in_mode_params(const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d,
const ObjectsInModeParams *params)
{
if (params->no_dup_data) {
FOREACH_BASE_IN_MODE_BEGIN (scene, view_layer, v3d, -1, params->object_mode, base_iter) {
ID *id = static_cast<ID *>(base_iter->object->data);
if (id) {
id->tag |= ID_TAG_DOIT;
}
}
FOREACH_BASE_IN_MODE_END;
}
Vector<Base *> bases;
FOREACH_BASE_IN_MODE_BEGIN (scene, view_layer, v3d, -1, params->object_mode, base_iter) {
if (params->filter_fn) {
if (!params->filter_fn(base_iter->object, params->filter_userdata)) {
continue;
}
}
if (params->no_dup_data) {
ID *id = static_cast<ID *>(base_iter->object->data);
if (id) {
if (id->tag & ID_TAG_DOIT) {
id->tag &= ~ID_TAG_DOIT;
}
else {
continue;
}
}
}
bases.append(base_iter);
}
FOREACH_BASE_IN_MODE_END;
return bases;
}
Vector<Object *> BKE_view_layer_array_from_objects_in_mode_params(
const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d,
const ObjectsInModeParams *params)
{
const Vector<Base *> bases = BKE_view_layer_array_from_bases_in_mode_params(
scene, view_layer, v3d, params);
Vector<Object *> objects(bases.size());
std::transform(
bases.begin(), bases.end(), objects.begin(), [](Base *base) { return base->object; });
return objects;
}
Vector<Object *> BKE_view_layer_array_from_objects_in_edit_mode(const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d)
{
ObjectsInModeParams params = {0};
params.object_mode = OB_MODE_EDIT;
return BKE_view_layer_array_from_objects_in_mode_params(scene, view_layer, v3d, &params);
}
Vector<Base *> BKE_view_layer_array_from_bases_in_edit_mode(const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d)
{
ObjectsInModeParams params = {0};
params.object_mode = OB_MODE_EDIT;
return BKE_view_layer_array_from_bases_in_mode_params(scene, view_layer, v3d, &params);
}
Vector<Object *> BKE_view_layer_array_from_objects_in_edit_mode_unique_data(const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d)
{
ObjectsInModeParams params = {0};
params.object_mode = OB_MODE_EDIT;
params.no_dup_data = true;
return BKE_view_layer_array_from_objects_in_mode_params(scene, view_layer, v3d, &params);
}
Vector<Base *> BKE_view_layer_array_from_bases_in_edit_mode_unique_data(const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d)
{
ObjectsInModeParams params = {0};
params.object_mode = OB_MODE_EDIT;
params.no_dup_data = true;
return BKE_view_layer_array_from_bases_in_mode_params(scene, view_layer, v3d, &params);
}
Vector<Object *> BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(
const Scene *scene, ViewLayer *view_layer, const View3D *v3d)
{
ObjectsInModeParams params = {0};
params.object_mode = OB_MODE_EDIT;
params.no_dup_data = true;
params.filter_fn = BKE_view_layer_filter_edit_mesh_has_uvs;
return BKE_view_layer_array_from_objects_in_mode_params(scene, view_layer, v3d, &params);
}
Vector<Object *> BKE_view_layer_array_from_objects_in_mode_unique_data(const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d,
const eObjectMode mode)
{
ObjectsInModeParams params = {0};
params.object_mode = mode;
params.no_dup_data = true;
return BKE_view_layer_array_from_objects_in_mode_params(scene, view_layer, v3d, &params);
}
ViewLayer: Lazy sync of scene data. When a change happens which invalidates view layers the syncing will be postponed until the first usage. This will improve importing or adding many objects in a single operation/script. `BKE_view_layer_need_resync_tag` is used to tag the view layer to be out of sync. Before accessing `BKE_view_layer_active_base_get`, `BKE_view_layer_active_object_get`, `BKE_view_layer_active_collection` or `BKE_view_layer_object_bases` the caller should call `BKE_view_layer_synced_ensure`. Having two functions ensures that partial syncing could be added as smaller patches in the future. Tagging a view layer out of sync could be replaced with a partial sync. Eventually the number of full resyncs could be reduced. After all tagging has been replaced with partial syncs the ensure_sync could be phased out. This patch has been added to discuss the details and consequences of the current approach. For clarity the call to BKE_view_layer_ensure_sync is placed close to the getters. In the future this could be placed in more strategical places to reduce the number of calls or improve performance. Finding those strategical places isn't that clear. When multiple operations are grouped in a single script you might want to always check for resync. Some areas found that can be improved. This list isn't complete. These areas aren't addressed by this patch as these changes would be hard to detect to the reviewer. The idea is to add changes to these areas as a separate patch. It might be that the initial commit would reduce performance compared to master, but will be fixed by the additional patches. **Object duplication** During object duplication the syncing is temporarily disabled. With this patch this isn't useful as when disabled the view_layer is accessed to locate bases. This can be improved by first locating the source bases, then duplicate and sync and locate the new bases. Will be solved in a separate patch for clarity reasons ({D15886}). **Object add** `BKE_object_add` not only adds a new object, but also selects and activates the new base. This requires the view_layer to be resynced. Some callers reverse the selection and activation (See `get_new_constraint_target`). We should make the selection and activation optional. This would make it possible to add multiple objects without having to resync per object. **Postpone Activate Base** Setting the basact is done in many locations. They follow a rule as after an action find the base and set the basact. Finding the base could require a resync. The idea is to store in the view_layer the object which base will be set in the basact during the next sync, reducing the times resyncing needs to happen. Reviewed By: mont29 Maniphest Tasks: T73411 Differential Revision: https://developer.blender.org/D15885
2022-09-14 21:33:51 +02:00
ListBase *BKE_view_layer_object_bases_get(ViewLayer *view_layer)
{
BLI_assert_msg((view_layer->flag & VIEW_LAYER_OUT_OF_SYNC) == 0,
"Object Bases out of sync, invoke BKE_view_layer_synced_ensure.");
return &view_layer->object_bases;
}
Base *BKE_view_layer_active_base_get(ViewLayer *view_layer)
{
BLI_assert_msg((view_layer->flag & VIEW_LAYER_OUT_OF_SYNC) == 0,
"Active Base out of sync, invoke BKE_view_layer_synced_ensure.");
return view_layer->basact;
}
LayerCollection *BKE_view_layer_active_collection_get(ViewLayer *view_layer)
{
BLI_assert_msg((view_layer->flag & VIEW_LAYER_OUT_OF_SYNC) == 0,
"Active Collection out of sync, invoke BKE_view_layer_synced_ensure.");
return view_layer->active_collection;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Filter Functions
* \{ */
bool BKE_view_layer_filter_edit_mesh_has_uvs(const Object *ob, void * /*user_data*/)
{
if (ob->type == OB_MESH) {
const Mesh *mesh = static_cast<const Mesh *>(ob->data);
if (const BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
Mesh: Move UV layers to generic attributes Currently the `MLoopUV` struct stores UV coordinates and flags related to editing UV maps in the UV editor. This patch changes the coordinates to use the generic 2D vector type, and moves the flags into three separate boolean attributes. This follows the design in T95965, with the ultimate intention of simplifying code and improving performance. Importantly, the change allows exporters and renderers to use UVs "touched" by geometry nodes, which only creates generic attributes. It also allows geometry nodes to create "proper" UV maps from scratch, though only with the Store Named Attribute node for now. The new design considers any 2D vector attribute on the corner domain to be a UV map. In the future, they might be distinguished from regular 2D vectors with attribute metadata, which may be helpful because they are often interpolated differently. Most of the code changes deal with passing around UV BMesh custom data offsets and tracking the boolean "sublayers". The boolean layers are use the following prefixes for attribute names: vert selection: `.vs.`, edge selection: `.es.`, pinning: `.pn.`. Currently these are short to avoid using up the maximum length of attribute names. To accommodate for these 4 extra characters, the name length limit is enlarged to 68 bytes, while the maximum user settable name length is still 64 bytes. Unfortunately Python/RNA API access to the UV flag data becomes slower. Accessing the boolean layers directly is be better for performance in general. Like the other mesh SoA refactors, backward and forward compatibility aren't affected, and won't be changed until 4.0. We pay for that by making mesh reading and writing more expensive with conversions. Resolves T85962 Differential Revision: https://developer.blender.org/D14365
2023-01-10 00:47:04 -05:00
if (CustomData_has_layer(&em->bm->ldata, CD_PROP_FLOAT2)) {
return true;
}
}
}
return false;
}
bool BKE_view_layer_filter_edit_mesh_has_edges(const Object *ob, void * /*user_data*/)
{
if (ob->type == OB_MESH) {
const Mesh *mesh = static_cast<const Mesh *>(ob->data);
if (const BMEditMesh *em = mesh->runtime->edit_mesh.get()) {
if (em->bm->totedge != 0) {
return true;
}
}
}
return false;
}
Object *BKE_view_layer_non_active_selected_object(const Scene *scene,
ViewLayer *view_layer,
const View3D *v3d)
{
ViewLayer: Lazy sync of scene data. When a change happens which invalidates view layers the syncing will be postponed until the first usage. This will improve importing or adding many objects in a single operation/script. `BKE_view_layer_need_resync_tag` is used to tag the view layer to be out of sync. Before accessing `BKE_view_layer_active_base_get`, `BKE_view_layer_active_object_get`, `BKE_view_layer_active_collection` or `BKE_view_layer_object_bases` the caller should call `BKE_view_layer_synced_ensure`. Having two functions ensures that partial syncing could be added as smaller patches in the future. Tagging a view layer out of sync could be replaced with a partial sync. Eventually the number of full resyncs could be reduced. After all tagging has been replaced with partial syncs the ensure_sync could be phased out. This patch has been added to discuss the details and consequences of the current approach. For clarity the call to BKE_view_layer_ensure_sync is placed close to the getters. In the future this could be placed in more strategical places to reduce the number of calls or improve performance. Finding those strategical places isn't that clear. When multiple operations are grouped in a single script you might want to always check for resync. Some areas found that can be improved. This list isn't complete. These areas aren't addressed by this patch as these changes would be hard to detect to the reviewer. The idea is to add changes to these areas as a separate patch. It might be that the initial commit would reduce performance compared to master, but will be fixed by the additional patches. **Object duplication** During object duplication the syncing is temporarily disabled. With this patch this isn't useful as when disabled the view_layer is accessed to locate bases. This can be improved by first locating the source bases, then duplicate and sync and locate the new bases. Will be solved in a separate patch for clarity reasons ({D15886}). **Object add** `BKE_object_add` not only adds a new object, but also selects and activates the new base. This requires the view_layer to be resynced. Some callers reverse the selection and activation (See `get_new_constraint_target`). We should make the selection and activation optional. This would make it possible to add multiple objects without having to resync per object. **Postpone Activate Base** Setting the basact is done in many locations. They follow a rule as after an action find the base and set the basact. Finding the base could require a resync. The idea is to store in the view_layer the object which base will be set in the basact during the next sync, reducing the times resyncing needs to happen. Reviewed By: mont29 Maniphest Tasks: T73411 Differential Revision: https://developer.blender.org/D15885
2022-09-14 21:33:51 +02:00
BKE_view_layer_synced_ensure(scene, view_layer);
Object *ob_active = BKE_view_layer_active_object_get(view_layer);
Object *ob_result = nullptr;
FOREACH_SELECTED_OBJECT_BEGIN (view_layer, v3d, ob_iter) {
if (ob_iter == ob_active) {
continue;
}
if (ob_result == nullptr) {
ob_result = ob_iter;
}
else {
ob_result = nullptr;
break;
}
}
FOREACH_SELECTED_OBJECT_END;
return ob_result;
}
/** \} */
/* -------------------------------------------------------------------- */
/** \name Active object accessors.
* \{ */
Object *BKE_view_layer_active_object_get(const ViewLayer *view_layer)
{
ViewLayer: Lazy sync of scene data. When a change happens which invalidates view layers the syncing will be postponed until the first usage. This will improve importing or adding many objects in a single operation/script. `BKE_view_layer_need_resync_tag` is used to tag the view layer to be out of sync. Before accessing `BKE_view_layer_active_base_get`, `BKE_view_layer_active_object_get`, `BKE_view_layer_active_collection` or `BKE_view_layer_object_bases` the caller should call `BKE_view_layer_synced_ensure`. Having two functions ensures that partial syncing could be added as smaller patches in the future. Tagging a view layer out of sync could be replaced with a partial sync. Eventually the number of full resyncs could be reduced. After all tagging has been replaced with partial syncs the ensure_sync could be phased out. This patch has been added to discuss the details and consequences of the current approach. For clarity the call to BKE_view_layer_ensure_sync is placed close to the getters. In the future this could be placed in more strategical places to reduce the number of calls or improve performance. Finding those strategical places isn't that clear. When multiple operations are grouped in a single script you might want to always check for resync. Some areas found that can be improved. This list isn't complete. These areas aren't addressed by this patch as these changes would be hard to detect to the reviewer. The idea is to add changes to these areas as a separate patch. It might be that the initial commit would reduce performance compared to master, but will be fixed by the additional patches. **Object duplication** During object duplication the syncing is temporarily disabled. With this patch this isn't useful as when disabled the view_layer is accessed to locate bases. This can be improved by first locating the source bases, then duplicate and sync and locate the new bases. Will be solved in a separate patch for clarity reasons ({D15886}). **Object add** `BKE_object_add` not only adds a new object, but also selects and activates the new base. This requires the view_layer to be resynced. Some callers reverse the selection and activation (See `get_new_constraint_target`). We should make the selection and activation optional. This would make it possible to add multiple objects without having to resync per object. **Postpone Activate Base** Setting the basact is done in many locations. They follow a rule as after an action find the base and set the basact. Finding the base could require a resync. The idea is to store in the view_layer the object which base will be set in the basact during the next sync, reducing the times resyncing needs to happen. Reviewed By: mont29 Maniphest Tasks: T73411 Differential Revision: https://developer.blender.org/D15885
2022-09-14 21:33:51 +02:00
Base *base = BKE_view_layer_active_base_get((ViewLayer *)view_layer);
return base ? base->object : nullptr;
}
Object *BKE_view_layer_edit_object_get(const ViewLayer *view_layer)
{
Object *ob = BKE_view_layer_active_object_get(view_layer);
if (ob == nullptr) {
return nullptr;
}
if (!(ob->mode & OB_MODE_EDIT)) {
return nullptr;
}
return ob;
}
/** \} */