def test(self): _sphere1 = hom.CMDS.polySphere() _sphere2 = hom.CMDS.polySphere() _sphere3 = hom.CMDS.polySphere() # Test apply euler rotation to sphere _rot = hom.HEulerRotation(0, 0, math.pi) _rot.apply_to(_sphere1) assert cmds.getAttr(_sphere1.rotate) == [(0, 0, 180)] # Test apply euler rot as mtx to sphere hom.HMatrix().apply_to(_sphere1) _rot = hom.HEulerRotation(0, math.pi, 0) _mtx = _rot.as_mtx() cmds.xform(_sphere1, matrix=_mtx) assert cmds.getAttr(_sphere1.rotate) == [(0, 180, 0)] hom.HMatrix().apply_to(_sphere1) _mtx.apply_to(_sphere2) assert cmds.getAttr(_sphere2.rotate) == [(0, 180, 0)] # Test apply vector + rot as mtxs to sphere hom.HMatrix().apply_to(_sphere1) _mtx1 = hom.HVector(5, 10, 20).as_mtx() _mtx2 = hom.HEulerRotation(0, math.pi, 0).as_mtx() (_mtx2 * _mtx1).apply_to(_sphere1) print cmds.getAttr(_sphere1.rotate) assert cmds.getAttr(_sphere1.rotate) == [(0, 180, 0)] assert cmds.getAttr(_sphere1.translate) == [(5, 10, 20)] (_mtx1 * _mtx2).apply_to(_sphere1) assert cmds.getAttr(_sphere1.rotate) == [(0, 180, 0)] assert cmds.getAttr(_sphere1.translate) == [(-4.999999999999997, 10.0, -20.0)] (_mtx1 * _mtx1).apply_to(_sphere1) assert cmds.getAttr(_sphere1.rotate) == [(0, 0, 0)] assert cmds.getAttr(_sphere1.translate) == [(10, 20, 40)] # Offset matrix cookbook _mtx_b = _rand_m() _mtx_a = _rand_m() _offs_a_to_b = _mtx_a.inverse() * _mtx_b assert _mtx_a * _offs_a_to_b == _mtx_b _mtx_b.apply_to(_sphere1) _mtx_a.apply_to(_sphere2) (_mtx_a * _offs_a_to_b).apply_to(_sphere3) assert hom.get_m(_sphere3) == _mtx_b # Test load/save preset _sphere2.tx.set_val(10) _tmp_preset = '{}/tmp.preset'.format(tempfile.gettempdir()) assert (_sphere1.get_p() - _sphere2.get_p()).length() _sphere1.save_preset(_tmp_preset) print _tmp_preset _sphere2.load_preset(_tmp_preset) print(_sphere1.get_p() - _sphere2.get_p()).length() assert not round((_sphere1.get_p() - _sphere2.get_p()).length(), 5)
def _casted_result_fn(*args, **kwargs): import maya_psyhive.open_maya as hom _result = func(*args, **kwargs) lprint('CASTING RESULT', _result, verbose=verbose) if isinstance(_result, float): return _result elif isinstance(_result, om.MPoint): return hom.HPoint(_result) elif isinstance(_result, om.MVector): return hom.HVector(_result) elif isinstance(_result, om.MMatrix): return hom.HMatrix(_result) raise ValueError(_result)
def _mesh_to_ray_intersection(ray, mesh, both_dirs=True, clean_up=True, above_only=True): """Calculate ray/mesh intersections. Args: ray (HVRay): ray object mesh (HFnMehs): mesh object both_dirs (bool): check intersections in both directions (ie. if this if false then intersections coming out of faces will not be flagged) clean_up (bool): remove duplicate points - these can occur if a ray intersects an the edge between two faces above_only (bool): remove points behind the ray's start point Returns: (HPoint list): list of points """ from maya_psyhive import open_maya as hom _ray_src = om.MFloatPoint(*ray.pnt.to_tuple()) _ray_dir = om.MFloatVector(*ray.vec.to_tuple()) _space = om.MSpace.kWorld _max_param = 999999 _result = mesh.allIntersections( _ray_src, _ray_dir, _space, _max_param, both_dirs, ) # Convert to HPoints _pts = [] for _item in _result[0]: _pt = hom.HPoint(_item) _pts.append(_pt) lprint('FOUND {:d} PTS'.format(len(_pts)), _pts, verbose=0) # Remove duplicate points if clean_up: _clean_pts = [] while _pts: _clean_pts.append(_pts.pop()) for _pt in copy.copy(_pts): for _clean_pt in _clean_pts: if not (_pt - _clean_pt).length(): _pts.remove(_pt) _pts = _clean_pts # Remove points behind the ray's start point if above_only: _above_pts = [] _plane = ray.to_plane() for _pt in _pts: _dist = _plane.distanceToPoint(hom.HVector(_pt), signed=True) if _dist > 0.0001: _above_pts.append(_pt) _pts = _above_pts return _pts
def __sub__(self, other): from maya_psyhive import open_maya as hom return hom.HVector(self[0] - other[0], self[1] - other[1], self[2] - other[2])