Skip to content

Commit 219ffb4

Browse files
committed
Merge pull request #90 from ldmoser/setPresets
Adding setPresets() to Parameter class.
2 parents e4456ef + 5a6a7e1 commit 219ffb4

File tree

11 files changed

+174
-43
lines changed

11 files changed

+174
-43
lines changed

include/IECore/CompoundParameter.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//////////////////////////////////////////////////////////////////////////
22
//
3-
// Copyright (c) 2007-2012, Image Engine Design Inc. All rights reserved.
3+
// Copyright (c) 2007-2013, Image Engine Design Inc. All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without
66
// modification, are permitted provided that the following conditions are
@@ -71,11 +71,17 @@ class CompoundParameter : public Parameter
7171
/// of all the child objects.
7272
/// \threading It is not safe to call this from multiple concurrent threads.
7373
virtual const Object *defaultValue() const;
74+
/// \deprecated Use getPresets instead.
75+
virtual const PresetsContainer &presets() const;
7476
/// If true was passed to adoptChildPresets at construction, then update the presets
7577
/// with the intersection of the presets of all the child parameters, otherwise returns
76-
/// an empty container. Please note that the map returned may differ between one call
78+
/// an empty container or the presets defined by setPresets().
79+
/// Please note that the map returned may differ between one call
7780
/// to presets() and the next.
78-
virtual const PresetsContainer &presets() const;
81+
virtual const PresetsContainer &getPresets() const;
82+
/// Defines presets for this Parameter.
83+
/// Throws an exception if true was passed to adoptChildPresets at construction.
84+
virtual void setPresets( const PresetsContainer &presets );
7985
/// Implemented to return true only if all children have presetsOnly() true and
8086
/// true was passed to adoptChildPresets at construction.
8187
virtual bool presetsOnly() const;

include/IECore/Parameter.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,12 @@ class Parameter : public RunTimeTyped
7878
const std::string &description() const;
7979
/// Returns the default value for this parameter.
8080
virtual const Object *defaultValue() const;
81-
/// Returns the presets for this parameter.
81+
/// \deprecated Use getPresets instead.
8282
virtual const PresetsContainer &presets() const;
83+
/// Returns the presets for this parameter.
84+
virtual const PresetsContainer &getPresets() const;
85+
/// Overrides the presets for this parameter.
86+
virtual void setPresets( const PresetsContainer &presets );
8387
/// Returns true if this parameter only accepts
8488
/// parameters present as presets.
8589
virtual bool presetsOnly() const;

src/IECore/CompoundParameter.cpp

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//////////////////////////////////////////////////////////////////////////
22
//
3-
// Copyright (c) 2007-2012, Image Engine Design Inc. All rights reserved.
3+
// Copyright (c) 2007-2013, Image Engine Design Inc. All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without
66
// modification, are permitted provided that the following conditions are
@@ -73,27 +73,32 @@ const Object *CompoundParameter::defaultValue() const
7373
}
7474

7575
const Parameter::PresetsContainer &CompoundParameter::presets() const
76+
{
77+
return getPresets();
78+
}
79+
80+
const Parameter::PresetsContainer &CompoundParameter::getPresets() const
7681
{
7782
if( !m_adoptChildPresets )
7883
{
79-
return Parameter::presets();
84+
return Parameter::getPresets();
8085
}
8186

8287
// naughty? nah! it gives the right semantics to an outside observer
83-
PresetsContainer &pr = const_cast<PresetsContainer &>( Parameter::presets() );
88+
PresetsContainer &pr = const_cast<PresetsContainer &>( Parameter::getPresets() );
8489
pr.clear();
8590
if( !m_namesToParameters.size() )
8691
{
8792
return pr;
8893
}
8994

9095
// get a references for each child preset map.
91-
// we only want to call presets() once for
96+
// we only want to call getPresets() once for
9297
// each child as the map returned may change between calls.
9398
vector<const PresetsContainer *> childPresets;
9499
for( size_t i=0; i<m_parameters.size(); i++ )
95100
{
96-
childPresets.push_back( &(m_parameters[i]->presets()) );
101+
childPresets.push_back( &(m_parameters[i]->getPresets()) );
97102
}
98103

99104
// find the intersection of all the child preset names
@@ -146,6 +151,15 @@ const Parameter::PresetsContainer &CompoundParameter::presets() const
146151
return pr;
147152
}
148153

154+
void CompoundParameter::setPresets( const PresetsContainer &presets )
155+
{
156+
if (m_adoptChildPresets)
157+
{
158+
throw Exception( "CompoundParameter cannot override presets when initialized with adoptChildPresets to true.");
159+
}
160+
Parameter::setPresets(presets);
161+
}
162+
149163
bool CompoundParameter::presetsOnly() const
150164
{
151165
if( !m_adoptChildPresets || !m_parameters.size() )

src/IECore/Parameter.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,10 +92,20 @@ const Object *Parameter::defaultValue() const
9292
}
9393

9494
const Parameter::PresetsContainer &Parameter::presets() const
95+
{
96+
return getPresets();
97+
}
98+
99+
const Parameter::PresetsContainer &Parameter::getPresets() const
95100
{
96101
return m_presets;
97102
}
98103

104+
void Parameter::setPresets( const PresetsContainer &presets )
105+
{
106+
m_presets = presets;
107+
}
108+
99109
bool Parameter::presetsOnly() const
100110
{
101111
return m_presetsOnly;
@@ -145,7 +155,7 @@ bool Parameter::valueValid( const Object *value, std::string *reason ) const
145155
{
146156
return true;
147157
}
148-
const PresetsContainer &pr = presets();
158+
const PresetsContainer &pr = getPresets();
149159
for( PresetsContainer::const_iterator it = pr.begin(); it!=pr.end(); it++ )
150160
{
151161
if( it->second->isEqualTo( value ) )
@@ -201,7 +211,7 @@ void Parameter::setValidatedValue( ObjectPtr value )
201211

202212
void Parameter::setValue( const std::string &presetName )
203213
{
204-
const PresetsContainer &pr = presets();
214+
const PresetsContainer &pr = getPresets();
205215

206216
PresetsContainer::const_iterator it;
207217
for( it=pr.begin(); it != pr.end(); it++ )
@@ -251,7 +261,7 @@ std::string Parameter::getCurrentPresetName() const
251261
// didn't have to do a copy of the value. but that breaks with CompoundParameter
252262
// as it builds the value dynamically in getValue().
253263
const Object *currentValue = getValue();
254-
const PresetsContainer &pr = presets();
264+
const PresetsContainer &pr = getPresets();
255265
PresetsContainer::const_iterator it;
256266
for( it=pr.begin(); it!=pr.end(); it++ )
257267
{

src/IECoreMaya/FromMayaMeshConverter.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -601,10 +601,10 @@ IECore::PrimitivePtr FromMayaMeshConverter::doPrimitiveConversion( MFnMesh &fnMe
601601
unsigned int interpolationIndex = interpolationPlug.asInt(MDGContext::fsNormal, &st);
602602
if ( st )
603603
{
604-
if ( interpolationIndex < m_interpolation->presets().size() - 1 )
604+
if ( interpolationIndex < m_interpolation->getPresets().size() - 1 )
605605
{
606606
// convert interpolation index to the preset value
607-
interpolation = staticPointerCast< StringData >( m_interpolation->presets()[interpolationIndex].second )->readable();
607+
interpolation = staticPointerCast< StringData >( m_interpolation->getPresets()[interpolationIndex].second )->readable();
608608
}
609609
else
610610
{

src/IECoreMaya/ToMayaMeshConverter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ bool ToMayaMeshConverter::setMeshInterpolationAttribute( MObject &object, std::s
495495
int interpolationValue = 0;
496496

497497
FromMayaMeshConverter fromMaya(object);
498-
const IECore::Parameter::PresetsContainer &presets = fromMaya.interpolationParameter()->presets();
498+
const IECore::Parameter::PresetsContainer &presets = fromMaya.interpolationParameter()->getPresets();
499499
IECore::Parameter::PresetsContainer::const_iterator it;
500500

501501
if ( interpolation != "default" )

src/IECoreNuke/PresetsOnlyParameterHandler.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//////////////////////////////////////////////////////////////////////////
22
//
3-
// Copyright (c) 2010-2011, Image Engine Design Inc. All rights reserved.
3+
// Copyright (c) 2010-2013, Image Engine Design Inc. All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without
66
// modification, are permitted provided that the following conditions are
@@ -49,7 +49,8 @@ void PresetsOnlyParameterHandler::knobs( const IECore::Parameter *parameter, con
4949
if( f.makeKnobs() )
5050
{
5151
m_names.clear();
52-
for( Parameter::PresetsContainer::const_iterator it = parameter->presets().begin(); it!=parameter->presets().end(); it++ )
52+
const Parameter::PresetsContainer &presets = parameter->getPresets();
53+
for( Parameter::PresetsContainer::const_iterator it = presets.begin(); it!=presets.end(); it++ )
5354
{
5455
if( it->second->isEqualTo( parameter->defaultValue() ) )
5556
{
@@ -79,12 +80,12 @@ void PresetsOnlyParameterHandler::setParameterValue( IECore::Parameter *paramete
7980
{
8081
presetIndex = (int)m_knob->get_value();
8182
}
82-
parameter->setValue( parameter->presets()[m_storage].second );
83+
parameter->setValue( parameter->getPresets()[m_storage].second );
8384
}
8485

8586
void PresetsOnlyParameterHandler::setKnobValue( const IECore::Parameter *parameter )
8687
{
87-
const Parameter::PresetsContainer &presets = parameter->presets();
88+
const Parameter::PresetsContainer &presets = parameter->getPresets();
8889
std::string currentPresetName = parameter->getCurrentPresetName();
8990
size_t presetIndex = 0;
9091
for( Parameter::PresetsContainer::const_iterator it = presets.begin(); it!=presets.end(); it++, presetIndex++ )

src/IECorePython/ParameterBinding.cpp

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//////////////////////////////////////////////////////////////////////////
22
//
3-
// Copyright (c) 2007-2010, Image Engine Design Inc. All rights reserved.
3+
// Copyright (c) 2007-2013, Image Engine Design Inc. All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without
66
// modification, are permitted provided that the following conditions are
@@ -75,21 +75,26 @@ static void validate( Parameter &that, ObjectPtr value )
7575
that.validate( value.get() );
7676
}
7777

78-
static dict presets( Parameter &that )
78+
static dict getPresets( Parameter &that )
7979
{
8080
dict result;
81-
const Parameter::PresetsContainer &p = that.presets();
81+
const Parameter::PresetsContainer &p = that.getPresets();
8282
for( Parameter::PresetsContainer::const_iterator it=p.begin(); it!=p.end(); it++ )
8383
{
8484
result[it->first] = it->second->copy();
8585
}
8686
return result;
8787
}
8888

89+
static void setPresets( Parameter &p, const object &presets )
90+
{
91+
p.setPresets( parameterPresets<Parameter::PresetsContainer>( presets ) );
92+
}
93+
8994
static boost::python::tuple presetNames( const Parameter &that )
9095
{
9196
boost::python::list result;
92-
const Parameter::PresetsContainer &p = that.presets();
97+
const Parameter::PresetsContainer &p = that.getPresets();
9398
for( Parameter::PresetsContainer::const_iterator it=p.begin(); it!=p.end(); it++ )
9499
{
95100
result.append( it->first );
@@ -100,7 +105,7 @@ static boost::python::tuple presetNames( const Parameter &that )
100105
static boost::python::tuple presetValues( const Parameter &that )
101106
{
102107
boost::python::list result;
103-
const Parameter::PresetsContainer &p = that.presets();
108+
const Parameter::PresetsContainer &p = that.getPresets();
104109
for( Parameter::PresetsContainer::const_iterator it=p.begin(); it!=p.end(); it++ )
105110
{
106111
result.append( it->second->copy() );
@@ -161,7 +166,9 @@ void bindParameter()
161166
.def( "validate", (void (Parameter::*)() const)&Parameter::validate )
162167
.def( "validate", &validate )
163168
.add_property( "presetsOnly", &Parameter::presetsOnly )
164-
.def( "presets", &presets, "Returns a dictionary containing presets for the parameter." )
169+
.def( "presets", &getPresets, "Deprecated function. Use getPresets() instead." )
170+
.def( "getPresets", &getPresets, "Returns a dictionary containing presets for the parameter." )
171+
.def( "setPresets", &setPresets, "Sets the presets for the parameter from a dictionary." )
165172
.def( "presetNames", &presetNames, "Returns a tuple containing the names of all presets for the parameter." )
166173
.def( "presetValues", &presetValues, "Returns a tuple containing the values of all presets for the parameter." )
167174
.def( "userData", &userData )

test/IECore/CompoundParameterTest.py

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def testPresets( self ) :
179179

180180
self.assertEqual( p.presetsOnly, True )
181181

182-
pr = p.presets()
182+
pr = p.getPresets()
183183
self.assertEqual( len( pr ), 3 )
184184
self.assert_( "one" in pr.keys() )
185185
self.assert_( "two" in pr.keys() )
@@ -191,6 +191,7 @@ def testPresets( self ) :
191191

192192
p.setValue( "four" )
193193
self.assertEqual( p.getCurrentPresetName(), "four" )
194+
self.assertRaises( RuntimeError, p.setPresets, [] ) # CompoundParameter created with adoptChildPresets=True does not allow overriding presets
194195

195196
p = CompoundParameter(
196197
name = "c",
@@ -220,7 +221,7 @@ def testPresets( self ) :
220221
)
221222

222223
self.assertEqual( p.presetsOnly, False )
223-
self.assertEqual( len( p.presets() ), 0 )
224+
self.assertEqual( len( p.getPresets() ), 0 )
224225

225226
def testLateValidation( self ) :
226227

@@ -346,18 +347,18 @@ def testAddParametersPresets( self ) :
346347
members = []
347348
)
348349

349-
self.assertEqual( p.presets(), {} )
350+
self.assertEqual( p.getPresets(), {} )
350351

351352
p.addParameter( IntParameter( name = "i", description = "d", defaultValue = 10, presets = ( ( "one", 1 ), ( "two", 2 ) ) ) )
352353

353-
self.assertEqual( len( p.presets() ), 2 )
354-
self.assertEqual( p.presets(), { "one" : CompoundObject( { "i" : IntData( 1 ) } ), "two" : CompoundObject( { "i" : IntData( 2 ) } ) } )
354+
self.assertEqual( len( p.getPresets() ), 2 )
355+
self.assertEqual( p.getPresets(), { "one" : CompoundObject( { "i" : IntData( 1 ) } ), "two" : CompoundObject( { "i" : IntData( 2 ) } ) } )
355356

356357
fParam = FloatParameter( name = "f", description = "d", defaultValue = 20, presets = ( ( "one", 1 ), ) )
357358
p.addParameter( fParam )
358359

359-
self.assertEqual( len( p.presets() ), 1 )
360-
self.assertEqual( p.presets(), { "one" : CompoundObject( { "i" : IntData( 1 ), "f" : FloatData( 1 ) } ) } )
360+
self.assertEqual( len( p.getPresets() ), 1 )
361+
self.assertEqual( p.getPresets(), { "one" : CompoundObject( { "i" : IntData( 1 ), "f" : FloatData( 1 ) } ) } )
361362

362363
p.insertParameter( IntParameter( name = "x", description = "x", defaultValue = 10 ), fParam )
363364
self.assertEqual( p.keys(), [ "i", "x", "f" ] )
@@ -586,7 +587,7 @@ def testAdoptChildPresets( self ) :
586587
],
587588
)
588589

589-
self.assertEqual( len( c.presets() ), 2 )
590+
self.assertEqual( len( c.getPresets() ), 2 )
590591
self.assertEqual( c.presetsOnly, True )
591592

592593
# no adoption of presets
@@ -618,7 +619,7 @@ def testAdoptChildPresets( self ) :
618619
adoptChildPresets = False,
619620
)
620621

621-
self.assertEqual( len( c.presets() ), 0 )
622+
self.assertEqual( len( c.getPresets() ), 0 )
622623
self.assertEqual( c.presetsOnly, False )
623624

624625
# no adoption of presets without use of keyword parameters
@@ -652,10 +653,35 @@ def testAdoptChildPresets( self ) :
652653
False,
653654
)
654655

655-
self.assertEqual( len( c.presets() ), 0 )
656+
self.assertEqual( len( c.getPresets() ), 0 )
656657
self.assertEqual( c.presetsOnly, False )
657658
self.assertEqual( c.userData()["ud"].value, 10 )
658659

660+
# when adoptChildPresets we can also set presets explicitly...
661+
c['a'].setValue("one")
662+
c['b'].setValue("two")
663+
p1 = c.getValue().copy()
664+
c['a'].setValue("two")
665+
c['b'].setValue("one")
666+
p2 = c.getValue().copy()
667+
c.setValue( c.defaultValue )
668+
669+
c.setPresets(
670+
[
671+
( "p1", p1 ),
672+
( "p2", p2 ),
673+
]
674+
)
675+
pr = c.getPresets()
676+
self.assertEqual( len( pr ), 2 )
677+
self.assertEqual( pr["p1"], p1 )
678+
self.assertEqual( pr["p2"], p2 )
679+
self.assertEqual( c.presetNames(), ( "p1", "p2" ) )
680+
c.setValue("p1")
681+
self.assertEqual( c.getValue(), p1 )
682+
c.setValue("p2")
683+
self.assertEqual( c.getValue(), p2 )
684+
659685
def testDerivingInPython( self ) :
660686

661687
class DerivedCompoundParameter( CompoundParameter ) :

test/IECore/EXRImageWriter.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -223,11 +223,11 @@ def testCompressionParameter( self ):
223223
img = r.read()
224224

225225
w = Writer.create( img, "test/IECore/data/exrFiles/output.exr" )
226-
w['compression'].setValue( w['compression'].presets()['zip'] )
226+
w['compression'].setValue( w['compression'].getPresets()['zip'] )
227227
w.write()
228228

229229
w = EXRImageWriter()
230-
w['compression'].setValue( w['compression'].presets()['zip'] )
230+
w['compression'].setValue( w['compression'].getPresets()['zip'] )
231231

232232
def testBlindDataToHeader( self ) :
233233

0 commit comments

Comments
 (0)