-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnoisedRandomize.py
More file actions
762 lines (660 loc) · 17.6 KB
/
noisedRandomize.py
File metadata and controls
762 lines (660 loc) · 17.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
bl_info = {
"name": "Noised Randomize",
"description": "Noised Randomize",
"author": "nukkio",
"version": (0, 1, 0),
"blender": (2, 80, 0),
"location": "Scene Settings",
"warning": "",
"wiki_url": "https://github.com/nukkio/blenderNoisedRandomize"
"",
"category": "Scene",
}
import bpy
import random
import math
import mathutils
#from math import ceil, floor, sqrt, radians
from math import radians
from bpy.types import Panel, Operator, Scene, PropertyGroup
from bpy.app.handlers import persistent, frame_change_pre
from bpy.props import (
BoolProperty,
EnumProperty,
FloatProperty,
IntProperty,
IntVectorProperty,
StringProperty,
PointerProperty,
)
#def countEnum(en):
# prop = rna_type.bl_rna.properties[prop_str]
# return [e.identifier for e in nrs.NRgrpSet]
#def nrAddGroup(nrs):
# print("nrAddGroup")
# scn = bpy.context.scene
# nrs = scn.nrsettings
#
# #salva i valori in una custom property della scena
## bpy.context.scene["NRsettings.001"]={'var1':1,'var2':'ciao','var3':[1,2,3]}
# #che si legge: bpy.context.scene["NRsettings.001"]['var1']
#
# #cerca nelle custom property
# if len(scn.keys()) > 1:
# print("Scene custom properties:")
# for K in scn.keys():
# if K not in '_RNA_UI':
# if K.startswith('NRsettings'):
# print("***********")
# print( K , "-" , scn[K] )
# print("+++++++++++")
# print(bpy.context.scene[K]['var1'])
# print(bpy.context.scene[K]['var2'])
# print(bpy.context.scene[K]['var3'])
#
## argrp = [e for e in nrs.NRgrpSet]
## ngrp=len(argrp)
## ngrp=ngrp+1
## rName="NRset."
## newName=rName+str(ngrp)
## print(newName)
#
#def nrChangedGroup(self, context):
# print("nrChangedGroup")
# scn = bpy.context.scene
# nrs = scn.nrsettings
# print(nrs.NRgrpSet)
## if nrs.NRtype=="RANDOM":
## nrval=mathutils.noise.random()
## else:
## nrval=mathutils.noise.noise((vX,vY,vZ), noise_basis=nrs.NRtype)
##
def nrUpdate(self, context):
# print("nrUpdate")
if nrUpdate.level is False:
nrUpdate.level = True
# print("update")
#cicla per le custom properties cercando "NRsetting..."
#prende il valore active
#se True
#prende tutti i valori
scn = bpy.context.scene
nrs = scn.nrsettings
if nrs.NR_is_hide==False:
setNRtransform()
nrUpdate.level = False
nrUpdate.level = False
@persistent
def nr_animation_update(scn):
""" Function for updating the effects when the scene update """
setNRtransform()
def setNRtransform():
# print("setNRtransform")
scn = bpy.context.scene
#frame = scn.frame_current
nrs = scn.nrsettings
#controlla tutti gli oggetti della collezione
if nrs.NR_enabled==True and nrs.NR_is_hide==False:
collName=nrs.NR_collName
# print("ready "+collName)
nrColl = bpy.data.collections[collName]
for ob in nrColl.objects:
# ob.select_set(state=False)
nrval=getNRval(ob,nrs)
#loc
ob.delta_location.x=nrval*nrs.NR_trsf_locx_s
ob.delta_location.y=nrval*nrs.NR_trsf_locy_s
ob.delta_location.z=nrval*nrs.NR_trsf_locz_s
# #rot
ob.delta_rotation_euler.x=radians(nrval*nrs.NR_trsf_rotx_s)
ob.delta_rotation_euler.y=radians(nrval*nrs.NR_trsf_roty_s)
ob.delta_rotation_euler.z=radians(nrval*nrs.NR_trsf_rotz_s)
# #scl delta_scale
ob.delta_scale.x=1+(nrval*nrs.NR_trsf_sclx_s)
ob.delta_scale.y=1+(nrval*nrs.NR_trsf_scly_s)
ob.delta_scale.z=1+(nrval*nrs.NR_trsf_sclz_s)
# else:
# print("NR not ready")
def copyNRtransform(nrs,removeNR):
collName=nrs.NR_collName
if bpy.data.collections.get(collName) is None:
print("NR - nothing to copy")
nrs.NR_enabled=False
else:
nrColl = bpy.data.collections[collName]
for ob in nrColl.objects:
#loc
ob.location.x=ob.location.x+ob.delta_location.x
ob.location.y=ob.location.y+ob.delta_location.y
ob.location.z=ob.location.z+ob.delta_location.z
#rot
ob.rotation_euler.x=ob.rotation_euler.x+ob.delta_rotation_euler.x
ob.rotation_euler.y=ob.rotation_euler.y+ob.delta_rotation_euler.y
ob.rotation_euler.z=ob.rotation_euler.z+ob.delta_rotation_euler.z
#scl
ob.scale.x=ob.scale.x*ob.delta_scale.x
ob.scale.y=ob.scale.y*ob.delta_scale.y
ob.scale.z=ob.scale.z*ob.delta_scale.z
if removeNR:
delNRtransform(nrs)
def hideNRtransform(self, context):
# print("hideNRtransform")
scn = bpy.context.scene
frame = scn.frame_current
nrs = scn.nrsettings
collName=nrs.NR_collName
if bpy.data.collections.get(collName) is None:
print("NR - nothing to hide or show")
nrs.NR_enabled=False
else:
nrColl = bpy.data.collections[collName]
if nrs.NR_is_hide:
for ob in nrColl.objects:
# ob.select_set(state=False)
#loc
ob.delta_location.x=0
ob.delta_location.y=0
ob.delta_location.z=0
#rot
ob.delta_rotation_euler.x=0
ob.delta_rotation_euler.y=0
ob.delta_rotation_euler.z=0
#scl
ob.delta_scale.x=1
ob.delta_scale.y=1
ob.delta_scale.z=1
else:
setNRtransform()
def selNRtransform(nrs):
collName=nrs.NR_collName
if bpy.data.collections.get(collName) is None:
print("NR - nothing to select")
nrs.NR_enabled=False
else:
nrColl = bpy.data.collections[collName]
for ob in nrColl.objects:
ob.select_set(state=True)
def delNRtransform(nrs):
collName=nrs.NR_collName
if bpy.data.collections.get(collName) is None:
print("NR - nothing to delete")
nrs.NR_enabled=False
else:
nrColl = bpy.data.collections[collName]
for ob in nrColl.objects:
# ob.select_set(state=False)
#loc
ob.delta_location.x=0
ob.delta_location.y=0
ob.delta_location.z=0
#rot
ob.delta_rotation_euler.x=0
ob.delta_rotation_euler.y=0
ob.delta_rotation_euler.z=0
#scl
ob.delta_scale.x=1
ob.delta_scale.y=1
ob.delta_scale.z=1
nrColl.objects.unlink(ob)
bpy.context.scene.collection.children.unlink(nrColl)
bpy.data.collections.remove(nrColl)
nrs.NR_enabled=False
nrs.NR_is_hide=False
def getNRval(ob,nrss):
# print("getNRval")
scn=bpy.context.scene
rnd = bpy.context.scene.render
frame = scn.frame_current
nrs = scn.nrsettings
ret=0
nS=0
nrval=0
#controlla la dimensione dell'immagine noise
# nS=nrs.NRscl/100
#nS=((nrs.NRscl*-1)+1000)/100
nS=((nrs.NRscl*-1)+1000)/1000
#nS=nrs.NRscl/1000
#posizione dell'immagine noise
nX=nrs.NRposx
nY=nrs.NRposy
nZ=nrs.NRposz
#posizione oggetto
sX=ob.location.x
sY=ob.location.y
sZ=ob.location.z
#valori posizione
vX=(nX-sX)*nS
vY=(nY-sY)*nS
vZ=(nZ-sZ)*nS
if nrs.NRtype=="RANDOM":
nrval=mathutils.noise.random()
else:
nrval=mathutils.noise.noise((vX,vY,vZ), noise_basis=nrs.NRtype)
return nrval
class NRSettings(PropertyGroup):
NR_enabled: BoolProperty(
name="NR ready",
default=False,
description="NR enabled",
)
# NR_is_animated: BoolProperty(
# name="NR animated",
# default=False,
# description="NR animated",
# update=followAnimation,
# )
NR_is_hide: BoolProperty(
name="NR hidden",
default=False,
description="NR hidden",
update=hideNRtransform,
)
NR_active: BoolProperty(
name="NR active",
default=False,
description="NR active",
)
# dimensione img noise
NRscl: FloatProperty(
name="noise scale",
default=550,
min=0,
max=1000,
description="Scale of noise",
update=nrUpdate
)
# posizione img noise xyz
NRposx: FloatProperty(
name="nrlX",
default=0,
min=-10000,
max=10000,
description="Position X of noise",
update=nrUpdate
)
NRposy: FloatProperty(
name="nrlY",
default=0,
min=-10000,
max=10000,
description="Position Y of noise",
update=nrUpdate
)
NRposz: FloatProperty(
name="nrlZ",
default=1,
min=-10000,
max=10000,
description="Position Z of noise",
update=nrUpdate,
)
# tipo noise
NRtype: EnumProperty(
name="",
description="Noise type",
items=(
("BLENDER", "BLENDER","BLENDER"),
("PERLIN_ORIGINAL","PERLIN_ORIGINAL","PERLIN_ORIGINAL"),
("PERLIN_NEW","PERLIN_NEW","PERLIN_NEW"),
("VORONOI_F1","VORONOI_F1","VORONOI_F1"),
("VORONOI_F2","VORONOI_F2","VORONOI_F2"),
("VORONOI_F3","VORONOI_F3","VORONOI_F3"),
("VORONOI_F4","VORONOI_F4","VORONOI_F4"),
("VORONOI_F2F1","VORONOI_F2F1","VORONOI_F2F1"),
("VORONOI_CRACKLE","VORONOI_CRACKLE","VORONOI_CRACKLE"),
("CELLNOISE","CELLNOISE","CELLNOISE"),
("RANDOM","Random","pure random")
),
default="BLENDER",
update=nrUpdate,
)
# nome collezione
NR_collName:StringProperty(
name = "nrCollName",
description = "The name of the new NR Collection",
default = "_NRcoll_")
#################################
NR_trsf_locx_s: FloatProperty(
name="nrlXs",
default=0,
min=-10000,
max=10000,
description="strength location X",
update=nrUpdate
)
NR_trsf_locy_s: FloatProperty(
name="nrlYs",
default=0,
min=-10000,
max=10000,
description="strength location Y",
update=nrUpdate
)
NR_trsf_locz_s: FloatProperty(
name="nrlZs",
default=0,
min=-10000,
max=10000,
description="strength location Z",
update=nrUpdate
)
##################################
NR_trsf_rotx_s: FloatProperty(
name="nrrXs",
default=0,
min=-10000,
max=10000,
description="strength rotation X",
update=nrUpdate
)
NR_trsf_roty_s: FloatProperty(
name="nrrYs",
default=0,
min=-10000,
max=10000,
description="strength rotation Y",
update=nrUpdate
)
NR_trsf_rotz_s: FloatProperty(
name="nrrZs",
default=0,
min=-10000,
max=10000,
description="strength rotation Z",
update=nrUpdate
)
#####################################
NR_trsf_sclx_s: FloatProperty(
name="nrsXs",
default=0,
min=-10000,
max=10000,
description="strength scale X",
update=nrUpdate
)
NR_trsf_scly_s: FloatProperty(
name="nrsYs",
default=0,
min=-10000,
max=10000,
description="strength scale Y",
update=nrUpdate
)
NR_trsf_sclz_s: FloatProperty(
name="nrsZs",
default=0,
min=-10000,
max=10000,
description="strength scale Z",
update=nrUpdate
)
#######################################
class NOISED_PT_Random(Panel):
bl_label = "Noised randomize"
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def draw(self, context):
layout = self.layout
scn = context.scene
nrs = scn.nrsettings
label_save="label"
rowList=[]
row1 = layout.row(align=True)
rowList.append(row1)
row2 = layout.row(align=True)
rowList.append(row2)
row3 = layout.row(align=True)
rowList.append(row3)
row4 = layout.row(align=True)
rowList.append(row4)
row5 = layout.row(align=True)
rowList.append(row5)
row6 = layout.row(align=True)
rowList.append(row6)
row7 = layout.row(align=True)
rowList.append(row7)
row8 = layout.row(align=True)
rowList.append(row8)
row9 = layout.row(align=True)
rowList.append(row9)
row10 = layout.row(align=True)
rowList.append(row10)
row11 = layout.row(align=True)
rowList.append(row11)
row12 = layout.row(align=True)
rowList.append(row12)
row13 = layout.row(align=True)
rowList.append(row13)
row14 = layout.row(align=True)
rowList.append(row14)
row15 = layout.row(align=True)
rowList.append(row15)
row16 = layout.row(align=True)
rowList.append(row16)
rn=0
rowList[rn].label(text="Noise settings")
rn+=1
rowList[rn].prop(nrs, "NRscl")
rn+=1
rowList[rn].prop(nrs, "NRposx",text="loc X")
rowList[rn].prop(nrs, "NRposy",text="loc Y")
rowList[rn].prop(nrs, "NRposz",text="loc Z")
rn+=1
rowList[rn].label(text="Noise type")
rowList[rn].prop(nrs, "NRtype")
rn+=1
# rowList[rn].label(text="Collection name")
# rowList[rn].prop(nrs, "NR_collName")
rowList[rn].label(text="Multiplier for Transformation")
rn+=1
rowList[rn].label(text="loc")
rowList[rn].prop(nrs, "NR_trsf_locx_s",text="X")
rowList[rn].prop(nrs, "NR_trsf_locy_s",text="Y")
rowList[rn].prop(nrs, "NR_trsf_locz_s",text="Z")
rn+=1
rowList[rn].label(text="rot")
rowList[rn].prop(nrs, "NR_trsf_rotx_s",text="X")
rowList[rn].prop(nrs, "NR_trsf_roty_s",text="Y")
rowList[rn].prop(nrs, "NR_trsf_rotz_s",text="Z")
rn+=1
rowList[rn].label(text="scl")
rowList[rn].prop(nrs, "NR_trsf_sclx_s",text="X")
rowList[rn].prop(nrs, "NR_trsf_scly_s",text="Y")
rowList[rn].prop(nrs, "NR_trsf_sclz_s",text="Z")
rnmin=rn
rn+=1
if nrs.NR_is_hide:
icon = 'HIDE_OFF'
txt = 'Show'
else:
icon = 'HIDE_ON'
txt = 'Hide'
rowList[rn].prop(nrs, 'NR_is_hide', text=txt, icon=icon, toggle = True)
rn+=1
rowList[rn].label(text="Copy to transform")
rn+=1
rowList[rn].operator("noised.randomcopy", text="Copy", icon="FORWARD")
rowList[rn].operator("noised.randomcopydel", text="Copy and remove", icon="FORWARD")
rn+=1
row14.separator()
rn+=1
rowList[rn].operator("noised.randomsel", text="Select all Object", icon="ARROW_LEFTRIGHT")
rn+=1
rowList[rn].operator("noised.randomdel", text="Remove", icon="CANCEL")
rn+=1
rnmax=rn
#
if nrs.NR_enabled:
icon="PLUS"
txt="Add object"
else:
icon="TRIA_RIGHT"
txt="Activate"
rowList[rn].operator("noised.random", text=txt, icon=icon)
for rnn in range(0,rnmax):
rowList[rnn].active = nrs.NR_enabled
if rnn>rnmin:
rowList[rnn].enabled = nrs.NR_enabled
# rn+=1
# rowList[rn].label(text="Noise settings groups")
# rowList[rn].operator("noised.randomaddgroup", text="Add new setting group", icon="ADD")
## row16.prop(nrs, "NRframe")
class NRClassOpAddgrp(Operator):
bl_idname = "noised.randomaddgroup"
bl_label = "add setting group"
bl_description = "noised random add a new setting group"
bl_options = {'REGISTER', 'UNDO'}
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def execute(self, context):
scn = context.scene
rnd = context.scene.render
nrs = scn.nrsettings
nrAddGroup(nrs)
return {'FINISHED'}
class NRClassOpSel(Operator):
bl_idname = "noised.randomsel"
bl_label = "Noised random select all"
bl_description = "noised random select all"
bl_options = {'REGISTER', 'UNDO'}
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def execute(self, context):
scn = context.scene
rnd = context.scene.render
nrs = scn.nrsettings
selNRtransform(nrs)
return {'FINISHED'}
class NRClassOpDel(Operator):
bl_idname = "noised.randomdel"
bl_label = "Noised random delete"
bl_description = "noised random delete"
bl_options = {'REGISTER', 'UNDO'}
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def execute(self, context):
scn = context.scene
rnd = context.scene.render
nrs = scn.nrsettings
delNRtransform(nrs)
return {'FINISHED'}
class NRClassOpCopy(Operator):
bl_idname = "noised.randomcopy"
bl_label = "Noised random copy to transform"
bl_description = "noised random copy to transform"
bl_options = {'REGISTER', 'UNDO'}
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def execute(self, context):
scn = context.scene
rnd = context.scene.render
nrs = scn.nrsettings
copyNRtransform(nrs,False)
return {'FINISHED'}
class NRClassOpCopyDel(Operator):
bl_idname = "noised.randomcopydel"
bl_label = "Noised random copy to transform and remove NR"
bl_description = "noised random copy to transform and remove NR"
bl_options = {'REGISTER', 'UNDO'}
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def execute(self, context):
scn = context.scene
rnd = context.scene.render
nrs = scn.nrsettings
copyNRtransform(nrs,True)
return {'FINISHED'}
class NRClassOpHide(Operator):
bl_idname = "noised.randomhide"
bl_label = "Hide Noised random"
bl_description = "Hide noised random"
bl_options = {'REGISTER', 'UNDO'}
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
bl_context = "scene"
def execute(self, context):
scn = context.scene
rnd = context.scene.render
nrs = scn.nrsettings
hideNRtransform()
return {'FINISHED'}
class NRClassOp(Operator):
bl_idname = "noised.random"
bl_label = "Noised random"
bl_description = "noised random description"
bl_options = {'REGISTER', 'UNDO'}
bl_space_type = 'PROPERTIES'
bl_region_type = 'WINDOW'
# bl_context = "object"
bl_context = "scene"
# @classmethod
# def poll(clss, context):
# return {'poll'}
def execute(self, context):
scn = context.scene
rnd = context.scene.render
nrs = scn.nrsettings
collName=nrs.NR_collName
if len(bpy.context.selected_objects)==0:
self.report({'WARNING'}, "Nothing selected")
else:
if nrs.NR_enabled==False:
#create collection
if bpy.data.collections.get(collName) is None:
nrColl = bpy.data.collections.new(collName)
bpy.context.scene.collection.children.link(nrColl)
#excude new collection from all view layer
vlayer_collection = scn.view_layers
for vlay in vlayer_collection:
vlay.layer_collection.children[collName].exclude=True
else:
nrColl = bpy.data.collections[collName]
nrs.NR_enabled=True
else:
nrColl = bpy.data.collections[collName]
#selected in collection
selection_names = bpy.context.selected_objects
for obsel in selection_names:
# print(nrColl.all_objects[i.name])
#check if is already in the collection
alreadyIn=False
for i in obsel.users_collection:
if i.name == collName:
alreadyIn=True
break
if alreadyIn==False:
nrColl.objects.link(obsel)
else:
print("already in")
setNRtransform()
return {'FINISHED'}
classes = (
NRSettings,
NOISED_PT_Random,
NRClassOp,
NRClassOpSel,
NRClassOpDel,
NRClassOpCopy,
NRClassOpCopyDel,
NRClassOpHide
)
def register():
from bpy.utils import register_class
for cls in classes:
register_class(cls)
bpy.types.Scene.nrsettings = PointerProperty(type=NRSettings)
bpy.app.handlers.frame_change_pre.append(nr_animation_update)
def unregister():
from bpy.utils import unregister_class
del bpy.types.Scene.nrsettings
for cls in classes:
unregister_class(cls)
bpy.app.handlers.frame_change_pre.remove(nr_animation_update)
if __name__ == "__main__":
register()