/usr/share/pyshared/xode/joint.py is in python-pyode 1.2.0-4+cvs20090320.1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | ######################################################################
# Python Open Dynamics Engine Wrapper
# Copyright (C) 2004 PyODE developers (see file AUTHORS)
# All rights reserved.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of EITHER:
# (1) The GNU Lesser General Public License as published by the Free
# Software Foundation; either version 2.1 of the License, or (at
# your option) any later version. The text of the GNU Lesser
# General Public License is included with this library in the
# file LICENSE.
# (2) The BSD-style license that is included with this library in
# the file LICENSE-BSD.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the files
# LICENSE and LICENSE-BSD for more details.
######################################################################
# XODE Importer for PyODE
"""
XODE Joint Parser
@author: U{Timothy Stranex<mailto:timothy@stranex.com>}
"""
import ode
import node, errors
class Joint(node.TreeNode):
"""
Represents an ode.Joint-based object and corresponds to the <joint> tag.
"""
def __init__(self, name, parent):
node.TreeNode.__init__(self, name, parent)
self._world = self.getFirstAncestor(ode.World).getODEObject()
try:
self._jg = self.getFirstAncestor(ode.JointGroup).getODEObject()
except node.AncestorNotFoundError:
self._jg = None
try:
self._body = self.getFirstAncestor(ode.Body).getODEObject()
except node.AncestorNotFoundError:
self._body = None
self._link1 = None
self._link2 = None
self.setODEObject(None)
def _getName(self, name):
root = self.getRoot()
try:
link = root.namedChild(name).getODEObject()
except KeyError:
raise errors.InvalidError('Joint link must reference an already '\
'parsed body.')
if (not isinstance(link, ode.Body)):
raise errors.InvalidError('Joint link must reference a body.')
return link
def _getLinks(self):
body = self._body or ode.environment
if (self._link1 is not None):
link1 = self._getName(self._link1)
else:
link1 = body
body = ode.environment
if (self._link2 is not None):
link2 = self._getName(self._link2)
else:
link2 = body
if (link1 is link2):
raise errors.InvalidError('Joint requires two objects.')
return link1, link2
def takeParser(self, parser):
"""
Handles further parsing. It should be called immediately after the
<joint> tag is encountered.
"""
self._parser = parser
self._parser.push(startElement=self._startElement,
endElement=self._endElement)
def _startElement(self, name, attrs):
if (name == 'link1'):
self._link1 = attrs['body']
elif (name == 'link2'):
self._link2 = attrs['body']
elif (name == 'ext'):
pass
elif (name == 'amotor'):
l1, l2 = self._getLinks()
self._parseAMotor(self._world, l1, l2)
elif (name == 'ball'):
l1, l2 = self._getLinks()
self._parseBallJoint(self._world, l1, l2)
elif (name == 'fixed'):
l1, l2 = self._getLinks()
self._parseFixedJoint(self._world, l1, l2)
elif (name == 'hinge'):
l1, l2 = self._getLinks()
self._parseHingeJoint(self._world, l1, l2)
elif (name == 'hinge2'):
l1, l2 = self._getLinks()
self._parseHinge2Joint(self._world, l1, l2)
elif (name == 'slider'):
l1, l2 = self._getLinks()
self._parseSliderJoint(self._world, l1, l2)
elif (name == 'universal'):
l1, l2 = self._getLinks()
self._parseUniversalJoint(self._world, l1, l2)
else:
raise errors.ChildError('joint', name)
def _endElement(self, name):
if (name == 'joint'):
if (self.getODEObject() is None):
raise errors.InvalidError('No joint type element found.')
self._parser.pop()
def _applyAxisParams(self, joint, anum, axis):
def setParam(name):
attr = 'Param%s' % name
if (anum != 0):
attr = '%s%i' % (attr, anum+1)
joint.setParam(getattr(ode, attr), float(axis[name]))
if (axis.has_key('LowStop')):
axis['LoStop'] = axis['LowStop']
del axis['LowStop']
for name in axis.keys():
if (name not in ['x', 'y', 'z']):
if (name in ['LoStop', 'HiStop', 'Vel', 'FMax', 'FudgeFactor',
'Bounce', 'CFM', 'StopERP', 'StopCFM',
'SuspensionERP', 'SuspensionCFM']):
setParam(name)
else:
raise errors.InvalidError('Invalid attribute %s' % `name` +
' of <axis> element.')
def _parseBallJoint(self, world, link1, link2):
anchor = [None]
def start(name, attrs):
if (name == 'anchor'):
anchor[0] = self._parser.parseVector(attrs)
else:
raise errors.ChildError('ball', name)
def end(name):
if (name == 'ball'):
joint = ode.BallJoint(world, self._jg)
joint.attach(link1, link2)
if (anchor[0] is not None):
joint.setAnchor(anchor[0])
self.setODEObject(joint)
self._parser.pop()
self._parser.push(startElement=start, endElement=end)
def _parseFixedJoint(self, world, link1, link2):
def start(name, attrs):
raise errors.ChildError('fixed', name)
def end(name):
if (name == 'fixed'):
self._parser.pop()
joint = ode.FixedJoint(world, self._jg)
joint.attach(link1, link2)
self.setODEObject(joint)
self._parser.push(startElement=start, endElement=end)
def _parseHingeJoint(self, world, link1, link2):
anchor = [None]
axes = []
def start(name, attrs):
if (name == 'anchor'):
anchor[0] = self._parser.parseVector(attrs)
elif (name == 'axis'):
axes.append(attrs)
else:
raise errors.ChildError('hinge', name)
def end(name):
if (name == 'hinge'):
joint = ode.HingeJoint(world, self._jg)
joint.attach(link1, link2)
if (anchor[0] is not None):
joint.setAnchor(anchor[0])
if (len(axes) != 1):
raise errors.InvalidError('Wrong number of axes for hinge'
' joint.')
joint.setAxis(self._parser.parseVector(axes[0]))
self._applyAxisParams(joint, 0, axes[0])
self.setODEObject(joint)
self._parser.pop()
self._parser.push(startElement=start, endElement=end)
def _parseSliderJoint(self, world, link1, link2):
axes = []
def start(name, attrs):
if (name == 'axis'):
axes.append(attrs)
else:
raise errors.ChildError('slider', name)
def end(name):
if (name == 'slider'):
joint = ode.SliderJoint(world, self._jg)
joint.attach(link1, link2)
if (len(axes) != 1):
raise errors.InvalidError('Wrong number of axes for slider'
' joint.')
joint.setAxis(self._parser.parseVector(axes[0]))
self._applyAxisParams(joint, 0, axes[0])
self.setODEObject(joint)
self._parser.pop()
self._parser.push(startElement=start, endElement=end)
def _parseUniversalJoint(self, world, link1, link2):
anchor = [None]
axes = []
def start(name, attrs):
if (name == 'anchor'):
anchor[0] = self._parser.parseVector(attrs)
elif (name == 'axis'):
axes.append(attrs)
else:
raise errors.ChildError('universal', name)
def end(name):
if (name == 'universal'):
joint = ode.UniversalJoint(world, self._jg)
joint.attach(link1, link2)
if (anchor[0] is not None):
joint.setAnchor(anchor[0])
if (len(axes) != 2):
raise errors.InvalidError('Wrong number of axes for '
' universal joint.')
joint.setAxis1(self._parser.parseVector(axes[0]))
self._applyAxisParams(joint, 0, axes[0])
joint.setAxis2(self._parser.parseVector(axes[1]))
self._applyAxisParams(joint, 1, axes[1])
self.setODEObject(joint)
self._parser.pop()
self._parser.push(startElement=start, endElement=end)
def _parseHinge2Joint(self, world, link1, link2):
anchor = [None]
axes = []
def start(name, attrs):
if (name == 'anchor'):
anchor[0] = self._parser.parseVector(attrs)
elif (name == 'axis'):
axes.append(attrs)
else:
raise errors.ChildError('hinge2', name)
def end(name):
if (name == 'hinge2'):
joint = ode.Hinge2Joint(world, self._jg)
joint.attach(link1, link2)
if (anchor[0] is not None):
joint.setAnchor(anchor[0])
if (len(axes) != 2):
raise errors.InvalidError('Wrong number of axes for '
' hinge2 joint.')
joint.setAxis1(self._parser.parseVector(axes[0]))
self._applyAxisParams(joint, 0, axes[0])
joint.setAxis2(self._parser.parseVector(axes[1]))
self._applyAxisParams(joint, 1, axes[1])
self.setODEObject(joint)
self._parser.pop()
self._parser.push(startElement=start, endElement=end)
def _parseAMotor(self, world, link1, link2):
anchor = [None]
axes = []
def start(name, attrs):
# The XODE specification allows anchor elements for AMotor but
# there is no way to set the anchor of an AMotor.
#if (name == 'anchor'):
# anchor[0] = self._parser.parseVector(attrs)
if (name == 'axis'):
axes.append(attrs)
else:
raise errors.ChildError('amotor', name)
def end(name):
if (name == 'amotor'):
joint = ode.AMotor(world, self._jg)
joint.attach(link1, link2)
if (anchor[0] is not None):
joint.setAnchor(anchor[0])
if (len(axes) > 3):
raise errors.InvalidError('Wrong number of axes for '
' amotor joint.')
joint.setNumAxes(len(axes))
for i in range(len(axes)):
joint.setAxis(i, 0, self._parser.parseVector(axes[i]))
self._applyAxisParams(joint, i, axes[i])
self.setODEObject(joint)
self._parser.pop()
self._parser.push(startElement=start, endElement=end)
|