Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Port Formula mk3 to 2.80 #2546

Merged
merged 3 commits into from
Sep 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions docs/nodes/number/formula3.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
Formula Node Mk3
================

Functionality
-------------

This node allows one to evaluate (almost) arbitrary Python expressions, using inputs as variables.
It is possible to calculate numeric values, construct lists, tuples, vertices and matrices.

The node allows to evaluate up to 4 formulas for each set of input values.

Expression syntax
-----------------

Syntax being used for formulas is standard Python's syntax for expressions.
For exact syntax definition, please refer to https://docs.python.org/3/reference/expressions.html.

In short, you can use usual mathematical operations (`+`, `-`, `*`, `/`, `**` for power), numbers, variables, parenthesis, and function call, such as `sin(x)`.

One difference with Python's syntax is that you can call only restricted number of Python's functions. Allowed are:

- Functions from math module:
- acos, acosh, asin, asinh, atan, atan2,
atanh, ceil, copysign, cos, cosh, degrees,
erf, erfc, exp, expm1, fabs, factorial, floor,
fmod, frexp, fsum, gamma, hypot, isfinite, isinf,
isnan, ldexp, lgamma, log, log10, log1p, log2, modf,
pow, radians, sin, sinh, sqrt, tan, tanh, trunc;
- Constants from math module: pi, e;
- Additional functions: abs, sign;
- From mathutlis module: Vector, Matrix;
- Python type conversions: tuple, list.

This restriction is for security reasons. However, Python's ecosystem does not guarantee that noone can call some unsafe operations by using some sort of language-level hacks. So, please be warned that usage of this node with JSON definition obtained from unknown or untrusted source can potentially harm your system or data.

Examples of valid expressions are:

* 1.0
* x
* x+1
* 0.75*X + 0.25*Y
* R * sin(phi)

Inputs
------

Set of inputs for this node depends on used formulas. Each variable used in formula becomes one input. If there are no variables used in formula, then this node will have no inputs.

Parameters
----------

This node has the following parameters:

- **Dimensions**. This parameter is available in the N panel only. It defines how many formulas the node will allow to specify and evaluate. Default value is 1. Maximum value is 4.
- **Formula 1** to **Formula 4** input boxes. Formulas theirselve. If no formula is specified, then nothing will be calculated for this dimension. Number of formula input boxes is defined by **Dimensions** parameter.
- **Separate**. If the flag is set, then for each combination of input values, list of values calculated by formula is enclosed in separate list. Usually you will want to uncheck this if you are using only one formula. Usually you will want to check this if you are using more than one formula. Other combinations can be of use in specific cases. Unchecked by default.
- **Wrap**. If checked, then the whole output of the node will be enclosed in additional brackets. Checked by default.

For example, let's consider the following setup:

.. image:: https://user-images.githubusercontent.com/284644/53962080-00c78700-410c-11e9-9563-855fca16537a.png

Then the following combinations of flags are possible:

+-----------+-----------+--------------------+
| Separate | Wrap | Result |
+===========+===========+====================+
| Checked | Checked | [[[1, 3], [2, 4]]] |
+-----------+-----------+--------------------+
| Checked | Unchecked | [[1, 3], [2, 4]] |
+-----------+-----------+--------------------+
| Unchecked | Checked | [[1, 3, 2, 4]] |
+-----------+-----------+--------------------+
| Unchecked | Unchecked | [1, 3, 2, 4] |
+-----------+-----------+--------------------+

Outputs
-------

**Result** - what we got as result.

Usage examples
--------------

.. image:: https://user-images.githubusercontent.com/284644/53965898-dbd71200-4113-11e9-83c7-cb3c7ced8c1e.png

.. image:: https://user-images.githubusercontent.com/284644/53967764-9f0d1a00-4117-11e9-92e3-a047dbd2981b.png

1 change: 1 addition & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@
SvEasingNode
SvMixNumbersNode
Formula2Node
SvFormulaNodeMk3
---
SvGenFibonacci
SvGenExponential
Expand Down
2 changes: 2 additions & 0 deletions nodes/number/formula2.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ class Formula2Node(bpy.types.Node, SverchCustomTreeNode):
base_name = 'n'
multi_socket_type = 'SvStringsSocket'

replacement_nodes = [('SvFormulaNodeMk3', None, None)]

def draw_buttons(self, context, layout):
layout.prop(self, "formula", text="")

Expand Down
Loading