Viktor dynamo plug-in

Hi there,
I’m trying to use Viktor’s nodes in Dynamo.
I would like to retrieve the data I stored.
Here is the very simple code.

import viktor
import viktor.geometry as vg

from viktor.core import ViktorController
from viktor.views import GeometryView, GeometryResult, GeometryAndDataView, GeometryAndDataResult
from viktor.parametrization import Parametrization, NumberField, IntegerField


class LineParameter(Parametrization):
    xDir = NumberField("xDir", default=0.4, step=0.1, suffix="m")
    yDir = NumberField("yDir", default=0.2, step=0.1, suffix="m")
    zDir = NumberField("zDir", default=1, min=1, max=10, step=0.5, suffix="m", variant="slider")
    nDiv = IntegerField("nDiv", default=1, min=1, max=2)

class Controller(ViktorController):
    label = 'First Beam'
    parametrization = LineParameter

    @GeometryView("Pattern", duration_guess=1)
    def line_vis(self, params, **kwargs):
        
        storage  = viktor.core.Storage()
        pt = vg.Vector(0, 0, 0)

        sphere = []
        points = []
        for i in range(5):
            dir = vg.Vector(params.xDir*i, params.yDir*i, params.zDir*i)
            temp_vec = pt + dir
            temp_pt = vg.Point(temp_vec.x, temp_vec.y, temp_vec.z)
            points.append(temp_pt)
            sphere.append(vg.Sphere(temp_pt, 1))
            file = viktor.File.from_data(f'pt:{temp_pt.coordinates}')
            storage.set(f'pt{i}', data=file, scope='workspace')

        #test = storage.get('pt4', scope='workspace')
        #print(test.getvalue())

        return GeometryResult( sphere ) 

the information are stored.

Viktor workspace.

Here is where I am getting F entity, but how do I reach the storage?

What is the best way to store information like points?
Is possible to store a csv file?
Because once I am in dynamo I need a way to read that information.

Thanks for the support.

Hi, it is not possible to access the storage, but I will bump our internal issue. Supported nodes can be found here.

Alternatively, you could use a SetParamsButton to store data on the entity params (e.g. on a HiddenField) itself.

And how I can get the HiddenField from Dyn if the only 2 nodes to get info are LastSaveParams and LastSaveSummery?
Or what you are saying is that at the moment there isn’t a solution to bring data from the app to a dynamo graph?

The LastSavedParams contains all the input fields that are defined. So in case of this HiddenField:

class Parametrization(ViktorParametrization):
    hidden = HiddenField(...)

you can expect its content in Dynamo on dictionary key hidden when using the LastSavedParams node.

Thanks @khameeteman I will try it and report if I have problems but it should be fine.

@khameeteman I tested, but I can’t get the data in Dynamo.
Here is the code I am using it.

import viktor
import viktor.geometry as vg

from viktor.core import ViktorController
from viktor.views import GeometryView, GeometryResult, GeometryAndDataView, GeometryAndDataResult, Label
from viktor.parametrization import Parametrization, NumberField, IntegerField, HiddenField

class LineParameter(Parametrization):
    xDir = NumberField("xDir", default=0.4, step=0.1, suffix="m")
    yDir = NumberField("yDir", default=0.2, step=0.1, suffix="m")
    zDir = NumberField("zDir", default=1, min=1, max=10, step=0.5, suffix="m", variant="slider")
    nDiv = IntegerField("nDiv", default=1, min=1, max=2)
    hidden = HiddenField("hidden")

class Controller(ViktorController):
    label = 'Test'
    parametrization = LineParameter

    @GeometryView("Pattern", duration_guess=1)
    def line_vis(self, params, **kwargs):
        
        storage  = viktor.core.Storage()
        pt = vg.Vector(0, 0, 0)

        sphere = []
        points = []
        for i in range(5):
            dir = vg.Vector(params.xDir*i, params.yDir*i, params.zDir*i)
            temp_vec = pt + dir
            temp_pt = vg.Point(temp_vec.x, temp_vec.y, temp_vec.z)
            points.append(temp_pt.coordinates)
            sphere.append(vg.Sphere(temp_pt, 1))

        params.hidden = np.array2string(np.asarray(points), separator='|')
        print(params.hidden)
        return GeometryResult( sphere ) 

what I received is always the default values.
image

Did you save the entity in the VIKTOR interface?

No I didn’t.
But now I did, the parameters are changed but the hidden field still null
image

There is no record of changes for the hidden field.
image

Like I said, you have to make use of a SetParamsButton, please see User input, fields and buttons - Set params using a button | VIKTOR Documentation

from pathlib import Path
import numpy as np
import viktor
import viktor.geometry as vg

from viktor.core import ViktorController
from viktor.views import GeometryView, GeometryResult
from viktor.result import SetParamsResult
from viktor.parametrization import Parametrization, NumberField, IntegerField, HiddenField, SetParamsButton

class FieldParameters(Parametrization):
    xDir = NumberField("xDir", default=0.4, step=0.1, suffix="m")
    yDir = NumberField("yDir", default=0.2, step=0.1, suffix="m")
    zDir = NumberField("zDir", default=1, min=1, max=10, step=0.5, suffix="m", variant="slider")
    nDiv = IntegerField("nDiv", default=1, min=1, max=2)
    hidden = HiddenField("hidden")
    set_params = SetParamsButton("Set Params for Dyn", method="set_params")

class Controller(ViktorController):
    label = 'Viktor to Dynamo'
    parametrization = FieldParameters

    @GeometryView("Pattern", duration_guess=1)
    def line_vis(self, params, **kwargs):
        
        storage  = viktor.core.Storage()
        start_pt = vg.Vector(0, 0, 0)

        sphere = []
        points = []
        for i in range(5):
            dir = vg.Vector(params.xDir*i, params.yDir*i, params.zDir*i)
            temp_vec = start_pt + dir
            temp_pt = vg.Point(temp_vec.x, temp_vec.y, temp_vec.z)
            points.append(temp_pt.coordinates)
            sphere.append(vg.Sphere(temp_pt, 1))

        file_from_data = viktor.File.from_data(np.array2string(np.asarray(points), separator='|'))
        storage.set('from_data', data=file_from_data, scope='workspace')

        return GeometryResult( sphere, labels=labels) 

    def set_params(self, params, **kwargs):
        storage  = viktor.core.Storage()
        data = storage.get('from_data', scope='workspace')
        dataParam = {"hidden" : data.getvalue()}
        return SetParamsResult(dataParam)

image
Thanks @khameeteman

2 Likes

I have the same issue, I would like to reach Storage by using the dynamo node. The HiddenField solution does not work for me as I use a lot of data, thus the editor slows down too much if I store my data in a HiddenField.

1 Like