Skip to content

Commit 590bce7

Browse files
authored
Merge pull request #59 from abelnation/refactor-enums-2
Refactor enums to store enum strings in named arrays
2 parents ea9fa85 + 91957f2 commit 590bce7

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, CInt, Instance, Enum, List, Tuple, 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

@@ -464,20 +468,13 @@ class Material(Widget):
464468

465469
# id = TODO
466470
name = Unicode(sync=True)
467-
side = Enum(['FrontSide', 'BackSide', 'DoubleSide'], 'DoubleSide').tag(sync=True)
471+
side = Enum(Side, 'DoubleSide').tag(sync=True)
468472
opacity = CFloat(1.0).tag(sync=True)
469473
transparent = Bool().tag(sync=True)
470-
blending = Enum(['NoBlending', 'NormalBlending', 'AdditiveBlending',
471-
'SubtractiveBlending', 'MultiplyBlending',
472-
'CustomBlending'], 'NormalBlending').tag(sync=True)
473-
blendSrc = Enum(['ZeroFactor', 'OneFactor', 'SrcColorFactor',
474-
'OneMinusSrcColorFactor', 'SrcAlphaFactor',
475-
'OneMinusSrcAlphaFactor', 'DstAlphaFactor',
476-
'OneMinusDstAlphaFactor'], 'SrcAlphaFactor').tag(sync=True)
477-
blendDst = Enum(['DstColorFactor', 'OneMinusDstColorFactor',
478-
'SrcAlphaSaturateFactor'], 'OneMinusDstColorFactor').tag(sync=True)
479-
blendEquation = Enum(['AddEquation', 'SubtractEquation',
480-
'ReverseSubtractEquation'], 'AddEquation').tag(sync=True)
474+
blending = Enum(BlendingMode, 'NormalBlending').tag(sync=True)
475+
blendSrc = Enum(DestinationFactors, 'SrcAlphaFactor').tag(sync=True)
476+
blendDst = Enum(SourceFactors, 'OneMinusDstColorFactor').tag(sync=True)
477+
blendEquation = Enum(Equations, 'AddEquation').tag(sync=True)
481478
depthTest = Bool(True).tag(sync=True)
482479
depthWrite = Bool(True).tag(sync=True)
483480
polygonOffset = Bool(True).tag(sync=True)
@@ -498,8 +495,8 @@ class BasicMaterial(Material):
498495
wireframeLinewidth = CFloat(1.0).tag(sync=True)
499496
wireframeLinecap = Unicode('round').tag(sync=True)
500497
wireframeLinejoin = Unicode('round').tag(sync=True)
501-
shading = Enum(['SmoothShading', 'FlatShading', 'NoShading'], 'SmoothShading').tag(sync=True)
502-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
498+
shading = Enum(Shading, 'SmoothShading').tag(sync=True)
499+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
503500
fog = Bool().tag(sync=True)
504501
map = Instance(Texture, allow_none=True).tag(sync=True, **widget_serialization)
505502
lightMap = Instance(Texture, allow_none=True).tag(sync=True, **widget_serialization)
@@ -516,8 +513,7 @@ class LambertMaterial(BasicMaterial):
516513
emissive = Color('black').tag(sync=True)
517514
reflectivity = CFloat(1.0).tag(sync=True)
518515
refractionRatio = CFloat(0.98).tag(sync=True)
519-
combine = Enum(['MultiplyOperation', 'MixOperation', 'AddOperation'],
520-
'MultiplyOperation').tag(sync=True)
516+
combine = Enum(Operations, 'MultiplyOperation').tag(sync=True)
521517

522518

523519
class PhongMaterial(BasicMaterial):
@@ -529,8 +525,7 @@ class PhongMaterial(BasicMaterial):
529525
shininess = CFloat(30).tag(sync=True)
530526
reflectivity = CFloat(1.0).tag(sync=True)
531527
refractionRatio = CFloat(0.98).tag(sync=True)
532-
combine = Enum(['MultiplyOperation', 'MixOperation', 'AddOperation'],
533-
'MultiplyOperation').tag(sync=True)
528+
combine = Enum(Operations, 'MultiplyOperation').tag(sync=True)
534529

535530

536531
class DepthMaterial(Material):
@@ -555,7 +550,7 @@ class LineBasicMaterial(_LineMaterial):
555550
linecap = Unicode('round').tag(sync=True)
556551
linejoin = Unicode('round').tag(sync=True)
557552
fog = Bool().tag(sync=True)
558-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
553+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
559554

560555

561556
class LineDashedMaterial(_LineMaterial):
@@ -567,7 +562,7 @@ class LineDashedMaterial(_LineMaterial):
567562
scale = CFloat(1.0).tag(sync=True)
568563
dashSize = CFloat(3.0).tag(sync=True)
569564
gapSize = CFloat(1.0).tag(sync=True)
570-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
565+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
571566
fog = Bool().tag(sync=True)
572567

573568

@@ -576,7 +571,7 @@ class NormalMaterial(Material):
576571
_model_name = Unicode('NormalMaterialModel').tag(sync=True)
577572

578573
morphTargets = Bool().tag(sync=True)
579-
shading = Enum(['SmoothShading', 'FlatShading', 'NoShading'], 'SmoothShading').tag(sync=True)
574+
shading = Enum(Shading, 'SmoothShading').tag(sync=True)
580575
wireframe = Bool().tag(sync=True)
581576
wireframeLinewidth = CFloat(1.0).tag(sync=True)
582577

@@ -603,10 +598,10 @@ class ShaderMaterial(Material):
603598
lights = Bool().tag(sync=True)
604599
morphNormals = Bool().tag(sync=True)
605600
wireframe = Bool().tag(sync=True)
606-
vertexColors = Enum(['NoColors', 'FaceColors', 'VertexColors'], 'NoColors').tag(sync=True)
601+
vertexColors = Enum(Colors, 'NoColors').tag(sync=True)
607602
skinning = Bool().tag(sync=True)
608603
fog = Bool().tag(sync=True)
609-
shading = Enum(['SmoothShading', 'FlatShading', 'NoShading'], 'SmoothShading').tag(sync=True)
604+
shading = Enum(Shading, 'SmoothShading').tag(sync=True)
610605
linewidth = CFloat(1.0).tag(sync=True)
611606
wireframeLinewidth = CFloat(1.0).tag(sync=True)
612607

@@ -646,7 +641,7 @@ class Line(Mesh):
646641
_view_name = Unicode('LineView').tag(sync=True)
647642
_model_name = Unicode('LineModel').tag(sync=True)
648643

649-
type = Enum(['LineStrip', 'LinePieces'], 'LineStrip').tag(sync=True)
644+
type = Enum(Lines, 'LineStrip').tag(sync=True)
650645
material = Instance(_LineMaterial).tag(sync=True, **widget_serialization)
651646

652647

@@ -764,7 +759,7 @@ class Renderer(DOMWidget):
764759

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

0 commit comments

Comments
 (0)