15d9b3c5ccb1917491f643380755ba6f511ca9d6
[blender.git] / source / blender / editors / screen / screen_context.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2008 Blender Foundation.
19  * All rights reserved.
20  *
21  *
22  * ***** END GPL LICENSE BLOCK *****
23  */
24
25 /** \file blender/editors/screen/screen_context.c
26  *  \ingroup edscr
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "MEM_guardedalloc.h"
34
35 #include "DNA_object_types.h"
36 #include "DNA_armature_types.h"
37 #include "DNA_brush_types.h"
38 #include "DNA_gpencil_types.h"
39 #include "DNA_sequence_types.h"
40 #include "DNA_scene_types.h"
41 #include "DNA_screen_types.h"
42 #include "DNA_space_types.h"
43 #include "DNA_windowmanager_types.h"
44 #include "DNA_workspace_types.h"
45
46 #include "BLI_utildefines.h"
47
48 #include "BKE_brush.h"
49 #include "BKE_context.h"
50 #include "BKE_object.h"
51 #include "BKE_action.h"
52 #include "BKE_armature.h"
53 #include "BKE_paint.h"
54 #include "BKE_main.h"
55 #include "BKE_gpencil.h"
56 #include "BKE_layer.h"
57 #include "BKE_screen.h"
58 #include "BKE_sequencer.h"
59 #include "BKE_workspace.h"
60
61 #include "DEG_depsgraph.h"
62
63 #include "RNA_access.h"
64
65 #include "ED_armature.h"
66 #include "ED_gpencil.h"
67 #include "ED_anim_api.h"
68 #include "ED_uvedit.h"
69
70 #include "WM_api.h"
71 #include "UI_interface.h"
72
73 #include "screen_intern.h"
74
75 const char *screen_context_dir[] = {
76         "scene", "view_layer", "visible_objects", "visible_bases", "selectable_objects", "selectable_bases",
77         "selected_objects", "selected_bases",
78         "editable_objects", "editable_bases",
79         "selected_editable_objects", "selected_editable_bases",
80         "objects_in_mode", "objects_in_mode_unique_data",
81         "visible_bones", "editable_bones", "selected_bones", "selected_editable_bones",
82         "visible_pose_bones", "selected_pose_bones", "selected_pose_bones_from_active_object",
83         "active_bone", "active_pose_bone",
84         "active_base", "active_object", "object", "edit_object",
85         "sculpt_object", "vertex_paint_object", "weight_paint_object",
86         "image_paint_object", "particle_edit_object", "uv_sculpt_object",
87         "sequences", "selected_sequences", "selected_editable_sequences", /* sequencer */
88         "gpencil_data", "gpencil_data_owner", /* grease pencil data */
89         "visible_gpencil_layers", "editable_gpencil_layers", "editable_gpencil_strokes",
90         "active_gpencil_layer", "active_gpencil_frame",
91         "active_operator", "selected_editable_fcurves",
92         NULL};
93
94 int ed_screen_context(const bContext *C, const char *member, bContextDataResult *result)
95 {
96         wmWindow *win = CTX_wm_window(C);
97         View3D *v3d = CTX_wm_view3d(C); /* This may be NULL in a lot of cases. */
98         bScreen *sc = CTX_wm_screen(C);
99         ScrArea *sa = CTX_wm_area(C);
100         Scene *scene = WM_window_get_active_scene(win);
101         ViewLayer *view_layer = WM_window_get_active_view_layer(win);
102         Object *obact = (view_layer && view_layer->basact) ? view_layer->basact->object : NULL;
103         Object *obedit = view_layer ? OBEDIT_FROM_VIEW_LAYER(view_layer) : NULL;
104
105         if (CTX_data_dir(member)) {
106                 CTX_data_dir_set(result, screen_context_dir);
107                 return 1;
108         }
109         else if (CTX_data_equals(member, "scene")) {
110                 CTX_data_id_pointer_set(result, &scene->id);
111                 return 1;
112         }
113         else if (CTX_data_equals(member, "visible_objects")) {
114                 FOREACH_VISIBLE_OBJECT_BEGIN(view_layer, v3d, ob)
115                 {
116                         CTX_data_id_list_add(result, &ob->id);
117                 }
118                 FOREACH_VISIBLE_BASE_END;
119                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
120                 return 1;
121         }
122         else if (CTX_data_equals(member, "selectable_objects")) {
123                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
124                         if (v3d && v3d->localvd && ((base->local_view_bits & v3d->local_view_uuid) == 0)) {
125                                 continue;
126                         }
127                         if (v3d && ((v3d->object_type_exclude_viewport & (1 << base->object->type)) != 0)) {
128                                 continue;
129                         }
130                         if (v3d && ((v3d->object_type_exclude_select & (1 << base->object->type)) != 0)) {
131                                 continue;
132                         }
133                         if (((base->flag & BASE_VISIBLE) != 0) && ((base->flag & BASE_SELECTABLE) != 0)) {
134                                 CTX_data_id_list_add(result, &base->object->id);
135                         }
136                 }
137                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
138                 return 1;
139         }
140         else if (CTX_data_equals(member, "selected_objects")) {
141                 FOREACH_SELECTED_OBJECT_BEGIN(view_layer, v3d, ob)
142                 {
143                         CTX_data_id_list_add(result, &ob->id);
144                 }
145                 FOREACH_SELECTED_OBJECT_END;
146                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
147                 return 1;
148         }
149         else if (CTX_data_equals(member, "selected_editable_objects")) {
150                 FOREACH_SELECTED_OBJECT_BEGIN(view_layer, v3d, ob)
151                 {
152                         if (0 == BKE_object_is_libdata(ob)) {
153                                 CTX_data_id_list_add(result, &ob->id);
154                         }
155                 }
156                 FOREACH_SELECTED_OBJECT_END;
157                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
158                 return 1;
159         }
160         else if (CTX_data_equals(member, "editable_objects")) {
161                 /* Visible + Editable, but not necessarily selected */
162                 FOREACH_VISIBLE_OBJECT_BEGIN(view_layer, v3d, ob)
163                 {
164                         if (0 == BKE_object_is_libdata(ob)) {
165                                 CTX_data_id_list_add(result, &ob->id);
166                         }
167                 }
168                 FOREACH_VISIBLE_OBJECT_END;
169                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
170                 return 1;
171         }
172         else if ( CTX_data_equals(member, "visible_bases")) {
173                 FOREACH_VISIBLE_BASE_BEGIN(view_layer, v3d, base)
174                 {
175                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
176                 }
177                 FOREACH_VISIBLE_BASE_END;
178                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
179                 return 1;
180         }
181         else if (CTX_data_equals(member, "selectable_bases")) {
182                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
183                         if (v3d && v3d->localvd && ((base->local_view_bits & v3d->local_view_uuid) == 0)) {
184                                 continue;
185                         }
186                         if (v3d && ((v3d->object_type_exclude_viewport & (1 << base->object->type)) != 0)) {
187                                 continue;
188                         }
189                         if (v3d && ((v3d->object_type_exclude_select & (1 << base->object->type)) != 0)) {
190                                 continue;
191                         }
192                         if ((base->flag & BASE_VISIBLE) && (base->flag & BASE_SELECTABLE) != 0) {
193                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
194                         }
195                 }
196                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
197                 return 1;
198         }
199         else if (CTX_data_equals(member, "selected_bases")) {
200                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
201                         if (v3d && v3d->localvd && ((base->local_view_bits & v3d->local_view_uuid) == 0)) {
202                                 continue;
203                         }
204                         if (v3d && ((v3d->object_type_exclude_viewport & (1 << base->object->type)) != 0)) {
205                                 continue;
206                         }
207                         if ((base->flag & BASE_SELECTED) != 0) {
208                                 CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
209                         }
210                 }
211                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
212                 return 1;
213         }
214         else if (CTX_data_equals(member, "selected_editable_bases")) {
215                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
216                         if (v3d && v3d->localvd && ((base->local_view_bits & v3d->local_view_uuid) == 0)) {
217                                 continue;
218                         }
219                         if (v3d && ((v3d->object_type_exclude_viewport & (1 << base->object->type)) != 0)) {
220                                 continue;
221                         }
222                         if ((base->flag & BASE_SELECTED) != 0) {
223                                 if (0 == BKE_object_is_libdata(base->object)) {
224                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
225                                 }
226                         }
227                 }
228                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
229                 return 1;
230         }
231         else if (CTX_data_equals(member, "editable_bases")) {
232                 /* Visible + Editable, but not necessarily selected */
233                 for (Base *base = view_layer->object_bases.first; base; base = base->next) {
234                         if (v3d && v3d->localvd && ((base->local_view_bits & v3d->local_view_uuid) == 0)) {
235                                 continue;
236                         }
237                         if (v3d && ((v3d->object_type_exclude_viewport & (1 << base->object->type)) != 0)) {
238                                 continue;
239                         }
240                         if ((base->flag & BASE_VISIBLE) != 0) {
241                                 if (0 == BKE_object_is_libdata(base->object)) {
242                                         CTX_data_list_add(result, &scene->id, &RNA_ObjectBase, base);
243                                 }
244                         }
245                 }
246                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
247                 return 1;
248         }
249         else if (CTX_data_equals(member, "objects_in_mode")) {
250                 if (obact && (obact->mode != OB_MODE_OBJECT)) {
251                         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) {
252                                 CTX_data_id_list_add(result, &ob_iter->id);
253                         } FOREACH_OBJECT_IN_MODE_END;
254                 }
255                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
256                 return 1;
257         }
258         else if (CTX_data_equals(member, "objects_in_mode_unique_data")) {
259                 if (obact && (obact->mode != OB_MODE_OBJECT)) {
260                         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) {
261                                 ob_iter->id.tag |= LIB_TAG_DOIT;
262                         } FOREACH_OBJECT_IN_MODE_END;
263                         FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, obact->type, obact->mode, ob_iter) {
264                                 if (ob_iter->id.tag & LIB_TAG_DOIT) {
265                                         ob_iter->id.tag &= ~LIB_TAG_DOIT;
266                                         CTX_data_id_list_add(result, &ob_iter->id);
267                                 }
268                         } FOREACH_OBJECT_IN_MODE_END;
269                 }
270                 CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
271                 return 1;
272         }
273         else if (CTX_data_equals(member, "visible_bones") || CTX_data_equals(member, "editable_bones")) {
274                 bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : NULL;
275                 EditBone *ebone, *flipbone = NULL;
276                 const bool editable_bones = CTX_data_equals(member, "editable_bones");
277
278                 if (arm && arm->edbo) {
279                         uint objects_len;
280                         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, CTX_wm_view3d(C), &objects_len);
281                         for (uint i = 0; i < objects_len; i++) {
282                                 Object *ob = objects[i];
283                                 arm = ob->data;
284
285                                 /* Attention: X-Axis Mirroring is also handled here... */
286                                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
287                                         /* first and foremost, bone must be visible and selected */
288                                         if (EBONE_VISIBLE(arm, ebone)) {
289                                                 /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
290                                                  * so that most users of this data don't need to explicitly check for it themselves.
291                                                  *
292                                                  * We need to make sure that these mirrored copies are not selected, otherwise some
293                                                  * bones will be operated on twice.
294                                                  */
295                                                 if (arm->flag & ARM_MIRROR_EDIT)
296                                                         flipbone = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
297
298                                                 /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
299                                                 if (editable_bones) {
300                                                         /* only selected + editable */
301                                                         if (EBONE_EDITABLE(ebone)) {
302                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
303
304                                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
305                                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
306                                                         }
307                                                 }
308                                                 else {
309                                                         /* only include bones if visible */
310                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
311
312                                                         if ((flipbone) && EBONE_VISIBLE(arm, flipbone) == 0)
313                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
314                                                 }
315                                         }
316                                 }
317                         }
318                         MEM_freeN(objects);
319
320                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
321                         return 1;
322                 }
323         }
324         else if (CTX_data_equals(member, "selected_bones") || CTX_data_equals(member, "selected_editable_bones")) {
325                 bArmature *arm = (obedit && obedit->type == OB_ARMATURE) ? obedit->data : NULL;
326                 EditBone *ebone, *flipbone = NULL;
327                 const bool selected_editable_bones = CTX_data_equals(member, "selected_editable_bones");
328
329                 if (arm && arm->edbo) {
330                         uint objects_len;
331                         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data(view_layer, CTX_wm_view3d(C), &objects_len);
332                         for (uint i = 0; i < objects_len; i++) {
333                                 Object *ob = objects[i];
334                                 arm = ob->data;
335
336                                 /* Attention: X-Axis Mirroring is also handled here... */
337                                 for (ebone = arm->edbo->first; ebone; ebone = ebone->next) {
338                                         /* first and foremost, bone must be visible and selected */
339                                         if (EBONE_VISIBLE(arm, ebone) && (ebone->flag & BONE_SELECTED)) {
340                                                 /* Get 'x-axis mirror equivalent' bone if the X-Axis Mirroring option is enabled
341                                                  * so that most users of this data don't need to explicitly check for it themselves.
342                                                  *
343                                                  * We need to make sure that these mirrored copies are not selected, otherwise some
344                                                  * bones will be operated on twice.
345                                                  */
346                                                 if (arm->flag & ARM_MIRROR_EDIT)
347                                                         flipbone = ED_armature_ebone_get_mirrored(arm->edbo, ebone);
348
349                                                 /* if we're filtering for editable too, use the check for that instead, as it has selection check too */
350                                                 if (selected_editable_bones) {
351                                                         /* only selected + editable */
352                                                         if (EBONE_EDITABLE(ebone)) {
353                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
354
355                                                                 if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
356                                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
357                                                         }
358                                                 }
359                                                 else {
360                                                         /* only include bones if selected */
361                                                         CTX_data_list_add(result, &arm->id, &RNA_EditBone, ebone);
362
363                                                         if ((flipbone) && !(flipbone->flag & BONE_SELECTED))
364                                                                 CTX_data_list_add(result, &arm->id, &RNA_EditBone, flipbone);
365                                                 }
366                                         }
367                                 }
368                         }
369                         MEM_freeN(objects);
370
371                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
372                         return 1;
373                 }
374         }
375         else if (CTX_data_equals(member, "visible_pose_bones")) {
376                 Object *obpose = BKE_object_pose_armature_get(obact);
377                 if (obpose && obpose->pose && obpose->data) {
378                         if (obpose != obact) {
379                                 FOREACH_PCHAN_VISIBLE_IN_OBJECT_BEGIN (obpose, pchan) {
380                                         CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
381                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
382                         }
383                         else if (obact->mode & OB_MODE_POSE) {
384                                 FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter) {
385                                         FOREACH_PCHAN_VISIBLE_IN_OBJECT_BEGIN (ob_iter, pchan) {
386                                                 CTX_data_list_add(result, &ob_iter->id, &RNA_PoseBone, pchan);
387                                         } FOREACH_PCHAN_VISIBLE_IN_OBJECT_END;
388                                 } FOREACH_OBJECT_IN_MODE_END;
389                         }
390                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
391                         return 1;
392                 }
393         }
394         else if (CTX_data_equals(member, "selected_pose_bones")) {
395                 Object *obpose = BKE_object_pose_armature_get(obact);
396                 if (obpose && obpose->pose && obpose->data) {
397                         if (obpose != obact) {
398                                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (obpose, pchan) {
399                                         CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
400                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
401                         }
402                         else if (obact->mode & OB_MODE_POSE) {
403                                 FOREACH_OBJECT_IN_MODE_BEGIN (view_layer, v3d, OB_ARMATURE, OB_MODE_POSE, ob_iter) {
404                                         FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (ob_iter, pchan) {
405                                                 CTX_data_list_add(result, &ob_iter->id, &RNA_PoseBone, pchan);
406                                         } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
407                                 } FOREACH_OBJECT_IN_MODE_END;
408                         }
409                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
410                         return 1;
411                 }
412         }
413         else if (CTX_data_equals(member, "selected_pose_bones_from_active_object")) {
414                 Object *obpose = BKE_object_pose_armature_get(obact);
415                 if (obpose && obpose->pose && obpose->data) {
416                         if (obpose != obact) {
417                                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (obpose, pchan) {
418                                         CTX_data_list_add(result, &obpose->id, &RNA_PoseBone, pchan);
419                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
420                         }
421                         else if (obact->mode & OB_MODE_POSE) {
422                                 FOREACH_PCHAN_SELECTED_IN_OBJECT_BEGIN (obact, pchan) {
423                                         CTX_data_list_add(result, &obact->id, &RNA_PoseBone, pchan);
424                                 } FOREACH_PCHAN_SELECTED_IN_OBJECT_END;
425                         }
426                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
427                         return 1;
428                 }
429         }
430         else if (CTX_data_equals(member, "active_bone")) {
431                 if (obact && obact->type == OB_ARMATURE) {
432                         bArmature *arm = obact->data;
433                         if (arm->edbo) {
434                                 if (arm->act_edbone) {
435                                         CTX_data_pointer_set(result, &arm->id, &RNA_EditBone, arm->act_edbone);
436                                         return 1;
437                                 }
438                         }
439                         else {
440                                 if (arm->act_bone) {
441                                         CTX_data_pointer_set(result, &arm->id, &RNA_Bone, arm->act_bone);
442                                         return 1;
443                                 }
444                         }
445                 }
446         }
447         else if (CTX_data_equals(member, "active_pose_bone")) {
448                 bPoseChannel *pchan;
449                 Object *obpose = BKE_object_pose_armature_get(obact);
450
451                 pchan = BKE_pose_channel_active(obpose);
452                 if (pchan) {
453                         CTX_data_pointer_set(result, &obpose->id, &RNA_PoseBone, pchan);
454                         return 1;
455                 }
456         }
457         else if (CTX_data_equals(member, "active_base")) {
458                 if (view_layer->basact)
459                         CTX_data_pointer_set(result, &scene->id, &RNA_ObjectBase, view_layer->basact);
460
461                 return 1;
462         }
463         else if (CTX_data_equals(member, "active_object")) {
464                 if (obact)
465                         CTX_data_id_pointer_set(result, &obact->id);
466
467                 return 1;
468         }
469         else if (CTX_data_equals(member, "object")) {
470                 if (obact)
471                         CTX_data_id_pointer_set(result, &obact->id);
472
473                 return 1;
474         }
475         else if (CTX_data_equals(member, "edit_object")) {
476                 /* convenience for now, 1 object per scene in editmode */
477                 if (obedit)
478                         CTX_data_id_pointer_set(result, &obedit->id);
479
480                 return 1;
481         }
482         else if (CTX_data_equals(member, "sculpt_object")) {
483                 if (obact && (obact->mode & OB_MODE_SCULPT))
484                         CTX_data_id_pointer_set(result, &obact->id);
485
486                 return 1;
487         }
488         else if (CTX_data_equals(member, "vertex_paint_object")) {
489                 if (obact && (obact->mode & OB_MODE_VERTEX_PAINT))
490                         CTX_data_id_pointer_set(result, &obact->id);
491
492                 return 1;
493         }
494         else if (CTX_data_equals(member, "weight_paint_object")) {
495                 if (obact && (obact->mode & OB_MODE_WEIGHT_PAINT))
496                         CTX_data_id_pointer_set(result, &obact->id);
497
498                 return 1;
499         }
500         else if (CTX_data_equals(member, "image_paint_object")) {
501                 if (obact && (obact->mode & OB_MODE_TEXTURE_PAINT))
502                         CTX_data_id_pointer_set(result, &obact->id);
503
504                 return 1;
505         }
506         else if (CTX_data_equals(member, "particle_edit_object")) {
507                 if (obact && (obact->mode & OB_MODE_PARTICLE_EDIT))
508                         CTX_data_id_pointer_set(result, &obact->id);
509
510                 return 1;
511         }
512         else if (CTX_data_equals(member, "uv_sculpt_object")) {
513                 /* TODO(campbell): most likely we change rules for uv_sculpt. */
514                 if (obact && (obact->mode & OB_MODE_EDIT)) {
515                         const ToolSettings *ts = scene->toolsettings;
516                         if (ts->use_uv_sculpt) {
517                                 if (ED_uvedit_test(obedit)) {
518                                         WorkSpace *workspace = CTX_wm_workspace(C);
519                                         if ((workspace->tools_space_type == SPACE_IMAGE) &&
520                                             (workspace->tools_mode == SI_MODE_UV))
521                                         {
522                                                 CTX_data_id_pointer_set(result, &obact->id);
523                                         }
524                                 }
525                         }
526                 }
527                 return 1;
528         }
529         else if (CTX_data_equals(member, "sequences")) {
530                 Editing *ed = BKE_sequencer_editing_get(scene, false);
531                 if (ed) {
532                         Sequence *seq;
533                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
534                                 CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
535                         }
536                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
537                         return 1;
538                 }
539         }
540         else if (CTX_data_equals(member, "selected_sequences")) {
541                 Editing *ed = BKE_sequencer_editing_get(scene, false);
542                 if (ed) {
543                         Sequence *seq;
544                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
545                                 if (seq->flag & SELECT) {
546                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
547                                 }
548                         }
549                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
550                         return 1;
551                 }
552         }
553         else if (CTX_data_equals(member, "selected_editable_sequences")) {
554                 Editing *ed = BKE_sequencer_editing_get(scene, false);
555                 if (ed) {
556                         Sequence *seq;
557                         for (seq = ed->seqbasep->first; seq; seq = seq->next) {
558                                 if (seq->flag & SELECT && !(seq->flag & SEQ_LOCK)) {
559                                         CTX_data_list_add(result, &scene->id, &RNA_Sequence, seq);
560                                 }
561                         }
562                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
563                         return 1;
564                 }
565         }
566         else if (CTX_data_equals(member, "gpencil_data")) {
567                 /* FIXME: for some reason, CTX_data_active_object(C) returns NULL when called from these situations
568                  * (as outlined above - see Campbell's #ifdefs). That causes the get_active function to fail when
569                  * called from context. For that reason, we end up using an alternative where we pass everything in!
570                  */
571                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
572
573                 if (gpd) {
574                         CTX_data_id_pointer_set(result, &gpd->id);
575                         return 1;
576                 }
577         }
578         else if (CTX_data_equals(member, "gpencil_data_owner")) {
579                 /* pointer to which data/datablock owns the reference to the Grease Pencil data being used (as gpencil_data)
580                  * XXX: see comment for gpencil_data case...
581                  */
582                 bGPdata **gpd_ptr = NULL;
583                 PointerRNA ptr;
584
585                 /* get pointer to Grease Pencil Data */
586                 gpd_ptr = ED_gpencil_data_get_pointers_direct((ID *)sc, sa, scene, obact, &ptr);
587
588                 if (gpd_ptr) {
589                         CTX_data_pointer_set(result, ptr.id.data, ptr.type, ptr.data);
590                         return 1;
591                 }
592         }
593         else if (CTX_data_equals(member, "active_gpencil_layer")) {
594                 /* XXX: see comment for gpencil_data case... */
595                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
596
597                 if (gpd) {
598                         bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
599
600                         if (gpl) {
601                                 CTX_data_pointer_set(result, &gpd->id, &RNA_GPencilLayer, gpl);
602                                 return 1;
603                         }
604                 }
605         }
606         else if (CTX_data_equals(member, "active_gpencil_frame")) {
607                 /* XXX: see comment for gpencil_data case... */
608                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
609
610                 if (gpd) {
611                         bGPDlayer *gpl = BKE_gpencil_layer_getactive(gpd);
612
613                         if (gpl) {
614                                 CTX_data_pointer_set(result, &gpd->id, &RNA_GPencilLayer, gpl->actframe);
615                                 return 1;
616                         }
617                 }
618         }
619         else if (CTX_data_equals(member, "visible_gpencil_layers")) {
620                 /* XXX: see comment for gpencil_data case... */
621                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
622
623                 if (gpd) {
624                         bGPDlayer *gpl;
625
626                         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
627                                 if ((gpl->flag & GP_LAYER_HIDE) == 0) {
628                                         CTX_data_list_add(result, &gpd->id, &RNA_GPencilLayer, gpl);
629                                 }
630                         }
631                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
632                         return 1;
633                 }
634         }
635         else if (CTX_data_equals(member, "editable_gpencil_layers")) {
636                 /* XXX: see comment for gpencil_data case... */
637                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
638
639                 if (gpd) {
640                         bGPDlayer *gpl;
641
642                         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
643                                 if (gpencil_layer_is_editable(gpl)) {
644                                         CTX_data_list_add(result, &gpd->id, &RNA_GPencilLayer, gpl);
645                                 }
646                         }
647                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
648                         return 1;
649                 }
650         }
651         else if (CTX_data_equals(member, "editable_gpencil_strokes")) {
652                 /* XXX: see comment for gpencil_data case... */
653                 bGPdata *gpd = ED_gpencil_data_get_active_direct((ID *)sc, sa, scene, obact);
654                 const bool is_multiedit = (bool)GPENCIL_MULTIEDIT_SESSIONS_ON(gpd);
655
656                 if (gpd) {
657                         bGPDlayer *gpl;
658
659                         for (gpl = gpd->layers.first; gpl; gpl = gpl->next) {
660                                 if (gpencil_layer_is_editable(gpl) && (gpl->actframe)) {
661                                         bGPDframe *gpf;
662                                         bGPDstroke *gps;
663                                         bGPDframe *init_gpf = gpl->actframe;
664                                         if (is_multiedit) {
665                                                 init_gpf = gpl->frames.first;
666                                         }
667
668                                         for (gpf = init_gpf; gpf; gpf = gpf->next) {
669                                                 if ((gpf == gpl->actframe) || ((gpf->flag & GP_FRAME_SELECT) && (is_multiedit))) {
670                                                         for (gps = gpf->strokes.first; gps; gps = gps->next) {
671                                                                 if (ED_gpencil_stroke_can_use_direct(sa, gps)) {
672                                                                         /* check if the color is editable */
673                                                                         if (ED_gpencil_stroke_color_use(obact, gpl, gps) == false) {
674                                                                                 continue;
675                                                                         }
676
677                                                                         CTX_data_list_add(result, &gpd->id, &RNA_GPencilStroke, gps);
678                                                                 }
679                                                         }
680                                                 }
681                                                 /* if not multiedit out of loop */
682                                                 if (!is_multiedit) {
683                                                         break;
684                                                 }
685                                         }
686                                 }
687                         }
688                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
689                         return 1;
690                 }
691         }
692         else if (CTX_data_equals(member, "active_operator")) {
693                 wmOperator *op = NULL;
694
695                 SpaceFile *sfile = CTX_wm_space_file(C);
696                 if (sfile) {
697                         op = sfile->op;
698                 }
699                 else if ((op = UI_context_active_operator_get(C))) {
700                         /* do nothing */
701                 }
702                 else {
703                         /* note, this checks poll, could be a problem, but this also
704                          * happens for the toolbar */
705                         op = WM_operator_last_redo(C);
706                 }
707                 /* TODO, get the operator from popup's */
708
709                 if (op && op->ptr) {
710                         CTX_data_pointer_set(result, NULL, &RNA_Operator, op);
711                         return 1;
712                 }
713         }
714         else if (CTX_data_equals(member, "selected_editable_fcurves")) {
715                 bAnimContext ac;
716
717                 if (ANIM_animdata_get_context(C, &ac) && ELEM(ac.spacetype, SPACE_ACTION, SPACE_IPO)) {
718                         bAnimListElem *ale;
719                         ListBase anim_data = {NULL, NULL};
720
721                         int filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_FOREDIT | ANIMFILTER_NODUPLIS | ANIMFILTER_SEL) |
722                                      (ac.spacetype == SPACE_IPO ? ANIMFILTER_CURVE_VISIBLE : ANIMFILTER_LIST_VISIBLE);
723
724                         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
725
726                         for (ale = anim_data.first; ale; ale = ale->next) {
727                                 if (ale->type == ANIMTYPE_FCURVE)
728                                         CTX_data_list_add(result, ale->id, &RNA_FCurve, ale->data);
729                         }
730
731                         ANIM_animdata_freelist(&anim_data);
732
733                         CTX_data_type_set(result, CTX_DATA_TYPE_COLLECTION);
734                         return 1;
735                 }
736         }
737         else {
738                 return 0; /* not found */
739         }
740
741         return -1; /* found but not available */
742 }