Skip to content

Commit f0c26a4

Browse files
committed
make cns from string: added tracking cns
1 parent 1011138 commit f0c26a4

File tree

1 file changed

+81
-34
lines changed

1 file changed

+81
-34
lines changed

utils.py

Lines changed: 81 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,45 +1338,92 @@ def make_constraints_from_string(owner, target, subtarget, fstring):
13381338
:return:
13391339
"""
13401340

1341-
# regex is (type)(influence*)(space-space*)(use_offset*)(head_tail*)
1342-
13431341
separator = '#'
1342+
cns_blocks = fstring.split(separator)
1343+
1344+
transform_type = ['CL', 'CR', 'CS', 'CT']
1345+
track_type = ['DT', 'TT']
1346+
1347+
for cns in cns_blocks:
1348+
1349+
if cns[0:2] in transform_type:
1350+
make_transform_constraint_from_string(owner, target, subtarget, cns)
1351+
1352+
if cns[0:2] in track_type:
1353+
make_track_constraint_from_string(owner, target, subtarget, cns)
1354+
1355+
1356+
def make_transform_constraint_from_string(owner, target, subtarget, fstring):
13441357

1358+
# regex is (type)(influence*)(space-space*)(use_offset*)(head_tail*)
13451359
regex = '^(CL|CR|CS|CT)([0-9]*\.?[0-9]+)*([LWP]{2})*(O*)([0-9]*\.?[0-9]+)*$'
13461360

13471361
constraint_type = {'CL': 'COPY_LOCATION', 'CR': 'COPY_ROTATION', 'CS': 'COPY_SCALE', 'CT': 'COPY_TRANSFORMS'}
13481362
constraint_space = {'L': 'LOCAL', 'W': 'WORLD', 'P': 'POSE'}
13491363

1350-
cns_blocks = fstring.split(separator)
1364+
re_object = re.match(regex, fstring)
1365+
if not re_object:
1366+
return
1367+
else:
1368+
cns_props = re_object.groups()
1369+
1370+
cns_type = constraint_type[cns_props[0]]
1371+
const = owner.constraints.new(cns_type)
1372+
const.target = target
1373+
const.subtarget = subtarget
1374+
1375+
if cns_type == 'COPY_LOCATION':
1376+
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1377+
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1378+
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1379+
const.use_offset = bool(cns_props[3])
1380+
const.head_tail = float(cns_props[4]) if bool(cns_props[4]) else 0.0
1381+
if cns_type == 'COPY_ROTATION':
1382+
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1383+
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1384+
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1385+
const.use_offset = bool(cns_props[3])
1386+
if cns_type == 'COPY_SCALE':
1387+
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1388+
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1389+
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1390+
const.use_offset = bool(cns_props[3])
1391+
if cns_type == 'COPY_TRANSFORMS':
1392+
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1393+
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1394+
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1395+
const.head_tail = float(cns_props[4]) if bool(cns_props[4]) else 0.0
1396+
1397+
1398+
def make_track_constraint_from_string(owner, target, subtarget, fstring):
1399+
1400+
# regex is (type)(influence*)(track_axis*)(space-space*)(head_tail*)
1401+
regex = '^(TT|DT)([0-9]*\.?[0-9]+)*(-*[XYZ])*([LWP]{2})*([0-9]*\.?[0-9]+)*$'
1402+
1403+
constraint_type = {'DT': 'DAMPED_TRACK', 'TT': 'TRACK_TO'}
1404+
constraint_space = {'L': 'LOCAL', 'W': 'WORLD', 'P': 'POSE'}
1405+
track_axis = {'X': 'TRACK_X', '-X': 'TRACK_NEGATIVE_X', 'Y': 'TRACK_Y', '-Y': 'TRACK_NEGATIVE_Y',
1406+
'Z': 'TRACK_Z', '-Z': 'TRACK_NEGATIVE_Z'}
13511407

1352-
for cns in cns_blocks:
1353-
re_object = re.match(regex, cns)
1354-
if not re_object:
1355-
continue
1356-
else:
1357-
cns_props = re_object.groups()
1358-
cns_type = constraint_type[cns_props[0]]
1359-
const = owner.constraints.new(cns_type)
1360-
const.target = target
1361-
const.subtarget = subtarget
1362-
if cns_type == 'COPY_LOCATION':
1363-
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1364-
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1365-
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1366-
const.use_offset = bool(cns_props[3])
1367-
const.head_tail = float(cns_props[4]) if bool(cns_props[4]) else 0.0
1368-
if cns_type == 'COPY_ROTATION':
1369-
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1370-
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1371-
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1372-
const.use_offset = bool(cns_props[3])
1373-
if cns_type == 'COPY_SCALE':
1374-
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1375-
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1376-
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1377-
const.use_offset = bool(cns_props[3])
1378-
if cns_type == 'COPY_TRANSFORMS':
1379-
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1380-
const.target_space = constraint_space[cns_props[2][0]] if bool(cns_props[2]) else "LOCAL"
1381-
const.owner_space = constraint_space[cns_props[2][1]] if bool(cns_props[2]) else "LOCAL"
1382-
const.head_tail = float(cns_props[4]) if bool(cns_props[4]) else 0.0
1408+
re_object = re.match(regex, fstring)
1409+
if not re_object:
1410+
return
1411+
else:
1412+
cns_props = re_object.groups()
1413+
1414+
cns_type = constraint_type[cns_props[0]]
1415+
const = owner.constraints.new(cns_type)
1416+
const.target = target
1417+
const.subtarget = subtarget
1418+
1419+
if cns_type == 'DAMPED_TRACK':
1420+
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1421+
const.track_axis = track_axis[cns_props[2]] if bool(cns_props[2]) else "TRACK_Y"
1422+
const.head_tail = float(cns_props[4]) if bool(cns_props[4]) else 0.0
1423+
1424+
if cns_type == 'TRACK_TO':
1425+
const.influence = float(cns_props[1]) if bool(cns_props[1]) else 1.0
1426+
const.track_axis = track_axis[cns_props[2]] if bool(cns_props[2]) else "TRACK_Y"
1427+
const.target_space = constraint_space[cns_props[3][0]] if bool(cns_props[3]) else "LOCAL"
1428+
const.owner_space = constraint_space[cns_props[3][1]] if bool(cns_props[3]) else "LOCAL"
1429+
const.head_tail = float(cns_props[4]) if bool(cns_props[4]) else 0.0

0 commit comments

Comments
 (0)