Blender V5.0
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_math_vector.h"
13#include "BLI_rand.hh"
14
15#include "BKE_context.hh"
16#include "BKE_layer.hh"
17
18#include "RNA_access.hh"
19#include "RNA_define.hh"
20
22
23#include "WM_api.hh"
24#include "WM_types.hh"
25
26#include "ED_object.hh"
27#include "ED_transverts.hh"
28
29#include "object_intern.hh"
30
31namespace blender::ed::object {
32
36
38 const float offset,
39 const float uniform,
40 const float normal_factor,
41 const uint seed)
42{
43 bool use_normal = (normal_factor != 0.0f);
44 TransVert *tv;
45 int a;
46
47 if (!tvs || !(tvs->transverts)) {
48 return false;
49 }
50
52
53 tv = tvs->transverts;
54 for (a = 0; a < tvs->transverts_tot; a++, tv++) {
55 const float t = max_ff(0.0f, uniform + ((1.0f - uniform) * rng.get_float()));
56 float3 vec = rng.get_unit_float3();
57
58 if (use_normal && (tv->flag & TX_VERT_USE_NORMAL)) {
59 float no[3];
60
61 /* avoid >90d rotation to align with normal */
62 if (dot_v3v3(vec, tv->normal) < 0.0f) {
63 negate_v3_v3(no, tv->normal);
64 }
65 else {
66 copy_v3_v3(no, tv->normal);
67 }
68
69 interp_v3_v3v3_slerp_safe(vec, vec, no, normal_factor);
70 }
71
72 madd_v3_v3fl(tv->loc, vec, offset * t);
73 }
74
75 return true;
76}
77
79{
80 const Scene *scene = CTX_data_scene(C);
81 ViewLayer *view_layer = CTX_data_view_layer(C);
82 Object *ob_active = CTX_data_edit_object(C);
83 const int ob_mode = ob_active->mode;
84
85 const float offset = RNA_float_get(op->ptr, "offset");
86 const float uniform = RNA_float_get(op->ptr, "uniform");
87 const float normal_factor = RNA_float_get(op->ptr, "normal");
88 const uint seed = RNA_int_get(op->ptr, "seed");
89
90 bool changed_multi = false;
93 scene, view_layer, CTX_wm_view3d(C), eObjectMode(ob_mode));
94 for (const int ob_index : objects.index_range()) {
95 Object *ob_iter = objects[ob_index];
96
97 TransVertStore tvs = {nullptr};
98
99 if (ob_iter) {
100 int mode = TM_ALL_JOINTS;
101
102 if (normal_factor != 0.0f) {
103 mode |= TX_VERT_USE_NORMAL;
104 }
105
106 if (shape_key_report_if_locked(ob_iter, op->reports)) {
107 continue;
108 }
109
110 const Object *ob_iter_eval = DEG_get_evaluated(depsgraph, ob_iter);
111 ED_transverts_create_from_obedit(&tvs, ob_iter_eval, mode);
112 if (tvs.transverts_tot == 0) {
113 continue;
114 }
115
116 int seed_iter = seed;
117 /* This gives a consistent result regardless of object order. */
118 if (ob_index) {
119 seed_iter += BLI_ghashutil_strhash_p(ob_iter->id.name);
120 }
121
122 object_rand_transverts(&tvs, offset, uniform, normal_factor, seed_iter);
123
124 ED_transverts_update_obedit(&tvs, ob_iter);
125 ED_transverts_free(&tvs);
126
128 changed_multi = true;
129 }
130 }
131
132 return changed_multi ? OPERATOR_FINISHED : OPERATOR_CANCELLED;
133}
134
136{
137 /* identifiers */
138 ot->name = "Randomize";
139 ot->description = "Randomize vertices";
140 ot->idname = "TRANSFORM_OT_vertex_random";
141
142 /* API callbacks. */
144 ot->poll = ED_transverts_poll;
145
146 /* flags */
148
149 /* props */
151 ot->srna, "offset", 0.0f, -FLT_MAX, FLT_MAX, "Amount", "Distance to offset", -10.0f, 10.0f);
153 "uniform",
154 0.0f,
155 0.0f,
156 1.0f,
157 "Uniform",
158 "Increase for uniform offset distance",
159 0.0f,
160 1.0f);
162 "normal",
163 0.0f,
164 0.0f,
165 1.0f,
166 "Normal",
167 "Align offset direction to normals",
168 0.0f,
169 1.0f);
171 ot->srna, "seed", 0, 0, 10000, "Random Seed", "Seed for the random number generator", 0, 50);
172
173 /* Set generic modal callbacks. */
175}
176
177} // namespace blender::ed::object
Depsgraph * CTX_data_ensure_evaluated_depsgraph(const bContext *C)
Scene * CTX_data_scene(const bContext *C)
Object * CTX_data_edit_object(const bContext *C)
View3D * CTX_wm_view3d(const bContext *C)
ViewLayer * CTX_data_view_layer(const bContext *C)
blender::Vector< Object * > BKE_view_layer_array_from_objects_in_mode_unique_data(const Scene *scene, ViewLayer *view_layer, const View3D *v3d, eObjectMode mode)
unsigned int BLI_ghashutil_strhash_p(const void *ptr)
MINLINE float max_ff(float a, float b)
MINLINE void madd_v3_v3fl(float r[3], const float a[3], float f)
MINLINE void copy_v3_v3(float r[3], const float a[3])
MINLINE void negate_v3_v3(float r[3], const float a[3])
void interp_v3_v3v3_slerp_safe(float target[3], const float a[3], const float b[3], float t)
MINLINE float dot_v3v3(const float a[3], const float b[3]) ATTR_WARN_UNUSED_RESULT
unsigned int uint
T * DEG_get_evaluated(const Depsgraph *depsgraph, T *id)
eObjectMode
Object is a sort of wrapper for general info.
@ OPERATOR_CANCELLED
@ OPERATOR_FINISHED
bool ED_transverts_poll(bContext *C)
void ED_transverts_create_from_obedit(TransVertStore *tvs, const Object *obedit, int mode)
void ED_transverts_free(TransVertStore *tvs)
@ TX_VERT_USE_NORMAL
void ED_transverts_update_obedit(TransVertStore *tvs, Object *obedit)
@ TM_ALL_JOINTS
#define C
Definition RandGen.cpp:29
#define ND_DRAW
Definition WM_types.hh:461
@ OPTYPE_UNDO
Definition WM_types.hh:182
@ OPTYPE_REGISTER
Definition WM_types.hh:180
#define NC_OBJECT
Definition WM_types.hh:379
BPy_StructRNA * depsgraph
static unsigned long seed
Definition btSoftBody.h:39
IndexRange index_range() const
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)
VecBase< float, 3 > float3
int RNA_int_get(PointerRNA *ptr, const char *name)
float RNA_float_get(PointerRNA *ptr, const char *name)
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)
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)
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)
#define FLT_MAX
Definition stdcycles.h:14
char name[258]
Definition DNA_ID.h:432
TransVert * transverts
float * loc
float normal[3]
struct ReportList * reports
struct PointerRNA * ptr
void WM_event_add_notifier(const bContext *C, uint type, void *reference)
wmOperatorType * ot
Definition wm_files.cc:4237
void WM_operator_type_modal_from_exec_for_object_edit_coords(wmOperatorType *ot)