@@ -270,6 +270,30 @@ bool MLClient::refreshModelsAndKnobsFromServer(std::string& errorMsg)
270
270
_chosenModel = 0 ;
271
271
}
272
272
273
+ // We try to select the model saved in the serial serialiseKnob if any.
274
+ bool restoreModel = false ;
275
+ MLClientModelKnob* modelKnob = nullptr ;
276
+ DD::Image::Knob* k = knob (" serialiseKnob" );
277
+ if (k != nullptr ) {
278
+ modelKnob = dynamic_cast <MLClientModelKnob*>(k);
279
+ if (modelKnob != nullptr ) {
280
+ std::string modelLabel = modelKnob->getModel ();
281
+ if (modelLabel != " " ) {
282
+ const std::vector<std::string>& models = pSelectModelEnum->menu ();
283
+ int i = 0 ;
284
+ for (auto & m : models) {
285
+ if (m == modelLabel) {
286
+ _chosenModel = i;
287
+ _selectedModelknob->set_value (_chosenModel);
288
+ restoreModel = true ;
289
+ break ;
290
+ }
291
+ i++;
292
+ }
293
+ }
294
+ }
295
+ }
296
+
273
297
// Set member variables to indicate our connections and model set-up succeeded.
274
298
_modelSelected = true ;
275
299
_showDynamic = true ;
@@ -280,10 +304,52 @@ bool MLClient::refreshModelsAndKnobsFromServer(std::string& errorMsg)
280
304
getModelManager ().parseOptions (m);
281
305
}
282
306
setNumNewKnobs (replace_knobs (knob (" models" ), getNumNewKnobs (), addDynamicKnobs, this ->firstOp ()));
307
+
308
+ // If we have restored a model, we also need to restore its parameters
309
+ // now that its knobs have been created,
310
+ if (restoreModel && (modelKnob != nullptr )) {
311
+ for (const std::pair<std::string, std::string>& keyVal: modelKnob->getParameters ()) {
312
+ restoreKnobValue (keyVal.first , keyVal.second );
313
+ }
314
+ }
315
+
283
316
// Return true if control made it here, success.
284
317
return true ;
285
318
}
286
319
320
+ void MLClient::restoreKnobValue (const std::string& knobName, const std::string& value)
321
+ {
322
+ // We look for the corresponding knob
323
+ DD::Image::Knob* paramKnob = knob (knobName.c_str ());
324
+ if (paramKnob != nullptr ) {
325
+ // Is this an animation curve?
326
+ if (value.substr (0 , 6 ) == " {curve" ) {
327
+ // This is a curve, we remove the { }
328
+ std::string curveString = value.substr (1 , value.find (" }" ) - 1 );
329
+ paramKnob->set_animation (curveString.c_str (), 0 );
330
+ }
331
+ else if (value.substr (0 , 1 ) == " {" ) {
332
+ // That's an expression
333
+ std::string expressionString = value.substr (1 , value.find (" }" ) - 1 );
334
+ // If the expression is within double quote, we need to extract it
335
+ if (expressionString.substr (0 , 1 ) == " \" " ) {
336
+ expressionString.erase (0 , 1 );
337
+ expressionString = expressionString.substr (0 , expressionString.find (" \" " ));
338
+ } else {
339
+ // The expression might be followed by keys that we ignore here.
340
+ if (expressionString.find (" " ) != std::string::npos) {
341
+ expressionString = expressionString.substr (0 , expressionString.find (" " ));
342
+ }
343
+ }
344
+ paramKnob->set_expression (expressionString.c_str (), 0 );
345
+ }
346
+ else {
347
+ // That's one value
348
+ paramKnob->set_text (value.c_str ());
349
+ }
350
+ }
351
+ }
352
+
287
353
// ! Return whether we successfully managed to pull model
288
354
// ! info from the server at some time in the past, and the selected model is
289
355
// ! valid.
@@ -537,25 +603,29 @@ void MLClient::addDynamicKnobs(void* p, Knob_Callback f)
537
603
std::string name = ((MLClient *)p)->getModelManager ().getDynamicIntName (i);
538
604
std::string label = ((MLClient *)p)->getModelManager ().getDynamicIntName (i);
539
605
Int_knob (f, ((MLClient *)p)->getModelManager ().getDynamicIntValue (i), name.c_str (), label.c_str ());
606
+ SetFlags (f, Knob::DO_NOT_WRITE);
540
607
Newline (f, " " );
541
608
}
542
609
for (int i = 0 ; i < ((MLClient *)p)->getModelManager ().getNumOfFloats (); i++) {
543
610
std::string name = ((MLClient *)p)->getModelManager ().getDynamicFloatName (i);
544
611
std::string label = ((MLClient *)p)->getModelManager ().getDynamicFloatName (i);
545
612
Float_knob (f, ((MLClient *)p)->getModelManager ().getDynamicFloatValue (i), name.c_str (), label.c_str ());
546
613
ClearFlags (f, Knob::SLIDER);
614
+ SetFlags (f, Knob::DO_NOT_WRITE);
547
615
Newline (f, " " );
548
616
}
549
617
for (int i = 0 ; i < ((MLClient *)p)->getModelManager ().getNumOfBools (); i++) {
550
618
std::string name = ((MLClient *)p)->getModelManager ().getDynamicBoolName (i);
551
619
std::string label = ((MLClient *)p)->getModelManager ().getDynamicBoolName (i);
552
620
Bool_knob (f, ((MLClient *)p)->getModelManager ().getDynamicBoolValue (i), name.c_str (), label.c_str ());
621
+ SetFlags (f, Knob::DO_NOT_WRITE);
553
622
Newline (f, " " );
554
623
}
555
624
for (int i = 0 ; i < ((MLClient *)p)->getModelManager ().getNumOfStrings (); i++) {
556
625
std::string name = ((MLClient *)p)->getModelManager ().getDynamicStringName (i);
557
626
std::string label = ((MLClient *)p)->getModelManager ().getDynamicStringName (i);
558
627
String_knob (f, ((MLClient *)p)->getModelManager ().getDynamicStringValue (i), name.c_str (), label.c_str ());
628
+ SetFlags (f, Knob::DO_NOT_WRITE);
559
629
Newline (f, " " );
560
630
}
561
631
for (int i = 0 ; i < ((MLClient *)p)->getModelManager ().getNumOfButtons (); i++) {
@@ -570,7 +640,11 @@ void MLClient::addDynamicKnobs(void* p, Knob_Callback f)
570
640
void MLClient::knobs (Knob_Callback f)
571
641
{
572
642
String_knob (f, &_host, " host" );
643
+ SetFlags (f, Knob::ALWAYS_SAVE);
644
+
573
645
Int_knob (f, &_port, " port" );
646
+ SetFlags (f, Knob::ALWAYS_SAVE);
647
+
574
648
Button (f, " connect" , " Connect" );
575
649
Divider (f, " " );
576
650
static const char * static_choices[] = {
@@ -581,6 +655,11 @@ void MLClient::knobs(Knob_Callback f)
581
655
}
582
656
SetFlags (f, Knob::SAVE_MENU);
583
657
658
+ // We create a knob to save/load the current state of the dynamic knobs.
659
+ if (f.makeKnobs ()) {
660
+ CustomKnob1 (MLClientModelKnob, f, this , " serialiseKnob" );
661
+ }
662
+
584
663
if (!f.makeKnobs ()) {
585
664
MLClient::addDynamicKnobs (this ->firstOp (), f);
586
665
}
@@ -654,7 +733,6 @@ int MLClient::knob_changed(Knob* knobChanged)
654
733
mlserver::StringAttrib pythonScript = object.values (0 ).string_attributes (0 );
655
734
// Run Python Script in Nuke
656
735
if (pythonScript.values_size () != 0 ) {
657
- std::cout << " cmd=\n " << pythonScript.values (0 ) << " \n " << std::flush;
658
736
script_command (pythonScript.values (0 ).c_str (), true , false );
659
737
script_unlock ();
660
738
}
0 commit comments