How do I spawn objects through a script & how do I call a script through the Python API

Typically: "How do I... ", "How can I... " questions
Post Reply
nebuchadnezzar
Posts: 2
Joined: 24 Jan 2017, 16:35

How do I spawn objects through a script & how do I call a script through the Python API

Post by nebuchadnezzar » 24 Jan 2017, 17:37

Hi all,

Little introduction:
I've installed V-REP and I'm trying to automatically generated 3D 'dungeons' (or mazes with rooms) in V-REP so my robot can drive around. A 2D example of a dungeon can be seen here: http://pygame.org/shots/2007.png.

Now I can generate these dungeons as simple occupancy grids in python. I then want to build the maze using simple Cuboids in V-REP. My idea is to build an algorithm (in Python) that tries to build a maze using as little as possible Cuboids. Then use the Python-VREP API to spawn these Cuboids at the correct places in Python.

One should notice this 'scene creation' is done offline - if my script generates a scene I will simply save it and use it later.

The Questions:
From fiddling around through a (unassociated) customization script containing the following:

Code: Select all

-- this is called just after this script was created (or reinitialized)
	-- Do some initialization here
    myStaticObjectHandle=simGetObjectHandle('Cuboid')
    worldframe=simGetObjectHandle('ResizableFloor_1_5')
    posx_0_1=0
    posy_0_1=0
    posz_0_1=2
    simSetObjectPosition(myStaticObjectHandle,worldframe,{posx_0_1,posy_0_1,posz_0_1})
I learned how to move stuff around. What I couldn't find is how do I actually spawn cuboids through the scripting interface? And how can I set the size properties? I could not find it :(

Second question:
I was having a problems calling functions through python. Through the examples I got a working connection between VREP and Python (simpleTest.py) but when I do a function call in python:

Code: Select all

        res,retInts,retFloats,retStrings,retBuffer=vrep.simxCallScriptFunction(clientID,'ResizableFloor_1_5',vrep.sim_scripttype_customizationscript,'displayText_function',[],[],['MyDummyName'],emptyBuff,vrep.simx_opmode_blocking)

I get a Error: [string -unknown location]:?: Call failed. (simCallScriptFunctionEx on displayText_function@ResizableFloor_1_5).

Now I get that this error originates from the fact that I have not defined a function displayText_function properly. I have an associated customization script (with ResizableFloor_1_5) but how do I define a function with the name displayText_function in Lua in VREP? I'm having trouble understanding where I define custom functions and how I should call them.

In the end I want a Python call that gives a size and a position of a cuboid and that in the LUA function I generate this cuboid using the information passed from the Python program.

Thanks for your time!

Justus
Posts: 42
Joined: 24 Jan 2017, 07:37

Re: How do I spawn objects through a script & how do I call a script through the Python API

Post by Justus » 24 Jan 2017, 20:05

Hi Nebuchadnezzar, (epic name by the way ;-)

Try spawning a simple cube this way:

Code: Select all

	-- Define 'constants' to improve code readability
	local CULLED_BACKFACES=1
	local VISIBLE_EDGES=2
	local APPEAR_SMOOTH=4
	local RESPONDABLE_SHAPE=8
	local STATIC_SHAPE=16
	local CYL_OPEN_ENDS=32

	local tblSize={1, 0.4, 0.1}
	local hndShape=simCreatePureShape(
		0, -- primitiveType
		VISIBLE_EDGES + RESPONDABLE_SHAPE + STATIC_SHAPE, --options
		tblSize, --sizes
		0, --mass
		nil --precision
		)
tblSize contains the size of the shape (X/Y/Z).
hndShape contains the handle to the spawned cuboid shape.

More info: http://www.coppeliarobotics.com/helpFil ... eShape.htm

Hope this helps!

Justus

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

Re: How do I spawn objects through a script & how do I call a script through the Python API

Post by coppelia » 25 Jan 2017, 09:13

Hello,

to your second question: you will have to put a function declaration in your customization script (e.g. at the very top of the script), something like:

Code: Select all

displayText_function=function(inInts,inFloats,inStrings,inBuffer)
    -- do something here
    return {},{},{},''
end
There is a simple python example located in programming/remoteApiBindings/python/python/complexCommandTest.py

Cheers

nebuchadnezzar
Posts: 2
Joined: 24 Jan 2017, 16:35

Re: How do I spawn objects through a script & how do I call a script through the Python API

Post by nebuchadnezzar » 25 Jan 2017, 09:49

Justus, coppelia,

Thanks for your input, I got a snippet working!

Will show the result once it's done.

Post Reply