BruceSherwood/glowscript

Support for python seems to be limited in the offline package

Closed this issue · 6 comments

I had developed the bouncing ball in a box demo mentioned in vpython intro(the pdf doc) in anaconda.
For better coding style, I used a class definition to add multiple particle instances, it worked OK in anaconda, but failed to create any class instance without any warning/error when imported in the offline package.

Please provide a brief example that fails, as classes are supported in GlowScript VPython. (I'll also comment that "sphere" IS a class, so s1 = sphere() and s2 = sphere() are two instances of the class "sphere".)

Codes which failed in the offline package but successed in anaconda

from vpython import *

wallL = box(pos=vector(-6,0,0), 
                size=vector(0.2,12,12), 
                color=color.green,
                opacity = 0.5)
wallR = box(pos=vector(6,0,0), 
                size=vector(0.2,12,12), 
                color=color.green,
                opacity = 0.5)
wallU = box(pos=vector(0,6,0), 
                size=vector(12,0.2,12), 
                color=color.green,
                opacity = 0.5)
wallD = box(pos=vector(0,-6,0), 
                size=vector(12,0.2,12), 
                color=color.green,
                opacity = 0.5)
wallB = box(pos=vector(0,0,-6), 
                size=vector(12,12,0.2), 
                color=color.green,
                opacity = 0.5)
wallF = vector(0,0,6)

class particle(object):
    def __init__(self, pos, velocity, color_param, make_trail = True, vscale = 0.1):
        self.time = 0.0
        self.pos = pos
        self.velocity = velocity
        self.color = color_param
        self.vscale = vscale
        self.entity = sphere(pos=self.pos, radius=0.5, color=self.color, make_trail=make_trail)
        self.varr = arrow(pos=self.pos, axis=self.vscale*self.velocity, color=color.yellow)
    def propagate(self, deltat):
        if self.pos.x > wallR.pos.x-wallR.size.x*0.5 or self.pos.x < wallL.pos.x+wallL.size.x*0.5:
            self.velocity.x = -self.velocity.x
        if self.pos.y > wallU.pos.y-wallU.size.y*0.5 or self.pos.y < wallD.pos.y+wallD.size.y*0.5:
            self.velocity.y = -self.velocity.y
        if self.pos.z > wallF.z-wallB.size.z*0.5 or self.pos.z < wallB.pos.z+wallB.size.z*0.5:
            self.velocity.z = -self.velocity.z

        self.varr.pos = self.entity.pos
        self.varr.axis = self.vscale*self.velocity
        self.entity.pos = self.entity.pos + self.velocity*deltat

        self.pos = self.entity.pos
            
ball_001 = particle(vector(0,0,0),vector(25,5,10),color.red)
ball_002 = particle(vector(0,0,0),vector(10,15,5),color.cyan)

deltat = 0.005
t = 0
scene.autoscale = False

while t < 100:
    rate(100)

    ball_001.propagate(deltat)
    ball_002.propagate(deltat)

    t = t + deltat
    

Codes which are OK. Besides, import vpython as vp is not supported, but I prefer this style to avoid naming conflicts with existing codes.

from vpython import *

wallL = box(pos=vector(-6,0,0), 
                size=vector(0.2,12,12), 
                color=color.green,
                opacity = 0.5)
wallR = box(pos=vector(6,0,0), 
                size=vector(0.2,12,12), 
                color=color.green,
                opacity = 0.5)
wallU = box(pos=vector(0,6,0), 
                size=vector(12,0.2,12), 
                color=color.green,
                opacity = 0.5)
wallD = box(pos=vector(0,-6,0), 
                size=vector(12,0.2,12), 
                color=color.green,
                opacity = 0.5)
wallB = box(pos=vector(0,0,-6), 
                size=vector(12,12,0.2), 
                color=color.green,
                opacity = 0.5)
wallF = vector(0,0,6)

vscale = 0.1
velocity = vector(25,5,10)
entity = sphere(pos=vector(0,0,0), radius=0.5, color=color.red, make_trail=True)
varr = arrow(pos=entity.pos, axis=vscale*velocity, color=color.yellow)

deltat = 0.005
t = 0
scene.autoscale = False

while t < 100:
    rate(100)

    if entity.pos.x > wallR.pos.x-wallR.size.x*0.5 or entity.pos.x < wallL.pos.x+wallL.size.x*0.5:
        velocity.x = -velocity.x
    if entity.pos.y > wallU.pos.y-wallU.size.y*0.5 or entity.pos.y < wallD.pos.y+wallD.size.y*0.5:
        velocity.y = -velocity.y
    if entity.pos.z > wallF.z-wallB.size.z*0.5 or entity.pos.z < wallB.pos.z+wallB.size.z*0.5:
        velocity.z = -velocity.z

    varr.pos = entity.pos
    varr.axis = vscale*velocity
    entity.pos = entity.pos + velocity*deltat

    t = t + deltat
    

Just write "class particle():" or "class particle:"; and your program works. I think the need to inherit from object is a syntax that was needed in Python 2 but is irrelevant in Python 3, which is what RapydScript-NG is based on

Thanks for pointing out the failure of "import vpython as vp", which does work at glowscript.org. I think I see what needs to be fixed.

Bruce

Thanks a lot for testing those codes. More over, is there any plan to port the offine package to Python 3?

I'm not sure what you mean. As I said, the RapydScript-NG Python-to-JavaScript transpiler treats your code as being for Python 3. As for an offline package for true Python 3 (that is, installed Python 3 with access to all Python modules), that already exists; it's called VPython 7. See vpython.org for details.

I've updated the offline package to handle import statements correctly. Thanks again for pointing out this need.

Bruce