Visual Python

Ask a question or request a feature related to the website or forum...

Moderator: scott

User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

Visual Python

Post by agor95 »

I have been looking around for a general Visual Programing product.

https://en.wikipedia.org/wiki/VPython

This is the one I am looking at anyone that is interested in supplying Bessler related demos.

Here is the place.
Furcurequs
Devotee
Devotee
Posts: 1599
Joined: Sat Mar 17, 2012 4:50 am

re: Visual Python

Post by Furcurequs »

I used a python script and the free (for non-commercial use) python module of the Lightflow Rendering Tools to generate this image many years ago now:

Image

I did use a modeling program to generate the cup geometry, but I wrote my own python module to import that geometry into my scene. The table and sphere I simply scripted in python itself. (ETA: After reading what I said in another thread and rethinking things, I believe I could have imported the geometry for the sphere instead of scripting it, but scripting it would have been simple enough.)

Here is an image of one of the examples that came with the Lightflow module. It was entirely scripted and so the object was generated with a math equation rather than a 3D modeler.

Image

http://www.lightflowtech.com/images/bspline5_b.jpg

I've also scripted and rendered (the frames for) some simple animations, but of course with the python language it should also be possible to do proper mathematical modeling and thus generate a decent simulation.

You would have to use an older version of Python with these rendering tools, however.
Lightflow Rendering Tools
Python Module 1.5

The Python Module is an extension for the Python language incorporating almost all of the Lightflow Rendering Tools.

This tool is meant for advanced 3d users who don't need any 3d modeling tool to develop their own scenes, and still prefer simple scripting...

Supported platforms include Linux and Windows NT.

This trial product is completely free for non commercial uses.
http://www.lightflowtech.com/news.html

http://www.lightflowtech.com

I've also done some simple animations with the scripted ray tracer POV-Ray. It's own built in scripting language is probably powerful enough to do what we would need to do, too. It seemed to be quite similar to that used with the Python Lightflow module.

http://www.povray.org/

Dwayne

ETA: I've also found this - someone who has written a Python library called Vapory so that POV-Ray can be used directly from Python itself:
Things You Can Do With Python and POV-Ray

This post presents Vapory, a library I wrote to bring POV-Ray’s 3D rendering capabilities to Python.
http://zulko.github.io/blog/2014/11/13/ ... d-pov-ray/

ETA2: Here's one of the frames of something I scripted and rendered with POV-Ray (without using any sort of Python library). It's not meant to be any sort of Bessler wheel design, btw.

Image

Here's the link to the small animation:

http://www.besslerwheel.com/forum/files/flywheel.mpg

Here's the link to the original thread:

http://www.besslerwheel.com/forum/viewt ... p?p=107011

Here's the thread in which I first posted the above Lightflow image:

"Golems 3d sandbox physics simulation program"

http://www.besslerwheel.com/forum/viewt ... p?p=137284

I rendered the frames for this animation using Python and the Lightflow Rendering Tools Python module. I think it is close to 3 MB in size. It's a shame Lightflow cannot be used for commercial use.

http://www.besslerwheel.com/forum/files/dvd.mpg
I don't believe in conspiracies!
I prefer working alone.
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

I think Blender also can be used by python scripts.

It is all about using the right tool for the job; that the right time.
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

I have been working on the V Python scripts.

So far it's been a process of dynamic illustration development.
Instead of POV [blender like] static images.

Also I have been getting my head around formula graphing in the same product.

That will lead to demo simulation with graphical support documentation.

P.S. The product is not restricted to XY simulations.

The Unicode of formula is done and has been implemented on two private web servers.
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

Here is the script and graphs that appear when run in Visual Python.

Code: Select all

from __future__ import print_function, division
from visual.graph import *

print("""
Formula Horizontal Rorationg System
""")

# Using a graph-plotting module

t = 0

mass = 1            # [kg]
r    = 2            # [m]         Radius
v    = 1            # [m/s]       velocity

Rinc = -0.1         # [m]   Reduction in Radius

ltv  = 1            # [m/s] Linear Tangental Velocity
wi   = 0            # [j]   Work Increment
wt   = 0            # [j]   Work Total

#ltv = ((t*Rinc)+r) * v / (((t+1)*Rinc)+r)
ke = 0.5*mass*v**2                                       # [j] Kinetic Energy         'KE'
cf = mass*((ltv+v)/2)**2/((((t+1)*Rinc+r)+(t*Rinc+r))/2) # [N] Centripital Force      'CF'
am = r*mass*v                                            # [kg m2/s] Angular Momentun 'L'
lm = mass*v                                              # [kg m/s] Linear Momentum   'p'

# Rotational Check of energy

moi = mass*r**2     # [kg m2]     Moment of Inertia
av = ltv/r          # [radians/s] Angular Volocity
rke = 0.5*moi*av**2 # [j]         Rotational Kinetic Energy

# If xmax, xmin, ymax, or ymin specified, the related axis is not autoscaled
# Can turn off autoscaling with
#    linera.autoscale[0]=0 for x or oscillation.autoscale[1]=0 for y

linera = gdisplay(x=0,y=0,title='Horizontal Rotating System',xtitle='Count', ytitle='Response (click and drag mouse to see coordinates)')
funct1 = gcurve(display=linera,color=color.cyan)
funct2 = gcurve(display=linera,color=color.red)
funct3 = gcurve(display=linera,color=color.yellow)
funct4 = gcurve(display=linera,color=color.blue)
funct5 = gcurve(display=linera,color=color.green)

Rotate = gdisplay(x=0,y=400,title='Rotational check of Energy',xtitle='Count', ytitle='Response (click and drag mouse to see coordinates)')
funct6 = gcurve(display=linera,color=color.cyan)
funct7 = gcurve(display=linera,color=color.red)
funct8 = gcurve(display=linera,color=color.yellow)

#funct2 = gvbars(delta=0.5, color=color.red)
#funct3 = gdots(color=color.yellow)

for t in arange(0, 11, 1):

    # ltv cyan
#    print (ltv)
    funct1.plot( pos=(t,ltv))
    ltv = ((t*Rinc+r) * v) /((t+1)*Rinc+r)

    # ke red
    ke = 0.5*mass*v**2
#    print (ke)
    funct2.plot( pos=(t,ke))
    
    # cf yellow
#    print (cf)
    funct3.plot( pos=(t,cf))
    cf = mass*((ltv+v)/2)**2/((((t+1)*Rinc+r)+(t*Rinc+r))/2)
    
    # am blue
##    print (am)
##    funct4.plot( pos=(t,am))
    
    # lm blue

    lm = mass*v

#    print (lm)
    funct5.plot( pos=(t,lm))

    
    # moi green
    
    moi = mass*(t*Rinc+r)**2

#    print (moi)
    funct6.plot( pos=(t,moi))

    # W angular velocity
    
    av = v / (t*Rinc+r)

#    print (av)
    funct7.plot( pos=(t,av))
    
    # ke rotational kinetic energy
    
    rke =  0.5*moi*av**2

#    print (rke)
    funct8.plot( pos=(t,rke))

    v = ltv
Last edited by agor95 on Mon Apr 25, 2016 9:23 pm, edited 1 time in total.
User avatar
ME
Addict
Addict
Posts: 3512
Joined: Wed Jun 08, 2005 6:37 pm
Location: Netherlands

Post by ME »

Interesting, and nice to see an example.

It appears you calculate the wrong value for several graphs: you first plot and then calculate *) - it makes a difference because (I guess) your step-size is 10% of the range. A bit strange it doesn't include t=11.
Perhaps it's an idea to do the calculations first in a single block of code, and then plot in a second block of code; so you can separate calculus and output.

Do you need to do all the physics calculations yourself, or does it also include a library?

*) ok, I noticed (t+1)
Marchello E.
-- May the force lift you up. In case it doesn't, try something else.---
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

I used an excel example supplied in another topic.

Then cut down one of the Visual Python examples; that contained graphs.

Next

I used the print lines (they are now commented out #).

Did what was required to get the same results as the excel.

commented the prints and entered the pot commands.

for t in arange(0, 11, 1): gives t counting from 0 to 10

Python is really powerfully - it's motto is 'batteries included'

There is a world of Python solutions out there. Nasa & Google etc.

This is a simple and relevant example.

I recommend down loading the free V Python and run the doublependulum script example.
User avatar
ME
Addict
Addict
Posts: 3512
Joined: Wed Jun 08, 2005 6:37 pm
Location: Netherlands

Post by ME »

Installed it.
It looks like you still need to handle all physics (especially collisions) yourself.
Although the basic physics can be captured in formulas, some combinations need to be solved in a numerical way.
That's a lot of work before getting good results.
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

You will see the 'from visual.graph import *'

This is a library of commands for 3d visualization.
There are other libraries for doing other things.

I look forward to a day we can type

'from bessler import wheel *'

regards
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

This script creates two graphs showing force [Newtons] using a
4lb weight; Red is Gravity and the yellow is CF.

There are two rotation rates 26rpm and 20rpm.

Code: Select all

from __future__ import print_function, division
from visual.graph import *

print("""
Formula Acceleration dur to Rotation [AR] vs Acceleration due to Gravity [AG]
""")

# Using a graph-plotting module

# If xmax, xmin, ymax, or ymin specified, the related axis is not autoscaled
# Can turn off autoscaling with
#    linera.autoscale[0]=0 for x or oscillation.autoscale[1]=0 for y

Rotate = gdisplay(x=0,y=0,title='26 RPM Radious 12 feet Rotating System',xtitle='Radious', ytitle='Response (click and drag mouse to see coordinates)')
funct1 = gcurve(display=Rotate,color=color.red)
funct3 = gcurve(display=Rotate,color=color.yellow)

Rotate2 = gdisplay(x=0,y=400,title='20 RPM Radious 12 feet Rotating System',xtitle='Radious', ytitle='Response (click and drag mouse to see coordinates)')
funct6 = gcurve(display=Rotate2,color=color.red)
funct8 = gcurve(display=Rotate2,color=color.yellow)

#
# initial values
#

g = 9.81

mass  = 1.81437    # 4lb in [kg]

dr = 0.1           # delta radious
r     = 0          # [m] Radius

#
# initial calculations
#

# Rotational Check of energy

av = (2.0*(22.0/7.0))/(60.0/26.0)       # [radians/s] Angular Volocity RPM 26

av2 = (2.0*(22.0/7.0))/(60.0/20.0)      # [radians/s] Angular Volocity RPM 20

cf = mass*0*av**2

cf2 = mass*0*av2**2

moi = mass*1.8288**2                    # [kg m^2]    Moment of Inertia
rke = 0.5*moi*av**2                     # [j] Rotational Kinetic Energy

print (rke)

#
# recalculate loop
#

for r in arange(0.0, 1.8288+dr, dr):

    # g red
#    print (g)
    funct1.plot( pos=(r,g*mass))
    funct6.plot( pos=(r,g*mass))
    
    # cf yellow
#    print (cf)

    cf = mass*r*av**2

    funct3.plot( pos=(r,cf))

    cf2 = mass*r*av2**2

    funct8.plot( pos=(r,cf2))
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

If you follow bessler's cues then you may get to this idea.
Allow a weight on the hub drop for 135 degrees clockwise.
bounce the weight off the hub and collect it again.
This is test 1 were the weight/ball is separated from the hub.
However with the hub rotating at the correct rate you can
get the effect.

So the weight drops 9 feet and only need to be lifted around 4 feet.

Code: Select all

from __future__ import division
from visual import *

print("""
Right button drag to rotate "camera" to view scene.
  On a one-button mouse, right is Command + mouse.
Middle button to drag up or down to zoom in or out.
  On a two-button mouse, middle is left + right.
  On a one-button mouse, middle is Option + mouse.

This example one shows a bouncing ball next to a rotating hub.


""")

Myscene = display(title='Rotator',
                  x=0,y=0,width=800, height=800,
                  center=(0,0,0), background=(0,0,0),foreground=(1,1,1),
                  ambient=color.gray(0.2),
                  lights=
                  [distant_light(direction=(0.22, 0.44, 0.88), color=color.gray(0.8)),
                   distant_light(direction=(-0.88, -0.22, -0.44), color=color.gray(0.3))])


Myscene.cursor.visible = True    #Default

# How fast the render process takes in milliseconds
Myscene.show_rendertime = False

# Default close the screen closes the program
Myscene.exit = True

dpth  = 0.5
lth = 6

# Center of the hub
point  = sphere(radius=dpth, color=color.yellow)

# rotate the whole hub
frame0 = frame(pos=(0,0,0), axis=(0,1,0))

# The spokes
theta1 = 0
frame1 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box1 = box(frame=frame1, y=lth/2+1*dpth, size=(dpth,5,dpth))
frame1.rotate(axis=(0,0,1), angle=theta1)

theta2 = -0.25*pi
frame2 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box2 = box(frame=frame2, y=lth/2+1*dpth, size=(dpth,5,dpth))
frame2.rotate(axis=(0,0,1), angle=theta2)

theta3 = -0.5*pi
frame3 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box3 = box(frame=frame3, y=lth/2+1*dpth, size=(dpth,5,dpth), color=color.red)
frame3.rotate(axis=(0,0,1), angle=theta3)

theta4 = -0.75*pi
frame4 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box4 = box(frame=frame4, y=lth/2+1*dpth, size=(dpth,5,dpth))
frame4.rotate(axis=(0,0,1), angle=theta4)

theta5 = -1*pi
frame5 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box5 = box(frame=frame5, y=lth/2+1*dpth, size=(dpth,5,dpth))
frame5.rotate(axis=(0,0,1), angle=theta5)

theta6 = -1.25*pi
frame6 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box6 = box(frame=frame6, y=lth/2+1*dpth, size=(dpth,5,dpth))
frame6.rotate(axis=(0,0,1), angle=theta6)

theta7 = -1.5*pi
frame7 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box7 = box(frame=frame7, y=lth/2+1*dpth, size=(dpth,5,dpth))
frame7.rotate(axis=(0,0,1), angle=theta7)

theta8 = -1.75*pi
frame8 = frame(frame=frame0, pos=(0,0,0), axis=(0,0,1))
box8 = box(frame=frame8, y=lth/2+1*dpth, size=(dpth,5,dpth))
frame8.rotate(axis=(0,0,1), angle=theta8)

# The base block
floor1 = box(pos=(0,-5.5,-1), length=16, height=0.5, width=1, color=color.blue)
floor2 = box(pos=(-7.5,-5.5,0), length=1, height=0.5, width=1, color=color.blue)
floor3 = box(pos=(7.5,-5.5,0), length=1, height=0.5, width=1, color=color.blue)
floor4 = box(pos=(0,-5.5,1), length=16, height=0.5, width=1, color=color.blue)

frame9 = frame(pos=(4.5,-5.5,1), axis=(0,0,1))
bouncpaddle1 = box(frame=frame9, length=1, height=0.5, width=1.5, color=color.yellow)
frame9.rotate(axis=(0,0,1), angle=pi/4)

ball = sphere(pos=(4,6-0.25,1), radius=0.5, color=color.green,opacity=0.5)
ball.velocity = vector(0,-1,0)

dt = 0.005
t = 0.

dtheta1 = 0.

while t < 2&#58;
    rate&#40;50&#41; # 50 loop per second

    frame0.rotate&#40;axis=&#40;0,0,1&#41;, angle=-dtheta1&#41;
    dtheta1 = 1.75*dt

    ball.pos = ball.pos + ball.velocity*dt

    if ball.y < -5.5+0.75&#58;
        ball.velocity.y = -ball.velocity.y
        ball.velocity.x = -ball.velocity.y
    else&#58;
        ball.velocity.y = ball.velocity.y - 9.8*dt
        if ball.x > 4&#58;
            ball.velocity.x = 0

    if ball.x < -6+0.25&#58;
         ball.velocity.x = -ball.velocity.x
 

    t = t+dt
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

Simple 1 second bounce ten times.

I wanted to get the timing aspect up to speed.

Copy te code into a text files and load it in V python.
previously installed

You can comment out this section:

Code: Select all

from __future__ import division
from visual import *
from visual.graph import *

import time

Myscene = display&#40;title='Ten Second Bounce',
                  x=0,y=0,width=800, height=400,
                  center=&#40;0,0,0&#41;, background=&#40;0,0,0&#41;,foreground=&#40;1,1,1&#41;,
                  ambient=color.gray&#40;0.2&#41;,
                  lights=
                  &#91;distant_light&#40;direction=&#40;0.22, 0.44, 0.88&#41;, color=color.gray&#40;0.8&#41;&#41;,
                   distant_light&#40;direction=&#40;-0.88, -0.22, -0.44&#41;, color=color.gray&#40;0.3&#41;&#41;&#93;&#41;

vhight = gdisplay&#40;x=1, y=400, xtitle='Time', ytitle='Response &#40;click and drag mouse to see coordinates&#41;'&#41;
funct1 = gcurve&#40;color=color.cyan&#41;
#funct2 = gvbars&#40;delta=0.5, color=color.red&#41;
#funct3 = gdots&#40;color=color.yellow&#41;


#Myscene.background=&#40;1,1,1&#41;
#Myscene.foreground=&#40;0,0,0&#41;
#Myscene.ambient=color.gray&#40;0.2&#41;

Myscene.cursor.visible = True    #Default

# How fast the render process takes in milliseconds
Myscene.show_rendertime = True

Myscene.select&#40;&#41;

# Default close the screen closes the program
Myscene.exit = True

# controlling the view
#Myscene.autocenter = True

# range or scale
# scale &#40;0.1,0.1,0.1&#41; same as range &#40;10.10.10&#41;
Myscene.autoscale = True        #Default
# Myscene.scale = 1

#
# Mouse Interactions
#
def pause&#40;&#41;&#58;
    while True&#58;
        rate&#40;50&#41;
        if Myscene.mouse.events&#58;
            m = Myscene.mouse.getevent&#40;&#41;
            if m.click == 'left'&#58; return
        elif Myscene.kb.keys&#58;
            k = Myscene.kb.getkey&#40;&#41;
            return

#
# clock
#
def hourminute&#40;&#41;&#58;
    now = time.localtime&#40;time.time&#40;&#41;&#41;
    hour = now&#91;3&#93; % 12
    minute = now&#91;4&#93;
    second = now&#91;5&#93;
    return &#40;hour, minute,second&#41;

class analog_clock&#58;
    def __init__&#40;self, pos=&#40;0,0,0&#41;, radius=1., axis=&#40;0,0,1&#41;&#41;&#58;
        self.pos = vector&#40;pos&#41;
        self.axis = vector&#40;axis&#41;
        self.radius = radius
        self.spheres = &#91;&#93;
        self.hour = 0
        self.minute = -1
        for n in range&#40;12&#41;&#58;
            self.spheres.append&#40;sphere&#40;pos=self.pos+rotate&#40;radius*scene.up,
                    axis=self.axis, angle=-2.*pi*n/12.&#41;, radius=radius/20.,
                    color=color.hsv_to_rgb&#40;&#40;n/12.,1,1&#41;&#41; &#41;&#41;
        self.hand = arrow&#40;pos=pos, axis=0.95*radius*scene.up,
                    shaftwidth=radius/10., color=color.cyan&#41;
        self.update&#40;&#41;
        
    def update&#40;self&#41;&#58;
        hour, minute, second = hourminute&#40;&#41;
        if self.hour == hour and self.minute == minute&#58; return
        self.hand.axis = rotate&#40;0.95*self.radius*scene.up,
                    axis=self.axis, angle=-2.*pi*minute/60.&#41;
        self.spheres&#91;self.hour&#93;.radius = self.radius/20.
        self.spheres&#91;hour&#93;.radius = self.radius/10.
        self.hour = hour
        self.minute = minute
        self.second = second

# Decorate back slab with a gold box and a clock
##box&#40;pos=&#40;0,h/2.,-R+d/2+0.1&#41;, size=&#40;w/2.,w/2.,0.2&#41;, 
##    color=&#40;1,0.8,0&#41;, material=materials.wood&#41;
##clock = analog_clock&#40;pos=&#40;0,h/2.,-R+d/2+0.2+0.2*h/10&#41;,
##                    radius=0.2*w, axis=&#40;0,0,1&#41;&#41;

    # Update the analog clock on the back slab
##    clock.update&#40;&#41;

print&#40;'Pausing'&#41;

#pause&#40;&#41;

#point = sphere&#40;pos=&#40;0.,0.25,1.&#41;, radius=0.5,color=color.yellow&#41;

# top surface of floor y = 0
floor = box&#40;pos=&#40;0.,-0.5,0.&#41;,length=8, height=0.5, width=8, color=color.blue&#41;

# bottom of ball - not it's centor
ball = sphere&#40;pos=&#40;0.,&#40;9.81/8&#41;+0.25,0.&#41;, radius=0.5, color=color.red&#41;
ball.velocity = vector&#40;0.,0.,0.&#41;
ball.mass = 1.81437 # 4lb in kg

dt = 0.001
t  = 0.

hour, minute, second = hourminute&#40;&#41;

s = str&#40;hour&#41;+"&#58;"+str&#40;minute&#41;+"&#58;"+str&#40;second&#41;
print&#40;s&#41;

while t < 10&#58;
    rate&#40;1/dt&#41;
    ball.pos = ball.pos + ball.velocity*dt
    if ball.y < 0.25&#58;
        ball.velocity.y = -ball.velocity.y
    else&#58;
        ball.velocity.y = ball.velocity.y - 9.81*dt

    funct1.plot&#40;pos=&#40;t,ball.y-0.25&#41;&#41;

    t = t + dt
#    pause&#40;&#41;

hour, minute, second = hourminute&#40;&#41;

s = str&#40;hour&#41;+"&#58;"+str&#40;minute&#41;+"&#58;"+str&#40;second&#41;
print&#40;s&#41;
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

For those with up to date browsers

http://doc.x3dom.org/tutorials/animatio ... ample.html

If you see a house you can walk around it.

This proves you would be able to see a moving Bessler illustrations in 3D using your browser.
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

Just a test for new browser users

http://steampunk.ddns.net/cube.html
User avatar
agor95
Addict
Addict
Posts: 7456
Joined: Wed Sep 24, 2008 8:09 pm
Location: Earth Orbit
Contact:

re: Visual Python

Post by agor95 »

Post Reply