nortikin / sverchok

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

Extra Objects into Script Nodes #1005

Closed enzyme69 closed 7 years ago

enzyme69 commented 7 years ago

Originally I was asking a trivial question: Why can't we have these Extra Objects operators as Node and can't it be live and auto update the generation at any time? https://github.com/nortikin/sverchok/issues/1003

I am attempting to just copy pasting the python code, credit to original authors of this Extra Curves Add-On.

🐌

''' bl_info = { "name": "Curveaceous Galore!", "author": "Jimmy Hazevoet, testscreenings", "version": (0, 2), "blender": (2, 59, 0), "location": "View3D > Add > Curve", "description": "Adds many different types of Curves", "warning": "", # used for warning icon and text in addons panel "wiki_url": "http://wiki.blender.org/index.php/Extensions:2.6/Py/" "Scripts/Curve/Curves_Galore", "category": "Add Curve", } '''

enzyme69 commented 7 years ago

This makes FlowerCurve points.

def sv_main(petals=8, innerradius=0.5, outerradius=1.0, petalwidth=2.0):

    in_sockets = [
        ['s', 'Petals',  petals],
        ['s', 'Inner Radius',  innerradius],
        ['s', 'Outer Radius',  outerradius],
        ['s', 'Petal Width',  petalwidth]
    ]

    from math import sin, cos, radians, pi, sqrt
    from mathutils import Vector, Euler

    #Verts = []
    #verts_new = Verts.append

    newpoints = []

    step = (2.0/(petals))
    pet = (step/pi*2)*petalwidth
    i = 0
    while i < petals:
        t = (i*step)
        x1 = cos(t*pi-(pi/petals))*innerradius
        y1 = sin(t*pi-(pi/petals))*innerradius
        newpoints.append([x1, y1, 0])
        x2 = cos(t*pi-pet)*outerradius
        y2 = sin(t*pi-pet)*outerradius
        newpoints.append([x2, y2, 0])
        x3 = cos(t*pi+pet)*outerradius
        y3 = sin(t*pi+pet)*outerradius
        newpoints.append([x3, y3, 0])
        i += 1

    out_sockets = [
        ['v', 'Verts', [newpoints]]
    ]

    return in_sockets, out_sockets
enzyme69 commented 7 years ago

screen shot 2016-11-30 at 21 47 24

enzyme69 commented 7 years ago

@zeffii Any tips to vectorize node like above? I want it to work with your Random Num Gen node.

enzyme69 commented 7 years ago

A quick hack is to Monad it 💃

screen shot 2016-11-30 at 21 58 50

zeffii commented 7 years ago

before you expend too much energy you can import these functions directly from the source

"""
in petals       s d=8    n=2 
in innerradius  s d=0.5  n=2 
in outerradius  s d=1.0  n=2
in petalwidth   s d=2.0  n=2
out points      v
"""

from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve

points = [FlowerCurve(petals, innerradius, outerradius, petalwidth)]
enzyme69 commented 7 years ago

Face palms... thanks @zeffii that's amazing!!

enzyme69 commented 7 years ago

This makes Splat Curve.

def sv_main(sides=24, scale=1.0, seed=0, basis=0, radius=1.0):

    in_sockets = [
        ['s', 'Sides',  sides],
        ['s', 'Scale',  scale],
        ['s', 'Seed',  seed],
        ['s', 'Basis',  basis],
        ['s', 'Radius',  radius]
    ]

    from math import sin, cos, radians, pi, sqrt
    from mathutils import Vector, Euler
    import mathutils.noise as Noise

    # Generate random number:
    def randnum(low=0.0, high=1.0, seed=0):
        """
        randnum( low=0.0, high=1.0, seed=0 )

        Create random number

            Parameters:
                low - lower range
                    (type=float)
                high - higher range
                    (type=float)
                seed - the random seed number, if seed is 0, the current time will be used instead
                    (type=int)
            Returns:
                a random number
                    (type=float)
        """

        Noise.seed_set(seed)
        rnum = Noise.random()
        rnum = rnum*(high-low)
        rnum = rnum+low
        return rnum

    # Make some noise:
    def vTurbNoise(x, y, z, iScale=0.25, Size=1.0, Depth=6, Hard=0, Basis=0, Seed=0):
        """
        vTurbNoise((x,y,z), iScale=0.25, Size=1.0, Depth=6, Hard=0, Basis=0, Seed=0 )

        Create randomised vTurbulence noise

            Parameters:
                xyz - (x,y,z) float values.
                    (type=3-float tuple)
                iScale - noise intensity scale
                    (type=float)
                Size - noise size
                    (type=float)
                Depth - number of noise values added.
                    (type=int)
                Hard - noise hardness: 0 - soft noise; 1 - hard noise
                    (type=int)
                basis - type of noise used for turbulence
                    (type=int)
                Seed - the random seed number, if seed is 0, the current time will be used instead
                    (type=int)
            Returns:
                the generated turbulence vector.
                    (type=3-float list)
        """
        rand = randnum(-100, 100, Seed)
        if Basis == 9:
            Basis = 14
        vTurb = Noise.turbulence_vector((x/Size+rand, y/Size+rand, z/Size+rand), Depth, Hard, Basis)
        tx = vTurb[0]*iScale
        ty = vTurb[1]*iScale
        tz = vTurb[2]*iScale
        return tx, ty, tz

    #Verts = []
    #verts_new = Verts.append

    newpoints = []

    step = (2.0/(sides))
    i = 0
    while i < sides:
        t = (i*step)
        turb = vTurbNoise(t, t, t, 1.0, scale, 6, 0, basis, seed)
        turb = turb[2] * 0.5 + 0.5
        x = sin(t*pi)*radius * turb
        y = cos(t*pi)*radius * turb
        newpoints.append([x, y, 0])
        i += 1

    out_sockets = [
        ['v', 'Verts', [newpoints]]
    ]

    return in_sockets, out_sockets
enzyme69 commented 7 years ago

Something funky happened when I connect the vectorized data into UV Connection. Point seems ok.

screen shot 2016-11-30 at 22 19 00

zeffii commented 7 years ago

to vectorize the SN, and SNLite scripts, isn't too hard, you just go one nested level up

"""
in petals       s d=8    n=1 
in innerradius  s d=0.5  n=1 
in outerradius  s d=1.0  n=1
in petalwidth   s d=2.0  n=1
out points      v
"""

from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve
from sverchok.data_structure import match_long_repeat

def listify(data):
    print(data)
    if isinstance(data, (int, float)):
        data = [data]
    return data

petals = listify(petals)
innerradius = listify(innerradius)
outerradius = listify(outerradius)
petalwidth = listify(petalwidth)

# match longest
params = match_long_repeat([petals, innerradius, outerradius, petalwidth])

points = []
add_point_list = points.append
for param_set in zip(*params):
    add_point_list(FlowerCurve(*param_set))
enzyme69 commented 7 years ago

@zeffii What is going on when SN Lite does not update when I change the params?

zeffii commented 7 years ago

also, a more reusable general vectorize function

"""
in petals       s d=8    n=1 
in innerradius  s d=0.5  n=1 
in outerradius  s d=1.0  n=1
in petalwidth   s d=2.0  n=1
out points      v
"""

from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve
from sverchok.data_structure import match_long_repeat

def vectorize(all_data):

    def listify(data):
        print(data)
        if isinstance(data, (int, float)):
            data = [data]
        return data

    for idx, d in enumerate(all_data):
        all_data[idx] = listify(d)

    return match_long_repeat(all_data)

params = vectorize([petals, innerradius, outerradius, petalwidth])

points = []
add_point_list = points.append
for param_set in zip(*params):
    add_point_list(FlowerCurve(*param_set))
zeffii commented 7 years ago

@zeffii What is going on when SN Lite does not update when I change the params?

you'd have to share the script, also..look at the terminal/console messages..

enzyme69 commented 7 years ago

My code failed to update: 😞

"""
in theeth       s d=8    n=2 
in innerradius  s d=0.8  n=2 
in middleradius  s d=0.95  n=2
in outerradius   s d=1.0  n=2
in bevel s d=0.5 n=2
out points      v
"""

#from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve
#points = [FlowerCurve(petals, innerradius, outerradius, petalwidth)]

from add_curve_extra_objects.add_curve_aceous_galore import CogCurve

points = [CogCurve( theeth=8, innerradius=0.8, middleradius=0.95, outerradius=1.0, bevel=0.5 )]
zeffii commented 7 years ago

even shorter..

"""
in petals       s d=8    n=1 
in innerradius  s d=0.5  n=1 
in outerradius  s d=1.0  n=1
in petalwidth   s d=2.0  n=1
out points      v
"""

from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve
from sverchok.data_structure import match_long_repeat

def vectorize(all_data):

    def listify(data):
        print(data)
        if isinstance(data, (int, float)):
            data = [data]
        return data

    for idx, d in enumerate(all_data):
        all_data[idx] = listify(d)

    return match_long_repeat(all_data)

params = vectorize([petals, innerradius, outerradius, petalwidth])

points = [FlowerCurve(*param_set) for param_set in zip(*params)]
enzyme69 commented 7 years ago

Whoa...

Didn't we need to use .=

enzyme69 commented 7 years ago

Potential bug or I need to Flip or somethin? screen shot 2016-11-30 at 22 38 39

zeffii commented 7 years ago

you are calling the Cog function with the same parameters, regardless of the slider values. look carefully.

points = [CogCurve(theeth=8, innerradius=0.8, middleradius=0.95, outerradius=1.0, bevel=0.5)]
enzyme69 commented 7 years ago

Face Palms x 2. Thanks man.

Now how about the UV Connection is that a new topic?

zeffii commented 7 years ago

that's a viewer draw artefact, i think... turn on tesselate ngon in the sidebar of VDMK2

zeffii commented 7 years ago

by default we don't tesselate because it's faster, but if you want to throw ~rubbish~ irregular ngon geometry at it, i added tesselate to give it a chance..

zeffii commented 7 years ago

image

enzyme69 commented 7 years ago

Yes, the poly face works for a single output, but not multiple.

enzyme69 commented 7 years ago

screen shot 2016-11-30 at 22 52 02 The edge seems to be alright

enzyme69 commented 7 years ago

screen shot 2016-11-30 at 22 53 07

zeffii commented 7 years ago

send over the gist :)

enzyme69 commented 7 years ago

Roger that: https://gist.github.com/52694d064ba45125131573d8a2960525

Your vectorized SNL_FlowerCurve.py

I will have to study the Python for Vectorizing. But may use Monad for now if I talk about this at Live Noding.

zeffii commented 7 years ago

yep, that is weird...

zeffii commented 7 years ago

I think that's a VDMK2 bug,, reusing older vertex indices..

enzyme69 commented 7 years ago

I see, I did encounter that situation a few times, and always avoiding it.

zeffii commented 7 years ago

ooh not a bug..

enzyme69 commented 7 years ago

Didn't work with BMesh MK2 either btw. Ehh. you figure out something?

zeffii commented 7 years ago

uv connect is expecting the same number of verts for each vert list.

enzyme69 commented 7 years ago

Oh.. it needs enhancement 😗

enzyme69 commented 7 years ago

screen shot 2016-11-30 at 23 10 50

This is similar to Iterate Matrix case. I still don't get it. It tends to over vectorize.

zeffii commented 7 years ago

no, not really,

you should be outputting 'face' data too for each pointset in points..

enzyme69 commented 7 years ago

Ok... it needs to be in the SN code?

zeffii commented 7 years ago

yeah, and whenever you throw 'random' input at algorithms that you don't know you should be using small values first, and use the index viewer to see where the verts are (sequence and location is important, and not always obvious)

"""
in petals       s d=8    n=1 
in innerradius  s d=0.5  n=1 
in outerradius  s d=1.0  n=1
in petalwidth   s d=2.0  n=1
out points      v
out faces       s
"""

from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve
from sverchok.data_structure import match_long_repeat

def vectorize(all_data):

    def listify(data):
        print(data)
        if isinstance(data, (int, float)):
            data = [data]
        return data

    for idx, d in enumerate(all_data):
        all_data[idx] = listify(d)

    return match_long_repeat(all_data)

params = vectorize([petals, innerradius, outerradius, petalwidth])

points = [FlowerCurve(*param_set) for param_set in zip(*params)]

faces = [[list(range(len(n)))] for n in points]
zeffii commented 7 years ago

like this ; https://gist.github.com/a7609483ac63f7f96d82662e1ba8a6eb

it's impossible to sanely be able to correct for input like this: image

zeffii commented 7 years ago

The error here is with the User i'm afraid. but interesting to debug your own logic :)

zeffii commented 7 years ago

but the UV Connect does seem to choke on that particular kind of input... let me think about this.

enzyme69 commented 7 years ago

I just made a Live Noding video about this :D

~UV connection seems to also choke in generating "edges" for this guy:~

(fixed December 1)

"""
in number       s  d=100    n=2
in height       s  d=2.0    n=2
in startangle   s  d=0.0    n=2
in endangle     s  d=360.0  n=2
in width        s  d=1.0    n=2
in a            s  d=0.0    n=2
in b            s  d=0.0    n=2
out points      v
"""

from add_curve_extra_objects.add_curve_aceous_galore import HelixCurve

points = [HelixCurve(number, height, startangle, endangle, width, a, b)]
zeffii commented 7 years ago

btw, a simplified UV connect SNLite version..

"""
in vert_lists v n=[] d=0
out polygons s
"""

polygons = []
for vlist in vert_lists:
    polygons.append([list(range(len(vlist)))])

expects each sublist of vertices to represent an individual polygon. Notice here the randomized input is carefully limited to not produce self intersecting polygons.

image

https://gist.github.com/456df3679137d4661c7fcf25d5e7b380

zeffii commented 7 years ago

Scripted Node is more safe?

i haven't needed to use sn1 since creating snlite :) but the more people test it the faster i'll be able to fix weird bugs that I didn't envisage while designing it.

zeffii commented 7 years ago

https://github.com/nortikin/sverchok/pull/1007 ( you should update blender / restart )

enzyme69 commented 7 years ago

Zeff you are genius man. Thanks for spotting weird error like min max in random.

currently on mobile but will study all this once I am on laptop

On Thu., 1 Dec. 2016 at 9:05 am, Dealga McArdle notifications@github.com wrote:

1007 https://github.com/nortikin/sverchok/pull/1007

( you should update blender / restart )

— You are receiving this because you were assigned. Reply to this email directly, view it on GitHub https://github.com/nortikin/sverchok/issues/1005#issuecomment-264010981, or mute the thread https://github.com/notifications/unsubscribe-auth/ADxQL1cLb-T39gIgRclaMvaPM17zuTofks5rDfMkgaJpZM4LADaO .

enzyme69 commented 7 years ago

Will add more results here, but will close :D

zeffii commented 7 years ago

i've added injection of the vectorize function locally to the sn script, and also the list of parameters. So this is possible now. (See change log for info)

"""
in petals       s d=8    n=1 
in innerradius  s d=0.5  n=1 
in outerradius  s d=1.0  n=1
in petalwidth   s d=2.0  n=1
out points      v
inject
"""

from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve

# look . magic! 
params = vectorize(parameters)

points = [FlowerCurve(*param_set) for param_set in zip(*params)]

https://gist.github.com/90a356de608943ac59f3aa98cd5f987e (Extra Curve objects must be enabled i think..oh..apparently not 👍 )

enzyme69 commented 7 years ago

I'll study this as soon as possible! Thanks Zefff!!


On 1 December 2016 at 19:46, Dealga McArdle notifications@github.com wrote:

i've added injection of the 'vectorize' function locally to the sn script, and also the list of parameters. So this is possible now. (See change log for info)

"""in petals s d=8 n=1 in innerradius s d=0.5 n=1 in outerradius s d=1.0 n=1in petalwidth s d=2.0 n=1out points v""" from add_curve_extra_objects.add_curve_aceous_galore import FlowerCurve

look . magic!

params = vectorize(parameters)

points = [FlowerCurve(param_set) for param_set in zip(params)]

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/nortikin/sverchok/issues/1005#issuecomment-264113742, or mute the thread https://github.com/notifications/unsubscribe-auth/ADxQL3fCHIjrZ1uoGRKtT0OHjwp7R33eks5rDolMgaJpZM4LADaO .

enzyme69 commented 7 years ago

@zeffii Am I doing it right? https://gist.github.com/c0e867e8e887712f34c5397250116805

Seems like the "lofting" operation is not right.

zeffii commented 7 years ago

don't know can't look atm.