-
Notifications
You must be signed in to change notification settings - Fork 0
/
VertexColorGreyNoiseWithUI.py
235 lines (196 loc) · 9.48 KB
/
VertexColorGreyNoiseWithUI.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
# VertexColorGreyNoiseWithUI.py
## Version 1.0 ##
## Leif Peterson 2016 ##
# Import Modules
import pymel.core as pm
import random
import functools
## UI Function ##
def createUI( SWindowTitle, pApplyCallback ):
windowID = 'vcgnWindowID'
# If Window is Already Open, Delete it and Open a New One
if pm.window( windowID, exists=True ):
pm.deleteUI( windowID )
# Init Window
pm.window( windowID, title=SWindowTitle, sizeable=False, resizeToFitChildren=True )
pm.rowColumnLayout( numberOfColumns=3, columnWidth=[ (1, 75), (2, 75), (3,75) ], columnOffset=[ (1,'right', 3) ])
pm.text( label='Min Value:' )
# Minimum Value Field, Default = 0.0, min = 0.0, max = 1.0, invisible slider step = 0.01
MinValueField = pm.floatField( value=0.0, minValue=0.0, maxValue=1.0, step=0.01 )
pm.separator( h=10, style='none' )
pm.text( label='Max Value:' )
# Maximum Value Field, Default = 1.0, min = 0.0, max = 1.0, invisible slider step = 0.01
MaxValueField = pm.floatField( value=1.0, minValue=0.0, maxValue=1.0, step=0.01 )
pm.separator( h=10, style='none' )
# Formatting
pm.separator( h=10, style='none' )
pm.separator( h=10, style='none' )
pm.separator( h=10, style='none' )
pm.text( label='Noise Type:' )
# Noise Options - Disabled For Version 1.0 Will Be Enabled in 2.0
NoiseOption = pm.optionMenu( 'NoiseFunctions', enable=False )
pm.menuItem( label='Simple', parent='NoiseFunctions' )
pm.menuItem( label='Perlin', parent='NoiseFunctions' )
pm.menuItem( label='OpenSimplex', parent='NoiseFunctions' )
pm.separator( h=10, style='none' )
# Formatting
pm.separator( h=10, style='none' )
pm.separator( h=10, style='none' )
pm.separator( h=10, style='none' )
# Buttons
pm.separator( h=10, style='none' )
pm.button( label='Apply', command=functools.partial(pApplyCallback,
NoiseOption,
MaxValueField,
MinValueField) )
def cancelCallback( *Args ):
if pm.window( windowID, exists=True ):
pm.deleteUI( windowID )
pm.button( label='Cancel', command=cancelCallback )
pm.showWindow()
## Noise Generation Functions Start Here ##
# Check The Objects Number of Vertex Color Sets
def NumColorSets():
ColorSets = pm.polyColorSet( query=True, allColorSets=True )
# If there are No Color Sets, Return 0, if there is Exactly 1, Return 1, if there are at least 2, Return 2.
if ColorSets == None:
return 0
elif len(ColorSets) == 1:
return 1
elif len(ColorSets) > 1:
return 2
# Create Second Vertex Color Set
def CreateColorSet( IntIndex ):
i = IntIndex
colorSetName = 'colorSet%d' % i
print colorSetName
pm.polyColorSet( create=True, colorSet = colorSetName )
# Set Vertex Color to 0.5 Grey
def SetMiddleGrey( NObject ):
NObjName = '%s' % NObject.name()
# Select the Objects Vertices
pm.selectMode( co=True )
pm.selectType( pv=True )
pm.polySelectConstraint( type=0x0001, mode=3 )
pm.select()
# List the Objects Vertices
ObjectVerts = pm.ls( selection=True, fl=True )
pm.select( cl=True )
# For Every Vertex on the Object, Set its Vertex Color to 0.5 Grey
for v in range(len(ObjectVerts)):
pm.polyColorPerVertex( ObjectVerts[v], colorRGB=(0.5,0.5,0.5), alpha=1.0)
# Release the Selection Constraints
pm.polySelectConstraint( mode=0 )
pm.selectMode( o=True )
# Select the Object Again
pm.select( NObjName )
### Noise Functions ##
# Generate a Simple Random Noise Gradient
def SimpleNoise( NObject, FMin, FMax ):
# Set Local Variables
NObjName = '%s' % NObject.name()
min = FMin
max = FMax
# Select the Objects Vertices
pm.selectMode( co=True )
pm.selectType( pv=True )
pm.polySelectConstraint( type=0x0001, mode=3 )
pm.select()
# List the Objects Vertices
ObjectVerts = pm.ls( selection=True, fl=True )
pm.select( cl=True )
# For Every Vertex on the Object, Set its Vertex Color to 0.5 Grey
for v in range(len(ObjectVerts)):
FValue = random.uniform( min, max )
pm.polyColorPerVertex( ObjectVerts[v], colorRGB=( FValue, FValue, FValue ), alpha=1.0)
# Release the Selection Constraints
pm.polySelectConstraint( mode=0 )
pm.selectMode( o=True )
# Select the Object Again
pm.select( NObjName )
# Primary Function
def GenerateVertexColor( StrNoiseOpt, FMax, FMin ):
NoiseFunction = StrNoiseOpt
Min = FMin
Max = FMax
# Check that a Valid Selection has been made
Selected = pm.ls( selection = True )
if len(Selected) == 0:
return "No Objects Selected"
elif len(Selected) > 1:
return "More Than One Object Selected"
# If there is a Valid Selection
elif len(Selected) == 1:
# Get the Selection's Shape
SelectionShape = Selected[0].getShape()
# Check if the Object Type is a Polygon Mesh
bIsValidObjType = pm.objectType( SelectionShape, isType='mesh' )
# If it is Not, Exit the Function and tell the User
if bIsValidObjType != True:
return "Selected Object is Not a Polygon Mesh"
# If Everything in Valid, Execute The Script
else:
# Check Number of Color Sets
IntNumOfColorSets = NumColorSets()
if IntNumOfColorSets == 0:
# Create 2 Color Sets, Set the first one to .5
# and Run the Noise Function on the Second
for i in range(2):
CreateColorSet(i+1)
# If the Current Color Set is colorSet1 set it to 0.5 Grey
if pm.polyColorSet( query=True, colorSet=True, currentColorSet=True ) == 'colorSet1':
SetMiddleGrey( Selected[0] )
# Else If it is not the Current Color Set, Set to the Current and Set it's Color to 0.5 Grey
elif pm.polyColorSet( query=True, colorSet=True, currentColorSet=True ) != 'colorSet1':
pm.polyColorSet( currentColorSet=True, colorSet='colorSet1' )
SetMiddleGrey( Selected[0] )
pm.polyColorSet( currentColorSet=True, colorSet='colorSet2' )
if pm.polyColorSet( query=True, currentColorSet=True, colorSet=True ) == 'colorSet1':
return "Unable To Set Current Color Set to colorSet2"
else:
if NoiseFunction == 'Simple':
SimpleNoise( Selected[0], Min, Max )
return "Random Noise for 'colorSet2' Was Set for %s" % Selected[0].name()
else:
return "Invalid Noise Function Sepcified"
elif IntNumOfColorSets == 1:
# Create 'colorSet2'
CreateColorSet(2)
# Set 'colorSet2' as the Active Color Set if it's not the Active Color Set
if pm.polyColorSet( query=True, currentColorSet=True, colorSet=True ) != 'colorSet2':
pm.polyColorSet( currentColorSet=True, colorSet='colorSet2' )
# Run Noise Function
if NoiseFunction == 'Simple':
SimpleNoise( Selected[0], Min, Max )
return "Random Noise for 'colorSet2' Was Set for %s" % Selected[0].name()
else:
return "Invalid Noise Function Sepcified"
elif IntNumOfColorSets == 2:
# List All Color Sets
AllColorSets = pm.polyColorSet( query=True, allColorSets=True )
# Check for 'colorSet2"
if 'colorSet2' in AllColorSets:
if pm.polyColorSet( query=True, currentColorSet=True, colorSet=True ) != 'colorSet2':
pm.polyColorSet( currentColorSet=True, colorSet='colorSet2' )
# Run the Noise Function
SimpleNoise( Selected[0], Min, Max )
return "Random Noise for 'colorSet2' Was Set for %s" % Selected[0].name()
# if it does not exist Create 'colorSet2'
CreateColorSet(2)
# Set 'colorSet2' as the Active Color Set if it's not the Active Color Set
if pm.polyColorSet( query=True, currentColorSet=True, colorSet=True ) != 'colorSet2':
pm.polyColorSet( currentColorSet=True, colorSet='colorSet2' )
# Run the Noise Function
if NoiseFunction == 'Simple':
SimpleNoise( Selected[0], Min, Max )
return "Random Noise for 'colorSet2' Was Set for %s" % Selected[0].name()
else:
return "Invalid Noise Function Sepcified"
else:
return "Unknown Error Occurred"
def applyCallback( pNoiseOption, pMaxValueField, pMinValueField, *pArgs ):
NoiseOptionState = pm.optionMenu( pNoiseOption, query=True, value=True )
MaxValue = pm.floatField( pMaxValueField, query=True, value=True )
MinValue = pm.floatField( pMinValueField, query=True, value=True )
GenerateVertexColor( NoiseOptionState, MaxValue, MinValue )
createUI( 'NoiseGen', applyCallback )