I followed the Panda3D tutorial and got everything working so tried adding my own custom arguments using argparse. The first one I made was --no-rotate where it would stop the camera rotating which worked, but when I tried to add a --scale where you can edit the scale of the panda actor it broke. It says the scale variable is a NoneType when I set it as an integer in the function parameters.
from math import pi, sin, cos
from direct.showbase.ShowBase import ShowBase
from direct.task import Task
from direct.actor.Actor import Actor
class WalkingPanda(ShowBase):
def __init__(self, no_rotate=False, scale=1):
ShowBase.__init__(self)
# Load the environment model.
self.scene = self.loader.loadModel("models/environment")
# Reparent the model to render.
self.scene.reparentTo(self.render)
# Apply scale and position transforms on the model.
self.scene.setScale(0.25, 0.25, 0.25)
self.scene.setPos(-8, 42, 0)
if not no_rotate:
# Add the spinCameraTask procedure to the task manager.
self.taskMgr.add(self.spinCameraTask, "SpinCameraTask")
# Load and transform the panda actor.
self.pandaActor = Actor("models/panda-model",
{"walk": "models/panda-walk4"})
self.pandaActor.setScale(0.005*scale, 0.005*scale, 0.005*scale)
self.pandaActor.reparentTo(self.render)
# Loop its animation.
self.pandaActor.loop("walk")
# Define a procedure to move the camera.
def spinCameraTask(self, task):
angleDegrees = task.time * 6.0
angleRadians = angleDegrees * (pi / 180.0)
self.camera.setPos(20 * sin(angleRadians), -20.0 * cos(angleRadians), 3)
self.camera.setHpr(angleDegrees, 0, 0)
return Task.cont
from . import panda
import argparse
def cli():
parser = argparse.ArgumentParser(prog="walking_panda")
parser.add_argument("--no-rotate",help="Suppress Rotation",
action="store_true")
parser.add_argument("--scale", type=int)
args = parser.parse_args()
walking = panda.WalkingPanda(**vars(args))
walking.run()
from walking_panda.cli import cli
if __name__ == '__main__':
cli()
I don't know why it doesn't see the scale parameter as 1 when it sees the no_rotate parameter as False by default. Any help would be appreciated.
When I run the command:
python walking_panda.py --scale 5
the program functions properly and changes the scale of the panda to 5 times the default. It's just that when I don't add the --scale argument it doesn't see the default value and it returns an error.
minimal working codewhich we could simply copy and test.minimal working code- and it doesn't raise your error. I putprint(f"{scale = }")in__init__and it displays5. Better createminimal working codewhich we could copy to one file and test it. And check if it still gives error. Besides you didn't show FULL traceback so it hard to say which part of code makes problem. Maybe it is problem in different place.