Blender V5.3
rna_usd.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "RNA_define.hh"
10#include "RNA_enum_types.hh"
11
12#include "rna_internal.hh"
13
14#include "WM_types.hh"
15
16#ifdef RNA_RUNTIME
17
18# include "DNA_object_types.h"
19# include "WM_api.hh"
20
21# include "usd_api_hook.hh"
22
23namespace blender {
24
25using namespace io::usd;
26
27static StructRNA *rna_USDHook_refine(PointerRNA *ptr)
28{
29 USDHook *hook = static_cast<USDHook *>(ptr->data);
30 return (hook->rna_ext.srna) ? hook->rna_ext.srna : RNA_USDHook;
31}
32
33static bool rna_USDHook_unregister(Main * /*bmain*/, StructRNA *type)
34{
35 USDHook *hook = static_cast<USDHook *>(RNA_struct_blender_type_get(type));
36
37 if (hook == nullptr) {
38 return false;
39 }
40
41 /* free RNA data referencing this */
42 RNA_struct_free_extension(type, &hook->rna_ext);
44
46
47 /* unlink Blender-side data */
49
50 return true;
51}
52
53static StructRNA *rna_USDHook_register(Main *bmain,
54 ReportList *reports,
55 void *data,
56 const char *identifier,
57 StructValidateFunc validate,
60{
61 const char *error_prefix = "Registering USD hook class:";
62 USDHook dummy_hook{};
63
64 /* setup dummy type info to store static properties in */
65 PointerRNA dummy_hook_ptr = RNA_pointer_create_discrete(nullptr, RNA_USDHook, &dummy_hook);
66
67 /* validate the python class */
68 if (validate(&dummy_hook_ptr, data, nullptr) != 0) {
69 return nullptr;
70 }
71
72 if (strlen(identifier) >= sizeof(dummy_hook.idname)) {
73 BKE_reportf(reports,
75 "%s '%s' is too long, maximum length is %d",
76 error_prefix,
77 identifier,
78 int(sizeof(dummy_hook.idname)));
79 return nullptr;
80 }
81
82 /* check if we have registered this hook before, and remove it */
83 if (USDHook *hook = USD_find_hook_name(dummy_hook.idname)) {
84 BKE_reportf(reports,
86 "%s '%s', bl_idname '%s' has been registered before, unregistering previous",
87 error_prefix,
88 identifier,
89 dummy_hook.idname);
90
91 StructRNA *srna = hook->rna_ext.srna;
92 if (!rna_USDHook_unregister(bmain, srna)) {
93 BKE_reportf(reports,
95 "%s '%s', bl_idname '%s' %s",
96 error_prefix,
97 identifier,
98 dummy_hook.idname,
99 "could not be unregistered");
100 return nullptr;
101 }
102 }
103
104 /* create a new KeyingSetInfo type */
105 auto hook = std::make_unique<USDHook>();
106 *hook = dummy_hook;
107
108 /* set RNA-extensions info */
109 hook->rna_ext.srna = RNA_def_struct_ptr(&RNA_blender_rna_get(), hook->idname, RNA_USDHook);
110 hook->rna_ext.data = data;
111 hook->rna_ext.call = call;
112 hook->rna_ext.free = free;
113 RNA_struct_blender_type_set(hook->rna_ext.srna, hook.get());
114
115 /* add and register with other info as needed */
116 StructRNA *srna = hook->rna_ext.srna;
117 USD_register_hook(std::move(hook));
118
120
121 /* return the struct-rna added */
122 return srna;
123}
124
125} // namespace blender
126
127#else
128
129namespace blender {
130
131static void rna_def_usd_hook(BlenderRNA *brna)
132{
133 StructRNA *srna;
134 PropertyRNA *prop;
135
136 srna = RNA_def_struct(brna, "USDHook", nullptr);
137 RNA_def_struct_ui_text(srna, "USD Hook", "Defines callback functions to extend USD IO");
138 RNA_def_struct_sdna(srna, "USDHook");
139 RNA_def_struct_refine_func(srna, "rna_USDHook_refine");
140 RNA_def_struct_register_funcs(srna, "rna_USDHook_register", "rna_USDHook_unregister", nullptr);
141
143
144 RNA_define_verify_sdna(false); /* not in sdna */
145
146 prop = RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
147 RNA_def_property_string_sdna(prop, nullptr, "idname");
149 RNA_def_property_ui_text(prop, "ID Name", "");
150
151 prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
152 RNA_def_property_string_sdna(prop, nullptr, "name");
153 RNA_def_property_ui_text(prop, "UI Name", "");
156
157 prop = RNA_def_property(srna, "bl_description", PROP_STRING, PROP_NONE);
158 RNA_def_property_string_sdna(prop, nullptr, "description");
159 RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
161 RNA_def_property_ui_text(prop, "Description", "A short description of the USD hook");
162}
163
164/* --- */
165
167{
168 rna_def_usd_hook(brna);
169}
170
171} // namespace blender
172
173#endif
Object is a sort of wrapper for general info.
#define RNA_DYN_DESCR_MAX
#define NC_WINDOW
Definition WM_types.hh:388
void free(Subdiv *subdiv)
Definition subdiv.cc:192
void USD_unregister_hook(const USDHook *hook)
Definition usd_hook.cc:71
void USD_register_hook(std::unique_ptr< USDHook > hook)
Definition usd_hook.cc:60
USDHook * USD_find_hook_name(const char idname[])
Definition usd_hook.cc:77
BMesh Iterators.
void WM_main_add_notifier(uint type, void *reference)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_property_ui_text(PropertyRNA *prop, const char *name, const char *description)
void RNA_def_property_string_sdna(PropertyRNA *prop, const char *structname, const char *propname)
int(*)(PointerRNA *ptr, void *data, bool *have_function) StructValidateFunc
void RNA_def_struct_refine_func(StructRNA *srna, const char *refine)
static void rna_def_usd_hook(BlenderRNA *brna)
Definition rna_usd.cc:131
void * RNA_struct_blender_type_get(StructRNA *srna)
void RNA_def_property_string_maxlength(PropertyRNA *prop, int maxlength)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier_c_str, int type, int subtype)
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
void RNA_struct_free(BlenderRNA *brna, StructRNA *srna)
@ PROP_STRING
Definition RNA_types.hh:213
BMesh const char void * data
void RNA_def_usd(BlenderRNA *brna)
Definition rna_usd.cc:166
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void(*)(void *data) StructFreeFunc
void RNA_def_struct_register_funcs(StructRNA *srna, const char *reg, const char *unreg, const char *instance)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
Definition report.cc:191
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
@ PROP_REGISTER_OPTIONAL
Definition RNA_types.hh:482
@ PROP_REGISTER
Definition RNA_types.hh:481
void RNA_define_verify_sdna(bool verify)
void RNA_struct_free_extension(StructRNA *srna, ExtensionRNA *rna_ext)
void RNA_def_struct_name_property(StructRNA *srna, PropertyRNA *prop, const bool allow_replace)
int(*)(bContext *C, PointerRNA *ptr, FunctionRNA *func, ParameterList *list) StructCallbackFunc
BlenderRNA RNA_blender_rna_get()
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)