0

This question is about how to automatically call a command line tool to loop through every .obj file (which is a 3D file format) in a directory and process them into .pcd files (which is another 3D file format).

I am stuck at how to automate substitute file extensions from obj to pcd and in particular how to do that in a for loop.

Here is what the data directory looks like:

(folding) yuqiong@yuqiong-G7-7588:/media/yuqiong/DATA/problems$ tree
.
├── input
│   ├── UUID_2832c3d3-9605-41da-916b-779582a26a9c.obj
│   ├── UUID_6979a19e-24a0-42fe-91d6-594f257a7fa6.obj
│   ├── UUID_81b8510f-cab2-4e51-b730-6824f39877da.obj
│   ├── UUID_87007a99-879e-48e2-aaae-20c7000c2e11.obj
│   ├── UUID_8be34bf6-4b01-4a63-8550-3273f5ae412d.obj
│   ├── UUID_92043aaf-4f2c-46d3-9af2-c2150457f089.obj
│   ├── UUID_aa16a01d-b388-4acf-92bb-706127b059b0.obj
│   ├── UUID_aec77ad3-f558-4030-a9d4-392ad99fb771.obj
│   ├── UUID_b55151a7-01d8-4dbc-b011-bb6df74d5e6c.obj
│   ├── UUID_d022be8c-67a0-4d5d-92df-8bfbe8b27f7c.obj
│   ├── UUID_d94ce864-7437-460a-8a1a-c5bbd7b2eb5e.obj
│   └── UUID_f6ea8a38-9b24-4d3c-ba4d-099a1a76966c.obj
├── out
│   └── UUID_f6ea8a38-9b24-4d3c-ba4d-099a1a76966c.pcd
├── run.sh
└── Untitled.ipynb

To process an individual .obj file, I run this command:

pcl_mesh_sampling ./input/UUID_f6ea8a38-9b24-4d3c-ba4d-099a1a76966c.obj ./out/UUID_f6ea8a38-9b24-4d3c-ba4d-099a1a76966c.pcd -n_samples 2048

Here you might notice that the first argument to pcl_mesh_sampling is just a file in the input folder, while the second argument is the name of the output file in the out folder. They have the same name but different extension.

I would like to automate this process, but am unable to come up with a shell script. Currently I have this:

#!/usr/bin/env bash
shopt -s nullglob
for i in ./input/*.obj; do
    echo $(echo $i | awk -F. '{OFS="."} {$2="pcd"; print $0}') 
    pcl_mesh_sampling $i ./out/$fname -n_samples 2048
    echo $i
done

But this is not working as expected and I suspect I used awk wrong...

More, every time the pcl_mesh_sampling finishes one task, it will show the resulting file. The process won't return until I close the visualization Window. Is there a command line utility that suppresses it from doing so?

Any suggestions will be helpful! Thanks.

yuqli
  • 4,461
  • 8
  • 26
  • 46

1 Answers1

0

Solved the automation problem with the following code! I lack some knowledge on string manipulation. And we don't actually need awk or sed here...

#!/usr/bin/env bash
shopt -s nullglob

outdir="./out"

for i in ./input/*.obj; do
    dir=${i%/*}
    base=${i##*/}
    newbase=${base/.obj/.pcd}
    dest="$outdir/$newbase"
    pcl_mesh_sampling $i $dest -n_samples 2048
done
yuqli
  • 4,461
  • 8
  • 26
  • 46
  • 1
    You should still quote your variables properly. See https://stackoverflow.com/questions/10067266/when-to-wrap-quotes-around-a-shell-variable – tripleee Jan 10 '19 at 17:32