-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathNormalMagic.ms
More file actions
196 lines (167 loc) · 4.48 KB
/
NormalMagic.ms
File metadata and controls
196 lines (167 loc) · 4.48 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
/**
Copyright 2017 James Somervail
*/
fn polymesh_filt obj =
(
case superclassof obj of
(
GeometryClass: 1
default: 0
)
)
fn specifynormals obj =
(
nVerts = getNumVerts obj
for i = 1 to nVerts do
(
setNormal obj i [xcomp,ycomp,zcomp]
)
)
fn pointnormals obj ref isinverted =
(
nVerts = getNumVerts obj
if isinverted then inv = 1 else inv = -1
for i = 1 to nVerts do
(
vec = (ref.pos - getVert obj i ) * inv
normal = normalize vec
setNormal obj i normal
)
)
fn clonenormals obj src =
(
nVerts = getNumVerts obj
for i = 1 to nVerts do --iterate through all vertices in tar mesh
(
nVerts_src = getNumVerts src
closestvert = 1
for j = 1 to nVerts_src do --iterate through all faces in ref mesh
(
closestdist = getVert src closestvert - getVert obj i
currentdist = getVert src j - getVert obj i
--check if distance to face j is closer than to current closest
if length currentdist < length closestdist do
(
closestvert = j
)
)
norm = getNormal src closestvert
setNormal obj i norm
)
)
--GLOBAL VARIABLE DECLARATIONS
global tarmesh --target mesh
global refpoint --reference dummy for point normals
global srcmesh --source mesh for clone normals
global xcomp = 0 --x component
global ycomp = 0 --y component
global zcomp = 1 --z component
--UI ELEMENTS
MainWindow = newRolloutFloater "Normal Toolkit" 220 167
rollout SpecifyNormal "Specify Normals" width:220 height:24
(
spinner 'xspin' "" pos:[0,3] width:35 height:16 range:[-1,1,0] align:#left
spinner 'yspin' "" pos:[38,3] width:35 height:16 range:[-1,1,0] align:#left
spinner 'zspin' "" pos:[76,3] width:35 height:16 range:[-1,1,1] align:#left
button 'apply' "Apply" pos:[151,2] width:54 height:18 toolTip:"Apply specific normals to selection" align:#left
on xspin changed val do
xcomp = val
on yspin changed val do
yomp = val
on zspin changed val do
zcomp = val
on apply pressed do
(
undo on
(
selectarr = selection as array
for i in selectarr do
(
if polymesh_filt (i) == 1 do
(
try(convertToMesh i)catch()
specifynormals i
convertToPoly i
addModifier i (Edit_Normals ())
)
)
)
)
)
---------------------------------------------------------------------------------------
rollout PointNormal "Point Normals" width:162 height:78
(
pickbutton 'selectpoint' "Select Point Helper" pos:[-1,2] width:150 height:21 toolTip:"Select Point Helper to determine Target Mesh normal origin" align:#left
checkbox 'inverted' "Invert" pos:[2,23] width:54 height:15 toolTip:"Invert normal direction" align:#left
button 'apply' "Apply" pos:[151,2] width:54 height:21 toolTip:"Apply point normals to selection" align:#left enabled: false
on selectpoint picked obj do
(
refpoint = obj
selectpoint.text = "Point Helper: "+obj.name
apply.enabled = true
)
on apply pressed do
(
undo on
(
selectarr = selection as array
for i in selectarr do
(
if polymesh_filt(i) == 1 then
(
try(convertToMesh i)catch()
pointnormals i refpoint inverted.checked
convertToPoly i
addModifier i (Edit_Normals ())
)
)
)
)
)
---------------------------------------------------------------------------------------
rollout CloneNormal "Cloned Normals" width:162 height:84
(
pickbutton 'selectsrc' "Select Source Mesh" pos:[0,2] width:150 height:21 toolTip:"Select Source Mesh to clone normals from" align:#left
button 'apply' "Apply" pos:[151,2] width:54 height:21 toolTip:"Apply cloned normals to selection" align:#left enabled:false
on selectsrc picked obj do
(
if polymesh_filt(obj) == 1 then
(
srcmesh = obj
selectsrc.text = "Source Mesh: "+obj.name
apply.enabled = true
) else
(
--clear target mesh and button text
srcmesh = undefined
selectsrc.text = "Select Source Mesh"
--play error ping
ssounds = dotNetClass "System.Media.SystemSounds"
ssounds.Hand.Play()
apply.enabled = false
)
)
on apply pressed do
(
undo on
(
selectarr = selection as array
for i in selectarr do
(
if polymesh_filt(i) == 1 then
(
try(convertToMesh i)catch()
srcclone = copy srcmesh
convertTomesh srcclone
clonenormals i srcclone
delete srcclone
convertToPoly i
addModifier i (Edit_Normals ())
)
)
)
)
)
addrollout SpecifyNormal MainWindow
addrollout PointNormal MainWindow
addrollout CloneNormal MainWindow