How to use subprocess to return value?

I’m using MacBook 2019, python 3.10, and Panda3D 1.10.11.
I am not sure if my question is appropriate to post in the Panda 3D community. Please let me know if I’m doing it wrong.
I’m trying to use the system folder to select 3D files and import them into Panda3D.
When the program starts running, use Tkinter to open a folder and select a 3D model file. Then return the panda3D 3D model file path and create a 3D model.

Here is the main.py:

import subprocess
import sys

from direct.showbase.ShowBase import ShowBase


class MyApp(ShowBase):
    def __init__(self):
        ShowBase.__init__(self)

        # quit when esc is pressed
        self.accept('escape', sys.exit)

        self.jack = self.loader.loadModel("jack")
        self.jack.reparentTo(self.render)
        self.jack.setScale(2.0, 2.0, 2.0)
        self.jack.setPos(8, 50, 0)

        arch = subprocess.run("python3 /Users/gitdir/fyp/project/add3DObj.py | awk '{print $1}'",
                              universal_newlines=True, shell=True)

        model3D = arch

        print(model3D)    
        print(type(model3D))

        model_path = model3D

        object = self.loader.loadModel(model_path)

        object.reparentTo(self.render)
        object.setPos(self.cam, (0, 30, 0))


app = MyApp()
app.run()

I’m using awk to capture the file path.
The subprocess result:

/Users/gitdir/fyp/panda3d/myModels/christmas_tree.egg

<class 'str'>

Here is the add3DObj.py:

import os
import tkinter
from tkinter import filedialog

def getObj():
    window = tkinter.Tk()
    window.withdraw()
    folder_selected = filedialog.askopenfilename(initialdir=os.getcwd(), title="Select a File",
                                             filetypes=[('egg Data', '*.egg *.bam'), ('All Files', '*')])
    print(folder_selected)
    return folder_selected

getObj()

The error message:

:loader(error): Couldn't load file /Users/gitdir/fyp/panda3d/myModels/christmas_tree.egg
: does not exist.
Traceback (most recent call last):
  File "/Users/gitdir/fyp/project/init.py", line 37, in <module>
    app = MyApp()
  File "/Users/gitdir/fyp/project/init.py", line 31, in __init__
    object = self.loader.loadModel(model_path)
  File "/Users/opt/anaconda3/envs/py310/lib/python3.10/site-packages/direct/showbase/Loader.py", line 298, in loadModel
    raise IOError(message)
OSError: Could not load model file(s): ['/Users/gitdir/fyp/panda3d/myModels/christmas_tree.egg\n']

The Christmas tree model works when I change the code:

        object = self.loader.loadModel("/Users/tangkamsan/gitdir/fyp/panda3d/myModels/christmas_tree.egg")

I guess the mistake is about the ['/Users/gitdir/fyp/panda3d/myModels/christmas_tree.egg\n'] which the path have \n. But I don’t know how to remove it.

By the way, if you have a better solution please let me know.

Thank you for reading my post. Have a nice day.

print() adds a newline to the end. You can pass an argument to it to prevent it from adding the newline, but it’s just as easy to call strip() on the string before passing it to loadModel.

2 Likes