简体   繁体   中英

VPython Inheritance

I'm currently trying to make a class for the sole purpose of quickly creating a VPython object and appending additional values to the object. VPython automatically creates an object with such values like position and dimensions. However, I also want to add variables such as physical properties of the material and momentum. So here's my solution:

class Bsphere(physicsobject):

     def build(self):

         sphere(pos=ObjPosition, radius=Rad,color=color.red)

With physicsobject looking something like this:

class physicsobject:

    def __init__(self):

         self.momentum=Momentum

Essentially, I want this to still retain the original properties of the VPython sphere() object while adding new variables. This actually works initially, the object renders and the variables are added. But now, I have no way of changing the VPython object. If I type in:

Sphereobj.pos=(1,2,3)

The position will update as a variable, however, VPython will not update the rendered object. There is now a disconnect between the object and the rendered object. Is there any way to inherit the rendering aspects of a VPython object while creating a new object? I can't simply use

class Bsphere(sphere(pos=ObjPosition, radius=Rad,color=color.red)):

     self.momentum=Momentum

and there isn't much documentation on VPython.

I don't use VPython. However, from the look of it you are inheriting the property of physicsobject and not sphere . My recommendation is try this:

# Inherit from sphere instead
class Bsphere(sphere):
     # If you want to inherit init, don't overwrite init here
     # Hence, you can create by using
     # Bpshere(pos=ObjPosition, radius=Rad,color=color.red)
     def build(self, material, momentum):
         self.momentum = momentum
         self.material = material

You can then use:

 myobj = Bsphere(pos=(0,0,0), radius=Rad,color=color.red)
 myobj.pos(1,2,3)

However, I recommend overwrite the __init__ method in your child class, providing you know all arguments to declare in the original sphere construct.

from visual import *
class Physikobject(sphere):
    def __init__(self):
        sphere.__init__(self, pos = (0,0,0), color=(1,1,1))
        self.otherProperties = 0

I think this one helps - the question might be old be people might still think about it.

I am a big vpython user and I have never used stuff like this but I do know that vpython already has the feature you are trying to implement.
===============================Example====================================

 from visual import *
 myball = sphere()
 myball.weight = 50
 print (myball.weight)

This code creates a ball then initializes a variable called weight then displays it.

The wonderful thing about VPython is that you don't need to do that.

VPython does it for you!

This is all you need to do:

variable_name = sphere()#you can add pos and radius and other things to this if you want
variable_name.momentum = something

You can easily insert this into a function:

objectstuffs = []
def create_object(pos,radius,color,momentum):
    global objectstuffs
    objectstuffs.append(sphere(pos=pos,radius=radius,color=color))
    objectstuffs[len(objectstuffs)-1].momentum = momentum

That function is definitely not the best thing to use in every case, but you can edit the function easily, it was just for sake of example.

Have fun with VPython!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM