Fix: Viewer node crashes when the editor is pinned and has no Object ID

This case was not properly handled by the previous fix in #107621.

The viewer node path function still assumes that the node editor space
has a valid ID pointer (`snode.id`) and that it is an `Object`.
The ID pointer can be null when setting a pinned node tree through the
python API, like so:
```Py
space.pin = True
space.node_tree = some_other_node_tree
```

In this case the `ED_node_tree_start` function is called without and ID
pointer, which is allowed! This patch makes sure that the viewer node
path function is only used with a valid object context.

Pull Request: https://projects.blender.org/blender/blender/pulls/107737
This commit is contained in:
Lukas Tönne
2023-05-08 14:52:06 +02:00
parent 304e8a842d
commit 967cd7f99e

View File

@@ -24,6 +24,9 @@ static void viewer_path_for_geometry_node(const SpaceNode &snode,
const bNode &node,
ViewerPath &r_dst)
{
/* Only valid if the node space has a context object. */
BLI_assert(snode.id != nullptr && GS(snode.id->name) == ID_OB);
BKE_viewer_path_init(&r_dst);
Object *ob = reinterpret_cast<Object *>(snode.id);
@@ -87,7 +90,9 @@ void activate_geometry_node(Main &bmain, SpaceNode &snode, bNode &node)
}
ViewerPath new_viewer_path{};
BLI_SCOPED_DEFER([&]() { BKE_viewer_path_clear(&new_viewer_path); });
viewer_path_for_geometry_node(snode, node, new_viewer_path);
if (snode.id != nullptr && GS(snode.id->name) == ID_OB) {
viewer_path_for_geometry_node(snode, node, new_viewer_path);
}
bool found_view3d_with_enabled_viewer = false;
View3D *any_view3d_without_viewer = nullptr;
@@ -341,6 +346,11 @@ bool is_active_geometry_nodes_viewer(const bContext &C,
bNode *find_geometry_nodes_viewer(const ViewerPath &viewer_path, SpaceNode &snode)
{
/* Viewer path is only valid if the context object is set. */
if (snode.id == nullptr || GS(snode.id->name) != ID_OB) {
return nullptr;
}
const std::optional<ViewerPathForGeometryNodesViewer> parsed_viewer_path =
parse_geometry_nodes_viewer(viewer_path);
if (!parsed_viewer_path.has_value()) {