def get_curve_shape(curve, shape_index=0): """ Returns the shape for a curve transform :param curve: str :param shape_index: int :return: str or None """ if curve.find('.vtx'): curve = curve.split('.')[0] if maya.cmds.nodeType(curve) == 'nurbsCurve': curve = maya.cmds.listRelatives(curve, p=True)[0] shapes = shape_utils.get_shapes(curve) if not shapes: return if not maya.cmds.nodeType(shapes[0]) == 'nurbsCurve': return shape_count = len(shapes) if shape_index < shape_count: return shapes[0] elif shape_index >= shape_count: logger.warning( 'Curve {} does not have a shape count up to {}. Returning last shape'.format(curve, shape_index)) return shapes[-1] return shapes[shape_index]
def get_node_types(nodes, return_shape_type=True): """ Returns the Maya node type for the given nodes :param nodes: list<str>, list of nodes we want to check types of :param return_shape_type: bool, Whether to check shape type or not :return: dict<str>, [node_type_name], node dictionary of matching nodes """ from tpDcc.dccs.maya.core import shape nodes = python.force_list(nodes) found_type = dict() for n in nodes: node_type = maya.cmds.nodeType(n) if node_type == 'transform': if return_shape_type: shapes = shape.get_shapes(n) if shapes: node_type = maya.cmds.nodeType(shapes[0]) if node_type not in found_type: found_type[node_type] = list() found_type[node_type].append(n) return found_type
def snap_curve_to_surface(curve, surface, offset=1, project=False): """ Snaps curves CVs on given surface :param curve: str, name of the curve to snap onto surface :param surface: str, name of surface curve wil be snapped to :param offset: int, offset between curve and surface :param project: bool, Whether to snap or snap and project the curve into the surface """ from tpDcc.dccs.maya.core import mesh center = maya.cmds.xform(curve, query=True, ws=True, rp=True) shapes = shape_utils.get_shapes(curve) for shape in shapes: cvs = maya.cmds.ls('{}.cv[*]'.format(shape), flatten=True) for cv in cvs: pos = maya.cmds.xform(cv, query=True, ws=True, t=True) if mesh.is_a_mesh(surface): mesh_fn = api.MeshFunction(surface) if project: closest_point = mesh_fn.get_closest_intersection(pos, center) else: closest_point = mesh_fn.get_closest_position(pos) maya.cmds.xform(cv, ws=True, t=closest_point) maya.cmds.scale(offset, offset, offset, cvs, r=True)
def set_curve_type(self, type_name=None, keep_color=True, **kwargs): """ Updates the curves of the control with the given control type :param type_name: str :param keep_color: bool """ if not type_name: if dcc.attribute_exists(self.get(), 'curveType'): type_name = dcc.get_attribute_value(self.get(), 'curveType') shapes = shape_utils.get_shapes(self.get()) color = kwargs.pop('color', None) kwargs['color'] = color or (node_utils.get_rgb_color(shapes[0]) if shapes else 0) super(MayaRigControl, self).set_curve_type(type_name=type_name, keep_color=keep_color, **kwargs) self.update_shapes() string_attr = attr_utils.StringAttribute('curveType') string_attr.create(self._name) string_attr.set_value(type_name) return True
def get_components(transform): """ Returns the name of the components under a transform :param transform: str, name of a transform :return: name of all components under a transforms """ shapes = shape_utils.get_shapes(transform) return get_components_from_shapes(shapes)
def get_target_name(blend_shape, target_geo): """ Get blendShape target alias for the given target geometry :param blend_shape: str, blendshape node to get target name from :param target_geo: str, blendshape target geometry to get alias name for :return: str """ check_blendshape(blend_shape) if not maya.cmds.objExists(target_geo): raise Exception( 'Target geometry "{}" does not exists!'.format(target_geo)) # Get target shapes target_shape = shape.get_shapes(node_name=target_geo, non_intermediates=True, intermediates=False) if not target_shape: target_shape = maya.cmds.ls(maya.cmds.listRelatives(target_geo, ad=True, pa=True), shapes=True, noIntermediate=True) if not target_shape: raise Exception( 'No shapes found under target geometry "{}"!'.format(target_geo)) # Find target connection target_cnt = maya.cmds.listConnections(target_shape, sh=True, d=True, s=False, p=False, c=True) if not target_cnt.count(blend_shape): raise Exception( 'Target geometry "{}" is not connected to blendShape "{}"!'.format( target_geo, blend_shape)) target_cnt_index = target_cnt.index(blend_shape) target_cnt_attr = target_cnt[target_cnt_index - 1] target_cnt_plug = maya.cmds.listConnections(target_cnt_attr, sh=True, p=True, d=True, s=False)[0] # Get target index and alias target_index = int( target_cnt_plug.split('.')[2].split('[')[1].split(']')[0]) target_alias = maya.cmds.aliasAttr(blend_shape + '.weight[' + str(target_index) + ']', query=True) return target_alias
def set_shapes_as_text_curve(transform, text_string): """ Updates the shapes of the given transform with given text (as curves) :param transform: str, transform node we want to update shapes of :param text_string: str, text we want to add """ shapes = shape_utils.get_shapes(transform) maya.cmds.delete(shapes) text = maya.cmds.textCurves(ch=False, f='Arial|w400|h-1', t=text_string) maya.cmds.makeIdentity(text, apply=True, t=True) transforms = maya.cmds.listRelatives(text, ad=True, type='transform') for text_transform in transforms: shapes = shape_utils.get_shapes(text_transform) if not shapes: continue for shape in shapes: maya.cmds.parent(shape, transform, r=True, s=True) maya.cmds.delete(text) shape_utils.rename_shapes(transform)
def get_parameter_from_curve_length(curve, length_value): """ Returns the parameter value (UV) given the length section of a curve :param curve: str, name of a curve :param length_value: float, length along a curve :return: float, parameter value at the length """ curve_shapes = shape_utils.get_shapes(curve) curve = curve_shapes[0] if curve_shapes else curve curve = api.NurbsCurveFunction(curve) return curve.get_parameter_at_length(length_value)
def get_closest_position_on_curve(curve, value_list): """ Returns closes position on a curve from given vector :param curve: str, name of a curve :param value_list: list(float, float, float) :return: list(float, float, float) """ curve_shapes = shape_utils.get_shapes(curve) curve = curve_shapes[0] if curve_shapes else curve curve = api.NurbsCurveFunction(curve) return curve.get_closest_position(value_list)
def get_closest_parameter_on_curve(curve, value_list): """ Returns the closest parameter value (UV) on the curve given a vector :param curve: str, name of a curve :param value_list: list(int, int, int), vector from which to search for closest parameter :return: float """ curve_shapes = shape_utils.get_shapes(curve) curve = curve_shapes[0] if curve_shapes else curve curve = api.NurbsCurveFunction(curve) new_point = curve.get_closest_position(value_list) return curve.get_parameter_at_position(new_point)
def update_shapes(self): """ Force the update of the internal control shapes cache """ self._shapes = shape_utils.get_shapes(self._name)