Passing the path to scene file as command line argument

Requests or suggestions for new features
Post Reply
Pouya
Posts: 12
Joined: 15 Feb 2013, 11:19

Passing the path to scene file as command line argument

Post by Pouya »

Hello all.

In our lab we mostly use v-rep for developing plugins and one feature that we are missing a lot, is possibility to launch v-rep and automatically load a scene. E.g. something like this:

Code: Select all

./vrep -f myscene.ttt
So that when v-rep is launched, it opens myscene.ttt instead of an empty environment.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Passing the path to scene file as command line argument

Post by coppelia »

Hello,

You can already do that, but you will have to leave out the -f argument, so on Linux, typically:

./vrep.sh scenes/myScene.ttt

Will load the file located in the relative path scenes/myScene.ttt. You can also specify an absolute path.

Cheers

basti35
Posts: 5
Joined: 08 Aug 2016, 10:21

Re: Passing the path to scene file as command line argument

Post by basti35 »

Hello,
this is my first post, so first of all I want to thanks all the coppelia team for your work. It's great!

I found that relative path have to be relative to the vrep install dir. Usually we have scenes that have to be aligned with some project versioning system, so it's unlikely that they are archived in the vrep install dir folder. I would like to have the possibility of launching scenes from their relative path.

I took some time to propose you a patch for the vrep.sh file. It checks if there is a scenes relative to the user working directory, make it absolute (by appending the relative path to the given scene). This is the diff:

Code: Select all

15,24c15
< if [ -f $1 ]
< then
<   if [ -f "$PWD/$1" ]
<   then
<     SCENE="$PWD/$1 "
<     shift
<   fi
< fi
< 
< LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$dirname
---
> LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$dirname"
26c17,19
< "$dirname/$appname" ${SCENE}${1+"$@"}
---
> "$dirname/$appname" "$@"
> 
> 
And this is the resulting script

Code: Select all

#!/bin/sh

thisscript="$0"
while [ -L "$thisscript" ]; do
        thisscript="`readlink "$thisscript"`"
done

dirname=`dirname "$thisscript"`
if [ $dirname = "." ]; then
        dirname="$PWD"
fi

appname="`basename "$thisscript" | sed 's,\.sh$,,'`"

if [ -f $1 ]
then
  if [ -f "$PWD/$1" ]
  then
    SCENE="$PWD/$1 "
    shift
  fi
fi

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$dirname
export LD_LIBRARY_PATH
"$dirname/$appname" ${SCENE}${1+"$@"}
Nothing else is changed but the fact that if the scene file exists with a path relative to the user working directory, the user working directory is added. Every other parameter is left as it was.
If the file does not exist as relative, $SCENE will be empty. Otherwise, it will contain the absolute path plus a space, to separate it from the other parameters (${1+"$@"}) when recalling them in the last command.
I hope it can be useful to someone.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Passing the path to scene file as command line argument

Post by coppelia »

Thanks for the input!

there is the additional difficulty that there can be other types of arguments, not just a file to load, and the order can be any.

Cheers

basti35
Posts: 5
Joined: 08 Aug 2016, 10:21

Re: Passing the path to scene file as command line argument

Post by basti35 »

With sh this becomes more difficult (probably require a lot of shifts to modify the exact arguments. With bash the following solution should work.

Code: Select all

#!/bin/bash

thisscript="$0"
while [ -L "$thisscript" ]; do
        thisscript="`readlink "$thisscript"`"
done

dirname=`dirname "$thisscript"`
if [ $dirname = "." ]; then
        dirname="$PWD"
fi

appname="`basename "$thisscript" | sed 's,\.sh$,,'`"

PARAMETERS=( ${@} )

FILE_PATTERN='*ttt'
for i in `seq 0 $(( ${#PARAMETERS[@]} -1 ))`
do
  if [ -f "${PARAMETERS[$i]}" ] && [[ "${PARAMETERS[$i]}" == $FILE_PATTERN ]]
  then
    if [ -f "$PWD/${PARAMETERS[$i]}" ]
    then
      PARAMETERS[$i]="$PWD/${PARAMETERS[$i]}"
    fi
  fi
done

LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$dirname
export LD_LIBRARY_PATH

"$dirname/$appname" "${PARAMETERS[@]}"
I put all the received arguments in PARAMETERS array. Then only the one which contains a path to an existing file and ends with ".ttt" are modified by adding the current working dir.

coppelia
Site Admin
Posts: 10339
Joined: 14 Dec 2012, 00:25

Re: Passing the path to scene file as command line argument

Post by coppelia »

That's perfect, thanks a lot!

Cheers

Post Reply