In the default way to use VIKTOR, the answer to:
Is there a way to force a redraw by code?
is no. Methods belonging to views are triggered by params update for short views, and clicking a button for long views.
But you actually came up with a pretty good workaround yourself, updating the hidden booleanfield will work. I suspect in your test you tried to update it through an actionbutton, and that’s why it did not work. To actually update the params in the same call you can use a SetparamsButton/Method. Was that also what you were doing? Using the snippet below I did manage to trigger a redraw of the view:
import viktor as vkt
class Parametrization(vkt.Parametrization):
floors = vkt.NumberField('Floors', min=5, max=40, step=1)
invisible_bool = vkt.BooleanField('bool', visible=False)
button = vkt.SetParamsButton('Perform setparams', method='setparams_button_method')
class Controller(vkt.Controller):
parametrization = Parametrization
def setparams_button_method(self, params, **kwargs):
return vkt.SetParamsResult({'invisible_bool': True})
@vkt.GeometryView("Geometry", x_axis_to_right=True)
def get_geometry_view(self, params, **kwargs):
# Define Materials
glass = vkt.Material("Glass", color=vkt.Color(150, 150, 255))
facade = vkt.Material("Facade", color=vkt.Color.white())
# Create one floor
width = 30
length = 30
number_of_floors = params.floors
floor_glass = vkt.SquareBeam(width, length, 2, material=glass)
floor_facade = vkt.SquareBeam(width + 1, length + 1, 1, material=facade)
floor_facade.translate([0, 0, 1.5])
# Pattern (duplicate) the floor to create a building
floor = vkt.Group([floor_glass, floor_facade])
building = vkt.LinearPattern(floor, direction=[0, 0, 1], number_of_elements=number_of_floors, spacing=3)
return vkt.GeometryResult(building)
As an aside, this workaround does still leave you with the other problem, this will trigger a redraw whenever the params are updated. Using this workaround (or the long view option) I think in either case will force you to, inside the view method, do some checking and only draw something new when there is an update, and using a stored/cached result if not.
Does that help?