braidTool_Maya_script_error # NameError: name 'QtGui' is not defined - python

I found someone else's braid tool online and it seems to be working for other people but it doesn't for me. I can't reach that person for help so I am posting here instead.
There's 2 scripts, one is the actual making of the braid and the other is for loading UI. I keep getting The following error:
# Error: Traceback (most recent call last):
# File "<maya console>", line 2, in <module>
# File "/home/ykim/private/maya/2018/scripts/JUM/scripts/braid.py", line 12, in <module>
# list_form, list_base = load_ui_type(ui_file)
# File "/home/ykim/private/maya/2018/scripts/JUM/core/loadUIFile.py", line 30, in load_ui_type
# form_class = frame['Ui_{0}'.format(form_class)]
# File "<string>", line 1, in <module>
# NameError: name 'QtGui' is not defined
#
The following is the 1st script making the braid
import os
from JUM.core.loadUIFile import get_maya_window, load_ui_type
import random
import maya.cmds as cmds
ui_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'UI','braid.ui')
list_form, list_base = load_ui_type(ui_file)
class espiral(list_form, list_base):
def __init__(self, parent = get_maya_window()):
self.__radius = 0.0
self.__side = 6
self.__variation = 0
self.__circle = ''
self.__pt_position_A = []
self.__pt_position_B = []
self.__pt_position_C = []
self.__path = ''
self.__quantidade = 0.0
self.clock = True
##########################################################
# #
# UI elements #
# #
##########################################################
self.window_name = 'makeSpiralWin'
if cmds.window(self.window_name, exists = True):
cmds.deleteUI(self.window_name)
super(espiral, self).__init__(parent)
self.setupUi(self)
self.btn_selectPath.clicked.connect(self.getPath)
self.btn_create.setDisabled(True)
self.btn_create.clicked.connect(self.create)
def __makeEight(self):
side = 16
offset = self.spin_offset.value()
eight = cmds.circle(nr=(0, 1, 0), c=(0, 0, 0), degree=3, sections=side)
cmds.select(eight[0])
lattice = cmds.lattice(dv = (3, 2, 3), objectCentered = True )
cmds.setAttr(lattice[0]+'.local', 0)
cmds.select(lattice[1]+'.pt[2][0:1][0]',lattice[1]+'.pt[2][0:1][1]',lattice[1]+'.pt[2][0:1][2]')
cmds.scale(1, 1, -1, pivot = (1.108194, 0 , 0), relative = True)
cmds.select(lattice[1]+'.pt[1][0:1][0]',lattice[1]+'.pt[1][0:1][1]',lattice[1]+'.pt[1][0:1][2]')
cmds.scale(0, 0, 0, pivot = (0, 0 , 0), relative = True)
cmds.select(lattice[1]+'.pt[0][0:1][0]',lattice[1]+'.pt[2][0:1][2]',lattice[1]+'.pt[2][0:1][0]',lattice[1]+'.pt[0][0:1][2]')
cmds.scale(1, 1, 1.455556, pivot = (0, 0 , 0), relative = True)
cmds.scale(0.929167, 1, 1, pivot = (0, 0 , 0), relative = True)
cmds.select(eight[0])
cmds.delete(ch = True)
cmds.rotate(0,offset,0,eight[0])
cmds.makeIdentity(eight[0],apply = True, t = True, r = True, s = True, n = 0, pn = True)
return eight
def __next(self, porcentagem,eight,scale):
#print porcentagem
curva = self.ln_path.text()
position = cmds.pointOnCurve(curva, top=True, pr=porcentagem, position=True)
tangent = cmds.pointOnCurve(curva, top=True, pr=porcentagem, tangent=True)
angle = cmds.angleBetween(er=True, v1=(0.0, 1.0, 0.0), v2=tangent)
cmds.scale((scale * random.uniform((1-self.spin_random.value()), 1.0)),
(scale * random.uniform((1-self.spin_random.value()), 1.0)),
(scale * random.uniform((1-self.spin_random.value()), 1.0)),
eight[0])
cmds.move(position[0],
position[1],
position[2],
eight[0])
cmds.rotate(angle[0],
angle[1],
angle[2],
eight[0])
def __voltas(self):
steps = 16 * float(self.spin_loops.value())
porcent = 1.0 / steps
return int(steps), porcent
def __makeMesh(self,curva):
scale_0 = self.spin_radius.value()
scale_1 = self.spin_radius_1.value()
scale = self.spin_radius.value()
if (scale_0 >= scale_1):
tempMaior = scale_0
tempMenor = scale_1
else:
tempMaior = scale_1
tempMenor = scale_0
scale_extrude = tempMenor/tempMaior
position = cmds.pointOnCurve(curva, top=True, pr=0, position=True)
tangent = cmds.pointOnCurve(curva, top=True, pr=0, normalizedTangent=True)
angle = cmds.angleBetween(er=True, v1=(0.0, 1.0, 0.0), v2=tangent)
circle = cmds.circle(nr=(0, 1, 0), c=(0, 0, 0), degree=3, sections=16, r = 0.5)
cmds.scale(tempMaior,
tempMaior,
tempMaior,
circle[0])
cmds.move(position[0],
position[1],
position[2],
circle[0])
cmds.rotate(angle[0],
angle[1],
angle[2],
circle[0])
extrude = cmds.extrude(circle[0],
curva,
constructionHistory = True,
range = True,
polygon = 0,
extrudeType = 2,
useComponentPivot = 0,
fixedPath = 0,
useProfileNormal = 1,
rotation = 0,
scale = scale_extrude,
reverseSurfaceIfPathReversed = 1)
poly = cmds.nurbsToPoly(extrude[0], matchNormalDir = True, constructionHistory = False, format = 2, polygonType = 1, uType = 3, uNumber = 1, vType = 3, vNumber = 3)
cmds.delete(circle, extrude[0])
print poly
return poly
def __curve(self):
curve_A = cmds.curve(p=self.__pt_position_A)
curve_B = cmds.curve(p=self.__pt_position_B)
curve_C = cmds.curve(p=self.__pt_position_C)
if (self.btn_makeMesh.isChecked()):
mesh_A = self.__makeMesh(curve_A)
mesh_B = self.__makeMesh(curve_B)
mesh_C = self.__makeMesh(curve_C)
cmds.delete(curve_A, curve_B, curve_C)
cmds.select(mesh_A,mesh_B,mesh_C)
else:
cmds.select(curve_A,curve_B,curve_C)
def __braid(self):
steps, porcent = self.__voltas()
increment = porcent
eight = self.__makeEight()
list = range(int(steps))
offset = self.spin_offset.value()
offset_normalize = offset/360.0
self.progress_Create.setRange(0,len(list))
scale_0 = self.spin_radius.value()
scale_1 = self.spin_radius_1.value()
if (scale_0 >= scale_1):
scale_maior = scale_0
scale_menor = scale_1
else:
scale_maior = scale_1
scale_menor = scale_0
diference = scale_maior-scale_menor
percent = diference/steps
scale = scale_maior
if (self.btn_reverse.isChecked()):
curva = self.ln_path.text()
cmds.reverseCurve(curva,ch = False, replaceOriginal = True)
for i in list:
self.progress_Create.setValue(i)
self.__next(porcent,eight,scale)
porcent += increment
_pos_A = (i*0.0625)%1.0 + offset_normalize
_pos_B = (i*0.0625+0.333333)%1.0 + offset_normalize
_pos_C = (i*0.0625+0.666666666)%1.0 + offset_normalize
self.__pt_position_A.append(cmds.pointOnCurve( eight[0],top = True, pr= _pos_A, p=True ))
self.__pt_position_B.append(cmds.pointOnCurve( eight[0],top = True, pr= _pos_B, p=True ))
self.__pt_position_C.append(cmds.pointOnCurve( eight[0],top = True, pr= _pos_C, p=True ))
scale -= percent
self.progress_Create.reset()
#cmds.delete(self.__circle[0])
# return self.__pt_position
self.__curve()
cmds.delete(eight[0])
def getPath(self):
path = cmds.ls(sl = True)
if path == []:
self.ln_path.setText('Nothing selected')
self.btn_create.setDisabled(True)
self.ln_path.setStyleSheet("background-color: rgb(110, 90, 90);")
else:
shape_path = cmds.listRelatives(path[0])
if (cmds.objectType(shape_path)== 'nurbsCurve'):
self.ln_path.setText(path[0])
self.btn_create.setEnabled(True)
self.ln_path.setStyleSheet("background-color: rgb(90, 150, 50);")
else:
self.ln_path.setText('Path not valid')
self.btn_create.setDisabled(True)
self.ln_path.setStyleSheet("background-color: rgb(110, 90, 90);")
def create(self):
self.__braid()
self.__pt_position_A = []
self.__pt_position_B = []
self.__pt_position_C = []
def run():
espira = espiral()
espira.show()
Another script is for loading UI design
import shiboken2
from PySide2 import QtWidgets
from PySide2 import QtGui
import maya.OpenMayaUI as apiUI
from cStringIO import StringIO
import pyside2uic
import xml.etree.ElementTree as xml
def get_maya_window():
ptr = apiUI.MQtUtil.mainWindow()
if ptr is not None:
return shiboken.wrapInstance(long(ptr), QtGui.QMainWindow)
def load_ui_type(ui_file):
parsed = xml.parse(ui_file)
widget_class = parsed.find('widget').get('class')
form_class = parsed.find('class').text
with open(ui_file,'r') as f:
o = StringIO()
frame = {}
pysideu2ic.compileUi(f, o, indent = 0)
pyc = compile(o.getvalue(), '<string>', 'exec')
exec pyc in frame
# Fetch the base_class and form class based on their type in the xml from design
form_class = frame['Ui_{0}'.format(form_class)]
base_class = eval('QtGui.{0}'.format(widget_class))
return form_class, base_class

Related

problem with moving data from device to host

Hi I'm trying to create an AI inspired NEAT but I ran into a problem after performing the calculation, the data from the device can't be moved to the host.
I'm trying to use cuda.synchronize () before moving the data, but in this case the error has already occurred on this line.
I tried the cuda features before and they worked without problems until now.
I'm attaching the code with an error.
Please can you help me?
code:
import os
# needs to appear before cuda import
os.environ["NUMBA_ENABLE_CUDASIM"] = "0"
# set to "1" for more debugging, but slower performance
os.environ["NUMBA_CUDA_DEBUGINFO"] = "0"
from numba import cuda
import numpy as np
from random import uniform
from pprint import pprint
#cuda.jit(device=True)
def LeakyRelu(x):
return max(x * 0.1, x)
#cuda.jit
def Calculate(set_io, many_neurons, many_inputs, reindex_io, memory_io, weights_io, biases_io, nets_info_io, auxmemort_io):
x = cuda.grid(1)
shape = set_io.shape
if x < shape[0]:
netidx = reindex_io[x, 0]
neuidx = reindex_io[x, 1]
weiidx = reindex_io[x, 2]
result = 0
for i in range(nets_info_io[netidx, 1]):
result += memory_io[nets_info_io[netidx, 0] + i] * weights_io[weiidx + i]
result += biases_io[x]
result = LeakyRelu(result)
auxmemort_io[neuidx] = result
def CalculateSlow():
pass
class ANET:
def __init__(self, many_inputs, many_outputs, many_networks, info = True, activation_function = LeakyRelu):
self.many_inputs = many_inputs
self.many_outputs = many_outputs
self.many_networks = many_networks
self.activation_function = activation_function
self.info = info
self.netid = -1
self.cuda_many_input = cuda.to_device(self.many_inputs)
if self.info:
print("starting Setup:\n|")
device = str(cuda.get_current_device()).split("'b'")[1].split("''")[0]
print(f"| cuda run on: {device}")
print(f"| generate genomes")
self.networks_genomes = [self._GenerateGenome() for _ in range(self.many_networks)]
self._BuildPopulation()
def _CudaPre(self, block, array):
griddim = tuple(np.array(array.shape) // block + 1)
blockdim = tuple(np.full_like(griddim, block))
return griddim, blockdim
def _GenerateGenome(self):
neurons_genome = [[self.activation_function, uniform(-1,1), i + self.many_inputs] for i in range(self.many_outputs)]
synapses_genome = [[[i, ii], uniform(-1,1), True] for i in range(self.many_inputs) for ii in range(self.many_inputs, self.many_outputs + self.many_inputs)]
self.netid += 1
return [neurons_genome, synapses_genome, self.netid]
def _BuildPopulation(self):
memory_len = sum([(len(i[0]) + self.many_inputs) for i in self.networks_genomes])
memory = np.zeros(memory_len, dtype=np.float64)
auxmemory = np.copy(memory)
weights_len = sum([(len(i[1]) + self.many_inputs + 1) for i in self.networks_genomes])
weights = np.zeros(weights_len, dtype=np.float64)
biases_len = sum([len(i[0]) for i in self.networks_genomes])
biases = np.zeros(biases_len, dtype=np.float64)
nets_info = np.zeros([self.many_networks, 5], dtype=np.int64)
movmem = 0
movwei = 0
movbia = 0
for idx,i in enumerate(self.networks_genomes):
nets_info[idx] = (movmem, len(i[0]) + self.many_inputs, movwei, movbia, i[2])
movmem += len(i[0]) + self.many_inputs
movwei += len(i[1]) + self.many_inputs + 1
movbia += len(i[0])
biaidx = 0
for genome, net_info in zip(self.networks_genomes, nets_info):
for gen, biagen in zip(genome[1], genome[0]):
if gen[2]:
if gen[0][0] < self.many_inputs:
target = gen[0][0]
else:
target = genome[0][gen[0][0] - self.many_inputs - self.many_outputs][2] + self.many_inputs
weights[(gen[0][1] - self.many_inputs) * net_info[1] + net_info[2] + target] = gen[1]
biases[biaidx] = biagen[1]
biaidx += 1
reindex = np.empty([sum([(i[1] - self.many_inputs) for i in nets_info]), 3], dtype=np.int64)
write = 0
weiidx = 0
for idx,inf in enumerate(nets_info):
for i in range(inf[1] - self.many_inputs):
reindex[write][0] = idx
reindex[write][1] = inf[0] + i + self.many_inputs
reindex[write][2] = weiidx
weiidx += inf[1]
write += 1
if self.info:
print("| move arrays to device")
self.memory = memory
self.cuda_auxmemory = cuda.to_device(auxmemory)
print(self.cuda_auxmemory.copy_to_host())
self.cuda_weights = cuda.to_device(weights)
self.cuda_biases = cuda.to_device(biases)
self.nets_info = nets_info
self.cuda_nets_info = cuda.to_device(nets_info)
self.many_neurons = np.sum(nets_info, axis=0)[1]
self.cuda_many_neurons = cuda.to_device(self.many_neurons)
self.cuda_reindex = cuda.to_device(reindex)
# print(nets_info)
# print(f"{memory_len} | {weights_len} | {biases_len}")
def MathPopulation(self, inputs):
for idx, inp in enumerate(inputs):
self.memory[self.nets_info[idx][0]: self.nets_info[idx][0] + self.many_inputs] = inp
self.cuda_memory = cuda.to_device(self.memory)
setlen = cuda.to_device(np.zeros(self.many_neurons))
Calculate[self._CudaPre(32, np.empty([self.many_neurons]))](setlen, self.cuda_many_neurons, self.cuda_many_input, self.cuda_reindex, self.cuda_memory, self.cuda_weights, self.cuda_biases, self.cuda_nets_info, self.cuda_auxmemory)
# cuda.synchronize()
arr = self.cuda_auxmemory.copy_to_host()
print(arr)
if __name__ == '__main__':
anet = ANET(many_inputs = 3, many_outputs = 2, many_networks = 5)
# pprint(anet._GenerateGenome())
anet.MathPopulation([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]])
error:
Traceback (most recent call last):
File "c:\Users\Ondra\Documents\Pythonporno\ANET\ANET.py", line 144, in <module>
anet.MathPopulation([[1,2,3], [4,5,6], [7,8,9], [10,11,12], [13,14,15]])
File "c:\Users\Ondra\Documents\Pythonporno\ANET\ANET.py", line 138, in MathPopulation
arr = self.cuda_auxmemory.copy_to_host()
File "C:\Users\Ondra\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\cuda\cudadrv\devices.py", line 232, in _require_cuda_context
return fn(*args, **kws)
File "C:\Users\Ondra\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\cuda\cudadrv\devicearray.py", line 277, in copy_to_host
_driver.device_to_host(hostary, self, self.alloc_size,
File "C:\Users\Ondra\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\cuda\cudadrv\driver.py", line 2998, in device_to_host
fn(host_pointer(dst), device_pointer(src), size, *varargs)
File "C:\Users\Ondra\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\cuda\cudadrv\driver.py", line 319, in safe_cuda_api_call
self._check_ctypes_error(fname, retcode)
File "C:\Users\Ondra\AppData\Local\Programs\Python\Python310\lib\site-packages\numba\cuda\cudadrv\driver.py", line 384, in _check_ctypes_error
raise CudaAPIError(retcode, msg)
numba.cuda.cudadrv.driver.CudaAPIError: [700] Call to cuMemcpyDtoH results in UNKNOWN_CUDA_ERROR

Attribute Error: module 'uctypes' has no attribute 'INT32'

When I try to run rcReceiver_IBUS.py, I am getting this error:
module 'uctypes' has no attribute 'INT32'
timer.py line 17
Your program tries to access attribute INT32 of an object of type 'uctypes', but this type doesn't have such attribute.
.
rcReceiver_IBUS.py
from machine import UART
import time
buffer=bytearray(31)
uart = UART(1, 115200) # uart1 tx-pin 4, rx-pin 5
while True:
c=uart.read(1)
if c[0] == 0x20:
uart.readinto(buffer)
checksum = 0xffff - 0x20
for i in range(29): checksum -= buffer[i]
if checksum == (buffer[30] << 8) | buffer[29]:
# skip buffer[0] = 0x40
ch1 = buffer[2]*255+buffer[1]
ch2 = buffer[4]*255+buffer[3]
ch3 = buffer[6]*255+buffer[5]
ch4 = buffer[8]*255+buffer[7]
ch5 = buffer[10]*255+buffer[9]
ch6 = buffer[12]*255+buffer[11]
print('ch 1-',ch1,' 2-',ch2,' 3-',ch3,' 4-',ch4,' 5-',ch5,' 6-',ch6)
.
timer.py
import ffilib
import uctypes
import array
import uos
import os
import utime
from signal import *
libc = ffilib.libc()
librt = ffilib.open("librt")
CLOCK_REALTIME = 0
CLOCK_MONOTONIC = 1
SIGEV_SIGNAL = 0
sigval_t = {
"sival_int": uctypes.INT32 | 0,
"sival_ptr": (uctypes.PTR | 0, uctypes.UINT8),
}
sigevent_t = {
"sigev_value": (0, sigval_t),
"sigev_signo": uctypes.INT32 | 8,
"sigev_notify": uctypes.INT32 | 12,
}
timespec_t = {
"tv_sec": uctypes.INT32 | 0,
"tv_nsec": uctypes.INT64 | 8,
}
itimerspec_t = {
"it_interval": (0, timespec_t),
"it_value": (16, timespec_t),
}
__libc_current_sigrtmin = libc.func("i", "__libc_current_sigrtmin", "")
SIGRTMIN = __libc_current_sigrtmin()
timer_create_ = librt.func("i", "timer_create", "ipp")
timer_settime_ = librt.func("i", "timer_settime", "PiPp")
def new(sdesc):
buf = bytearray(uctypes.sizeof(sdesc))
s = uctypes.struct(uctypes.addressof(buf), sdesc, uctypes.NATIVE)
return s
def timer_create(sig_id):
sev = new(sigevent_t)
#print(sev)
sev.sigev_notify = SIGEV_SIGNAL
sev.sigev_signo = SIGRTMIN + sig_id
timerid = array.array('P', [0])
r = timer_create_(CLOCK_MONOTONIC, sev, timerid)
os.check_error(r)
#print("timerid", hex(timerid[0]))
return timerid[0]
def timer_settime(tid, hz):
period = 1000000000 // hz
new_val = new(itimerspec_t)
new_val.it_value.tv_nsec = period
new_val.it_interval.tv_nsec = period
#print("new_val:", bytes(new_val))
old_val = new(itimerspec_t)
#print(new_val, old_val)
r = timer_settime_(tid, 0, new_val, old_val)
os.check_error(r)
#print("old_val:", bytes(old_val))
#print("timer_settime", r)
class Timer:
def __init__(self, id, freq):
self.id = id
self.tid = timer_create(id)
self.freq = freq
def callback(self, cb):
self.cb = cb
timer_settime(self.tid, self.freq)
org_sig = signal(SIGRTMIN + self.id, self.handler)
#print("Sig %d: %s" % (SIGRTMIN + self.id, org_sig))
def handler(self, signum):
#print('Signal handler called with signal', signum)
self.cb(self)
But when I open uctypes.py, INT32 is in the file. It seems there is no problem in file. What am I missing?
.
uctypes.py
ARRAY = -1073741824
BFINT16 = -671088640
BFINT32 = -402653184
BFINT8 = -939524096
BFUINT16 = -805306368
BFUINT32 = -536870912
BFUINT8 = -1073741824
BF_LEN = 22
BF_POS = 17
BIG_ENDIAN = 1
FLOAT32 = -268435456
FLOAT64 = -134217728
INT16 = 402653184
INT32 = 671088640
INT64 = 939524096
INT8 = 134217728
LITTLE_ENDIAN = 0
NATIVE = 2
PTR = 536870912
UINT16 = 268435456
UINT32 = 536870912
UINT64 = 805306368
UINT8 = 0
VOID = 0
def addressof():
pass
def bytearray_at():
pass
def bytes_at():
pass
def sizeof():
pass
class struct:
""

How to implement a lane change manoeuver on Carla

I am trying to implement a simple lane change manoeuver for a self driving car on Carla simulator. Specifically a left lane change.
However when retrieving waypoints from the left lane ( using carla.Waypoint.get_left_lane() function ), the waypoints I get are oscillating between left and right lanes. Below is the script I used :
import sys
import glob
import os
try:
sys.path.append(glob.glob('../../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
import time
import math
import numpy as np
import random
from agents.navigation.controller import VehiclePIDController
dt = 1.0 / 20.0
args_lateral_dict = {'K_P': 1.95, 'K_I': 0.05, 'K_D': 0.2, 'dt': dt}
args_longitudinal_dict = {'K_P': 1.0, 'K_I': 0.05, 'K_D': 0, 'dt': dt}
max_throt = 0.75
max_brake = 0.3
max_steer = 0.8
offset = 0
VEHICLE_VEL = 5
actorList = []
try:
client = carla.Client("localhost",2000)
client.set_timeout(10.0)
world = client.load_world("Town02")
spectator = world.get_spectator()
actorList.append(spectator)
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 1/20
world.apply_settings(settings)
blueprint_library = world.get_blueprint_library()
vehicle_bp = blueprint_library.filter("cybertruck")[0]
vehicle = None
while(vehicle is None):
spawn_points = world.get_map().get_spawn_points()
spawn_point = random.choice(spawn_points) if spawn_points else carla.Transform()
vehicle = world.try_spawn_actor(vehicle_bp, spawn_point)
actorList.append(vehicle)
vehicle_controller = VehiclePIDController(vehicle,
args_lateral=args_lateral_dict,
args_longitudinal=args_longitudinal_dict,
offset=offset,
max_throttle=max_throt,
max_brake=max_brake,
max_steering=max_steer)
old_yaw = math.radians(vehicle.get_transform().rotation.yaw)
old_x = vehicle.get_transform().location.x
old_y = vehicle.get_transform().location.y
```
spectator_transform = vehicle.get_transform()
spectator_transform.location += carla.Location(x = 10, y= 0, z = 5.0)
control = carla.VehicleControl()
control.throttle = 1.0
vehicle.apply_control(control)
while True:
####### Im suspecting the bug is within this portion of code ########################
current_waypoint = world.get_map().get_waypoint(vehicle.get_location())
# if not turned_left :
left_waypoint = current_waypoint.get_left_lane()
if(left_waypoint is not None and left_waypoint.lane_type == carla.LaneType.Driving) :
target_waypoint = left_waypoint.previous(0.3)[0]
turned_left = True
else :
if(turned_left) :
target_waypoint = waypoint.previous(0.3)[0]
else : # I tryed commenting this else section but the bug is still present so I dont suspect the bug relates to this else part
target_waypoint = waypoint.next(0.3)[0]
################### End of suspected bug ############################################
world.debug.draw_string(target_waypoint.transform.location, 'O', draw_shadow=False,
color=carla.Color(r=255, g=0, b=0), life_time=120.0,
persistent_lines=True)
control_signal = vehicle_controller.run_step(VEHICLE_VEL,target_waypoint)
vehicle.apply_control(control_signal)
new_yaw = math.radians(vehicle.get_transform().rotation.yaw)
spectator_transform = vehicle.get_transform()
spectator_transform.location += carla.Location(x = -10*math.cos(new_yaw), y= -10*math.sin(new_yaw), z = 5.0)
spectator.set_transform(spectator_transform)
world.tick()
finally:
print("Destroying actors")
client.apply_batch([carla.command.DestroyActor(x) for x in actorList])
I just figured out the cause of the problem. The reason is that I was manipulating Carla waypoints as undirected points. However, in Carla, each waypoint is directed by the road direction.
In the scene, I was testing my code in, all the roads have two lanes, each one in an opposite direction. Hence, the left lane of each lane is the remaining lane.
The issue in the previous code was that I was not changing my view to match the direction of the lane. I was assuming a global reference frame, but in Carla, the waypoints are relative to the frames attached to their respective lanes. And since only one of the two coordinate frames (for each lane) was matching my imagined global reference frame, I was getting an oscillatory behavior.
Another issue was that I was updating the target waypoints to track too early. This caused the vehicle to move a very short distance without going through all the target waypoints. I changed this to keep tracking the same target waypoint until the distance separating it to my vehicle becomes too close before moving to the next waypoint. This helped to perform the lane change behavior.
Below is the script I used :
import sys
import glob
import os
#The added path depends on where the carla binaries are stored
try:
sys.path.append(glob.glob('../../carla/dist/carla-*%d.%d-%s.egg' % (
sys.version_info.major,
sys.version_info.minor,
'win-amd64' if os.name == 'nt' else 'linux-x86_64'))[0])
except IndexError:
pass
import carla
import time
import math
import numpy as np
import random
from agents.navigation.controller import VehiclePIDController
VEHICLE_VEL = 5
class Player():
def __init__(self, world, bp, vel_ref = VEHICLE_VEL, max_throt = 0.75, max_brake = 0.3, max_steer = 0.8):
self.world = world
self.max_throt = max_throt
self.max_brake = max_brake
self.max_steer = max_steer
self.vehicle = None
self.bp = bp
while(self.vehicle is None):
spawn_points = world.get_map().get_spawn_points()
spawn_point = random.choice(spawn_points) if spawn_points else carla.Transform()
self.vehicle = world.try_spawn_actor(vehicle_bp, spawn_point)
self.spectator = world.get_spectator()
dt = 1.0 / 20.0
args_lateral_dict = {'K_P': 1.95, 'K_I': 0.05, 'K_D': 0.2, 'dt': dt}
args_longitudinal_dict = {'K_P': 1.0, 'K_I': 0.05, 'K_D': 0, 'dt': dt}
offset = 0
self.controller = VehiclePIDController(self.vehicle,
args_lateral=args_lateral_dict,
args_longitudinal=args_longitudinal_dict,
offset=offset,
max_throttle=max_throt,
max_brake=max_brake,
max_steering=max_steer)
self.vel_ref = vel_ref
self.waypointsList = []
self.current_pos = self.vehicle.get_transform().location
self.past_pos = self.vehicle.get_transform().location
def dist2Waypoint(self, waypoint):
vehicle_transform = self.vehicle.get_transform()
vehicle_x = vehicle_transform.location.x
vehicle_y = vehicle_transform.location.y
waypoint_x = waypoint.transform.location.x
waypoint_y = waypoint.transform.location.y
return math.sqrt((vehicle_x - waypoint_x)**2 + (vehicle_y - waypoint_y)**2)
def go2Waypoint(self, waypoint, draw_waypoint = True, threshold = 0.3):
if draw_waypoint :
# print(" I draw")
self.world.debug.draw_string(waypoint.transform.location, 'O', draw_shadow=False,
color=carla.Color(r=255, g=0, b=0), life_time=10.0,
persistent_lines=True)
current_pos_np = np.array([self.current_pos.x,self.current_pos.y])
past_pos_np = np.array([self.past_pos.x,self.past_pos.y])
waypoint_np = np.array([waypoint.transform.location.x,waypoint.transform.location.y])
vec2wp = waypoint_np - current_pos_np
motion_vec = current_pos_np - past_pos_np
dot = np.dot(vec2wp,motion_vec)
if (dot >=0):
while(self.dist2Waypoint(waypoint) > threshold) :
control_signal = self.controller.run_step(self.vel_ref,waypoint)
self.vehicle.apply_control(control_signal)
self.update_spectator()
def getLeftLaneWaypoints(self, offset = 2*VEHICLE_VEL, separation = 0.3):
current_waypoint = self.world.get_map().get_waypoint(self.vehicle.get_location())
left_lane = current_waypoint.get_left_lane()
self.waypointsList = left_lane.previous(offset)[0].previous_until_lane_start(separation)
def getRightLaneWaypoints(self, offset = 2*VEHICLE_VEL, separation = 0.3):
current_waypoint = self.world.get_map().get_waypoint(self.vehicle.get_location())
right_lane = current_waypoint.get_left_lane()
self.waypointsList = right_lane.next(offset)[0].next_until_lane_end(separation)
def do_left_lane_change(self):
self.getLeftLaneWaypoints()
for i in range(len(self.waypointsList)-1):
self.current_pos = self.vehicle.get_location()
self.go2Waypoint(self.waypointsList[i])
self.past_pos = self.current_pos
self.update_spectator()
def do_right_lane_change(self):
self.getRightLaneWaypoints()
for i in range(len(self.waypointsList)-1)
self.current_pos = self.vehicle.get_location()
self.go2Waypoint(self.waypointsList[i])
self.past_pos = self.current_pos
self.update_spectator()
def update_spectator(self):
new_yaw = math.radians(self.vehicle.get_transform().rotation.yaw)
spectator_transform = self.vehicle.get_transform()
spectator_transform.location += carla.Location(x = -10*math.cos(new_yaw), y= -10*math.sin(new_yaw), z = 5.0)
self.spectator.set_transform(spectator_transform)
self.world.tick()
def is_waypoint_in_direction_of_motion(self,waypoint):
current_pos = self.vehicle.get_location()
def draw_waypoints(self):
for waypoint in self.waypointsList:
self.world.debug.draw_string(waypoint.transform.location, 'O', draw_shadow=False,
color=carla.Color(r=255, g=0, b=0), life_time=10.0,
persistent_lines=True)
dt = 1.0 / 20.0
args_lateral_dict = {'K_P': 1.95, 'K_I': 0.05, 'K_D': 0.2, 'dt': dt}
args_longitudinal_dict = {'K_P': 1.0, 'K_I': 0.05, 'K_D': 0, 'dt': dt}
offset = 0
VEHICLE_VEL = 10
actorList = []
try:
client = carla.Client("localhost",2000)
client.set_timeout(10.0)
world = client.load_world("Town02")
spectator = world.get_spectator()
actorList.append(spectator)
settings = world.get_settings()
settings.synchronous_mode = True
settings.fixed_delta_seconds = 1/20
world.apply_settings(settings)
blueprint_library = world.get_blueprint_library()
vehicle_bp = blueprint_library.filter("cybertruck")[0]
player = Player(world, vehicle_bp)
actorList.append(player.vehicle)
actorList.append(player.spectator)
while(True):
player.update_spectator()
manoeuver = input("Enter manoeuver: ")
if ( manoeuver == "l"): #Perform left lane change
player.do_left_lane_change()
elif (manoeuver == "r"): #Perform right lane change
player.do_right_lane_change()
elif (manoeuver == "d"): #Just draws the waypoints for debugging purpose
player.getLeftLaneWaypoints()
player.draw_waypoints()
finally:
print("Destroying actors")
client.apply_batch([carla.command.DestroyActor(x) for x in actorList])

How to add a member function to an existing Python object?

Previously I created a lot of Python objects of class A, and I would like to add a new function plotting_in_PC_space_with_coloring_option() (the purpose of this function is to plot some data in this object) to class A and use those old objects to call plotting_in_PC_space_with_coloring_option().
An example is:
import copy
import numpy as np
from math import *
from pybrain.structure import *
from pybrain.supervised.trainers import BackpropTrainer
from pybrain.datasets.supervised import SupervisedDataSet
import pickle
import neural_network_related
class A(object):
"""the neural network for simulation"""
'''
todo:
- find boundary
- get_angles_from_coefficients
'''
def __init__(self,
index, # the index of the current network
list_of_coor_data_files, # accept multiple files of training data
energy_expression_file, # input, output files
preprocessing_settings = None,
connection_between_layers = None, connection_with_bias_layers = None,
PCs = None, # principal components
):
self._index = index
self._list_of_coor_data_files = list_of_coor_data_files
self._energy_expression_file = energy_expression_file
self._data_set = []
for item in list_of_coor_data_files:
self._data_set += self.get_many_cossin_from_coordiantes_in_file(item)
self._preprocessing_settings = preprocessing_settings
self._connection_between_layers = connection_between_layers
self._connection_with_bias_layers = connection_with_bias_layers
self._node_num = [8, 15, 2, 15, 8]
self._PCs = PCs
def save_into_file(self, filename = None):
if filename is None:
filename = "network_%s.pkl" % str(self._index) # by default naming with its index
with open(filename, 'wb') as my_file:
pickle.dump(self, my_file, pickle.HIGHEST_PROTOCOL)
return
def get_cossin_from_a_coordinate(self, a_coordinate):
num_of_coordinates = len(a_coordinate) / 3
a_coordinate = np.array(a_coordinate).reshape(num_of_coordinates, 3)
diff_coordinates = a_coordinate[1:num_of_coordinates, :] - a_coordinate[0:num_of_coordinates - 1,:] # bond vectors
diff_coordinates_1=diff_coordinates[0:num_of_coordinates-2,:];diff_coordinates_2=diff_coordinates[1:num_of_coordinates-1,:]
normal_vectors = np.cross(diff_coordinates_1, diff_coordinates_2);
normal_vectors_normalized = np.array(map(lambda x: x / sqrt(np.dot(x,x)), normal_vectors))
normal_vectors_normalized_1 = normal_vectors_normalized[0:num_of_coordinates-3, :];normal_vectors_normalized_2 = normal_vectors_normalized[1:num_of_coordinates-2,:];
diff_coordinates_mid = diff_coordinates[1:num_of_coordinates-2]; # these are bond vectors in the middle (remove the first and last one), they should be perpendicular to adjacent normal vectors
cos_of_angles = range(len(normal_vectors_normalized_1))
sin_of_angles_vec = range(len(normal_vectors_normalized_1))
sin_of_angles = range(len(normal_vectors_normalized_1)) # initialization
for index in range(len(normal_vectors_normalized_1)):
cos_of_angles[index] = np.dot(normal_vectors_normalized_1[index], normal_vectors_normalized_2[index])
sin_of_angles_vec[index] = np.cross(normal_vectors_normalized_1[index], normal_vectors_normalized_2[index])
sin_of_angles[index] = sqrt(np.dot(sin_of_angles_vec[index], sin_of_angles_vec[index])) * np.sign(sum(sin_of_angles_vec[index]) * sum(diff_coordinates_mid[index]));
return cos_of_angles + sin_of_angles
def get_many_cossin_from_coordinates(self, coordinates):
return map(self.get_cossin_from_a_coordinate, coordinates)
def get_many_cossin_from_coordiantes_in_file (self, filename):
coordinates = np.loadtxt(filename)
return self.get_many_cossin_from_coordinates(coordinates)
def mapminmax(self, my_list): # for preprocessing in network
my_min = min(my_list)
my_max = max(my_list)
mul_factor = 2.0 / (my_max - my_min)
offset = (my_min + my_max) / 2.0
result_list = np.array(map(lambda x : (x - offset) * mul_factor, my_list))
return (result_list, (mul_factor, offset)) # also return the parameters for processing
def get_mapminmax_preprocess_result_and_coeff(self,data=None):
if data is None:
data = self._data_set
data = np.array(data)
data = np.transpose(data)
result = []; params = []
for item in data:
temp_result, preprocess_params = self.mapminmax(item)
result.append(temp_result)
params.append(preprocess_params)
return (np.transpose(np.array(result)), params)
def mapminmax_preprocess_using_coeff(self, input_data=None, preprocessing_settings=None):
# try begin
if preprocessing_settings is None:
preprocessing_settings = self._preprocessing_settings
temp_setttings = np.transpose(np.array(preprocessing_settings))
result = []
for item in input_data:
item = np.multiply(item - temp_setttings[1], temp_setttings[0])
result.append(item)
return result
# try end
def get_expression_of_network(self, connection_between_layers=None, connection_with_bias_layers=None):
if connection_between_layers is None:
connection_between_layers = self._connection_between_layers
if connection_with_bias_layers is None:
connection_with_bias_layers = self._connection_with_bias_layers
node_num = self._node_num
expression = ""
# first part: network
for i in range(2):
expression = '\n' + expression
mul_coef = connection_between_layers[i].params.reshape(node_num[i + 1], node_num[i])
bias_coef = connection_with_bias_layers[i].params
for j in range(np.size(mul_coef, 0)):
temp_expression = 'layer_%d_unit_%d = tanh( ' % (i + 1, j)
for k in range(np.size(mul_coef, 1)):
temp_expression += ' %f * layer_%d_unit_%d +' % (mul_coef[j, k], i, k)
temp_expression += ' %f);\n' % (bias_coef[j])
expression = temp_expression + expression # order of expressions matter in OpenMM
# second part: definition of inputs
index_of_backbone_atoms = [2, 5, 7, 9, 15, 17, 19];
for i in range(len(index_of_backbone_atoms) - 3):
index_of_coss = i
index_of_sins = i + 4
expression += 'layer_0_unit_%d = (raw_layer_0_unit_%d - %f) * %f;\n' % \
(index_of_coss, index_of_coss, self._preprocessing_settings[index_of_coss][1], self._preprocessing_settings[index_of_coss][0])
expression += 'layer_0_unit_%d = (raw_layer_0_unit_%d - %f) * %f;\n' % \
(index_of_sins, index_of_sins, self._preprocessing_settings[index_of_sins][1], self._preprocessing_settings[index_of_sins][0])
expression += 'raw_layer_0_unit_%d = cos(dihedral_angle_%d);\n' % (index_of_coss, i)
expression += 'raw_layer_0_unit_%d = sin(dihedral_angle_%d);\n' % (index_of_sins, i)
expression += 'dihedral_angle_%d = dihedral(p%d, p%d, p%d, p%d);\n' % \
(i, index_of_backbone_atoms[i], index_of_backbone_atoms[i+1],index_of_backbone_atoms[i+2],index_of_backbone_atoms[i+3])
return expression
def write_expression_into_file(self, out_file = None):
if out_file is None: out_file = self._energy_expression_file
expression = self.get_expression_of_network()
with open(out_file, 'w') as f_out:
f_out.write(expression)
return
def get_mid_result(self, input_data=None, connection_between_layers=None, connection_with_bias_layers=None):
if input_data is None: input_data = self._data_set
if connection_between_layers is None: connection_between_layers = self._connection_between_layers
if connection_with_bias_layers is None: connection_with_bias_layers = self._connection_with_bias_layers
node_num = self._node_num
temp_mid_result = range(4)
mid_result = []
# first need to do preprocessing
for item in self.mapminmax_preprocess_using_coeff(input_data, self._preprocessing_settings):
for i in range(4):
mul_coef = connection_between_layers[i].params.reshape(node_num[i + 1], node_num[i]) # fix node_num
bias_coef = connection_with_bias_layers[i].params
previous_result = item if i == 0 else temp_mid_result[i - 1]
temp_mid_result[i] = np.dot(mul_coef, previous_result) + bias_coef
if i != 3: # the last output layer is a linear layer, while others are tanh layers
temp_mid_result[i] = map(tanh, temp_mid_result[i])
mid_result.append(copy.deepcopy(temp_mid_result)) # note that should use deepcopy
return mid_result
def get_PC_and_save_it_to_network(self):
'''get PCs and save the result into _PCs
'''
mid_result = self.get_mid_result()
self._PCs = [item[1] for item in mid_result]
return
def train(self):
####################### set up autoencoder begin #######################
node_num = self._node_num
in_layer = LinearLayer(node_num[0], "IL")
hidden_layers = [TanhLayer(node_num[1], "HL1"), TanhLayer(node_num[2], "HL2"), TanhLayer(node_num[3], "HL3")]
bias_layers = [BiasUnit("B1"),BiasUnit("B2"),BiasUnit("B3"),BiasUnit("B4")]
out_layer = LinearLayer(node_num[4], "OL")
layer_list = [in_layer] + hidden_layers + [out_layer]
molecule_net = FeedForwardNetwork()
molecule_net.addInputModule(in_layer)
for item in (hidden_layers + bias_layers):
molecule_net.addModule(item)
molecule_net.addOutputModule(out_layer)
connection_between_layers = range(4); connection_with_bias_layers = range(4)
for i in range(4):
connection_between_layers[i] = FullConnection(layer_list[i], layer_list[i+1])
connection_with_bias_layers[i] = FullConnection(bias_layers[i], layer_list[i+1])
molecule_net.addConnection(connection_between_layers[i]) # connect two neighbor layers
molecule_net.addConnection(connection_with_bias_layers[i])
molecule_net.sortModules() # this is some internal initialization process to make this module usable
####################### set up autoencoder end #######################
trainer = BackpropTrainer(molecule_net, learningrate=0.002,momentum=0.4,verbose=False, weightdecay=0.1, lrdecay=1)
data_set = SupervisedDataSet(node_num[0], node_num[4])
sincos = self._data_set
(sincos_after_process, self._preprocessing_settings) = self.get_mapminmax_preprocess_result_and_coeff(data = sincos)
for item in sincos_after_process: # is it needed?
data_set.addSample(item, item)
trainer.trainUntilConvergence(data_set, maxEpochs=50)
self._connection_between_layers = connection_between_layers
self._connection_with_bias_layers = connection_with_bias_layers
print("Done!\n")
return
def create_sge_files_for_simulation(self,potential_centers = None):
if potential_centers is None:
potential_centers = self.get_boundary_points()
neural_network_related.create_sge_files(potential_centers)
return
def get_boundary_points(self, list_of_points = None, num_of_bins = 5):
if list_of_points is None: list_of_points = self._PCs
x = [item[0] for item in list_of_points]
y = [item[1] for item in list_of_points]
temp = np.histogram2d(x,y, bins=[num_of_bins, num_of_bins])
hist_matrix = temp[0]
# add a set of zeros around this region
hist_matrix = np.insert(hist_matrix, num_of_bins, np.zeros(num_of_bins), 0)
hist_matrix = np.insert(hist_matrix, 0, np.zeros(num_of_bins), 0)
hist_matrix = np.insert(hist_matrix, num_of_bins, np.zeros(num_of_bins + 2), 1)
hist_matrix = np.insert(hist_matrix, 0, np.zeros(num_of_bins +2), 1)
hist_matrix = (hist_matrix != 0).astype(int)
sum_of_neighbors = np.zeros(np.shape(hist_matrix)) # number of neighbors occupied with some points
for i in range(np.shape(hist_matrix)[0]):
for j in range(np.shape(hist_matrix)[1]):
if i != 0: sum_of_neighbors[i,j] += hist_matrix[i - 1][j]
if j != 0: sum_of_neighbors[i,j] += hist_matrix[i][j - 1]
if i != np.shape(hist_matrix)[0] - 1: sum_of_neighbors[i,j] += hist_matrix[i + 1][j]
if j != np.shape(hist_matrix)[1] - 1: sum_of_neighbors[i,j] += hist_matrix[i][j + 1]
bin_width_0 = temp[1][1]-temp[1][0]
bin_width_1 = temp[2][1]-temp[2][0]
min_coor_in_PC_space_0 = temp[1][0] - 0.5 * bin_width_0 # multiply by 0.5 since we want the center of the grid
min_coor_in_PC_space_1 = temp[2][0] - 0.5 * bin_width_1
potential_centers = []
for i in range(np.shape(hist_matrix)[0]):
for j in range(np.shape(hist_matrix)[1]):
if hist_matrix[i,j] == 0 and sum_of_neighbors[i,j] != 0: # no points in this block but there are points in neighboring blocks
temp_potential_center = [round(min_coor_in_PC_space_0 + i * bin_width_0, 2), round(min_coor_in_PC_space_1 + j * bin_width_1, 2)]
potential_centers.append(temp_potential_center)
return potential_centers
# this function is added after those old objects of A were created
def plotting_in_PC_space_with_coloring_option(self,
list_of_coordinate_files_for_plotting=None, # accept multiple files
color_option='pure'):
'''
by default, we are using training data, and we also allow external data input
'''
if list_of_coordinate_files_for_plotting is None:
PCs_to_plot = self._PCs
else:
temp_sincos = []
for item in list_of_coordinate_files_for_plotting:
temp_sincos += self.get_many_cossin_from_coordiantes_in_file(item)
temp_mid_result = self.get_mid_result(input_data = temp_sincos)
PCs_to_plot = [item[1] for item in temp_mid_result]
(x, y) = ([item[0] for item in PCs_to_plot], [item[1] for item in PCs_to_plot])
# coloring
if color_option == 'pure':
coloring = 'red'
elif color_option == 'step':
coloring = range(len(x))
fig, ax = plt.subplots()
ax.scatter(x,y, c=coloring)
ax.set_xlabel("PC1")
ax.set_ylabel("PC2")
plt.show()
return
But it seems that plotting_in_PC_space_with_coloring_option() was not binded to those old objects, is here any way to fix it (I do not want to recreate these objects since creation involves CPU-intensive calculation and would take very long time to do it)?
Thanks!
Something like this:
class A:
def q(self): print 1
a = A()
def f(self): print 2
setattr(A, 'f', f)
a.f()
This is called a monkey patch.

Raname script for Python in Maya

I have this script and I want to parent the controls together and rename the icons. Right now it names the groups, but doesn't name the controls or parents them. Can anyone help me?
def priming(*args):
jointChain = pm.ls(sl = True, dag = True)
jointChain.pop(-1)
ori = raw_input()
systemName = raw_input()
suffix = "prime"
prime1Name = "{0}_{1}_00_{2}1".format(ori, systemName, suffix)
prime2Name = "{0}_{1}_00_{2}2".format(ori, systemName, suffix)
pm.select(cl = True)
for jointName in jointChain:
primeIcon = pm.circle(nr = [1, 0, 0])
groupOne = pm.group(em = True, n = prime1Name)
groupTwo = pm.group(em = True, n = prime2Name)
pm.parent(groupTwo, groupOne)
pm.parent(primeIcon, groupTwo)
tempConstraint = pm.parentConstraint(jointName, groupOne, mo = False)
pm.delete(tempConstraint)
pm.makeIdentity(primeIcon, a = True, t = 1, r = 1, s = 1)
tempConstraintTwo = pm.orientConstraint(primeIcon, jointName, mo = True)
Here is a modified version of your code. This one parents the groups and the controllers and also names them based on a naming scheme (that you can tweak if needed):
def priming(*args):
jointChain = pm.ls(sl=True, dag=True)
print jointChain
jointChain.pop(-1)
print jointChain
ori = raw_input()
systemName = raw_input()
suffix = "prime"
prime1Name = "{0}_{1}_00_{2}1".format(ori, systemName, suffix)
prime2Name = "{0}_{1}_00_{2}2".format(ori, systemName, suffix)
iconNameBase = "your_icon" # Your icon naming scheme here
pm.select(cl=True)
last_created_icon = None
name_index = 1
for jointName in jointChain:
# Modify your iconName with every iteration here
# Sample scheme: your_icon_1, your_icon_2 etc.
iconName = "%s_%i" % (iconNameBase, name_index)
name_index += 1
primeIcon = pm.circle(nr=[1, 0, 0], n=iconName)
groupOne = pm.group(em=True, n=prime1Name)
groupTwo = pm.group(em=True, n=prime2Name)
pm.parent(groupTwo, groupOne)
pm.parent(primeIcon, groupTwo)
tempConstraint = pm.parentConstraint(jointName, groupOne, mo=False)
pm.delete(tempConstraint)
pm.makeIdentity(primeIcon, a=True, t=True, r=True, s=True)
tempConstraintTwo = pm.orientConstraint(primeIcon, jointName, mo=True)
if last_created_icon:
pm.parent(groupOne, last_created_icon)
last_created_icon = primeIcon

Categories