def get_listPathTo(node, node2, fullPath=True): """ Get the list path from one object to another :parameters: node(str): node to query node2(str): node to check against :returns status(bool) """ _str_func = 'get_listPathTo' _node = VALID.mNodeString(node) _node2 = VALID.mNodeString(node2) _nodeLong = NAME.get_long(_node) _res = [] if is_parentTo(_node, _node2): log.debug("|{0}| >> isParent mode".format(_str_func)) _node2Long = NAME.get_long(_node2) l_parents = parents_get(_node2, True) self_index = l_parents.index(_nodeLong) l_parents = l_parents[:self_index + 1] log.debug("|{0}| >> index {1} | {2}".format(_str_func, self_index, l_parents)) l_parents.reverse() for o in l_parents: _res.append(o) if o == _node2Long: break _res.append(_node2Long) elif is_childTo(_node, _node2): log.debug("|{0}| >> isChild mode".format(_str_func)) l_parents = parents_get(_node, True) #l_parents.reverse() _res.append(_nodeLong) for o in l_parents: _res.append(o) if o == _nodeLong: break else: return False if not fullPath: return [NAME.get_short(o) for o in _res] return _res
def duplicate_shape(shape): """ mc.duplicate can't duplicate a shape. This provides for it :parameters: shape(str): shape to dupliate :returns [shape,transform] """ try: _str_func = 'duplicate_shape' _type = VALID.get_mayaType(shape) if _type == 'nurbsCurve': _bfr = mc.duplicateCurve(shape) parentObj = mc.listRelatives(shape, p=True, fullPath=True) mc.delete(mc.parentConstraint(parentObj, _bfr[0])) _l_shapes = mc.listRelatives(_bfr[0], s=True, f=True) return [_bfr[0]] + _l_shapes else: log.debug("|{0}| >> mesh shape assumed...".format(_str_func)) _transform = SEARCH.get_transform(shape) _shapes = mc.listRelatives(_transform, s=True, fullPath=True) _idx = _shapes.index(coreNames.get_long(shape)) _bfr = mc.duplicate(shape) _newShapes = mc.listRelatives(_bfr[0], s=True, fullPath=True) _dupShape = _newShapes[_idx] _newShapes.pop(_idx) mc.delete(_newShapes) return [_bfr[0], _dupShape] except Exception, err: pprint.pprint(vars()) if not SEARCH.is_shape(shape): log.error("|{0}| >> Failure >> Not a shape: {1}".format( _str_func, shape)) raise Exception, "|{0}| >> failed! | err: {1}".format(_str_func, err)
def is_shape(node=None): """ Check to see if an node is a shape :parameters: node(str): Object to check :returns status(bool) """ _str_func = 'is_shape' _node = stringArg(node, False, _str_func) log.debug("|{0}| >> node: '{1}' ".format(_str_func, _node)) _shape = mc.ls(node, type='shape', long=True) if _shape: if len(_shape) == 1: if _shape[0] == NAME.get_long(_node): return True return False
def is_parentTo(node, child): """ Query whether a node is parent to a given child :parameters: node(str): node to query child(str): parent to check against :returns status(bool) """ _str_func = 'is_parentTo' _node = VALID.mNodeString(node) if child in [None, False]: return False _child = VALID.mNodeString(child) _c_long = NAME.get_long(_child) for p in descendents_get(_node, True): if p == _c_long: return True return False
def is_childTo(node, parent): """ Query whether a node is child to a given parent :parameters: node(str): node to query parent(str): parent to check against :returns status(bool) """ _str_func = 'is_childTo' _node = VALID.mNodeString(node) if parent in [None, False]: return False _parent = VALID.mNodeString(parent) _p_long = NAME.get_long(_parent) for p in parents_get(_node, True): if p == _p_long: return True return False
def shapeParent_in_place(obj, shapeSource, keepSource=True, replaceShapes=False, snapFirst=False): """ Shape parent a curve in place to a obj transform :parameters: obj(str): Object to modify shapeSource(str): Curve to shape parent keepSource(bool): Keep the curve shapeParented as well replaceShapes(bool): Whether to remove the obj's original shapes or not snapFirst(bool): whether to snap source to obj before transfer :returns success(bool) """ _str_func = 'shapeParent_in_place' l_shapes = VALID.listArg(shapeSource) obj = VALID.mNodeString(obj) log.debug( "|{0}| >> obj: {1} | shapeSource: {2} | keepSource: {3} | replaceShapes: {4}" .format(_str_func, obj, shapeSource, keepSource, replaceShapes)) if replaceShapes: _l_objShapes = mc.listRelatives(obj, s=True, fullPath=True) if _l_objShapes: log.debug("|{0}| >> Removing obj shapes...| {1}".format( _str_func, _l_objShapes)) mc.delete(_l_objShapes) mc.select(cl=True) #mc.refresh() for c in l_shapes: try: _shapeCheck = SEARCH.is_shape(c) if not _shapeCheck and not mc.listRelatives( c, f=True, shapes=True, fullPath=True): raise ValueError, "Has no shapes" if coreNames.get_long(obj) == coreNames.get_long(c): raise ValueError, "Cannot parentShape self" if VALID.get_mayaType(c) == 'nurbsCurve': mc.ls(['%s.ep[*]' % (c)], flatten=True) #This is for a really weird bug in 2016 where offset curve shapes don't work right unless they're components are queried. if _shapeCheck: _dup_curve = duplicate_shape(c)[0] log.debug("|{0}| >> shape duplicate".format(_str_func)) if snapFirst: SNAP.go(_dup_curve, obj) else: log.debug("|{0}| >> regular duplicate".format(_str_func)) _dup_curve = mc.duplicate(c)[0] for child in TRANS.children_get(_dup_curve, True): mc.delete(child) if snapFirst: SNAP.go(_dup_curve, obj) _l_parents = SEARCH.get_all_parents(obj) ATTR.set_standardFlags(_dup_curve, lock=False, visible=True, keyable=True) _dup_curve = parent_set(_dup_curve, False) copy_pivot(_dup_curve, obj) #piv_pos = mc.xform(obj, q=True, ws=True, rp = True) #mc.xform(_dup_curve,ws=True, rp = piv_pos) pos = mc.xform(obj, q=True, os=True, rp=True) curveScale = mc.xform(_dup_curve, q=True, s=True, r=True) objScale = mc.xform(obj, q=True, s=True, r=True) #account for freezing #mc.makeIdentity(_dup_curve,apply=True,translate =True, rotate = True, scale=False) # make our zero out group #group = rigging.groupMeObject(obj,False) group = create_at(obj, 'null') _dup_curve = mc.parent(_dup_curve, group)[0] # zero out the group mc.xform(group, ws=True, t=pos) #mc.xform(group,roo = 'xyz', p=True) mc.xform(group, ra=[0, 0, 0], p=False) mc.xform(group, ro=[0, 0, 0], p=False) mc.makeIdentity(_dup_curve, apply=True, translate=True, rotate=True, scale=False) #main scale fix baseMultiplier = [0, 0, 0] baseMultiplier[0] = (curveScale[0] / objScale[0]) baseMultiplier[1] = (curveScale[1] / objScale[1]) baseMultiplier[2] = (curveScale[2] / objScale[2]) mc.setAttr(_dup_curve + '.sx', baseMultiplier[0]) mc.setAttr(_dup_curve + '.sy', baseMultiplier[1]) mc.setAttr(_dup_curve + '.sz', baseMultiplier[2]) #parent scale fix if _l_parents: _l_parents.reverse() multiplier = [ baseMultiplier[0], baseMultiplier[1], baseMultiplier[2] ] for p in _l_parents: scaleBuffer = mc.xform(p, q=True, s=True, r=True) multiplier[0] = ((multiplier[0] / scaleBuffer[0])) multiplier[1] = ((multiplier[1] / scaleBuffer[1])) multiplier[2] = ((multiplier[2] / scaleBuffer[2])) mc.setAttr(_dup_curve + '.sx', multiplier[0]) mc.setAttr(_dup_curve + '.sy', multiplier[1]) mc.setAttr(_dup_curve + '.sz', multiplier[2]) _dup_curve = parent_set(_dup_curve, False) mc.delete(group) #freeze for parent shaping mc.makeIdentity(_dup_curve, apply=True, translate=True, rotate=True, scale=True) shape = mc.listRelatives(_dup_curve, f=True, shapes=True, fullPath=True) mc.parent(shape, obj, add=True, shape=True) mc.delete(_dup_curve) if not keepSource: mc.delete(c) except Exception, err: cgmGEN.cgmExceptCB(Exception, err, msg=vars())