Blender  V2.93
geometry_attributes.c
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2020 Blender Foundation.
17  * All rights reserved.
18  */
19 
24 #include "BKE_attribute.h"
25 #include "BKE_context.h"
26 
27 #include "RNA_access.h"
28 #include "RNA_define.h"
29 #include "RNA_enum_types.h"
30 
31 #include "DEG_depsgraph.h"
32 
33 #include "WM_api.h"
34 #include "WM_types.h"
35 
36 #include "ED_object.h"
37 
38 #include "geometry_intern.h"
39 
40 /*********************** Attribute Operators ************************/
41 
43 {
44  Object *ob = ED_object_context(C);
45  ID *data = (ob) ? ob->data : NULL;
46  return (ob && !ID_IS_LINKED(ob) && data && !ID_IS_LINKED(data)) &&
48 }
49 
52  PropertyRNA *UNUSED(prop),
53  bool *r_free)
54 {
55  if (C == NULL) {
56  return DummyRNA_NULL_items;
57  }
58 
59  Object *ob = ED_object_context(C);
60  if (ob == NULL) {
61  return DummyRNA_NULL_items;
62  }
63 
64  return rna_enum_attribute_domain_itemf(ob->data, r_free);
65 }
66 
68 {
69  Object *ob = ED_object_context(C);
70  ID *id = ob->data;
71 
72  char name[MAX_NAME];
73  RNA_string_get(op->ptr, "name", name);
74  CustomDataType type = (CustomDataType)RNA_enum_get(op->ptr, "data_type");
75  AttributeDomain domain = (AttributeDomain)RNA_enum_get(op->ptr, "domain");
76  CustomDataLayer *layer = BKE_id_attribute_new(id, name, type, domain, op->reports);
77 
78  if (layer == NULL) {
79  return OPERATOR_CANCELLED;
80  }
81 
83 
86 
87  return OPERATOR_FINISHED;
88 }
89 
91 {
92  /* identifiers */
93  ot->name = "Add Geometry Attribute";
94  ot->description = "Add attribute to geometry";
95  ot->idname = "GEOMETRY_OT_attribute_add";
96 
97  /* api callbacks */
101 
102  /* flags */
104 
105  /* properties */
106  PropertyRNA *prop;
107 
108  prop = RNA_def_string(ot->srna, "name", "Attribute", MAX_NAME, "Name", "Name of new attribute");
110 
111  prop = RNA_def_enum(ot->srna,
112  "data_type",
115  "Data Type",
116  "Type of data stored in attribute");
118 
119  prop = RNA_def_enum(ot->srna,
120  "domain",
123  "Domain",
124  "Type of element that attribute is stored on");
127 }
128 
130 {
131  Object *ob = ED_object_context(C);
132  ID *id = ob->data;
134 
135  if (layer == NULL) {
136  return OPERATOR_CANCELLED;
137  }
138 
139  if (!BKE_id_attribute_remove(id, layer, op->reports)) {
140  return OPERATOR_CANCELLED;
141  }
142 
145 
146  return OPERATOR_FINISHED;
147 }
148 
150 {
151  /* identifiers */
152  ot->name = "Remove Geometry Attribute";
153  ot->description = "Remove attribute from geometry";
154  ot->idname = "GEOMETRY_OT_attribute_remove";
155 
156  /* api callbacks */
159 
160  /* flags */
162 }
Generic geometry attributes built on CustomData.
bool BKE_id_attribute_remove(struct ID *id, struct CustomDataLayer *layer, struct ReportList *reports)
Definition: attribute.c:154
struct CustomDataLayer * BKE_id_attributes_active_get(struct ID *id)
Definition: attribute.c:239
bool BKE_id_attributes_supported(struct ID *id)
Definition: attribute.c:104
void BKE_id_attributes_active_set(struct ID *id, struct CustomDataLayer *layer)
Definition: attribute.c:269
struct CustomDataLayer * BKE_id_attribute_new(struct ID *id, const char *name, const int type, const AttributeDomain domain, struct ReportList *reports)
Definition: attribute.c:137
AttributeDomain
Definition: BKE_attribute.h:41
@ ATTR_DOMAIN_POINT
Definition: BKE_attribute.h:43
#define UNUSED(x)
void DEG_id_tag_update(struct ID *id, int flag)
@ ID_RECALC_GEOMETRY
Definition: DNA_ID.h:611
#define ID_IS_LINKED(_id)
Definition: DNA_ID.h:426
CustomDataType
@ CD_PROP_FLOAT
#define MAX_NAME
Definition: DNA_defs.h:62
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
struct Object * ED_object_context(const struct bContext *C)
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
const EnumPropertyItem * rna_enum_attribute_domain_itemf(struct ID *id, bool *r_free)
@ PROP_SKIP_SAVE
Definition: RNA_types.h:204
#define C
Definition: RandGen.cpp:39
#define NC_GEOM
Definition: WM_types.h:294
@ OPTYPE_UNDO
Definition: WM_types.h:155
@ OPTYPE_REGISTER
Definition: WM_types.h:153
#define ND_DATA
Definition: WM_types.h:408
return(oflags[bm->toolflag_index].f &oflag) !=0
void GEOMETRY_OT_attribute_add(wmOperatorType *ot)
static const EnumPropertyItem * geometry_attribute_domain_itemf(bContext *C, PointerRNA *UNUSED(ptr), PropertyRNA *UNUSED(prop), bool *r_free)
static int geometry_attribute_remove_exec(bContext *C, wmOperator *op)
static int geometry_attribute_add_exec(bContext *C, wmOperator *op)
static bool geometry_attributes_poll(bContext *C)
void GEOMETRY_OT_attribute_remove(wmOperatorType *ot)
void RNA_string_get(PointerRNA *ptr, const char *name, char *value)
Definition: rna_access.c:6514
int RNA_enum_get(PointerRNA *ptr, const char *name)
Definition: rna_access.c:6402
const EnumPropertyItem rna_enum_attribute_domain_items[]
Definition: rna_attribute.c:52
const EnumPropertyItem rna_enum_attribute_type_items[]
Definition: rna_attribute.c:40
PropertyRNA * RNA_def_string(StructOrFunctionRNA *cont_, const char *identifier, const char *default_value, int maxlen, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3675
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
Definition: rna_define.c:1512
void RNA_def_enum_funcs(PropertyRNA *prop, EnumPropertyItemFunc itemfunc)
Definition: rna_define.c:3819
PropertyRNA * RNA_def_enum(StructOrFunctionRNA *cont_, const char *identifier, const EnumPropertyItem *items, int default_value, const char *ui_name, const char *ui_description)
Definition: rna_define.c:3771
const EnumPropertyItem DummyRNA_NULL_items[]
Definition: rna_rna.c:40
Definition: DNA_ID.h:273
void * data
int(* invoke)(struct bContext *, struct wmOperator *, const struct wmEvent *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:752
const char * name
Definition: WM_types.h:721
const char * idname
Definition: WM_types.h:723
bool(* poll)(struct bContext *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:776
struct StructRNA * srna
Definition: WM_types.h:802
const char * description
Definition: WM_types.h:726
int(* exec)(struct bContext *, struct wmOperator *) ATTR_WARN_UNUSED_RESULT
Definition: WM_types.h:736
struct ReportList * reports
struct PointerRNA * ptr
void WM_main_add_notifier(unsigned int type, void *reference)
PointerRNA * ptr
Definition: wm_files.c:3157
wmOperatorType * ot
Definition: wm_files.c:3156
int WM_operator_props_popup_confirm(bContext *C, wmOperator *op, const wmEvent *UNUSED(event))