Grasshopper

algorithmic modeling for Rhino

Is the SolveInstance() method called everytime an input is changed?

Hi guys,

I am trying to write a component which has a specific method requiring to be run only once. Is the SolveInstance() method called everytime an input is changed? This would explain why my component turns red so often...

Many thanks,

Arthur

Views: 827

Replies to This Discussion

SolveInstance may be called (more than once) during any solution. These are the conditions:

  • If the component has a non-optional input parameter which has no volatile data (i.e. no internal data and no remote data coming in through a wire), then SolveInstance will not be called.
  • If the component is disabled, SolveInstance will not be called.
  • If the component solution phase is Collected or Computed, SolveInstance will not be called.

So assuming the component is all hooked up, enabled and expired (i.e. something happened to it since the last solution that caused it to go stale), then SolveInstance will be called. It may be called once, or many times depending on the access flags of the input parameters and the datatree layout inside those parameters.

If you want to do something one once per solution (i.e., the first time SolveInstance is called within any given solution) then you can use the DA.Iteration field. If the iteration=0 then SolveInstance was just called for the first time within the current solution.

If you only want to do something once (ever!) then you should probably define a class level boolean that keeps track of this:

private bool m_didTheSpecialThing = false;

SolveInstance(blahblah)

{

  if (!m_didTheSpecialThing)

  {

    m_didTheSpecialThing = true;

    DoSpecialThing();

  }

  ...

}

There are also methods to do with the objects movement through documents that you may find interesting to override:

  • AddedToDocument
  • RemovedFromDocument
  • MovedBetweenDocuments
  • DocumentContextChanged

--

David Rutten

david@mcneel.com

Poprad, Slovakia

Thanks a lot David. Very helpful as usual!

The code is working again with an `if(variable==null){do the stuff once}` statement within the SolveInstance() method which allows a specific method to run only once.

RSS

About

Translate

Search

Videos

  • Add Videos
  • View All

© 2024   Created by Scott Davidson.   Powered by

Badges  |  Report an Issue  |  Terms of Service