Blender V4.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
9#include "RNA_access.hh"
10#include "RNA_define.hh"
11#include "RNA_enum_types.hh"
12
13#include "rna_internal.hh"
14
15#include "WM_types.hh"
16
17#include "usd.hh"
18
19#ifdef RNA_RUNTIME
20
21# include "DNA_object_types.h"
22# include "WM_api.hh"
23
24using namespace blender::io::usd;
25
26static StructRNA *rna_USDHook_refine(PointerRNA *ptr)
27{
28 USDHook *hook = (USDHook *)ptr->data;
29 return (hook->rna_ext.srna) ? hook->rna_ext.srna : &RNA_USDHook;
30}
31
32static bool rna_USDHook_unregister(Main * /*bmain*/, StructRNA *type)
33{
34 USDHook *hook = static_cast<USDHook *>(RNA_struct_blender_type_get(type));
35
36 if (hook == nullptr) {
37 return false;
38 }
39
40 /* free RNA data referencing this */
43
45
46 /* unlink Blender-side data */
48
49 return true;
50}
51
52static StructRNA *rna_USDHook_register(Main *bmain,
53 ReportList *reports,
54 void *data,
55 const char *identifier,
56 StructValidateFunc validate,
59{
60 const char *error_prefix = "Registering USD hook class:";
61 USDHook dummy_hook{};
62
63 /* setup dummy type info to store static properties in */
64 PointerRNA dummy_hook_ptr = RNA_pointer_create(nullptr, &RNA_USDHook, &dummy_hook);
65
66 /* validate the python class */
67 if (validate(&dummy_hook_ptr, data, nullptr) != 0) {
68 return nullptr;
69 }
70
71 if (strlen(identifier) >= sizeof(dummy_hook.idname)) {
72 BKE_reportf(reports,
74 "%s '%s' is too long, maximum length is %d",
75 error_prefix,
76 identifier,
77 (int)sizeof(dummy_hook.idname));
78 return nullptr;
79 }
80
81 /* check if we have registered this hook before, and remove it */
82 if (USDHook *hook = USD_find_hook_name(dummy_hook.idname)) {
83 BKE_reportf(reports,
85 "%s '%s', bl_idname '%s' has been registered before, unregistering previous",
86 error_prefix,
87 identifier,
88 dummy_hook.idname);
89
90 StructRNA *srna = hook->rna_ext.srna;
91 if (!rna_USDHook_unregister(bmain, srna)) {
92 BKE_reportf(reports,
94 "%s '%s', bl_idname '%s' %s",
95 error_prefix,
96 identifier,
97 dummy_hook.idname,
98 "could not be unregistered");
99 return nullptr;
100 }
101 }
102
103 /* create a new KeyingSetInfo type */
104 auto hook = std::make_unique<USDHook>();
105 *hook = dummy_hook;
106
107 /* set RNA-extensions info */
108 hook->rna_ext.srna = RNA_def_struct_ptr(&BLENDER_RNA, hook->idname, &RNA_USDHook);
109 hook->rna_ext.data = data;
110 hook->rna_ext.call = call;
111 hook->rna_ext.free = free;
112 RNA_struct_blender_type_set(hook->rna_ext.srna, hook.get());
113
114 /* add and register with other info as needed */
115 StructRNA *srna = hook->rna_ext.srna;
116 USD_register_hook(std::move(hook));
117
119
120 /* return the struct-rna added */
121 return srna;
122}
123
124#else
125
126static void rna_def_usd_hook(BlenderRNA *brna)
127{
128 StructRNA *srna;
129 PropertyRNA *prop;
130
131 srna = RNA_def_struct(brna, "USDHook", nullptr);
132 RNA_def_struct_ui_text(srna, "USD Hook", "Defines callback functions to extend USD IO");
133 RNA_def_struct_sdna(srna, "USDHook");
134 RNA_def_struct_refine_func(srna, "rna_USDHook_refine");
135 RNA_def_struct_register_funcs(srna, "rna_USDHook_register", "rna_USDHook_unregister", nullptr);
136
138
139 RNA_define_verify_sdna(false); /* not in sdna */
140
141 prop = RNA_def_property(srna, "bl_idname", PROP_STRING, PROP_NONE);
142 RNA_def_property_string_sdna(prop, nullptr, "idname");
144 RNA_def_property_ui_text(prop, "ID Name", "");
145
146 prop = RNA_def_property(srna, "bl_label", PROP_STRING, PROP_NONE);
147 RNA_def_property_string_sdna(prop, nullptr, "name");
148 RNA_def_property_ui_text(prop, "UI Name", "");
151
152 prop = RNA_def_property(srna, "bl_description", PROP_STRING, PROP_NONE);
153 RNA_def_property_string_sdna(prop, nullptr, "description");
154 RNA_def_property_string_maxlength(prop, RNA_DYN_DESCR_MAX); /* else it uses the pointer size! */
156 RNA_def_property_ui_text(prop, "Description", "A short description of the USD hook");
157}
158
159/* --- */
160
162{
163 rna_def_usd_hook(brna);
164}
165
166#endif
void BKE_reportf(ReportList *reports, eReportType type, const char *format,...) ATTR_PRINTF_FORMAT(3
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:746
@ PROP_STRING
Definition RNA_types.hh:68
void(*)(void *data) StructFreeFunc
Definition RNA_types.hh:751
int(*)(bContext *C, PointerRNA *ptr, FunctionRNA *func, ParameterList *list) StructCallbackFunc
Definition RNA_types.hh:747
@ PROP_REGISTER_OPTIONAL
Definition RNA_types.hh:301
@ PROP_REGISTER
Definition RNA_types.hh:300
@ PROP_NONE
Definition RNA_types.hh:136
#define NC_WINDOW
Definition WM_types.hh:342
void USD_unregister_hook(USDHook *hook)
Definition usd_hook.cc:53
void USD_register_hook(std::unique_ptr< USDHook > hook)
Definition usd_hook.cc:42
USDHook * USD_find_hook_name(const char idname[])
Definition usd_hook.cc:59
void RNA_struct_blender_type_set(StructRNA *srna, void *blender_type)
void * RNA_struct_blender_type_get(StructRNA *srna)
PointerRNA RNA_pointer_create(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:161
static void rna_def_usd_hook(BlenderRNA *brna)
Definition rna_usd.cc:126
StructRNA * srna
Definition RNA_types.hh:780
StructCallbackFunc call
Definition RNA_types.hh:781
StructFreeFunc free
Definition RNA_types.hh:782
void * data
Definition RNA_types.hh:42
ExtensionRNA rna_ext
Definition usd.hh:310
void WM_main_add_notifier(uint type, void *reference)
PointerRNA * ptr
Definition wm_files.cc:4126