def get_pos_by_vec_dist(startPos, vec, distance=1): """ Get a point along a ray given a point, ray and distance along that ray :parameters: point(list): [x,x,x] vector(list): [x,x,x] :returns distance(float) """ _str_func = 'get_pos_by_vec_dist' vec = VALID.euclidVector3Arg(vec) _startPos = MATHUTILS.Vector3(startPos[0], startPos[1], startPos[2]) _dir = MATHUTILS.Vector3(vec[0], vec[1], vec[2]) _new = _startPos + _dir * distance return _new.x, _new.y, _new.z
def get_vectorOffset(obj=None, origin=None, distance=0, asEuclid=False): """ Get the vector offset of a given object with a distance. Designed as a replacment for maya's curve offset as it's finicky coupled with :parameters: obj(str): obj to query origin(d3) - origin to calculate vector from distance(f) asEuclid(bool) - whether to return as Vector or not :returns pos(list/Vector3) """ _str_func = 'get_vectorOffset' pos = POS.get(obj) vec = MATHUTILS.get_vector_of_two_points(origin, pos) newPos = get_pos_by_vec_dist(pos, vec, distance) if asEuclid: return MATHUTILS.Vector3(newPos[0], newPos[1], newPos[2]) return newPos
def aim_atPoint(obj = None, position = [0,0,0], aimAxis = "z+", upAxis = "y+", mode = 'local',vectorUp = None,ignoreAimAttrs = False): """ Aim functionality. :parameters: obj(str): Object to modify position(array): object to copy from aimAxis(str): axis that is pointing forward upAxis(str): axis that is pointing up mode(str): 'local'-- use standard maya aiming with local axis 'world' -- use standard maya aiming with world axis 'matrix' -- use Bokser's fancy method 'vector' -- maya standard with vector up axis 'object' -- maya standard with object :returns success(bool) """ try: _str_func = 'aimAtPoint' _loc = False _obj = VALID.objString(obj, noneValid=False, calledFrom = __name__ + _str_func + ">> validate obj") try:position = position.x,position.y,position.z except:pass try:vectorUp = vectorUp.x,vectorUp.y,vectorUp.z except:pass log.debug("|{0}| >> obj: {1} | position:{2} | mode: {3}".format(_str_func,_obj,position,mode)) if not ignoreAimAttrs: _d_aim = ATTR.validate_arg(_obj,'axisAim') _d_up =ATTR.validate_arg(_obj,'axisUp') if ATTR.has_attr(_d_aim) and ATTR.has_attr(_d_up): aimAxis = ATTR.get_enumValueString(_d_aim) upAxis = ATTR.get_enumValueString(_d_up) log.debug("|{0}| >> obj: {1} aimable from attrs. aim: {2} | up: {3}".format(_str_func,_obj,aimAxis,upAxis)) if mode == 'matrix': '''Rotate transform based on look vector''' # get source and target vectors objPos = POS.get(_obj, asEuclid=True) targetPos = MATH.Vector3.Create(position) aim = (targetPos - objPos).normalized() if not vectorUp: upVector = MATH.Vector3.up() if upAxis == "y-": upVector = MATH.Vector3.down() elif upAxis == "z+": upVector = MATH.Vector3.forward() elif upAxis == "z-": upVector = MATH.Vector3.back() elif upAxis == "x+": upVector = MATH.Vector3.right() elif upAxis == "x-": upVector = MATH.Vector3.left() else: upVector = MATH.Vector3.up() vectorUp = MATH.transform_direction( _obj, upVector ) wantedAim, wantedUp = MATH.convert_aim_vectors_to_different_axis(aim, vectorUp, aimAxis, upAxis) xformPos = mc.xform(_obj, q=True, matrix = True, ws=True) pos = MATH.Vector3(xformPos[12], xformPos[13], xformPos[14]) rot_matrix = EUCLID.Matrix4.new_look_at(MATH.Vector3.zero(), -wantedAim, wantedUp) s = MATH.Vector3.Create( mc.xform(_obj, q=True, ws=True, s=True) ) scale_matrix = EUCLID.Matrix4() scale_matrix.a = s.x scale_matrix.f = s.y scale_matrix.k = s.z scale_matrix.p = 1 result_matrix = rot_matrix * scale_matrix transform_matrix = result_matrix[0:12] + [pos.x, pos.y, pos.z, 1.0] mc.xform(_obj, matrix = transform_matrix , roo="xyz", ws=True) """elif mode == 'world': _loc = mc.spaceLocator()[0] mc.move (position[0],position[1],position[2], _loc, ws=True) mAxis_aim = VALID.simpleAxis(aimAxis) mAxis_up = VALID.simpleAxis(upAxis) _constraint = mc.aimConstraint(_loc,_obj, maintainOffset = False, aimVector = mAxis_aim.p_vector, upVector = mAxis_up.p_vector, worldUpType = 'scene',) mc.delete(_constraint + [_loc])""" elif mode in ['local','world','vector','object']: _loc = mc.spaceLocator(name='test')[0] _loc_snap = POS.create_loc(_obj) mc.move (position[0],position[1],position[2], _loc, ws=True) mAxis_aim = VALID.simpleAxis(aimAxis) mAxis_up = VALID.simpleAxis(upAxis) if mode == 'world': _constraint = mc.aimConstraint(_loc,_loc_snap, maintainOffset = False, aimVector = mAxis_aim.p_vector, upVector = mAxis_up.p_vector, worldUpType = 'scene',) elif mode == 'object': vectorUp = VALID.mNodeString(vectorUp) _constraint = mc.aimConstraint(_loc,_loc_snap, maintainOffset = False, aimVector = mAxis_aim.p_vector, upVector = mAxis_up.p_vector, worldUpType = 'object', worldUpObject = vectorUp) #worldUpVector = _vUp) else: if mode == 'vector': _vUp = vectorUp else: _vUp = MATH.get_obj_vector(_obj,upAxis) _constraint = mc.aimConstraint(_loc,_loc_snap, maintainOffset = False, aimVector = mAxis_aim.p_vector, upVector = mAxis_up.p_vector, worldUpType = 'vector', worldUpVector = _vUp) go(obj,_loc_snap) mc.delete(_constraint,_loc_snap) else: raise NotImplementedError,"mode: {0}".format(mode) if _loc:mc.delete(_loc) return True except Exception,err: try:mc.delete(_loc) except:pass log.error( "aim_atPoint | obj: {0} | err: {1}".format(obj,err) )