-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathhostHelper.py
5021 lines (4225 loc) · 165 KB
/
hostHelper.py
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
# -*- coding: utf-8 -*-
"""
Copyright (C) <2010> Autin L. TSRI
This file git_upy/hostHelper.py is part of upy.
upy is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
upy is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with upy. If not, see <http://www.gnu.org/licenses/gpl-3.0.html>.
"""
import sys
import os
import math
import random
from math import *
usenumpy = False
try :
import numpy
import numpy.oldnumeric as Numeric
usenumpy = True
except :
usenumpy = False
usePIL = False
try :
import Image
usePIL = True
except:
usePIL = False
from upy import colors
if sys.version_info >= (3,0,0):
unicode = str
def vdistance(c0,c1):
"""get the distance between two points c0 and c1"""
d = numpy.array(c1) - numpy.array(c0)
s = numpy.sum(d*d)
return math.sqrt(s)
def vdiff(p1, p2):
# returns p1 - p2
x1,y1,z1 = p1
x2,y2,z2 = p2
return (x1-x2, y1-y2, z1-z2)
def vcross(v1,v2):
x1,y1,z1 = v1
x2,y2,z2 = v2
return (y1*z2-y2*z1, z1*x2-z2*x1, x1*y2-x2*y1)
def dot(v1,v2):
x1,y1,z1 = v1
x2,y2,z2 = v2
return ( x1 * x2 ) + ( y1 * y2 ) + ( z1 * z2 )
from math import sqrt
def vnorm(v1):
x1,y1,z1 = v1
n1 = 1./sqrt(x1*x1 + y1*y1 + z1*z1)
return (x1*n1, y1*n1, z1*n1)
def vlen(v1):
x1,y1,z1 = v1
return sqrt(x1*x1 + y1*y1 + z1*z1)
class Helper:
"""
The Helper abstract Object
==========================
This is the main class from which all helper derived. The Helper
give access to the basic function need for create and edit object
in the host.
Most of the function define at this loevel are overwrite by the class child.
matrix and transformation came from http://www.lfd.uci.edu/~gohlke/code/transformations.py.html
>>> import upy
>>> hClass = upy.getHelperClass()
>>> helper = helper.hClass()
See examples in upy/examples
"""
_usenumpy = usenumpy
_MGLTools = False
_usePIL = usePIL
BONES = None
IK = None
CAM_OPTIONS = {"ortho" :"ortho","persp" : "persp" }#define the type of camera
LIGHT_OPTIONS = {"Area":"AREA","Sun":"SUN","Spot":"SPOT"}#define the type of light
dupliVert = False
def __init__(self,):
try :
import DejaVu
_MGLTools = True
except :
_MGLTools = False
# self.createColorsMat()
self.noise_type ={
"boxNoise":None,
"buya":None,
"cellNoise":None,
"cellVoronoi":None,
"cranal":None,
"dents":None,
"displacedTurbulence":None,
"electrico":None,
"fbm":None,
"fire":None,
"gas":None,
"hama":None,
"luka":None,
"modNoie":None,
"naki":None,
"noise":None,
"none":None,
"nutous":None,
"ober":None,
"pezo":None,
"poxo":None,
"sema":None,
"sparseConvolution":None,
"stupl":None,
"turbulence":None,
"vlNoise":None,
"voronoi1":None,
"voronoi2":None,
"voronoi3":None,
"wavyTurbulence":None,
"zada":None,
}
self.usenumpy = self._usenumpy
self.nogui = False
self.instance_dupliFace = False
self.quad={"+Z" :[[-1,1,0],[1,1,0],[1,-1,0], [-1,-1,0]],#XY
"+Y" :[[-1,0,1],[1,0,1],[1,0,-1], [-1,0,-1]],#XZ
"+X" :[[0,-1,1],[0,1,1],[0,1,-1], [0,-1,-1]],#YZ
"-Z" :[[-1,1,0],[1,1,0],[1,-1,0], [-1,-1,0]],#XY
"-Y" :[[-1,0,1],[1,0,1],[1,0,-1], [-1,0,-1]],#XZ
"-X" :[[0,-1,1],[0,1,1],[0,1,-1], [0,-1,-1]],#YZ
}
self.axis_dic = {"+X":[1.,0.,0.],"-X":[-1.,0.,0.],"+Y":[0.,1.,0.],"-Y":[0.,-1.,0.],
"+Z":[0.,0.,1.],"-Z":[0.,0.,-1.]}
#==============================================================================
# some helper for treading and asynchrone stuff
#==============================================================================
def testForEscape(self,):
"""
return True if ESC is press
"""
return False
#==============================================================================
# mathutils
#==============================================================================
def norm(self,a ,b,c):
"""
return the norm of the vector [a,b,c]
>>> result = helper.norm(a,b,c) #a,b,c being double
@type a: float
@param a: first value of the vector
@type b: float
@param b: second value of the vector
@type c: float
@param c: thid value of the vector
@rtype: float
@return: the norm of the vector
"""
return (math.sqrt( a*a + b*b + c*c))
def normalize(self,A):
"""
return the normalized vector A [x,y,z]
>>> a = [1.0,3.0,5.0]
>>> a_normalized = helper.normalize(a)
@type A: vector
@param A: the 3d vector
@rtype: vector
@return: the normalized 3d vecor
"""
norm = self.norm(A[0],A[1],A[2])
if (norm ==0.0) : return A
else :return [A[0]/norm,A[1]/norm,A[2]/norm]
def measure_distance(self,c0,c1,vec=False):
""" measure distance between 2 point specify by x,y,z
c0,c1 should be Numeric.array
>>> a = [1.0,3.0,5.0]
>>> b = [5.0,1.0,2.0]
>>> distance = helper.measure_distance(a,b)
>>> distance, vector_a_to_b = helper.measure_distance(a,b)
@type c0: vector
@param c0: the first 3d vector
@type c1: vector
@param c1: the second 3d vector
@type vec: Boolean
@param vec: if the function return the vector c1-c0
@rtype: float ? vector
@return: the distance, and optionly the distance vetor
"""
if usenumpy :
d = numpy.array(c1) - numpy.array(c0)
s = numpy.sum(d*d)
else :
s=0.
d=[0.,0.,0.]
for i in range(3):
d[i] = c1[i] - c0[i]
s += d[i]^2
if vec :
return d,math.sqrt(s)
else :
return math.sqrt(s)
#direction, angle authorized
def advance_randpoint_onsphere(self,radius,marge=pi,vector=None):
#radius r, inclination θ, azimuth φ
r=radius
azimuth=random.uniform(-1,1) * ( marge * 2.0 )
inclination=random.uniform(-1,1) * ( marge )
x=r*sin(inclination)*cos(azimuth)
y=r*sin(inclination)*sin(azimuth)
z=r*cos(inclination)
pos = [x,y,z]
if vector is not None :
absolute_vector=numpy.array([0,0,radius])
matrice = self.rotVectToVect(absolute_vector,vector)
pos = self.ApplyMatrix([pos,],matrice)[0]
return pos
def randpoint_onsphere(self,radius,biased=None):
"""
Generate a random point on the outside of a sphere.
>>> r = 2.0
>>> bias = 2.0
>>> point = helper.randpoint_onsphere(r)
>>> point2 = helper.randpoint_onsphere(r,bias)
@type radius: float
@param radius: the radius of the sphere
@type biased: float
@param biased: optional float vale to use instead of the random function
@rtype: vector
@return: a random 3d point on the sphere of the given radius
-points (x,y,z) so that (x-a)^2 +(y-b)^2 + (z-c)^2 = R^2
-To generate a random point on the sphere, it is necessary only
to generate two random numbers, z between -R and R, phi between
0 and 2 pi, each with a uniform distribution.
To find the latitude (theta) of this point, note that z=R*sin(theta),
so theta=sin-1(z/R); its longitude is (surprise!) phi.
In rectilinear coordinates,
tetha = asin-1(z/R)
x=R*cos(theta)*cos(phi),
y=R*cos(theta)*sin(phi),
z=R*sin(theta)= (surprise!) z.
-hemispher
theta (0 <= theta < 360) and phi (0 <= phi <= pi/2)
x = cos(sqrt(phi)) cos(theta)
y = cos(sqrt(phi)) sin(theta)
z = sin(sqrt(phi))
A whole sphere is obtained by simply randomising the sign of z.
-Azimuth axis is X axis. The elevation angle is measured as the angle
between the Z-axis pointing upwards and the radius vector.
From elementary spherical geometry:
X coordinate=r*cos(pi/2-el)*cos(az)
Y coordinate=r*cos(pi/2-el)*sin(az)
Z Coordinate=r*sin(pi/2-el)
"""
if biased is not None:
theta = biased * ( 2 * pi )
u = biased * 2 - 1 #represent sin(phi)
else :
theta = random.uniform(0.,1.0)* ( 2 * pi )
u = random.uniform(0.,1.0) * 2 - 1
# print ("u ",u," theta ",theta)
# print ("radius ",radius)
# print ("sqrt ",( 1 - u**2)," sqrt ",sqrt( 1 - u**2))
# print ("cos ",cos(theta))
x = radius * sqrt( 1 - u**2) * cos(theta)
y = radius * sqrt( 1 - u**2) * sin(theta)
z = radius * u
return [x,y,z]
def transposeMatrix(self,matrice):
if matrice is not None :
matrice = numpy.array(matrice)
if isinstance(matrice,numpy.ndarray) :
mat = matrice.transpose().tolist()
return mat
else :
return matrice# = mat#numpy.array(matrice)
# blender_mat=mathutils.Matrix(mat)#from Blender.Mathutils
# blender_mat.transpose()
# return blender_mat
return matrice
def rotatePoint(self,pt,m,ax):
"""
Rotate the point pt [x,y,z] around axe ax[0],ax[1],ax[2] by ax[3] radians,
and translate by m [x,y,z].
>>> point = [1.0,2.0,0.0]
>>> trans = [5.0,0.0,0.0]
>>> axes = [1.0,0.0,0.0,math.pi]
>>> point = helper.rotatePoint(point,trans,axes)
>>> print point
[6.0, -2.0, 2.4492935982947064e-16] #[6.0, -2.0, 0.0]
@type pt: 3d vector
@param pt: the 3d point to be rotated
@type m: 3d vector
@param m: translation to apply after rotation
@type ax: 4d vector
@param ax: axe of rotation ax[0],ax[1],ax[2] and angle ax[3] radians
@rtype: 3d vector
@return: the transformed point
"""
x=pt[0]
y=pt[1]
z=pt[2]
u=ax[0]
v=ax[1]
w=ax[2]
ux=u*x
uy=u*y
uz=u*z
vx=v*x
vy=v*y
vz=v*z
wx=w*x
wy=w*y
wz=w*z
sa=sin(ax[3])
ca=cos(ax[3])
pt[0]=(u*(ux+vy+wz)+(x*(v*v+w*w)-u*(vy+wz))*ca+(-wy+vz)*sa)+ m[0]
pt[1]=(v*(ux+vy+wz)+(y*(u*u+w*w)-v*(ux+wz))*ca+(wx-uz)*sa)+ m[1]
pt[2]=(w*(ux+vy+wz)+(z*(u*u+v*v)-w*(ux+vy))*ca+(-vx+uy)*sa)+ m[2]
return pt
def eulerToMatrix(self,euler): #double heading, double attitude, double bank
"""
Code from 'http://www.euclideanspace.com/maths/geometry/rotations/conversions/'.
This conversion uses NASA standard aeroplane conventions as described on page:
'http://www.euclideanspace.com/maths/geometry/rotations/euler/index.htm'
Coordinate System: right hand
Positive angle: right hand
Order of euler angles: heading first, then attitude, then bank
matrix row column ordering:
[m00 m01 m02]
[m10 m11 m12]
[m20 m21 m22]
>>> euler = [0.8,3.14,2.0]#radians
>>> emat = helper.eulerToMatrix(euler)
>>> print emat
[[-0.69670582573323303, 0.65275180908484898, -0.29751650059422086, 0.0],
[0.0015926529164868282, 0.41614630875957009, 0.90929627358879683, 0.0],
[0.71735518109654839, 0.6330381706044601, -0.29097116474265428, 0.0],
[0.0, 0.0, 0.0, 1.0]]
@type euler: 3d array
@param euler: the euler angle to convert in matrice
@rtype: 4x4array
@return: the matrix computed from the euler angle
"""
# Assuming the angles are in radians.
heading=euler[0]
attitude=euler[1]
bank=euler[2]
m=[[ 1., 0., 0., 0.],
[ 0., 1., 0., 0.],
[ 0., 0., 1., 0.],
[ 0., 0., 0., 1.]]
ch = math.cos(heading)
sh = math.sin(heading)
ca = math.cos(attitude)
sa = math.sin(attitude)
cb = math.cos(bank)
sb = math.sin(bank)
m[0][0] = ch * ca
m[0][1] = sh*sb - ch*sa*cb
m[0][2] = ch*sa*sb + sh*cb
m[1][0] = sa
m[1][1] = ca*cb
m[1][2] = -ca*sb
m[2][0] = -sh*ca
m[2][1] = sh*sa*cb + ch*sb
m[2][2] = -sh*sa*sb + ch*cb
return m
def getTubeProperties(self,coord1,coord2):
"""
From two point return the length, and the orientation from one to another.
This function is used to build a cylinder from two points (see oneCylinder function)
>>> coord1 = [1.0,0.0,0.0]
>>> coord2 = [2.0,0.0,0.0]
>>> distance,rsz,rz,coord = helper.getTubeProperties(coord1,coord2)
>>> helper.setTransformation(obj,trans=coord,scale=[1., 1., distance],
rot=[0.,rz,rsz])
@type coord1: vector
@param coord1: first point
@type coord2: vector
@param coord2: second point
@rtype: tupple
@return: length, orientation (rotation XY,Z), and intermediate point OR
length and matrix of transformation (see getTubePropertiesMatrix that use numpy)
"""
x1 = float(coord1[0])
y1 = float(coord1[1])
z1 = float(coord1[2])
x2 = float(coord2[0])
y2 = float(coord2[1])
z2 = float(coord2[2])
laenge = math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)+(z1-z2)*(z1-z2))
wsz = atan2((y1-y2), (x1-x2))
wz = acos((z1-z2)/laenge)
return laenge,wsz,wz,[float(x1+x2)/2,(y1+y2)/2,(z1+z2)/2]
def isSphere(self, obj):
v = self.getMeshVertices(obj)
dmd = 10
r=10.0
if usenumpy:
alld = numpy.linalg.norm(v,axis=1)
#dmd = numpy.average(alld - numpy.average(alld))
r = alld.max()
dmd = r - numpy.average(alld)
else :
alld = [self.norm(ve[0],ve[1],ve[2]) for ve in v]
dm = sum(alld)/float(len(alld))
alldm = [ad - dm for ad in alld]
r = max(alld)
dmd = r - sum(alldm)/float(len(alldm))
if dmd < 0.1 :
obj["radius"] = r
return True
else :
return False
def update(self,):
"""
Update the host viewport, ui or gl draw
This function can't be call in a thread.
* overwrited by children class for each host
"""
pass
def fit_view3D(self):
"""
Function that should recenter the viewport to the object in the scene.
* overwrited by children class for each host
"""
pass
def checkName(self,name):
"""
Check the provide name to avoid invalid caracter for the
host. ie maya didnt support object name starting with number, and automatically rename the object.
In order to retrieve the object use this functon. If a invalid
caracter is found, the caracter is removed.
This function can be change in the features, as it currently only look for number.
>>> name = "1sphere"
>>> sphere_obj,sphere_mesh = helper.Sphere(name)
>>> print (sphere_obj,sphere_mesh)#in maya
(u'sphere', u'makeNurbSphere1')
>>> corrected_name = helper.checkName(name)
>>> print (corrected_name)
sphere
>>> sphere = helper.getObject(name)
>>> print (sphere)
sphere
@type name: string
@param name: name of the molecule.
@rtype: string
@return: corrected name of the molecule.
"""
invalid=[]
for i in range(9):
invalid.append(str(i))
if name[0] in invalid:
name= name[1:]
return name
def getObject(self,name):
"""
Retrieve an object from his name.
* overwrited by children class for each host
>>> oname = "mysphere"
>>> object= helper.getObject(oname)
>>> print oname,object#the result depnds on the host
mysphere <c4d.BaseObject object at 0x1e4fc4b0> # Cinema4D
mysphere # Maya
@type name: string
@param name: request name of an host object
@rtype: hostObject
@return: the object with the requested name or None
"""
return None
def getObjectName(self,o):
"""
Return the name of an host object.
* overwrited by children class for each host
>>> obj = helper.Sphere("mySphere")
>>> name = helper.getObjectName(obj)
>>> print (name)
mySphere
@type o: hostObject
@param o: an host object
@rtype: string
@return: the name of the host object
"""
pass
@classmethod
def getCurrentScene(self,):
"""
Return the current/active working document or scene.
* overwrited by children class for each host
>>> sc = helper.getCurrentScene()
>>> print (sc)
None #in maya there is no scene concept
<bpy_strct, Scene("Scene") #blender 2.6
[Scene "Scene"] #blender 2.49b
<c4d.documents.BaseDocument object at 0x246c01a0> #Cinema4D
@rtype: scene
@return: the active scene
"""
pass
@classmethod
def getCurrentSceneName(self):
"""
Return the current/active working document or scene name.
* overwrited by children class for each host
>>> scname = helper.getCurrentSceneName()
>>> print (scname)
None #maya
Scene #blender 2.6
Scene #blender 2.49b
Untitled #Cinema4D
@rtype: strng
@return: the active scene name
"""
pass
def getCurrentSelection(self,):
"""
Return the current/active selected object in the document or scene.
* overwrited by children class for each host
>>> liste_objects = helper.getCurrentSelection()
>>> print (liste_objects)
[<c4d.BaseObject object at 0x1e4fd3a0>, <c4d.BaseObject object at 0x1e4fd3d0>] #cinema4D
@rtype: liste
@return: the list of selected object
"""
pass
def setCurrentSelection(self,obj):
"""
Return the current/active selected object in the document or scene.
* overwrited by children class for each host
>>> liste_objects = [helper.getObject("obj1"),helper.getObject("obj2")]
>>> helper.setCurrentSelection(liste_objects)
@type obj: hostObject
@param obj: the object to be selected
"""
pass
def getPosUntilRoot(self,object):
"""
Go through the hierarchy of the object until reaching the top level,
increment the position to get the transformation due to parents.
DEPRECATED
@type object: hostObject
@param object: the object
@rtype: list
@return: the cumulative translation along the parenting hierarchy
"""
stop = False
#get the first parent
pos=[0,0,0]
while not stop :
#get the parent position, and add it to pos
#get the parent of the previous parent
parent=None
if parent is None :
stop = True
return pos
def addObjectToScene(self,doc,object,parent=None,centerRoot=True,rePos=None):
"""
Insert/add an object to the current document under the specified parent, and
at the specified location. This function is used by all the basic object creation function.
* overwrited by children class for each host
@type doc: hostScene
@param doc: the scene where to insert the object
@type object: hostObject
@param object: the object to insert
@type parent: hostObject
@param parent: the parent of the object to insert under
@type centerRoot: boolean
@param centerRoot: if the object have to be recentered according the top-level
@type rePos: list
@param rePos: the location of the object in the scene
"""
#get the object name
name=""
#if the object is not already in the scene
if self.getObject(name) == None:
if parent != None :
if type(parent) == str : parent = self.getObject(parent)
#if parent exist, insert the object under it
pass
if centerRoot :
#get the current position of the object
currentPos = []
if rePos != None :
parentPos = rePos
else :
parentPos = self.getPosUntilRoot(obj)#parent.GetPos()
#set the new position of the object
pass
else :
#insert the object
pass
def AddObject(self,object,parent=None,centerRoot=True,rePos=None):
"""
Insert/add an object to the current document under the specified parent, and
at the specified location. This function is an alias for addObjectToScene to
permit to some script to work either in dejavu and the host.
* overwrited by children class for each host
@type object: hostObject
@param object: the object to insert
@type parent: hostObject
@param parent: the parent of the object to insert under
@type centerRoot: boolean
@param centerRoot: if the object have to be recentered according the top-level
@type rePos: list
@param rePos: the location of the object in the scene
"""
doc = self.getCurrentScene()
self.addObjectToScene(doc,object,parent=parent,centerRoot=centerRoot,
rePos=rePos)
def ObjectsSelection(self,listeObjects,typeSel="new"):
"""
Modify the current object selection. Redundant with setCurrentSelection.
This function make the distinction between adding (typeSel="add") object to the selection and creating
a new selection (typeSel="new")
* overwrited by children class for each host
@type listeObjects: list
@param listeObjects: list of object to joins
@type typeSel: string
@param listeObjects: type of modification: new,add,...
"""
# dic={"add":c4d.SELECTION_ADD,"new":c4d.SELECTION_NEW}
sc = self.getCurrentScene()
#Put here the code to add/set an object to the current slection
#[sc.SetSelection(x,dic[typeSel]) for x in listeObjects]
def JoinsObjects(self,listeObjects):
"""
Merge the given liste of object in one unique geometry.
* overwrited by children class for each host
@type listeObjects: list
@param listeObjects: list of object to joins
"""
sc = self.getCurrentScene()
def addCameraToScene(self,name,Type,focal,center,scene,**kw):
"""
Add a camera object to the scene
* overwrited by children class for each host
>>> sc = helper.getCurrentScene()
>>> center=[0.,-12.,40.]
>>> cam = helper.addCameraToScene("cam1","persp",30.,center,sc)
@type name: string
@param name: name of the camera
@type Type: cameraType
@param Type: perspective, orthogonale etc...
@type focal: float
@param focal: the focal of the camera
@type center: list
@param center: the position of the camera
@type scene: host scene
@param scene: the scene
#we add a **kw for futur arguments
"""
pass
# cam = None
# self.addObjectToScene(scene,cam)
def addLampToScene(self,name,Type='Area',rgb=[1.,1.,1.],dist=25.0,energy=1.0,
soft=1.0,shadow=False,center=[0.,0.,0.],sc=None,**kw):
"""
Add a light to the scene
* overwrited by children class for each host
>>> sc = helper.getCurrentScene()
>>> center=[0.,-12.,40.]
>>> color = [1.,1.,1.]
>>> light = helper.addLampToScene("light1","Sun",color,20.,1.0,1.0,True,center,sc)
@type name: string
@param name: name of the instance
@type Type: light hostType/int etc..
@param Type: the light type : spot,sun,omni,etc..
@type rgb: list of int 0-255
@param rgb: color of the light in rgb
@type dist: float
@param dist: light distance of attenuation
@type energy: float
@param energy: intensity of the light
@type soft: bool
@param soft: soft light
@type shadow: boolean
@param shadow: does the light produce shadow
@type scene: host scene
@param scene: the scene
#we add a **kw for futur arguments
"""
dicType={'Area':0,'Sun':3}
lamp = None#c4d.BaseObject(LIGHT)
#lamp name (name)
#lamp position (center)
#lamp color (float(rgb[0]), float(rgb[1]), float(rgb[2]))#color
#lamp energy float(energy) #intensity
#lamp type dicType[Type] #type
if shadow :
#lampe shadow
pass
self.addObjectToScene(scene,lamp,centerRoot=False)
def newEmpty(self,name,location=None,parentCenter=None,**kw):
"""
Create a new Null/Empty Object
* overwrited by children class for each host
>>> empty = helper.newEmpty("null1",location=[10.0,0.0,0.0])
>>> empty_child = helper.newEmpty("null2",location=[15.0,0.0,0.0],parent = empty)
@type name: string
@param name: name of the empty
@type location: list
@param location: position of the null object
@type parentCenter: list
@param parentCenter: position of the parent object DEPRECATED
@type kw: dictionary
@param kw: you can add your own keyword, but it should be interpreted by all host
-"parent"
@rtype: hostObject
@return: the null object
"""
empty=None#
if location != None :
if parentCenter != None :
location = location - parentCenter
#set the position of the object to location
return empty
def newInstance(self,name,object,location=None,hostmatrice=None,matrice=None,**kw):
"""
Create a new Instance from another Object
* overwrited by children class for each host
>>> sph = helper.Sphere("sph1")
>>> instance_sph = helper.newInstance("isph1",sph,location = [10.0,0.0,0.0])
@type name: string
@param name: name of the instance
@type object: hostObject
@param object: the object to inherit from
@type location: list/Vector
@param location: position of the null object
@type hostmatrice: list/Matrix
@param hostmatrice: transformation matrix in host format
@type matrice: list/Matrix
@param matrice: transformation matrix in epmv/numpy format
@type kw: dictionary
@param kw: you can add your own keyword, but it should be interpreted by all host
-"parent"
-"material"
@rtype: hostObject
@return: the instance object
"""
#instance = None#
#instance parent = object
#instance name = name
if location != None :
#set the position of instance with location
pass
#set the instance matrice
self.setObjectMatrix(object,matrice=matrice,hostmatrice=hostmatrice)
return None
def getMasterInstance(self,instance,**kw):
"""
Return the object use for the instanciation
"""
return instance
def updateMasterInstance(self,instance, objects,add=True,hide=True,**kw):
"""
Update the reference of the passed instance by adding/removing-hiding objects
* overwrited by children class for each host
>>> sph = helper.Sphere("sph1")
>>> instance_sph = helper.newInstance("isph1",sph,location = [10.0,0.0,0.0])
@type instance: string/hostObj
@param instance: name of the instance
@type objects: list hostObject/string
@param objects: the list of object to remove/add to the instance reference
@type add: bool
@param add: if True add the objec else remove
@type hide: bool
@param hide: hide instead of remove
@type kw: dictionary
@param kw: you can add your own keyword, but it should be interpreted by all host
"""
pass
def toggleDisplay(self,object,display):
"""
Toggle on/off the display/visibility/rendermode of an hostObject in the host viewport.
* overwrited by children class for each host
>>> helper.toggleDisplay("polygone1",True)
>>> obj = helper.getObject("polygone1")
>>> helper.toggleDisplay(obj,False)
@type object: hostObject
@param object: the object
@type display: boolean
@param display: if the object is displayed
"""
def toggleXray(self,object,xray):
"""
Toggle on/off the Xray visibility of an hostObject in the host viewport. Currently not supported in Maya
* overwrited by children class for each host
>>> helper.toggleXray("polygone1",True)
>>> obj = helper.getObject("polygone1")
>>> helper.toggleXray(obj,False)
@type object: hostObject
@param object: the object
@type xray: boolean
@param xray: if the object is Xray displayed
"""
print("not supported yet in ",self.host)
def getVisibility(self,obj,editor=True, render=False, active=False):
"""
return the editor/renedring/active visibility state of the given object
* overwrited by children class for each host
@type obj: hostObject
@param obj: the object
@type editor: boolean
@param editor: request editor visibility
@type render: boolean
@param render: request rendering visibility
@type active: boolean
@param active: request active states ie C4D
@rtype: bool/array of bool
@return: the current visibility state of the object
"""
pass
def setViewport(self,**kw):
"""
set the property of the viewport
* overwrited by children class for each host
@type kw: dictionary
@param kw: the list of parameter and their value to change
"""
print ("setViewport helper class")
pass
def toggleEditMode(self):