Blender V5.0
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.hh"
22
23using namespace blender::io::usd;
24
25static StructRNA *rna_USDHook_refine(PointerRNA *ptr)
26{
27 USDHook *hook = (USDHook *)ptr->data;
28 return (hook->rna_ext.srna) ? hook->rna_ext.srna : &RNA_USDHook;
29}
30
31static bool rna_USDHook_unregister(Main * /*bmain*/, StructRNA *type)
32{
33 USDHook *hook = static_cast<USDHook *>(RNA_struct_blender_type_get(type));
34
35 if (hook == nullptr) {
36 return false;
37 }
38
39 /* free RNA data referencing this */
42
44
45 /* unlink Blender-side data */
47
48 return true;
49}
50
51static StructRNA *rna_USDHook_register(Main *bmain,
52 ReportList *reports,
53 void *data,
54 const char *identifier,
55 StructValidateFunc validate,
58{
59 const char *error_prefix = "Registering USD hook class:";
60 USDHook dummy_hook{};
61
62 /* setup dummy type info to store static properties in */
63 PointerRNA dummy_hook_ptr = RNA_pointer_create_discrete(nullptr, &RNA_USDHook, &dummy_hook);
64
65 /* validate the python class */
66 if (validate(&dummy_hook_ptr, data, nullptr) != 0) {
67 return nullptr;
68 }
69
70 if (strlen(identifier) >= sizeof(dummy_hook.idname)) {
71 BKE_reportf(reports,
73 "%s '%s' is too long, maximum length is %d",
74 error_prefix,
75 identifier,
76 int(sizeof(dummy_hook.idname)));
77 return nullptr;
78 }
79
80 /* check if we have registered this hook before, and remove it */
81 if (USDHook *hook = USD_find_hook_name(dummy_hook.idname)) {
82 BKE_reportf(reports,
84 "%s '%s', bl_idname '%s' has been registered before, unregistering previous",
85 error_prefix,
86 identifier,
87 dummy_hook.idname);
88
89 StructRNA *srna = hook->rna_ext.srna;
90 if (!rna_USDHook_unregister(bmain, srna)) {
91 BKE_reportf(reports,
93 "%s '%s', bl_idname '%s' %s",
94 error_prefix,
95 identifier,
96 dummy_hook.idname,
97 "could not be unregistered");
98 return nullptr;
99 }
100 }
101
102 /* create a new KeyingSetInfo type */
103 auto hook = std::make_unique<USDHook>();
104 *hook = dummy_hook;
105
106 /* set RNA-extensions info */
107 hook->rna_ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, hook->idname, &RNA_USDHook);
108 hook->rna_ext.data = data;
109 hook->rna_ext.call = call;
110 hook->rna_ext.free = free;
111 RNA_struct_blender_type_set(hook->rna_ext.srna, hook.get());
112
113 /* add and register with other info as needed */
114 StructRNA *srna = hook->rna_ext.srna;
115 USD_register_hook(std::move(hook));
116
118
119 /* return the struct-rna added */
120 return srna;
121}
122
123#else
124
125static void rna_def_usd_hook(BlenderRNA *brna)
126{
127 StructRNA *srna;
128 PropertyRNA *prop;
129
130 srna = RNA_def_struct(brna, "USDHook", nullptr);
131 RNA_def_struct_ui_text(srna, "USD Hook", "Defines callback functions to extend USD IO");
132 RNA_def_struct_sdna(srna, "USDHook");
133 RNA_def_struct_refine_func(srna, "rna_USDHook_refine");
134 RNA_def_struct_register_funcs(srna, "rna_USDHook_register", "rna_USDHook_unregister", nullptr);
135
137
138 RNA_define_verify_sdna(false); /* not in sdna */
139
140 prop = RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
141 RNA_def_property_string_sdna(prop, nullptr, "idname");
143 RNA_def_property_ui_text(prop, "ID Name", "");
144
145 prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
146 RNA_def_property_string_sdna(prop, nullptr, "name");
147 RNA_def_property_ui_text(prop, "UI Name", "");
150
151 prop = RNA_def_property(srna, "bl_description", PROP_STRING, PROP_NONE);
152 RNA_def_property_string_sdna(prop, nullptr, "description");
153 RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
155 RNA_def_property_ui_text(prop, "Description", "A short description of the USD hook");
156}
157
158/* --- */
159
161{
162 rna_def_usd_hook(brna);
163}
164
165#endif
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
@ RPT_INFO
Definition BKE_report.hh:35
@ RPT_ERROR
Definition BKE_report.hh:39
void BLI_kdtree_nd_ free(KDTree *tree)
Object is a sort of wrapper for general info.
#define RNA_DYN_DESCR_MAX
int(*)(PointerRNA *ptr, void *data, bool *have_function) StructValidateFunc
Definition RNA_types.hh:985
@ PROP_STRING
Definition RNA_types.hh:165
int(*)(bContext *C, PointerRNA *ptr, FunctionRNA *func, ParameterList *list) StructCallbackFunc
Definition RNA_types.hh:986
void(*)(void *data) StructFreeFunc
Definition RNA_types.hh:990
@ PROP_REGISTER_OPTIONAL
Definition RNA_types.hh:412
@ PROP_REGISTER
Definition RNA_types.hh:411
@ PROP_NONE
Definition RNA_types.hh:233
#define NC_WINDOW
Definition WM_types.hh:375
BMesh const char void * data
void USD_unregister_hook(const USDHook *hook)
Definition usd_hook.cc:82
void USD_register_hook(std::unique_ptr< USDHook > hook)
Definition usd_hook.cc:71
USDHook * USD_find_hook_name(const char idname[])
Definition usd_hook.cc:88
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
void * RNA_struct_blender_type_get(StructRNA *srna)
PointerRNA RNA_pointer_create_discrete(ID *id, StructRNA *type, void *data)
void RNA_def_struct_name_property(StructRNA *srna, PropertyRNA *prop)
void RNA_def_struct_refine_func(StructRNA *srna, const char *refine)
void RNA_define_verify_sdna(bool verify)
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)
void RNA_def_struct_ui_text(StructRNA *srna, const char *name, const char *description)
void RNA_def_struct_register_funcs(StructRNA *srna, const char *reg, const char *unreg, const char *instance)
void RNA_def_struct_sdna(StructRNA *srna, const char *structname)
void RNA_def_property_string_maxlength(PropertyRNA *prop, int maxlength)
PropertyRNA * RNA_def_property(StructOrFunctionRNA *cont_, const char *identifier, int type, int subtype)
StructRNA * RNA_def_struct_ptr(BlenderRNA *brna, const char *identifier, StructRNA *srnafrom)
StructRNA * RNA_def_struct(BlenderRNA *brna, const char *identifier, const char *from)
void RNA_struct_free_extension(StructRNA *srna, ExtensionRNA *rna_ext)
void RNA_struct_free(BlenderRNA *brna, StructRNA *srna)
void RNA_def_property_flag(PropertyRNA *prop, PropertyFlag flag)
BlenderRNA BLENDER_RNA
void RNA_def_usd(BlenderRNA *brna)
Definition rna_usd.cc:160
static void rna_def_usd_hook(BlenderRNA *brna)
Definition rna_usd.cc:125
StructRNA * srna
StructCallbackFunc call
StructFreeFunc free
ExtensionRNA rna_ext
Definition usd.hh:327
void WM_main_add_notifier(uint type, void *reference)
PointerRNA * ptr
Definition wm_files.cc:4238