nortikin / sverchok

Sverchok
http://nortikin.github.io/sverchok/
GNU General Public License v3.0
2.26k stars 233 forks source link

Mesh evaluate node / Mesh Expression #1304

Closed portnov closed 7 years ago

portnov commented 7 years ago

Initial idea. To create useful meshes, it would be nice to get a mesh modelled "by hand", and parametrize it.

Proposed implementation. Step 1: get selected mesh object and store it, for example, in JSON format (dict with "vertices", "edges" and "faces" keys). Step 2: replace some numbers in "vertices" list with variable names. Step 3: load resulting JSON and plug variable values as node inputs.

Initial implementation is in https://github.com/portnov/sverchok/tree/mesh_from_json.

portnov commented 7 years ago

eval_1_screen

zeffii commented 7 years ago

oh cool!! did you check _object_inlite ? stores a single mesh in json/restores from json in gist.

portnov commented 7 years ago

Yes, probably that feature will be needed too.

portnov commented 7 years ago

@zeffii I have a question about evaluating expressions in Profile node and similar (for example, Formula node). Do we really need own parser and evaluator? Can't we use smth like http://effbot.org/zone/librarybook-core-eval.htm ?

zeffii commented 7 years ago

it's up to you @portnov , but try to avoid external modules.. or make a platform independent way to ensure a module's availability if it's not part of std.

portnov commented 7 years ago

@zeffii Python has builtin eval() function, so we could use it with several precautions. But it is possible for "hacker" to use it for some "evil" things. I.e. one can run "rm -rf ~/*" with it. I'm trying to understand, why we implemented own parser in Profile node? Is it for security reasons?

portnov commented 7 years ago

output1

zeffii commented 7 years ago

Write a DSL then @portnov.

we use eval/exec in several nodes, do a search in the repo :) We also discussed the dangers a long time ago.

zeffii commented 7 years ago

why we implemented own parser in Profile node?

profile node, i like writing parsers :) -- plus Profile node isn't real SVG, just similar syntax - meaning some parsing was needed anyway to add features i wanted. The downside might be that adding features can be non-trivial (even for me)

portnov commented 7 years ago

Ok Currently I'm using python's ast+eval for parsing/evaluating expressions, and a simple trick with passing builtins = {} in environment, and restricted number of "safe" functions, to be more or less secure.

enzyme69 commented 7 years ago

@portnov I tried this node. It gives me nothing.

I kind of expect the output of this node to work with Text In JSON.

portnov commented 7 years ago

@enzyme69 please provide json that you tried and it did not work.

Later today I can provide some examples that are working for me.

And yes, this node obviously needs documentation. I hope to write it this week.

portnov commented 7 years ago

@enzyme69 Trivial plane example:


{
  "faces": [
    [
      0,
      1,
      3,
      2
    ]
  ],
  "edges": [
    [
      0,
      2
    ],
    [
      0,
      1
    ],
    [
      1,
      3
    ],
    [
      2,
      3
    ]
  ],
  "vertices": [
    [
      "-Size",
      "-Size",
      0.0
    ],
    [
      "Size",
      "-Size",
      0.0
    ],
    [
      "-Size",
      "Size",
      0.0
    ],
    [
      "Size",
      "Size",
      0.0
    ]
  ]
}

Nontrivial example:

https://gist.github.com/zeffii/e1652e817f5ca22a07752c3464752c95

enzyme69 commented 7 years ago

Any JSOn did not work in Viewer, I tried, not sure if I should pull git.

portnov commented 7 years ago

@enzyme69 Maybe the problem is that you did not connect anything to inputs? The node currently needs all inputs (variables) to be connected; you can for example plug Float nodes.

nortikin commented 7 years ago

Спасибо за узел, не хватало его Thanks for Uzel, ne hvatalo ego

zeffii commented 7 years ago

the node should really be in extended generators, i think..

also, to make it clearer to the user that the node has indeed read the json, i think it makes sense to wrap the updateNode function attached to self.filename , to do a self.adjust_inputs() -- else sockets aren't added until the user clicks a socket somewhere..

zeffii commented 7 years ago
  1. insert the json into the text editor
  2. locate it with the file selector on the MeshEvalNode
  3. click any socket.
  4. correct socket-set will appear, you can now connect the output of the node to viz
enzyme69 commented 7 years ago

Need at least 1 variable?

I can make Live Noding on this node. Powerful!

screen shot 2017-03-16 at 22 52 45

enzyme69 commented 7 years ago

However, it would be nice if we can have this Mesh Evaluate Node to pass Mesh Data even without any attribute. Cleaner.

But this is very cool and allows for lots of possibilities I cannot think of.

enzyme69 commented 7 years ago

It's also kind of unfortunate that it cannot accept "var" + 1, or ("var" * -1).

screen shot 2017-03-17 at 10 41 46

I think this kind of Mesh Eval node can become like Intermediate node.

portnov commented 7 years ago

@enzyme69 It accepts expressions, but they all should be enclosed in quotes. For example, "-1*a".

enzyme69 commented 7 years ago

MeshEval node, when exported out as GIST and imported back in, The Text is no longer pretty JSON, it become a single line text, but I think that's ok for now.

screen shot 2017-03-17 at 18 52 19

zeffii commented 7 years ago

@enzyme69 https://github.com/nortikin/sverchok/blob/master/nodes/generator/mesh_eval.py#L251

read the docs for json.dumps

zeffii commented 7 years ago

@portnov , you probably want store the mesh's json in Gists exactly as it appears in the datablock (regarding newlines), ..so flattened string, and restore from Gist using the string too.

enzyme69 commented 7 years ago

@zeffii @portnov I tested both approach Objects In Lite - Text Out, and also MeshEvalNode. What would be nice:

Thanks!

zeffii commented 7 years ago

All the mentioned nodes work complementary and have their own usecase.. the json edited with mesh eval doesn't make sense to load it with textin .

I do agree mesheval should not modify formatting when stored/retrieved in a gist., maybe even have a nicer initial formatting to begin with.

portnov commented 7 years ago

Well, I'll try to take notes in this thread into account and provide some fixes this weekend.

portnov commented 7 years ago

Please see #1387. :)

portnov commented 7 years ago

Dodecahedron: https://gist.github.com/portnov/79630a99cc5d6fd549ef0124c4d243f1

So this node can be useful also to make primitives like this and share them. So for example we do not actually need "dodecahedron" node anymore :)

zeffii commented 7 years ago

makes me wish that items in the defaults could reference other defaults already defined..

{
  "defaults": {
     "A": 0.5773502588272095,
     "B": 0.35682210326194763,
     "C": 0.9341723322868347,
     "amp": 1.0,
  },
  "vertices": [
    [  "A * amp",   "A * amp",    "A * amp"  ],
    [  "A * amp",   "A * amp",   "-A * amp"  ],
    [  "A * amp",  "-A * amp",    "A * amp"  ],
    [  "A * amp",  "-A * amp",   "-A * amp"  ],
    [ "-A * amp",   "A * amp",    "A * amp"  ],
    [ "-A * amp",   "A * amp",   "-A * amp"  ],
    [ "-A * amp",  "-A * amp",    "A * amp"  ],
    [ "-A * amp",  "-A * amp",   "-A * amp"  ],
    [  "B * amp",   "C * amp",    0.0        ],
    [ "-B * amp",   "C * amp",    0.0        ],
    [  "B * amp",  "-C * amp",    0.0        ],
    [ "-B * amp",  "-C * amp",    0.0        ],
    [  "C * amp",   0.0,          "B * amp"  ],
    [  "C * amp",   0.0,         "-B * amp"  ],
    [ "-C * amp",   0.0,          "B * amp"  ],
    [ "-C * amp",   0.0,         "-B * amp"  ],
    [  0.0,         "B * amp",    "C * amp"  ],
    [  0.0,        "-B * amp",    "C * amp"  ],
    [  0.0,         "B * amp",   "-C * amp"  ],
    [  0.0,        "-B * amp",   "-C * amp"  ]
  ],
  "edges": [  ],
  "faces": [
    [0,12,13,1,8],
    [0,8,9,4,16],
    [0,16,17,2,12],
    [8,1,18,5,9],
    [9,5,15,14,4],
    [4,14,6,17,16],
    [2,17,6,11,10],
    [2,10,3,13,12],
    [3,19,18,1,13],
    [5,18,19,7,15],
    [3,10,11,7,19],
    [6,14,15,7,11]
  ]
}
zeffii commented 7 years ago

can close this unless there's something else to be added

rdrgodavi commented 5 years ago

hey @enzyme69 , how can im trying to open your sushi vault ( https://github.com/enzyme69/blendersushi ) , but the .json doesnt open with the Mesh Expression node. How can i import those .json files corectly in SV and AN?

enzyme69 commented 5 years ago

@rdrgodavi which vault example? We can store Mesh as Mesh Expression and modify it by hand on the code, but which example is this?

JSON can be anything for Sverchok: preset, or maybe something you can load as node tree....

Try also posting this question at my Github?

Screen Shot 2019-05-20 at 14 51 11