-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathPMVCPlugin.py
126 lines (112 loc) · 3.22 KB
/
PMVCPlugin.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
# PMVCPlugin
# Initial code generated by Softimage SDK Wizard
# Executed Thu Jan 2 14:05:35 UTC+0100 2014 by ahmidou
#
# Tip: To add a command to this plug-in, right-click in the
# script editor and choose Tools > Add Command.
import win32com.client
from win32com.client import constants
from sipyutils import C # win32com.client.constants
import math
xsi = Application
null = None
false = 0
true = 1
def XSILoadPlugin( in_reg ):
in_reg.Author = "ahmidou"
in_reg.Name = "PMVCPlugin"
in_reg.Major = 1
in_reg.Minor = 0
in_reg.RegisterCommand("PMVC","PMVC")
in_reg.RegisterMenu(25,"PMVCMenu",false,false)
#RegistrationInsertionPoint - do not remove this line
return true
def XSIUnloadPlugin( in_reg ):
strPluginName = in_reg.Name
Application.LogMessage(str(strPluginName) + str(" has been unloaded."),constants.siVerbose)
return true
def PMVC_Init( in_ctxt ):
oCmd = in_ctxt.Source
oCmd.Description = ""
oCmd.ReturnValue = true
oArgs = oCmd.Arguments
oArgs.AddWithHandler("Arg","Collection");
return true
def pointsOnSphere(N):
N = float(N) # in case we got an int which we surely got
pts = [[],[],[]]
inc = math.pi * (3 - math.sqrt(5))
off = 2 / N
for k in range(0, int(N)):
y = k * off - 1 + (off / 2)
r = math.sqrt(1 - y*y)
phi = k * inc
pts[0].append(math.cos(phi)*r)
pts[1].append(y)
pts[2].append(math.sin(phi)*r)
return pts
def PMVC_Execute( arg ):
sparse = 1
c = xsi.PickElement( C.siObjectFilter, 'pick a cage', 'pick a cage')(2)
for d in arg:
cPrim = c.Activeprimitive
cGeo = cPrim.Geometry
dGeo = d.ActivePrimitive.Geometry
dPoints = dGeo.Points
#dGeo.RemoveICEAttribute("PMVWeight")
#dGeo.RemoveICEAttribute("PMVid")
nbSample = 64
Samples = pointsOnSphere(nbSample)
PMVC = [[],[]]
area = math.pi*4/nbSample
for p in dPoints:
w = [0]*cGeo.Points.Count
pos = p.Position
lPos = [pos.X,pos.Y,pos.Z]
posArray = [[lPos[i] for j in range(nbSample)] for i in range(3)]
loc = cGeo.GetRaycastIntersections( posArray, Samples,0 )
aI = cGeo.EvaluatePositions(loc)
aCoord = cGeo.GetTriangleWeightArray(loc)
ID = cGeo.GetTriangleVertexIndexArray( loc )
for s in range(nbSample):
if ID[0][s] != -1:
dist = XSIMath.CreateVector3(aI[0][s], aI[1][s], aI[2][s])
dist.SubInPlace(pos)
distScale = 1/dist.Length()*area
x=aCoord[0][s]*distScale
y=aCoord[1][s]*distScale
z=aCoord[2][s]*distScale
w[ID[0][s]]+= x
w[ID[1][s]]+= y
w[ID[2][s]]+= z
nW = []
id = []
tW = sum(w)
if sparse == 0:
for i in w:
if i/tW < 0.000001:
nW.append(0.0)
else:
nW.append(i/tW)
PMVC[0].append(nW)
else:
j=0
for i in w:
if i/tW > 0.000001:
nW.append(i/tW)
id.append(j)
j+=1
PMVC[0].append(nW)
PMVC[1].append(id)
attr = dGeo.AddIceAttribute("PMVWeight",4,2,2)
attr.DataArray2D = PMVC[0]
attr = dGeo.AddIceAttribute("PMVID",2,2,2)
attr.DataArray2D = PMVC[1]
op = xsi.ApplyICEOp( "PMVC", d )
input = xsi.AddICENode("GetDataNode", op)
input.Parameters("reference").Value = c.FullName
xsi.ConnectICENodes(op.Fullname+".PMVC.In_Name", input.FullName+".outname")
return true
def PMVCMenu_Init( in_ctxt ):
oMenu = in_ctxt.source
oMenu.AddCommandItem("apply PMVC cage","PMVC")