def __add__(self, other): from maya_psyhive import open_maya as hom if not isinstance(other, om.MBoundingBox): raise TypeError(other) _s_min, _o_min = self.min, other.min _s_max, _o_max = self.max, other.max _min = hom.HPoint(min([_s_min.x, _o_min.x]), min([_s_min.y, _o_min.y]), min([_s_min.z, _o_min.z])) _max = hom.HPoint(max([_s_max.x, _o_max.x]), max([_s_max.y, _o_max.y]), max([_s_max.z, _o_max.z])) return HBoundingBox(_min, _max)
def center(self): """Get bbox centrepoint. Returns: (HPoint): centre """ from maya_psyhive import open_maya as hom return hom.HPoint(super(HBoundingBox, self).center)
def min(self): """Get bbox min point. Returns: (HPoint): min """ from maya_psyhive import open_maya as hom return hom.HPoint(super(HBoundingBox, self).min)
def addUIDrawables(self, obj, painter, frame_context, data): """Add drawables - viewport 2.0 draw function. Executed on viewport update. Not sure why drawing happens here and not in draw function. Args: obj (MDagPath): path to object being drawn painter (MUIDrawManager): draw manager frame_context (MFrameContext): frame context data (MeshXRayerData): node data """ if not isinstance(data, MeshXRayerData): return painter.beginDrawable() _angle_to_cam = math.degrees(data.mesh_to_cam.angle(hom.Z_AXIS)) _hide_angle = data.hide_angle _hide_angle_r = math.radians(_hide_angle) # Draw polys over mesh if data.draw_mesh and _angle_to_cam <= _hide_angle: painter.beginDrawInXray() painter.setColor(data.color) painter.mesh(omr.MGeometry.kTriangles, data.mesh_tris) painter.endDrawInXray() # Draw control if data.draw_control: _rad = 2.0 painter.setColor(hom.get_col('red')) painter.circle(hom.ORIGIN, hom.Y_AXIS, _rad) _end = hom.HPoint(0, 0, _rad * 1.2) painter.line(hom.ORIGIN, _end) painter.cone(_end, hom.Z_AXIS, 0.2, 0.4) for _x_mult in [1, -1]: _x_cpnt = _x_mult * _rad * math.sin(_hide_angle_r) _z_cpnt = _rad * math.cos(_hide_angle_r) _pt = hom.HPoint(_x_cpnt, 0, _z_cpnt) painter.line(hom.ORIGIN, _pt) painter.endDrawable()
def get_corners(self): """Get list of 8 corner points. Returns: (HPoint list): corners """ from maya_psyhive import open_maya as hom _min = hom.HPoint(self.min) _max = hom.HPoint(self.max) return [ hom.HPoint(_min[0], _min[1], _max[2]), hom.HPoint(_max[0], _min[1], _max[2]), hom.HPoint(_min[0], _max[1], _max[2]), hom.HPoint(_max[0], _max[1], _max[2]), hom.HPoint(_min[0], _max[1], _min[2]), hom.HPoint(_max[0], _max[1], _min[2]), hom.HPoint(_min[0], _min[1], _min[2]), hom.HPoint(_max[0], _min[1], _min[2]) ]
def build_arrow(name='arrow', length=0.3): """Geometry in an arrow shape. Args: name (str): name for arrow curve length (float): length of wings (base is length 1.0) Returns: (HFnNurbsCurve): arrow curve """ import maya_psyhive.open_maya as hom return hom.CMDS.curve(name=name, degree=1, point=( hom.HPoint(0, 1 - length, length), hom.Y_AXIS, hom.ORIGIN, hom.Y_AXIS, hom.HPoint(0, 1 - length, -length), ))
def sph_rand(): """Generate a random point on a sphere. Returns: (mom.MPoint): random point """ from maya_psyhive import open_maya as hom _theta = random.random() * 2 * math.pi _phi = random.random() * math.pi return hom.HPoint( math.cos(_theta) * math.sin(_phi), math.sin(_theta) * math.sin(_phi), math.cos(_phi))
def get_bbox(obj, ignore_points=False, verbose=0): """Get bbox for the given data. Args: obj (list|HPoint|HVector|str): input data ignore_points (bool): ignore components with zero size bboxes (for lists of objects) verbose (int): print process data Returns: (HBoundingBox): bounding box """ from maya_psyhive import open_maya as hom if isinstance(obj, list): _bboxes = [get_bbox(_obj) for _obj in obj] if ignore_points: _bboxes = [_bbox for _bbox in _bboxes if _bbox.size().length()] return sum(_bboxes[1:], _bboxes[0]) elif isinstance(obj, hom.HPoint): return HBoundingBox(obj, obj) elif isinstance(obj, hom.HVector): _pt = hom.HPoint(obj) return HBoundingBox(_pt, _pt) elif isinstance(obj, (six.string_types, hom.HFnDependencyNode, hom.HFnTransform, hom.HFnMesh)): _result = cmds.exactWorldBoundingBox(obj, calculateExactly=True, ignoreInvisible=True) lprint('BBOX RESULT', _result, verbose=verbose) _min = hom.HPoint(_result[:3]) _max = hom.HPoint(_result[-3:]) return HBoundingBox(_min, _max) else: raise NotImplementedError('Unhandled get bbox {} ({})'.format( obj, type(obj).__name__))
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 scale(self): """Get translation.""" from maya_psyhive import open_maya as hom _tm = om.MTransformationMatrix(self) return hom.HPoint(_tm.scale(om.MSpace.kWorld))