Hello,
I really need some help, I have very little to no experience scripting in Python but I have to make this script where several locators are connected with a curve getting the shortest path.
For this I used the simannealer, but having run it I get a path in the correct order.
BUT I don’t know how to make a curve out of this input, it keeps giving me errors saying the output is in unicode :
[u’locator2’, u’locator4’, u’locator10’, u’locator6’, u’locator5’, u’locator9’, u’locator1’, u’locator3’, u’locator7’, u’locator11’, u’locator8’]
What I need is the positions for these locators in that order and to make a curve out of it, I am completely stuck and could really use some help.
This is the code I have so far:
def collect(*args):
selected = cmds.ls(selection=True)
cmds.button(btn3 , e=True, label="locator selected")
def distance(a, b):
"""Calculates distance between two latitude-longitude coordinates."""
R = 3963 # radius of Earth (miles)
lat1, lon1 = math.radians(a[0]), math.radians(a[1])
lat2, lon2 = math.radians(b[0]), math.radians(b[1])
return math.acos(math.sin(lat1) * math.sin(lat2) +
math.cos(lat1) * math.cos(lat2) * math.cos(lon1 - lon2)) * R
class TravellingSalesmanProblem(Annealer):
"""Test annealer with a travelling salesman problem.
"""
# pass extra data (the distance matrix) into the constructor
def __init__(self, state, distance_matrix):
self.distance_matrix = distance_matrix
super(TravellingSalesmanProblem, self).__init__(state) # important!
def move(self):
"""Swaps two locator_dictionary in the route."""
a = random.randint(0, len(self.state) - 1)
b = random.randint(0, len(self.state) - 1)
self.state[a], self.state[b] = self.state[b], self.state[a]
def energy(self):
"""Calculates the length of the route."""
e = 0
for i in range(len(self.state)):
e += self.distance_matrix[self.state[i-1]][self.state[i]]
return e
if __name__ == '__main__':
locators = cmds.ls( type='locator' )
loc_parents = cmds.listRelatives(*locators, p=True, f=True)
locator_dictionary={}
print loc_parents
for item in loc_parents:
cmds.select(item, r=True)
locator_name = cmds.ls(sl=True)[0]
translate_x_value = cmds.getAttr("%s.translateX" % item)
translate_y_value = cmds.getAttr("%s.translateY" % item)
translate_z_value = cmds.getAttr("%s.translateZ" % item)
locator_position = (translate_x_value, translate_y_value, translate_z_value)
locator_dictionary[locator_name]=locator_position
print len(locator_dictionary)
print locator_dictionary['locator1']
global pos1
pos = [translate_x_value, translate_y_value, translate_z_value]
print pos
# initial state, a randomly-ordered itinerary
init_state = list(locator_dictionary.keys())
random.shuffle(init_state)
# create a distance matrix
distance_matrix = {}
for ka, va in locator_dictionary.items():
distance_matrix[ka] = {}
for kb, vb in locator_dictionary.items():
if kb == ka:
distance_matrix[ka][kb] = 0.0
else:
distance_matrix[ka][kb] = distance(va, vb)
tsp = TravellingSalesmanProblem(init_state, distance_matrix)
tsp.copy_strategy = "slice"
state, e = tsp.anneal()
print("%i mile route:" % e)
for locator in state:
print(" ", locator)
cmds.getAttr
print state