Blender V5.3
object_random.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2014 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "DNA_layer_types.h"
10#include "DNA_object_types.h"
11
12#include "BLI_ghash.hh"
13#include "BLI_math_vector_c.hh"
14#include "BLI_rand.hh"
15
16#include "BKE_context.hh"
17#include "BKE_layer.hh"
18
19#include "RNA_access.hh"
20#include "RNA_define.hh"
21
23
24#include "WM_api.hh"
25#include "WM_types.hh"
26
27#include "ED_object.hh"
28#include "ED_transverts.hh"
29
30#include "object_intern.hh"
31
32namespace blender::ed::object {
33
37
39 const float offset,
40 const float uniform,
41 const float normal_factor,
42 const uint seed)
43{
44 bool use_normal = (normal_factor != 0.0f);
45 TransVert *tv;
46 int a;
47
48 if (!tvs || !(tvs->transverts)) {
49 return false;
50 }
51
52 RandomNumberGenerator rng(seed);
53
54 tv = tvs->transverts;
55 for (a = 0; a < tvs->transverts_tot; a++, tv++) {
56 const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * rng.get_float()));
57 float3 vec = rng.get_unit_float3();
58
59 if (use_normal && (tv->flag & TX_VERT_USE_NORMAL)) {
60 float no[3];
61
62 /* avoid >90d rotation to align with normal */
63 if (dot_v3v3(vec, tv->normal) < 0.0f) {
64 negate_v3_v3(no, tv->normal);
65 }
66 else {
67 copy_v3_v3(no, tv->normal);
68 }
69
70 interp_v3_v3v3_slerp_safe(vec, vec, no, normal_factor);
71 }
72
73 madd_v3_v3fl(tv->loc, vec, offset * t);
74 }
75
76 return true;
77}
78
80{
81 const Main *bmain = CTX_data_main(C);
82 const Scene *scene = CTX_data_scene(C);
83 ViewLayer *view_layer = CTX_data_view_layer(C);
84 Object *ob_active = CTX_data_edit_object(C);
85 const int ob_mode = ob_active->mode;
86
87 const float offset = RNA_float_get(op->ptr, "offset");
88 const float uniform = RNA_float_get(op->ptr, "uniform");
89 const float normal_factor = RNA_float_get(op->ptr, "normal");
90 const uint seed = RNA_int_get(op->ptr, "seed");
91
92 bool changed_multi = false;
93 Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);
95 *bmain, scene, view_layer, CTX_wm_view3d(C), eObjectMode(ob_mode));
96 for (const int ob_index : objects.index_range()) {
97 Object *ob_iter = objects[ob_index];
98
99 TransVertStore tvs = {nullptr};
100
101 if (ob_iter) {
102 int mode = TM_ALL_JOINTS;
103
104 if (normal_factor != 0.0f) {
105 mode |= TX_VERT_USE_NORMAL;
106 }
107
108 if (shape_key_report_if_locked(ob_iter, op->reports)) {
109 continue;
110 }
111
112 const Object *ob_iter_eval = DEG_get_evaluated(depsgraph, ob_iter);
113 ED_transverts_create_from_obedit(&tvs, ob_iter_eval, mode);
114 if (tvs.transverts_tot == 0) {
115 continue;
116 }
117
118 int seed_iter = seed;
119 /* This gives a consistent result regardless of object order. */
120 if (ob_index) {
121 seed_iter += BLI_ghashutil_strhash_p(ob_iter->id.name);
122 }
123
124 object_rand_transverts(&tvs, offset, uniform, normal_factor, seed_iter);
125
126 ED_transverts_update_obedit(&tvs, ob_iter);
127 ED_transverts_free(&tvs);
128
130 changed_multi = true;
131 }
132 }
133
134 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
135}
136
138{
139 /* identifiers */
140 ot->name = "Randomize";
141 ot->description = "Randomize vertices";
142 ot->idname = "TRANSFORM_OT_vertex_random";
143
144 /* API callbacks. */
147
148 /* flags */
150
151 /* props */
153 ot->srna, "offset", 0.0f, -FLT_MAX, FLT_MAX, "Amount", "Distance to offset", -10.0f, 10.0f);
155 "uniform",
156 0.0f,
157 0.0f,
158 1.0f,
159 "Uniform",
160 "Increase for uniform offset distance",
161 0.0f,
162 1.0f);
164 "normal",
165 0.0f,
166 0.0f,
167 1.0f,
168 "Normal",
169 "Align offset direction to normals",
170 0.0f,
171 1.0f);
173 ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
174
175 /* Set generic modal callbacks. */
177}
178
179} // namespace blender::ed::object
Object is a sort of wrapper for general info.
#define C
Definition RandGen.cpp:29
#define ND_TRANSFORM
Definition WM_types.hh:475
#define NC_OBJECT
Definition WM_types.hh:392
IndexRange index_range() const
#define FLT_MAX
#define uniform(slot)
bool shape_key_report_if_locked(const Object *obedit, ReportList *reports)
static wmOperatorStatus object_rand_verts_exec(bContext *C, wmOperator *op)
static bool object_rand_transverts(TransVertStore *tvs, const float offset, const float uniform, const float normal_factor, const uint seed)
void TRANSFORM_OT_vertex_random(wmOperatorType *ot)
MINLINE void negate_v3_v3(float r[3], const float a[3])
void ED_transverts_update_obedit(TransVertStore *tvs, Object *obedit)
MINLINE void copy_v3_v3(float r[3], const float a[3])
unsigned int uint
Definition BKE_text.h:55
@ TX_VERT_USE_NORMAL
bool ED_transverts_poll(bContext *C)
int RNA_int_get(PointerRNA *ptr, const char *name)
ViewLayer * CTX_data_view_layer(const bContext *C)
PropertyRNA * RNA_def_float_factor(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, int mode)
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C, bool rna_write_check=false)
Object * CTX_data_edit_object(const bContext *C)
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
Vector< Object * > BKE_view_layer_array_from_objects_in_mode_unique_data(const Main &bmain, const Scene *scene, ViewLayer *view_layer, const View3D *v3d, eObjectMode mode)
void interp_v3_v3v3_slerp_safe(float target[3], const float a[3], const float b[3], float t)
PropertyRNA * RNA_def_int(StructOrFunctionRNA *cont_, const char *identifier, const int default_value, const int hardmin, const int hardmax, const char *ui_name, const char *ui_description, const int softmin, const int softmax)
Main * CTX_data_main(const bContext *C)
MINLINE float max_ff(float a, float b)
void ED_transverts_free(TransVertStore *tvs)
PropertyRNA * RNA_def_float_distance(StructOrFunctionRNA *cont_, const char *identifier, const float default_value, const float hardmin, const float hardmax, const char *ui_name, const char *ui_description, const float softmin, const float softmax)
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
Scene * CTX_data_scene(const bContext *C)
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)
@ OPTYPE_REGISTER
Definition WM_types.hh:188
@ OPTYPE_UNDO
Definition WM_types.hh:190
VecBase< float, 3 > float3
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
float RNA_float_get(PointerRNA *ptr, const char *name)
View3D * CTX_wm_view3d(const bContext *C)
char name[258]
Definition DNA_ID.h:433
wmOperatorStatus(*) exec(bContext *C, wmOperator *op) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1125
const char * description
Definition WM_types.hh:1115
bool(*) poll(bContext *C) ATTR_WARN_UNUSED_RESULT
Definition WM_types.hh:1165
struct ReportList * reports