Python Subprocess for Command line batch processing

Feel free to ask any question here
Post Reply
rthurau
Posts: 9
Joined: Thu Feb 05, 2015 8:48 pm

Python Subprocess for Command line batch processing

Post by rthurau »

I'm trying to use Python to run batch export of .pyl to las. Here is my code:

##

Code: Select all

import os, subprocess

CCPath = r"U:\Programs\CloudCompare_v2.6.0_bin_x64\CloudCompare.exe"
filePath = r"U:\Testing\fwmm5b_v11.ply"

openFile=subprocess.Popen([CCPath, "CloudCompare", "-C_EXPORT_FMT", "LAS", "-O", filePath], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
openFile.wait()
print openFile.stderr.readlines()
print openFile.stdout.readlines()
My understanding is that this should run and simply export the .ply with the same file name and directory as a .las. No additional file is created however.
As is, my print statements return "[]" but I've messed around to get a child (CloudCompare) error of -1073741515.

Part of the problem could be my lack of understanding with subprocess although I've used successfully with other programs in the past. I'll dive more into documentation tonight.

Does anyone have any insight into whether this code is correct with regards to Python, Command line, or CloudCompare?

Any tips are much appreciated.

Thanks

Rich
daniel
Site Admin
Posts: 7713
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: Python Subprocess for Command line batch processing

Post by daniel »

I don't know about the Python side but if you manage to successfully run the command line mode of CC, you'll see a small "console" dialog appear with all the log info.

Once this is done, if you want to do conversion only then you have to explicitly tell CC to save the currently loaded cloud (in command line mode, CC only saves files if a processing has been performed). Use the '-SAVE_CLOUDS' command at the end of your command:

Code: Select all

CloudCompare -0 myfile.ply -C_EXPORT_FMT LAS -SAVE_CLOUDS
Daniel, CloudCompare admin
rthurau
Posts: 9
Joined: Thu Feb 05, 2015 8:48 pm

Re: Python Subprocess for Command line batch processing

Post by rthurau »

Thanks for the reply. I have two main questions that are a bit lengthy, but if I can work this stuff out this could be a very big tool for us to use in our organization.

1. Requirements for Command Line processing:
Pardon my lack of experience with this question please. To run command line directly I would need to add the path to the CC executable to my computer's environmental variables, correct? Once that is complete, then I should be able to type commands directly as the Wiki describes into Windows cmd.
I'm assuming this is what is meant by "Command Line Mode".

2. Python Subprocess through the CC executable. Maybe I can just call the executable (no to answer above)?
I've made some slight progress in that when I run my code CC opens and loads the file. This leads me to believe that all the other tasks in the code should run as well but there are some complications.

Here's my revised line of code that does the work:

Code: Select all

openFile=subprocess.Popen([CCPath, "CloudCompare", "-SILENT -O ", filePath, " -C_EXPORT_FMT LAS -SAVE_CLOUDS" ])
Here's what happens:
- Even though I have Silent on, the CC program opens. My guess is because I have to give some answers to CC about "Can't guess file type from extension" with the .ply. So I have to say "Apply" to the Type and PLY mapping params. I hit apply (all the CC guesses are correct).
- Global shift/scale menu pops so I also have to make a decision. I simply hit yes based on CC guesses.
- I get a "[Load]" error, "Can't guess file format: no file extension". But, the PLY file load window opens and the file loads no problem.

{Edit from my original: Note that when I add the .ply from the GUI, I do not get the file format errors. }

My question is really with the second part of the code where I want to export the PLY as LAS, which does not happen. I can save the clouds from the GUI and works no problem.
However, Python hangs while the CC GUI is open and quits as soon as I close the GUI.

I can imagine several different things that can be happening. Maybe you have an idea about how to move a little further toward the end result of being able to use CC to batch convert .ply to .las (and worry about other stuff later ;-).

Thank you for taking the time to look and consider.

Rich
daniel
Site Admin
Posts: 7713
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: Python Subprocess for Command line batch processing

Post by daniel »

1. Yes. Either you add CC's path to your system PATH, or you can start the command from CC's directory (but the first one is better).

2. Nope, if the Command line was correctly called you shouldn't see CC at all (only a Console dialog). You should try to do the same call manually (see 1) and you'll see the difference (hopefully).
Daniel, CloudCompare admin
rthurau
Posts: 9
Joined: Thu Feb 05, 2015 8:48 pm

Re: Python Subprocess for Command line batch processing

Post by rthurau »

Very excited to report success using the following code:

Code: Select all

import os#, subprocess
#####################################
CCPath = r"U:\Programs\CloudCompare_v2.6.0_bin_x64"
filePath = r"U:\Testing\fwmm5b_v11_sub.ply"
os.chdir(CCPath)
openFile=os.system("CloudCompare -O " + filePath + " -C_EXPORT_FMT LAS -SAVE_CLOUDS")
Thanks for the help. Mostly I had to figure out the Python and Command line side of things which was aided by the tips.
LarryTheCableGuy
Posts: 1
Joined: Tue Jul 02, 2019 9:37 pm

Re: Python Subprocess for Command line batch processing

Post by LarryTheCableGuy »

Hi all

I tried the latest code of rthurau for an ICP-registration between a .ply reference-mesh and a .las point cloud. It worked!
However, I have a bunch of .las files (500) I wish to register to the .ply file. I wrote a python script with a for loop walking through my directory w/ .las files.

Code: Select all

import os
filePath = r"C:\Users\Lars\Documents\CloudCompare\5tl" #folder in which the .las files are
ref = r"C:/Users/Lars/Documents/CloudCompare/REF.ply"
CCdir =r"C:/Users/Lars/Documents/CloudCompare"
for dirName, subdirList, fileList in os.walk(filePath):
    for file in fileList:
        os.chdir(CCdir)
        os.system("CloudCompare -silent -C_EXPORT_FMT LAS -NO_TIMESTAMP -O " + file + " -O " + ref +' -ICP')
There are 2 problems:
1) when changing the directory of your CC-path ( os.chdir(CCdir) ), my 'file' and 'ref' variables are not in the path anymore. I solved this by putting the .ply and .las files under the same directory as CC. (A bit unorthodox, I know, but it works).
2) when going through the for-loop, ICP is correctly done for the first .las file generating a '_REGISTERED' file. Then the second .las file has to be registered, nothing happens and CC closes.

Any ideas?

Thanks a lot!
Lars
Post Reply