Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 518
user1453064 Avatar asked Feb 19 '26 09:02

user1453064


1 Answers

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.

like image 144
nqngo Avatar answered Feb 20 '26 22:02

nqngo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!