Skip to content

Commit 7e624c9

Browse files
committed
DataTraits.dataTypeFromElement : Convert lists to preferred data type
When converting a list, it was simply using the first match, which would often not be the desired one. Ex: converting `["abc", "b"]` to `IECore.CharVectorData( [ 97, 98 ] )`, instead of `IECore.StringVectorData( [ 'abc', 'b' ] )`. The solution here is to use, on lists, the already existing flag on the `__dataTypesConversionDict` entries that indicated the preferred IECore.Data type for a given source. The options selected here as the preferred one when there was ambiguity were the same ones as returned by the simple (non-vector) types. That required a small update to `dataTypeFromElementType()` in order to avoid returning a random return, now that `list` entries in `__dataTypesConversionDict` can have a preferred flag set to `True`.
1 parent 2b7a2e3 commit 7e624c9

File tree

1 file changed

+29
-26
lines changed

1 file changed

+29
-26
lines changed

python/IECore/DataTraits.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ def isSequenceDataType(obj):
147147
IECore.M44fData: (imath.M44f, True),
148148
IECore.M44dData: (imath.M44d, True),
149149

150-
IECore.BoolVectorData: ( list, False, bool),
150+
IECore.BoolVectorData: ( list, True, bool),
151151
IECore.CharVectorData: (list, False, str),
152152
IECore.UCharVectorData: (list, False, int),
153-
IECore.IntVectorData: (list, False, int),
153+
IECore.IntVectorData: (list, True, int),
154154
IECore.UIntVectorData: (list, False, int),
155155
IECore.HalfVectorData: (list, False, float),
156156
IECore.FloatVectorData: (list, False, float),
157-
IECore.DoubleVectorData: (list, False, float),
158-
IECore.StringVectorData: (list, False, str),
159-
IECore.InternedStringVectorData: (list, False, IECore.InternedString),
157+
IECore.DoubleVectorData: (list, True, float),
158+
IECore.StringVectorData: (list, True, str),
159+
IECore.InternedStringVectorData: (list, True, IECore.InternedString),
160160
IECore.ShortVectorData: (list, False, int),
161161
IECore.UShortVectorData: (list, False, int),
162162
IECore.Int64VectorData: (list, False, int),
@@ -167,27 +167,27 @@ def isSequenceDataType(obj):
167167
IECore.V3fVectorDataBase: (list, False, imath.V3f),
168168
IECore.V3dVectorDataBase: (list, False, imath.V3d),
169169
IECore.V3iVectorDataBase: (list, False, imath.V3i),
170-
IECore.V2fVectorData: (list, False, imath.V2f),
171-
IECore.V2dVectorData: (list, False, imath.V2d),
172-
IECore.V2iVectorData: (list, False, imath.V2i),
173-
IECore.V3fVectorData: (list, False, imath.V3f),
174-
IECore.V3dVectorData: (list, False, imath.V3d),
175-
IECore.V3iVectorData: (list, False, imath.V3i),
176-
IECore.QuatfVectorData: (list, False, imath.Quatf),
177-
IECore.QuatdVectorData: (list, False, imath.Quatd),
178-
IECore.Box2iVectorData: (list, False, imath.Box2i),
179-
IECore.Box2fVectorData: (list, False, imath.Box2f),
180-
IECore.Box2dVectorData: (list, False, imath.Box2d),
181-
IECore.Box3iVectorData: (list, False, imath.Box3i),
182-
IECore.Box3fVectorData: (list, False, imath.Box3f),
183-
IECore.Box3dVectorData: (list, False, imath.Box3d),
184-
IECore.M33fVectorData: (list, False, imath.M33f),
185-
IECore.M33dVectorData: (list, False, imath.M33d),
186-
IECore.M44fVectorData: (list, False, imath.M44f),
187-
IECore.M44dVectorData: (list, False, imath.M44d),
188-
IECore.Color3fVectorData: (list, False, imath.Color3f),
170+
IECore.V2fVectorData: (list, True, imath.V2f),
171+
IECore.V2dVectorData: (list, True, imath.V2d),
172+
IECore.V2iVectorData: (list, True, imath.V2i),
173+
IECore.V3fVectorData: (list, True, imath.V3f),
174+
IECore.V3dVectorData: (list, True, imath.V3d),
175+
IECore.V3iVectorData: (list, True, imath.V3i),
176+
IECore.QuatfVectorData: (list, True, imath.Quatf),
177+
IECore.QuatdVectorData: (list, True, imath.Quatd),
178+
IECore.Box2iVectorData: (list, True, imath.Box2i),
179+
IECore.Box2fVectorData: (list, True, imath.Box2f),
180+
IECore.Box2dVectorData: (list, True, imath.Box2d),
181+
IECore.Box3iVectorData: (list, True, imath.Box3i),
182+
IECore.Box3fVectorData: (list, True, imath.Box3f),
183+
IECore.Box3dVectorData: (list, True, imath.Box3d),
184+
IECore.M33fVectorData: (list, True, imath.M33f),
185+
IECore.M33dVectorData: (list, True, imath.M33d),
186+
IECore.M44fVectorData: (list, True, imath.M44f),
187+
IECore.M44dVectorData: (list, True, imath.M44d),
188+
IECore.Color3fVectorData: (list, True, imath.Color3f),
189189
#IECore.Color3dVectorData: (list, False, IECore.Color3d),
190-
IECore.Color4fVectorData: (list, False, imath.Color4f),
190+
IECore.Color4fVectorData: (list, True, imath.Color4f),
191191
#IECore.Color4dVectorData: (list, False, IECore.Color4d),
192192

193193
IECore.CompoundData: (dict, True, None),
@@ -246,6 +246,9 @@ def valueTypeFromSequenceType(sequenceType):
246246
## \ingroup python
247247
def dataTypeFromElementType(elementType):
248248

249+
if elementType is list:
250+
raise TypeError( "`list` type is ambiguous and not a valid input to dataTypeFromElementType()" )
251+
249252
for (dataType, value) in __dataTypesConversionDict.items():
250253
if value is None:
251254
continue
@@ -265,7 +268,7 @@ def dataTypeFromElement(element):
265268
for (dataType, value) in __dataTypesConversionDict.items():
266269
if value is None:
267270
continue
268-
if value[0] is list and len(value) >= 2 and value[2] is elementValueType:
271+
if value[0] is list and len(value) >= 2 and value[1] and value[2] is elementValueType:
269272
return dataType
270273

271274
return dataTypeFromElementType(type(element))

0 commit comments

Comments
 (0)