Skip to content

Commit 91957f2

Browse files
committed
refactor enums to store enum strings in named arrays
1 parent 7878ea6 commit 91957f2

File tree

2 files changed

+183
-42
lines changed

2 files changed

+183
-42
lines changed

pythreejs/enums.py

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
r"""
2+
Three.js Enums
3+
4+
These correspond to the enum property names in the THREE js object
5+
"""
6+
7+
# Custom Blending Equation Constants
8+
# http://threejs.org/docs/index.html#Reference/Constants/CustomBlendingEquation
9+
10+
Equations = [
11+
'AddEquation',
12+
'SubtractEquation',
13+
'ReverseSubtractEquation',
14+
'MinEquation',
15+
'MaxEquation'
16+
]
17+
18+
DestinationFactors = [
19+
'ZeroFactor',
20+
'OneFactor',
21+
'SrcColorFactor',
22+
'OneMinusSrcColorFactor',
23+
'SrcAlphaFactor',
24+
'OneMinusSrcAlphaFactor',
25+
'DstAlphaFactor',
26+
'OneMinusDstAlphaFactor'
27+
]
28+
29+
SourceFactors = [
30+
'DstColorFactor',
31+
'OneMinusDstColorFactor',
32+
'SrcAlphaSaturateFactor'
33+
]
34+
35+
# Material Constants
36+
# http://threejs.org/docs/index.html#Reference/Constants/Materials
37+
Side = [
38+
'FrontSide',
39+
'BackSide',
40+
'DoubleSide'
41+
]
42+
43+
Shading = [
44+
'FlatShading',
45+
'SmoothShading'
46+
]
47+
48+
Colors = [
49+
'NoColors',
50+
'FaceColors',
51+
'VertexColors'
52+
]
53+
54+
BlendingMode = [
55+
'NoBlending',
56+
'NormalBlending',
57+
'AdditiveBlending',
58+
'SubtractiveBlending',
59+
'MultiplyBlending',
60+
'CustomBlending'
61+
]
62+
63+
64+
# Texture Constants
65+
# http://threejs.org/docs/index.html#Reference/Constants/Textures
66+
67+
Operations = [
68+
'MultiplyOperation',
69+
'MixOperation',
70+
'AddOperation'
71+
]
72+
73+
MappingModes = [
74+
'UVMapping',
75+
'CubeReflectionMapping',
76+
'CubeRefractionMapping',
77+
'EquirectangularReflectionMapping',
78+
'EquirectangularRefractionMapping',
79+
'SphericalReflectionMapping'
80+
]
81+
82+
WrappingModes = [
83+
'RepeatWrapping',
84+
'ClampToEdgeWrapping',
85+
'MirroredRepeatWrapping'
86+
]
87+
88+
Filters = [
89+
'NearestFilter',
90+
'NearestMipMapNearestFilter',
91+
'NearestMipMapLinearFilter',
92+
'LinearFilter',
93+
'LinearMipMapNearestFilter',
94+
'LinearMipMapLinearFilter'
95+
]
96+
97+
DataTypes = [
98+
'UnsignedByteType',
99+
'ByteType',
100+
'ShortType',
101+
'UnsignedShortType',
102+
'IntType',
103+
'UnsignedIntType',
104+
'FloatType',
105+
'HalfFloatType'
106+
]
107+
108+
PixelTypes = [
109+
'UnsignedShort4444Type',
110+
'UnsignedShort5551Type',
111+
'UnsignedShort565Type'
112+
]
113+
114+
PixelFormats = [
115+
'AlphaFormat',
116+
'RGBFormat',
117+
'RGBAFormat',
118+
'LuminanceFormat',
119+
'LuminanceAlphaFormat',
120+
'RGBEFormat'
121+
]
122+
123+
CompressedTextureFormats = [
124+
'RGB_S3TC_DXT1_Format',
125+
'RGBA_S3TC_DXT1_Format',
126+
'RGBA_S3TC_DXT3_Format',
127+
'RGBA_S3TC_DXT5_Format',
128+
'RGB_PVRTC_4BPPV1_Format',
129+
'RGB_PVRTC_2BPPV1_Format',
130+
'RGBA_PVRTC_4BPPV1_Format',
131+
'RGBA_PVRTC_2BPPV1_Format'
132+
]
133+
134+
# Misc
135+
136+
Lines = [
137+
'LineStrip',
138+
'LinePieces'
139+
]
140+
141+
Renderers = [
142+
'webgl',
143+
'canvas',
144+
'auto'
145+
]
146+

pythreejs/pythreejs.py

Lines changed: 37 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616
from traitlets import (Unicode, Int, CInt, Instance, Enum, List, Dict, Float,
1717
CFloat, Bool)
1818
from ._package import npm_pkg_name
19+
from .enums import (
20+
Equations, DestinationFactors, SourceFactors, Side, Shading, Colors,
21+
BlendingMode, Operations, MappingModes, WrappingModes, Filters,
22+
DataTypes, PixelTypes, PixelFormats, CompressedTextureFormats,
23+
Lines, Renderers)
24+
1925
from math import pi, sqrt
2026

27+
2128
def vector3(trait_type=CFloat, default=None, **kwargs):
2229
if default is None:
2330
default = [0, 0, 0]
@@ -56,25 +63,22 @@ class DataTexture(Texture):
5663
_model_name = Unicode('DataTextureModel').tag(sync=True)
5764

5865
data = List(CInt).tag(sync=True)
59-
format = Enum(['RGBAFormat', 'AlphaFormat', 'RGBFormat', 'LuminanceFormat',
60-
'LuminanceAlphaFormat'], 'RGBAFormat').tag(sync=True)
66+
format = Enum(PixelFormats, 'RGBAFormat').tag(sync=True)
6167
width = CInt(256).tag(sync=True)
6268
height = CInt(256).tag(sync=True)
63-
type = Enum(['UnsignedByteType', 'ByteType', 'ShortType',
64-
'UnsignedShortType', 'IntType', 'UnsignedIntType',
65-
'FloatType', 'UnsignedShort4444Type', 'UnsignedShort5551Type',
66-
'UnsignedShort565Type'], 'UnsignedByteType').tag(sync=True)
67-
mapping = Enum(['UVMapping', 'CubeReflectionMapping',
68-
'CubeRefractionMapping', 'SphericalReflectionMapping',
69-
'SphericalRefractionMapping'], 'UVMapping').tag(sync=True)
70-
wrapS = Enum(['ClampToEdgeWrapping', 'RepeatWrapping', 'MirroredRepeatWrapping'],
71-
'ClampToEdgeWrapping').tag(sync=True)
72-
wrapT = Enum(['ClampToEdgeWrapping', 'RepeatWrapping', 'MirroredRepeatWrapping'],
73-
'ClampToEdgeWrapping').tag(sync=True)
74-
magFilter = Enum(['LinearFilter', 'NearestFilter'], 'LinearFilter').tag(sync=True)
75-
minFilter = Enum(['NearestFilter', 'NearestMipMapNearestFilter',
76-
'NearestMipMapLinearFilter', 'LinearFilter',
77-
'LinearMipMapNearestFilter'], 'NearestFilter').tag(sync=True)
69+
type = Enum(DataTypes, 'UnsignedByteType').tag(sync=True)
70+
71+
# TODO: this enum includes both DataTypes and PixelTypes?
72+
# type = Enum(['UnsignedByteType', 'ByteType', 'ShortType',
73+
# 'UnsignedShortType', 'IntType', 'UnsignedIntType',
74+
# 'FloatType', 'UnsignedShort4444Type', 'UnsignedShort5551Type',
75+
# 'UnsignedShort565Type'], 'UnsignedByteType').tag(sync=True)
76+
77+
mapping = Enum(MappingModes, 'UVMapping').tag(sync=True)
78+
wrapS = Enum(WrappingModes, 'ClampToEdgeWrapping').tag(sync=True)
79+
wrapT = Enum(WrappingModes, 'ClampToEdgeWrapping').tag(sync=True)
80+
magFilter = Enum(Filters, 'LinearFilter').tag(sync=True)
81+
minFilter = Enum(Filters, 'NearestFilter').tag(sync=True)
7882
anisotropy = CInt(1).tag(sync=True)
7983

8084

@@ -460,20 +464,13 @@ class Material(Widget):
460464

461465
# id = TODO
462466
name = Unicode(sync=True)
463-
side = Enum(['FrontSide', 'BackSide', 'DoubleSide'], 'DoubleSide').tag(sync=True)
467+
side = Enum(Side, 'DoubleSide').tag(sync=True)
464468
opacity = CFloat(1.0).tag(sync=True)
465469
transparent = Bool().tag(sync=True)
466-
blending = Enum(['NoBlending', 'NormalBlending', 'AdditiveBlending',
467-
'SubtractiveBlending', 'MultiplyBlending',
468-
'CustomBlending'], 'NormalBlending').tag(sync=True)
469-
blendSrc = Enum(['ZeroFactor', 'OneFactor', 'SrcColorFactor',
470-
'OneMinusSrcColorFactor', 'SrcAlphaFactor',
471-
'OneMinusSrcAlphaFactor', 'DstAlphaFactor',
472-
'OneMinusDstAlphaFactor'], 'SrcAlphaFactor').tag(sync=True)
473-
blendDst = Enum(['DstColorFactor', 'OneMinusDstColorFactor',
474-
'SrcAlphaSaturateFactor'], 'OneMinusDstColorFactor').tag(sync=True)
475-
blendEquation = Enum(['AddEquation', 'SubtractEquation',
476-
'ReverseSubtractEquation'], 'AddEquation').tag(sync=True)
470+
blending = Enum(BlendingMode, 'NormalBlending').tag(sync=True)
471+
blendSrc = Enum(DestinationFactors, 'SrcAlphaFactor').tag(sync=True)
472+
blendDst = Enum(SourceFactors, 'OneMinusDstColorFactor').tag(sync=True)
473+
blendEquation = Enum(Equations, 'AddEquation').tag(sync=True)
477474
depthTest = Bool(True).tag(sync=True)
478475
depthWrite = Bool(True).tag(sync=True)
479476
polygonOffset = Bool(True).tag(sync=True)
@@ -494,8 +491,8 @@ class BasicMaterial(Material):
494491
wireframeLinewidth = CFloat(1.0).tag(sync=True)
495492
wireframeLinecap = Unicode('round').tag(sync=True)
496493
wireframeLinejoin = Unicode('round').tag(sync=True)
497-
shading = Enum(['SmoothShading', 'FlatShading', 'NoShading'], 'SmoothShading').tag(sync=True)
498-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
494+
shading = Enum(Shading, 'SmoothShading').tag(sync=True)
495+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
499496
fog = Bool().tag(sync=True)
500497
map = Instance(Texture, allow_none=True).tag(sync=True, **widget_serialization)
501498
lightMap = Instance(Texture, allow_none=True).tag(sync=True, **widget_serialization)
@@ -512,8 +509,7 @@ class LambertMaterial(BasicMaterial):
512509
emissive = Color('black').tag(sync=True)
513510
reflectivity = CFloat(1.0).tag(sync=True)
514511
refractionRatio = CFloat(0.98).tag(sync=True)
515-
combine = Enum(['MultiplyOperation', 'MixOperation', 'AddOperation'],
516-
'MultiplyOperation').tag(sync=True)
512+
combine = Enum(Operations, 'MultiplyOperation').tag(sync=True)
517513

518514

519515
class PhongMaterial(BasicMaterial):
@@ -525,8 +521,7 @@ class PhongMaterial(BasicMaterial):
525521
shininess = CFloat(30).tag(sync=True)
526522
reflectivity = CFloat(1.0).tag(sync=True)
527523
refractionRatio = CFloat(0.98).tag(sync=True)
528-
combine = Enum(['MultiplyOperation', 'MixOperation', 'AddOperation'],
529-
'MultiplyOperation').tag(sync=True)
524+
combine = Enum(Operations, 'MultiplyOperation').tag(sync=True)
530525

531526

532527
class DepthMaterial(Material):
@@ -551,7 +546,7 @@ class LineBasicMaterial(_LineMaterial):
551546
linecap = Unicode('round').tag(sync=True)
552547
linejoin = Unicode('round').tag(sync=True)
553548
fog = Bool().tag(sync=True)
554-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
549+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
555550

556551

557552
class LineDashedMaterial(_LineMaterial):
@@ -563,7 +558,7 @@ class LineDashedMaterial(_LineMaterial):
563558
scale = CFloat(1.0).tag(sync=True)
564559
dashSize = CFloat(3.0).tag(sync=True)
565560
gapSize = CFloat(1.0).tag(sync=True)
566-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
561+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
567562
fog = Bool().tag(sync=True)
568563

569564

@@ -572,7 +567,7 @@ class NormalMaterial(Material):
572567
_model_name = Unicode('NormalMaterialModel').tag(sync=True)
573568

574569
morphTargets = Bool().tag(sync=True)
575-
shading = Enum(['SmoothShading', 'FlatShading', 'NoShading'], 'SmoothShading').tag(sync=True)
570+
shading = Enum(Shading, 'SmoothShading').tag(sync=True)
576571
wireframe = Bool().tag(sync=True)
577572
wireframeLinewidth = CFloat(1.0).tag(sync=True)
578573

@@ -599,10 +594,10 @@ class ShaderMaterial(Material):
599594
lights = Bool().tag(sync=True)
600595
morphNormals = Bool().tag(sync=True)
601596
wireframe = Bool().tag(sync=True)
602-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
597+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
603598
skinning = Bool().tag(sync=True)
604599
fog = Bool().tag(sync=True)
605-
shading = Enum(['SmoothShading', 'FlatShading', 'NoShading'], 'SmoothShading').tag(sync=True)
600+
shading = Enum(Shading, 'SmoothShading').tag(sync=True)
606601
linewidth = CFloat(1.0).tag(sync=True)
607602
wireframeLinewidth = CFloat(1.0).tag(sync=True)
608603

@@ -642,7 +637,7 @@ class Line(Mesh):
642637
_view_name = Unicode('LineView').tag(sync=True)
643638
_model_name = Unicode('LineModel').tag(sync=True)
644639

645-
type = Enum(['LineStrip', 'LinePieces'], 'LineStrip').tag(sync=True)
640+
type = Enum(Lines, 'LineStrip').tag(sync=True)
646641
material = Instance(_LineMaterial).tag(sync=True, **widget_serialization)
647642

648643

@@ -760,7 +755,7 @@ class Renderer(DOMWidget):
760755

761756
width = Unicode('600').tag(sync=True) # TODO: stop relying on deprecated DOMWidget attribute
762757
height = Unicode('400').tag(sync=True)
763-
renderer_type = Enum(['webgl', 'canvas', 'auto'], 'auto').tag(sync=True)
758+
renderer_type = Enum(Renderers, 'auto').tag(sync=True)
764759
scene = Instance(Scene).tag(sync=True, **widget_serialization)
765760
camera = Instance(Camera).tag(sync=True, **widget_serialization)
766761
controls = List(Instance(Controls)).tag(sync=True, **widget_serialization)

0 commit comments

Comments
 (0)