-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathspiderweb.pyp
1176 lines (1003 loc) · 41.2 KB
/
spiderweb.pyp
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
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Plugin name : SPIDERWEB_GENERATOR
Copyright:
Written for CINEMA 4D Rxx.xxx
Modified Date:
"""
import os
import sys
import c4d
from c4d import plugins, utils, bitmaps, gui
from c4d.utils import GeRayCollider
from c4d import utils
import random
# Be sure to use a unique ID obtained from www.plugincafe.com
# 1000001-1000010
PLUGIN_ID = 1039523
##
## @brief Class Root for spider web generator
##
class SpiderGenerator:
def __repr__(self):
return str(self)
def __init__(self, hub= None, subdiv = 8, seed = 123456,radii = 26):
if hub is None:
return
else:
self.hub = hub
self.hubMg = hub.GetMg()
self.hubPos = self.hubMg.off
self.subdiv = subdiv
self.seed = seed
self.radii = radii
random.seed(seed)
##
## @brief anchors can be a vector or a list of vectors. This function get the length of the vector or the first element in the list.
##
## @param self
## @param a the vector or the list to calculate the length from
##
## @return
##
def _getLengthVector(self,a):
if type(a[1]) is list:
return a[1][0].GetLength()
else:
return a[1].GetLength()
##
## @brief anchors can be vector or list of vector. This return the vector or the first element of the list if it's a list
##
## @param self
## @param v the vector or the list of vector
##
## @return
##
def _getVector(self, v):
if type(v) is list:
return v[0]
else:
return v
return None
##
## @brief compute points positions using _getPointPositionAt()
##
## @param self
## @param p1 point start position vector
## @param p2 point end position vector
## @param hub position of the hub
## @param pcnt number of point to compute
##
## @return return array of points
##
def _getPointsPosition(self,p1, p2 , hubPos, pcnt):
#compute points on spline moving toward the hub
points = []
for cpt in xrange(pcnt+1):
t = float(cpt) / float(pcnt)
points.append( self._getPointPositionAt(p1,p2,hubPos,t) )
return points
##
## @brief compute bezier curve 3 points
##
## @param self
## @param p1 point start of the curve
## @param p2 Point end of the curve
## @param hub position of the center of the spider web (were the bezier curve have to go)
## @param t position of the point in the curve 0.0 < t < 1.0
##
## @return return the point position
##
def _getPointPositionAt(self,p1,p2,hubPos,t):
#calcul la position d'un points sur une spline bézier
p1 +=hubPos
p2 +=hubPos
mp = (p2 + p1 ) * 0.5
direction = hubPos - mp
direction.Normalize()
direction *= (p2-p1).GetLength() * 0.2
mp += direction
#calcul des positions
m = pow(1-t,2) * p1 + 2*(1-t)*t*mp + pow(t,2)*p2
return m - hubPos
##
## @brief Main class for Threads of the spider web
##
class Thread (SpiderGenerator,object):
##
##
##
def __init__(self, **kwargs):
super (Thread,self).__init__(**kwargs)
##
## @brief main class to compute and display the web
##
class ComputeAndDisplay(SpiderGenerator,object):
def __init__(self, radius = 400, desiredAnchorsCnt = 3 , **kwargs):
self.radius = radius
self.desiredAnchorsCnt = desiredAnchorsCnt
super (ComputeAndDisplay,self).__init__(**kwargs)
##
## @brief SimpleThread is a thread composed by only two points, can be straight or curved.
##
class SimpleThread (Thread, object):
##
## @brief constructor
##
## @param self
## @param kwargs arguments to call the parent
##
def __init__(self, startPoint = None, endPoint = None, **kwargs):
self.startPoint = startPoint
self.endPoint = endPoint
self.points = []
super(SimpleThread, self).__init__(**kwargs)
##
## @brief get points
##
## @param self
##
## @return return list of points
##
def _getPoints(self):
if len(self.points) > 0:
return self.points
else:
return None
##
## @brief set point value
##
## @param self
## @param startPoint start point coordinate
## @param endPoint end point coordinate
##
## @return
##
def _setPoints(self,startPoint=None,endPoint=None):
if startPoint is not None:
self.startPoint = startPoint
if endPoint is not None:
self.endPoint = endPoint
##
## @brief compute intermediates points
##
## @param self
##
## @return
##
def _computePoints(self, type = c4d.SPLINETYPE_LINEAR):
if type == c4d.SPLINETYPE_LINEAR:
mp = (self.startPoint + self.endPoint) * 0.5
self.points.append(self.startPoint)
self.points.extend(self._getPointsPosition(self.startPoint,self.endPoint, mp, self.subdiv)[1:-1])
self.points.append(self.endPoint)
return True
elif type == c4d.SPLINETYPE_BEZIER:
self.points.append(self.startPoint)
self.points.extend(self._getPointsPosition(self.startPoint,self.endPoint, self.hubPos,self.subdiv)[1:-1])
self.points.append(self.endPoint)
return True
else:
return False
return True
##
## @brief multiple thread is composed by multiple point or anchor points. (frame Thread, spiral)
##
class MultipleThread(Thread,object):
def __init__(self,**kwargs):
super(MultipleThread, self).__init__(**kwargs)
##
## @brief Support Thread are in the corner of the spider web.
##
class SupportThread(SimpleThread,object):
##
## @brief Init clas and call parent
##
## @param self
## @param kwargs used to call parent and it's constructor
##
def __init__(self,**kwargs):
self.done = False
super(SupportThread, self).__init__(**kwargs)
##
## @brief print the value of start and end point (from parent class)
##
## @param self
##
## @return a string of start and end point coordinate and if this corner is done or not
##
def __str__(self):
return '\r\n' + str(self.startPoint) + ',' + str(self.endPoint) + ',' + str(self.done)
##
## @brief return the value of 'self.done'
##
## @param self
##
## @return true or false
##
def _isDone(self):
return self.done
##
## @brief set the support thread as done
##
## @param self
##
## @return
##
def _setDone(self):
self.done = True
##
## @brief class for RadialThread More a Type
##
class RadialThread(SimpleThread,object):
##
## @brief init radial thread
##
## @param self
## @param ray_dir ray direction
## @param length length of the ray
## @param kwargs call parent constructor
##
def __init__(self, ray_dir = None, length = None,**kwargs):
self.length = length
self.ray_dir = ray_dir
super(RadialThread, self).__init__(**kwargs)
##
## @brief return the length of the thread
##
## @param self
##
## @return the length of the thread
##
def _getLength(self):
return self.length
##
## @brief set the length of the thread
##
## @param self
## @param a length of the thread
##
## @return
##
def _setLength(self,a):
self.length = a
##
## @brief return the point position at t with 0 < t <= 1
##
## @param self
## @param t point position 0 < t <= 1
##
## @return { description_of_the_return_value }
##
def _getPoint(self,t):
return self.startPoint+ ((self.endPoint-self.startPoint) * t)
##
## @brief The frame of the spider web
##
class FrameThread(MultipleThread,object):
def __init__(self,**kwargs):
self.anchors = []
#call parent
super(FrameThread,self).__init__(**kwargs)
##
## @brief define anchors points and will calculate intermediate points at subdivision self.subdiv
##
## @param self
## @param points list of points
##
## @return True or False
##
def _setPoints(self, points):
if type(points) is list:
self.anchors = list(points)
self.points = []
self._computePoints()
else:
return False
return True
##
## @brief Compute Points and intermediates points for the frame thread
##
## @param self
##
## @return
##
def _computePoints(self):
for i, point in enumerate(self.anchors):
apoint = self._getVector(point)
npoint = self._getVector(self.anchors[(i+1) % len(self.anchors)])
self.points.append(apoint)
self.points.extend(self._getPointsPosition(apoint,npoint,self.hubPos,self.subdiv))
##
## @brief return the points list
##
## @param self
##
## @return the points list
##
def _getPoints(self):
if len(self.points)>0:
return self.points
else:
return None
return None
##
## @brief get the number of anchors
##
## @param self
##
## @return the size of the array
##
def _getAnchorCount(self):
return len(self.anchors)
##
## @brief return the number of composed anchors
##
## @param self
##
## @return return the size of the array of the array created with only composed anchors
##
def _getComposedAnchorCount(self):
return len([t for t in self.anchors if type(t) is list])
##
## @brief return only the anchors even if they are composed anchors.
##
## @param self
##
## @return return a list of vectors (anchors)
##
def _getAnchors(self):
return [self._getVector(t) for t in self.anchors]
##
## @brief return the composed anchors
##
## @param self
##
## @return return a list of composed anchors with the sub points.
##
def _getComposedAnchors(self):
return [t for t in self.anchors if type(t) is list]
def _getPointCount(self):
return len(self.points)
##
## @brief store the spiral part of the spiderweb
##
class SpiralThread(MultipleThread,object):
##
## @brief The spiral is made of multiple segments of spine with xx points.
## init arrays to store point and number of point per segments.
##
## @param self
## @param kwargs
##
def __init__(self,**kwargs):
self.points = []
self.segments = []
#call parent
super(SpiralThread,self).__init__(**kwargs)
##
## @brief does the magic
##
class ComputeSpiderWeb(ComputeAndDisplay, object):
##
## @brief Init values
##
## @param self
## @param rotCnt rotation number for the spiral thread
## @param amin a minimum value for spiral thread
## @param amax a maximum value for spiral thread
## @param bmin b minimum value for spiral thread
## @param bmax b maximum value for spiral thread
## @param startDistance start distance from center, if None it will be calculated
## @param kwargs other arguments for parent class
##
def __init__(self, rotCnt = 50 , amin=0.0, amax=2.0, bmin=0.0, bmax = 4.0, startDistance = None, lengthWeight = 1.0, rndHubWeight = 1.0,offsetExtSpiral=0,
createCS = False, csRotCnt = 5 ,csamax = 2.0 , csbmax = 4.0, csOffSet =0 , cslgthWei = 1.0 ,**kwargs):
super(ComputeSpiderWeb,self).__init__(**kwargs)
#self.hairTag = hairTag
self.objList = []
self.rcObjList = []
## new class
self.frameThread = FrameThread (hub = self.hub, subdiv = self.subdiv)
#self.supportThread = SupportThread(hub = self.hub, subdiv = self.subdiv, seed = self.seed)
self.supportThreads = []
self.radialThreads = []
self.spiralThreads = SpiralThread (hub = self.hub, subdiv = self.subdiv, seed= self.seed)
self.rotCnt = rotCnt
self.amin = amin
self.amax = amax
self.bmin = bmin
self.bmax =bmax
self.startDistance = startDistance
self.lengthWeight = lengthWeight
self.rndHubWeight = rndHubWeight #random Position for radial thread start.
self.offsetExtSpiral = offsetExtSpiral #offset the radial tread to start from
self.createCS = createCS #Create or not the spiral at center
if self.createCS:
self.centerSpiralThreads = SpiralThread (hub = self.hub, subdiv = self.subdiv, seed= self.seed)
self.csRotCnt = csRotCnt #center spiral rotation count
self.csamax = csamax
self.csbmax = csbmax
self.csOffSet = csOffSet
self.cslgthWei = cslgthWei
else:
self.centerSpiralThreads = None
##
## @brief create an array of object used to cast ray on to find anchors points
## Create also an array of GeRayCollider initialise with an object.
##
## @param self
## @param objectList array of object
##
## @return Tru or False if the array could be created
##
def _setObjectList(self, objectList):
if objectList is None:
return False
if len(objectList) == 0:
return False
self.objList = objectList
#creation des RayCollider
for obj in self.objList:
if obj is None:
return False
self.rcObjList.append(GeRayCollider())
self.rcObjList[-1].Init(obj)
return True
##
## @brief Create an array of radial direction with some randomness (1°-5°)
##
## @param self
## @param raycnt number of ray to create
##
## @return array of vectors
##
def _getRayDirection(self, raycnt):
#tableau de directions
#random.seed(self.seed)
directions = []
rotation = c4d.utils.Rad(360.0 / float(raycnt))
for cpt in xrange(raycnt):
rndangle = random.uniform(0.0,0.5)
sn, cs = c4d.utils.SinCos(rotation * cpt + c4d.utils.Rad(rndangle))
#matrice de rotation sur l'axe Z
rotMatrix = c4d.Matrix( c4d.Vector(0) ,c4d.Vector(cs,sn,0), c4d.Vector(-sn,cs,0) , c4d.Vector(0,0,1) )
direction = c4d.Vector(0,1,0) * rotMatrix
direction.Normalize()
directions.append( direction)
return directions
##
## @brief cast rays and subcast ray if the ray didn't find any object
##
## @param self
## @param ray_origin_matrix matrix of the object where the ray should be emmited (will be convert to object space)
## @param distance length of the ray
## @param raycnt number of ray to cast
## @param subcast true or false let know if we are subcasting or not
##
## @return array of points if anchors has been found.
##
def _castRay(self, ray_origin_matrix, distance, raycnt, subcast = False):
anchorPoints = []
for ray_dir in self._getRayDirection(raycnt):
for obj, rc in zip(self.objList, self.rcObjList):
anchorFound = False
objMg = obj.GetMg()
ray_origin = ray_origin_matrix.off * ~objMg
#convertion en coordonnées objet
obj_ray_dir = (ray_dir * ray_origin_matrix - ray_origin_matrix.off + objMg.off) * ~objMg
rc.Intersect(ray_origin , obj_ray_dir, distance)
if rc.GetIntersectionCount() > 0:
if not rc.GetNearestIntersection()["backface"]:
anchorPoints.append(rc.GetNearestIntersection()['hitpos'] * objMg * ~self.hub.GetMg())
anchorFound = True
if (not subcast) and not anchorFound:
new_rom = c4d.Matrix(ray_origin_matrix)
new_rom.off = ray_dir * distance * ray_origin_matrix
subCastTab = None
subCastTab = [new_rom.off * ~self.hub.GetMg() ]
result = self._castRay(new_rom, distance * 0.1,raycnt,True)
subCastTab.append(result)
if len(subCastTab[1]) > 0:
anchorPoints.append(subCastTab)
#print obj
return anchorPoints
##
## @brief allow to get the n farrest anchors points without changing the order of the array
##
## @param self
## @param tab array of anchors points
## @param n how many number should we keep
##
## @return
##
def _sortedValue(self, tab , n = 3):
#create array of index, value for every value
index = [(i,value) for i, value in enumerate(tab)]
#sorted the array with the length
tab_sorted = sorted(index, key = self._getLengthVector)
#keep only the n bigger
tab_sorted = tab_sorted[-n:]
#sort back with the index value (the values get back to their original order)
tab_sorted = sorted (tab_sorted)
#rebuild the array with only the value.
return [v[1] for v in tab_sorted]
def _getLongestDistance(self, originPoint = c4d.Vector(0)):
distToFrame = 0
ppos = c4d.Vector(0)
dtoP = 0
for j in xrange(self.frameThread._getPointCount()):
ppos = self.frameThread.points[j]
dtoP = (ppos - originPoint).GetLength()
if dtoP > distToFrame or distToFrame==0:
distToFrame = dtoP
moreLess = float(random.randint(0,3)) / 100.0
return distToFrame * (0.1 + moreLess)
##
## @brief Compute the spider web so we can display it
##
## @param self
##
## @return
##
def _resolveSpiderWeb(self, useRayCast):
#ancPoints and frame threads
#
anchorPoints = []
hubMg = self.hubMg
if useRayCast:
anchorPoints = self._castRay(self.hubMg, self.radius, 12) #default for anchors, 12 rays
if len(anchorPoints) > self.desiredAnchorsCnt:
#sorted the array and negligate the closer points
self.frameThread._setPoints( self._sortedValue(anchorPoints, self.desiredAnchorsCnt) )
else:
self.frameThread._setPoints(anchorPoints)
if self.frameThread._getAnchorCount() < 3:
return False
#support threads
gv = self._getVector
anc = self.frameThread._getAnchors()
ancnt = len(anc)
ft = [SupportThread(hub = self.hub, subdiv = self.subdiv, seed = self.seed) for i in range (len(anc))]
for i, thr in enumerate(ft):
if thr._isDone():
continue
d = gv(anc[i]).GetLength()
if d < self.radius * 0.1:
thr._setDone()
elif ( gv(anc[i]) - gv(anc[(i+1) % ancnt])).GetLength() < self.radius * 0.35:
#too short, jump to next anc
if (gv(anc[(i+1) % ancnt]) - gv(anc[(i+2) % ancnt])).GetLength() < 0:
#too short
continue
else:
#Set the link and mark as done, next one also is set as done
#end point is 80% with random of 5%
ft[i].startPoint = self._getPointPositionAt(
gv(anc[(i-1) % ancnt]),
gv( anc[i]),
self.hubPos,
random.uniform(0.75,0.85))
#point d'arrivé dans les 20% avec un petit random de 5%
ft[(i) % len(ft)].endPoint = self._getPointPositionAt( gv(anc[(i+1) % ancnt]),
gv(anc[(i+2) % ancnt]),
self.hubPos,
random.uniform(0.15,0.25))
thr._setDone()
ft[(i+1) % len(ft)]._setDone()
else:
ft[i].startPoint = self._getPointPositionAt( gv(anc[(i-1) % ancnt]),
gv( anc[i]),
self.hubPos,
random.uniform(0.75,0.85))
ft[(i) % len(ft)].endPoint = self._getPointPositionAt( gv(anc[(i) % ancnt]),
gv(anc[(i+1) % ancnt]),
self.hubPos,
random.uniform(0.15,0.25))
thr._setDone()
self.supportThreads = [t for t in ft if t.startPoint is not None]
for t in self.supportThreads:
t._computePoints(c4d.SPLINETYPE_BEZIER)
## create the radial thread
##
##
for i,ray_dir in enumerate(self._getRayDirection(self.radii)):
ray_end = c4d.Vector(0)
fnp = c4d.Vector(0) #frame nearest Point
sfnp = c4d.Vector(0) #support frame nearest Point
distToFrame = 0
cntsecurity = 0
ppos = c4d.Vector(0)
dtoP = 0
while ((distToFrame == 0) or (distToFrame > self._getLongestDistance()* 0.1 )) and (cntsecurity < 10):
ray_end += ray_dir * distToFrame
for j in xrange(self.frameThread._getPointCount()):
#ppos = self._getNearestPoint(self.frameThread, ray_end, 1.0 / 10.0)
ppos = self.frameThread.points[j]
dtoP = (ppos - ray_end).GetLength()
if dtoP < distToFrame or distToFrame==0:
distToFrame = dtoP
fnp = ppos#ray_end
for st in self.supportThreads:
for point in (st.points):
ppos = point
dtoP = (ppos - ray_end).GetLength()
if dtoP < distToFrame or distToFrame==0:
distToFrame = dtoP
fnp = ppos#ray_end
cntsecurity +=1
self.radialThreads.append ( RadialThread( hub = self.hub,
subdiv = self.subdiv,
seed = self.seed,
startPoint = c4d.Vector(0),
endPoint = fnp,
ray_dir = (fnp-c4d.Vector(0)).GetNormalized() ,
length = (fnp-c4d.Vector(0)).GetLength()
)
)
for i,ray in enumerate(self.radialThreads):
if i % 8 == 0:
#start from center
ray.startPoint = c4d.Vector(0)
else:
#start at random from start of random radial thread created
ray.startPoint = random.choice(self.radialThreads)._getPoint(random.uniform(0.0,self.rndHubWeight))
## create the spiral thread
##
##
distToFrame = 0
ray_end = 0
ppos = c4d.Vector(0)
dtoP = 0
if self.startDistance is not None:
dFromCenter = self.startDistance
else:
dFromCenter = self._getLongestDistance()
self.spiralThreads.points = []
self.spiralThreads.segments = []
#equation r = a + b * theta
a = dFromCenter
b = 1
precDir = c4d.Vector(0,1,0)
rays = self._getRayDirection(self.radii)
first = True
ptcnt = 0
lenRadialThreads = len(self.radialThreads)
for tour in xrange(self.rotCnt):
#for ray in self.radialThreads:
for i in xrange(lenRadialThreads):
ray = self.radialThreads[(i+self.offsetExtSpiral) % lenRadialThreads]
if first:
precDir = ray.ray_dir
first= False
theta = ray.ray_dir.Dot(precDir)
b = random.uniform(self.bmin,self.bmax) / 100.0 * (1-( (1-a / ray.length)*self.lengthWeight ) )
a += random.uniform(self.amin,self.amax) / 100.0
r = a + b * theta
if r <= ray.length:
self.spiralThreads.points.append(ray.ray_dir * r)
ptcnt +=1
else:
if ptcnt >0:
self.spiralThreads.segments.append(ptcnt)
ptcnt =0
precDir = ray.ray_dir
# create the center spiral
#
if self.createCS:
distToFrame = 0
ray_end = 0
ppos = c4d.Vector(0)
dtoP = 0
dFromCenter = 0.01
self.centerSpiralThreads.points = []
self.centerSpiralThreads.segments = []
#equation r = a + b * theta
a = dFromCenter
b = 1
precDir = c4d.Vector(0,1,0)
rays = self._getRayDirection(self.radii)
first = True
ptcnt = 0
lenRadialThreads = len(self.radialThreads)
for tour in xrange(self.csRotCnt):
#for ray in self.radialThreads:
for i in xrange(lenRadialThreads):
ray = self.radialThreads[(i+self.csOffSet) % lenRadialThreads]
if first:
precDir = ray.ray_dir
first= False
theta = ray.ray_dir.Dot(precDir)
b = random.uniform(self.bmin,self.csbmax) / 100.0 * (1-( (1-a / ray.length)*self.cslgthWei ) )
a += random.uniform(self.amin,self.csamax) / 100.0
r = a + b * theta
if r <= ray.length:
self.centerSpiralThreads.points.append(ray.ray_dir * r)
ptcnt +=1
else:
if ptcnt >0:
self.centerSpiralThreads.segments.append(ptcnt)
ptcnt =0
precDir = ray.ray_dir
class DisplaySpiderWeb(ComputeAndDisplay,object):
def __init__(self,swg = None, subdiv = 8,**kwargs):
if swg is None:
self.swg= None
return False
else:
self.swg = swg
self.subdiv = subdiv
super(DisplaySpiderWeb,self).__init__(**kwargs)
def _getSplines(self):
container = c4d.BaseObject(c4d.Onull)
swg = self.swg
#main frame
framePoints = swg.frameThread._getPoints()
frameSpline = c4d.SplineObject(len(framePoints),c4d.SPLINETYPE_LINEAR)
#create points
for i,point in enumerate(framePoints):
frameSpline.SetPoint(i,point)
frameSpline[c4d.SPLINEOBJECT_CLOSED] = True
frameSpline.SetName('Frame Thread')
frameSpline.InsertUnder(container)
frameSpline.Message(c4d.MSG_UPDATE)
#composed Points
composedPoints = []
for i in swg.frameThread._getComposedAnchors():
composedPoints.extend([ [i[0], t] for t in i[1]])
composedSpline = c4d.SplineObject(len(composedPoints)*2,c4d.SPLINETYPE_LINEAR)
composedSpline.ResizeObject(len(composedPoints)*2,len(composedPoints))
for i,comPoint in enumerate(composedPoints):
composedSpline.SetSegment(i,2,False)
composedSpline.SetPoint(i+i,comPoint[0])
composedSpline.SetPoint(i+i+1,comPoint[1])
composedSpline.SetName('Composed anchor Points')
composedSpline.InsertUnder(container)
composedSpline.Message(c4d.MSG_UPDATE)
#create spline for support threads
#
pcnt = len(swg.supportThreads) * (swg.subdiv + 1 )
supportThreadsSpline = c4d.SplineObject(pcnt,c4d.SPLINETYPE_LINEAR )
supportThreadsSpline.ResizeObject(pcnt, len(swg.supportThreads))
allPoints = []
for i,spt in enumerate(swg.supportThreads):
supportThreadsSpline.SetSegment(i, swg.subdiv + 1, False) # x subdivision donne x-1 points
allPoints.extend(spt._getPoints())
for i, point in enumerate(allPoints):
supportThreadsSpline.SetPoint(i,point)
supportThreadsSpline.SetName('support Thread')
supportThreadsSpline.InsertUnder(container)
supportThreadsSpline.Message(c4d.MSG_UPDATE)
#create radial spline
#
radialSpline = c4d.SplineObject(0,c4d.SPLINETYPE_LINEAR)
radialSpline.ResizeObject(len(swg.radialThreads) * 2,len(swg.radialThreads))
for i,radthread in enumerate(swg.radialThreads):
radialSpline.SetSegment(i,2, False)
radialSpline.SetPoint(i+i,radthread.startPoint)
radialSpline.SetPoint(i+i+1,radthread.endPoint)
radialSpline.SetName('Radial Threads')
radialSpline.InsertUnder(container)
radialSpline.Message(c4d.MSG_UPDATE)
## spiral thread
##
##
spiralThreads = c4d.SplineObject(len(swg.spiralThreads.points),c4d.SPLINETYPE_LINEAR)
spiralThreads.ResizeObject(len(swg.spiralThreads.points),len(swg.spiralThreads.segments))
for i,poinInSegment in enumerate(swg.spiralThreads.segments):
spiralThreads.SetSegment(i,poinInSegment,False)
for i,p in enumerate(swg.spiralThreads.points):
spiralThreads.SetPoint(i,p)
spiralThreads.SetName('Spiral ')
spiralThreads.InsertUnder(container)
spiralThreads.Message(c4d.MSG_UPDATE)
#return the container
container.SetName('Splines')
## display center spiral Threads
if swg.createCS:
centerSpiralThreads = c4d.SplineObject(len(swg.centerSpiralThreads.points),c4d.SPLINETYPE_LINEAR)
centerSpiralThreads.ResizeObject(len(swg.centerSpiralThreads.points),len(swg.centerSpiralThreads.segments))
for i,poinInSegment in enumerate(swg.centerSpiralThreads.segments):
centerSpiralThreads.SetSegment(i,poinInSegment,False)
for i,p in enumerate(swg.centerSpiralThreads.points):
centerSpiralThreads.SetPoint(i,p)
centerSpiralThreads.SetName('Spiral ')
centerSpiralThreads.InsertUnder(container)
centerSpiralThreads.Message(c4d.MSG_UPDATE)
return container
class SPIDERWEB_GENERATOR(plugins.ObjectData):
"""docstring for SPIDERWEB_GENERATOR"""
HANDLECOUNT = 1
def Init(self, op):
self.InitAttr(op, float, [c4d.RADIUS_WEB])
self.InitAttr(op, int, [c4d.ANCHOR_POINTS_CNT])
self.InitAttr(op, bool, [c4d.USE_RAYCAST])
self.InitAttr(op, int, [c4d.SUBDIVISION])
self.InitAttr(op, int, [c4d.SEED_ID])
self.InitAttr(op, int, [c4d.RADII_CNT])
self.InitAttr(op, float, [c4d.RND_CENTER_DISTANCE])
self.InitAttr(op, float, [c4d.START_DISTANCE])
self.InitAttr(op, int, [c4d.OS_REVOLUTION_CNT])
self.InitAttr(op, int, [c4d.OS_OFFSET_START])
self.InitAttr(op, float, [c4d.OS_SPACE])
self.InitAttr(op, float, [c4d.OS_TURBULENCE])
self.InitAttr(op, float, [c4d.OS_LENGTH_WEIGHT])
self.InitAttr(op, bool, [c4d.CREATE_CENTRAL_SPIRAL])
self.InitAttr(op, int, [c4d.CS_REVOLUTION_CNT])
self.InitAttr(op, int, [c4d.CS_OFFSET_START])
self.InitAttr(op, float, [c4d.CS_SPACE])
self.InitAttr(op, float, [c4d.CS_TURBULENCE])
self.InitAttr(op, float, [c4d.CS_LENGTH_WEIGHT])
self.InitAttr(op, bool, [c4d.DRAWPHERE])
self.InitAttr(op, bool, [c4d.DRAWPHERE])
op[c4d.RADIUS_WEB] = 20
op[c4d.ANCHOR_POINTS_CNT] = 5
op[c4d.USE_RAYCAST] = False
op[c4d.SUBDIVISION] = 8
op[c4d.SEED_ID] = 123456
op[c4d.RADII_CNT] = 26
op[c4d.RND_CENTER_DISTANCE] = 0.01
op[c4d.START_DISTANCE] = 3
op[c4d.OS_REVOLUTION_CNT] = 27
op[c4d.OS_OFFSET_START] = 0
op[c4d.OS_SPACE] = 4.0
op[c4d.OS_TURBULENCE] = 40
op[c4d.OS_LENGTH_WEIGHT] = 1.0
op[c4d.CREATE_CENTRAL_SPIRAL] = False
op[c4d.CS_REVOLUTION_CNT] = 5
op[c4d.CS_OFFSET_START] = 4
op[c4d.CS_SPACE] = 4.0
op[c4d.CS_TURBULENCE] = 40
op[c4d.CS_LENGTH_WEIGHT] = 1.0
op[c4d.DRAWPHERE] = True
op[c4d.DRAWPLANE] = False
return True
def GetHandleCount(self,op):
#Called to get the number of handles the object has
return self.HANDLECOUNT
def GetHandle(self, op, i, info):
#Called to get the information for handle
if i == 0:
info.position = c4d.Vector(op[c4d.RADIUS_WEB], 0.0, 0.0)
info.direction = c4d.Vector(1.0, 0.0, 0.0)
info.type = c4d.HANDLECONSTRAINTTYPE_LINEAR
if i == 1:
info.position = c4d.Vector(0.0, op[c4d.OS_SPACE], 0.0)
info.direction = c4d.Vector(0.0, 1.0, 0.0)
info.type = c4d.HANDLECONSTRAINTTYPE_LINEAR
def SetHandle(self, op, i, p, info):
#called to update UI
data = op.GetDataInstance()
if data is None: return
tmp = c4d.HandleInfo()
self.GetHandle(op, i, tmp)