find out all attributes of an object

本文通过实例演示了如何创建Python中的复数对象,并利用dir()方法探索其所有属性。此外,还介绍了如何通过点操作符访问这些属性,包括实部、虚部及共轭等。

for example:

#create a complex data object

>>> complex_data=1+1j

#find out all attributes of complex_data using method dir()
>>> dir(complex_data)
['__abs__', '__add__', '__class__', '__coerce__', '__delattr__', '__div__', '__divmod__', '__doc__', '__eq__', '__float__', '__floordiv__', '__format__', '__ge__', '__getattribute__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__int__', '__le__', '__long__', '__lt__', '__mod__', '__mul__', '__ne__', '__neg__', '__new__', '__nonzero__', '__pos__', '__pow__', '__radd__', '__rdiv__', '__rdivmod__', '__reduce__', '__reduce_ex__', '__repr__', '__rfloordiv__', '__rmod__', '__rmul__', '__rpow__', '__rsub__', '__rtruediv__', '__setattr__', '__sizeof__', '__str__', '__sub__', '__subclasshook__', '__truediv__', 'conjugate', 'imag', 'real']

#access complex_data's attributes using ' . '

>>> complex_data.real
1.0
>>> complex_data.imag
1.0
>>> complex_data.conjugate()
(1-1j)
>>> 




sample_object_model_3d (Operator) Name sample_object_model_3d — Sample a 3D object model. Signature sample_object_model_3d( : : ObjectModel3D, Method, SamplingParam, GenParamName, GenParamValue : SampledObjectModel3D) Description sample_object_model_3d creates a sampled version of the 3D object model ObjectModel3D and returns it in SampledObjectModel3D. Depending on the method used, SamplingParam controls the minimum distance or the number of points in SampledObjectModel3D. The created 3D object model is returned in SampledObjectModel3D. Using sample_object_model_3d is recommended if complex point clouds are to be thinned out for faster postprocessing or if primitives are to be converted to point clouds. Note that if the 3D object model is triangulated and should be simplified by preserving its original geometry as good as possible, simplify_object_model_3d should be used instead. If the input object model ObjectModel3D contains only points, several sampling methods are available which can be selected using the parameter Method: 'fast': The default method 'fast' adds all points from the input model which are not closer than SamplingParam to any point that was earlier added to the output model. If present, normals, XYZ-mapping and extended point attributes are copied to the output model. 'fast_compute_normals': The method 'fast_compute_normals' selects the same points as the method 'fast', but additionally calculates the normals for all points that were selected. For this, the input object model must either contain normals, which are copied, or it must contain a XYZ-mapping attribute from which the normals are computed. The z-component of the calculated normal vectors is always positive. The XYZ-mapping is created by xyz_to_object_model_3d. 'accurate': The method 'accurate' goes through the points of the 3D object model ObjectModel3D and calculates whether any other points are within a sphere with the radius SamplingParam around the examined point. If there are no other points, the original point is stored in SampledObjectModel3D. If there are other points, the center of gravity of these points (including the original point) is stored in SampledObjectModel3D. This procedure is repeated with the remaining points until there are no points left. Extended attributes of the input 3D object model are not copied, but normals and XYZ-mapping are copied. For this method, a noise removal is possible by specifying a value for 'min_num_points' in GenParamName and GenParamValue, which removes all interpolated points that had less than the specified number of neighbor points in the original model. 'accurate_use_normals': The method 'accurate_use_normals' requires normals in the input 3D object model and interpolates only points with similar normals. The similarity depends on the angle between the normals. The threshold of the angle can be specified in GenParamName and GenParamValue with 'max_angle_diff'. The default value is 180 degrees. Additionally, outliers can be removed as described in the method 'accurate', by setting the generic parameter 'min_num_points'. 'xyz_mapping': The method 'xyz_mapping' can only be applied to 3D object models that contain an XYZ-mapping (for example, if it was created using xyz_to_object_model_3d). This mapping stores for each 3D point its original image coordinates. The method 'xyz_mapping' subdivides those original images into squares with side length SamplingParam (which is given in pixel) and selects one 3D point per square. The method behaves similar to applying zoom_image_factor onto the original XYZ-images. Note that this method does not use the 3D-coordinates of the points for the point selection, only their 2D image coordinates. It is important to notice that for this method, the parameter SamplingParam corresponds to a distance in pixels, not to a distance in 3D space. 'xyz_mapping_compute_normals': The method 'xyz_mapping_compute_normals' selects the same points as the method 'xyz_mapping', but additionally calculates the normals for all points that were selected. The z-component of the normal vectors is always positive. If the input object model contains normals, those normals are copied to the output. Otherwise, the normals are computed based on the XYZ-mapping. 'furthest_point': The method 'furthest_point' iteratively adds the point of the input object to the output object that is furthest from all points already added to the output model. This usually leads to a reasonably uniform sampling. For this method, the desired number of points in the output model is passed in SamplingParam. If that number exceeds the number of points in the input object, then all points of the input object are returned. The first point added to the output object is the point that is furthest away from the center of the axis aligned bounding box around the points of the input object. 'furthest_point_compute_normals': The method 'furthest_point_compute_normals' selects the same points as the method 'furthest_point', but additionally calculates the normals for all points that were selected. The number of desired points in the output object is passed in SamplingParam. To compute the normals, the input object model must either contain normals, which are copied, or it must contain a XYZ-mapping attribute from which the normals are computed. The z-component of the calculated normal vectors is always positive. The XYZ-mapping is created by xyz_to_object_model_3d. If the input object model contains faces (triangles or polygons) or is a 3D primitive, the surface is sampled with the given distance. In this case, the method specified in Method is ignored. The directions of the computed normals depend on the face orientation of the model. Usually, the orientation of the faces does not vary within one CAD model, which results in a set of normals that is either pointing inwards or outwards. Note that planes and cylinders must have finite extent. If the input object model contains lines, the lines are sampled with the given distance SamplingParam. The sampling process approximates surfaces by creating new points in the output object model. Therefore, any extended attributes from the input object model are discarded. For mixed input object models, the sampling priority is (from top to bottom) faces, lines, primitives and points, i.e., only the objects of the highest priority are sampled. The parameter SamplingParam accepts either one value, which is then used for all 3D object models passed in ObjectModel3D, or one value per input object model. If SamplingParam is a distance in 3D space the unit is the usual HALCON-internal unit 'm'. Execution Information Multithreading type: reentrant (runs in parallel with non-exclusive operators). Multithreading scope: global (may be called from any thread). Processed without parallelization. This operator supports canceling timeouts and interrupts. Parameters ObjectModel3D (input_control) object_model_3d(-array) → (handle) Handle of the 3D object model to be sampled. Method (input_control) string → (string) Selects between the different subsampling methods. Default: 'fast' List of values: 'accurate', 'accurate_use_normals', 'fast', 'fast_compute_normals', 'furthest_point', 'furthest_point_compute_normals', 'xyz_mapping', 'xyz_mapping_compute_normals' SamplingParam (input_control) real(-array) → (real / integer) Sampling distance or number of points. Number of elements: SamplingParam == 1 || SamplingParam == ObjectModel3D Default: 0.05 GenParamName (input_control) string-array → (string) Names of the generic parameters that can be adjusted. Default: [] List of values: 'max_angle_diff', 'min_num_points' GenParamValue (input_control) number-array → (real / integer / string) Values of the generic parameters that can be adjusted. Default: [] Suggested values: 1, 2, 5, 10, 20, 0.1, 0.25, 0.5 SampledObjectModel3D (output_control) object_model_3d(-array) → (handle) Handle of the 3D object model that contains the sampled points. Number of elements: SampledObjectModel3D == ObjectModel3D
09-14
ldtcommon代码: """ .. module:: ldtcommon :synopsis: Constants and templates non dcc specific used in other packages. .. moduleauthor:: Ezequiel Mastrasso """ import lucidity import logging import os logger = logging.getLogger(__name__) LOOKDEVTOOLS_FOLDER = os.environ['LOOKDEVTOOLS'] #: Attributes for tagging meshes for surfacing and texture-to-mesh matching ATTR_SURFACING_PROJECT = "surfacing_project" ATTR_SURFACING_OBJECT = "surfacing_object" #: Attribute for tagging Materials. Values: name of assigned project or object ATTR_MATERIAL = "surfacing_material" #: Attribute for tagging material assignmnents. Values: 'project' or 'object'] ATTR_MATERIAL_ASSIGN = "surfacing_assign" #: Attribute for tagging viewport material. Values: 'color' or 'pattern'] ATTR_MATERIAL_VP = "surfacing_vp" #: Global string matching ratios to compare strings against lucidity parsed files. #: Notice that the ratio constant should be high enough, TEXTURE_MATCHING_RATIO = 90 TEXTURE_CHANNEL_MATCHING_RATIO = 90 #: Texture file template, ANCHOR RIGHT TEXTURE_FILE_PATTERN = '{surfacing_project}_{surfacing_object}_{channel}_{colorspace}.{udim}.{extension}' #: Default shader node to use DEFAULT_SHADER = 'PxrSurface' #: Materials json Config, contains texture name mappings to shader plugs CONFIG_MATERIALS_JSON = os.path.join( os.environ['LOOKDEVTOOLS'], 'python', 'ldtconfig', 'materials.json') def texture_file_template(custom_pattern=None): """ Get a lucidity Template object using a custom template. Kwargs: custom_pattern (str): Custom lucidity file pattern. Returns: lucity.Template object with the custom partern """ logger.info('Loading lucidity with:\n %s' % custom_pattern) texture_file_template = lucidity.Template( 'textureset_element', custom_pattern, anchor=lucidity.Template.ANCHOR_END # TODO (Eze) Add STRICT? ) return texture_file_template ldtmaya代码: """ .. module:: ldtmaya :synopsis: general maya functions. .. moduleauthor:: Ezequiel Mastrasso """ import ldtcommon import ldtutils from ldtcommon import ATTR_SURFACING_PROJECT from ldtcommon import ATTR_SURFACING_OBJECT from ldtcommon import ATTR_MATERIAL from ldtcommon import ATTR_MATERIAL_ASSIGN from ldtcommon import ATTR_MATERIAL_VP from ldtui import qtutils from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import os import sys import traceback import random import logging import pymel.core as pm import maya.mel as mel import maya.cmds as mc logger = logging.getLogger(__name__) def surfacingInit(): """ Initialize the scene for surfacing projects. Creates the surfacing root, an empty surfacing project and object, and runs the validation to create and connect the partition Returns: bool. Valid scene. """ root = create_surfacing_root() if not root.members: surfacing_project = create_surfacing_project("defaultProject") create_surfacing_object(surfacing_project, "defaultObject") validate_surfacing() def create_surfacing_root_node(): """Create projects root node""" surfacing_root = pm.createNode( "objectSet", name="surfacing_root" ) surfacing_root.setAttr( "surfacing_root", "", force=True ) return surfacing_root def create_surfacing_root(): """Create projects root if it doesnt exist.""" if not get_surfacing_root(): surfacing_root = get_surfacing_root() return surfacing_root else: return get_surfacing_root() def create_surfacing_project(name=None): """ Creates a surfacing project. Kwargs: name (str): surfacing project name """ if not name: name = "project" surfacing_project = pm.createNode( "objectSet", name=name ) surfacing_project.setAttr( ATTR_SURFACING_PROJECT, "", force=True ) create_surfacing_object(surfacing_project) get_surfacing_root().add(surfacing_project) update_surfacing_partition() return surfacing_project def create_surfacing_object(project, name=None): """ Creates a surfacing Object under a given project. Args: project (PyNode): surfacing project Kwargs: name (str): surfacing object name """ if not name: name = "object" surfacing_set = pm.createNode( "objectSet", name=name ) surfacing_set.setAttr( ATTR_SURFACING_OBJECT, "", force=True ) project.add(surfacing_set) return surfacing_set def get_surfacing_root(): """ Get the project root node. Returns: PyNode. Surfacing root node Raises: Exception. """ objSetLs = [ item for item in pm.ls(type="objectSet") if item.hasAttr("surfacing_root") ] if len(objSetLs) == 0: logger.info( "surfacing_root node found, creating one" ) return create_surfacing_root_node() elif len(objSetLs) > 1: raise Exception( "More than 1 surfacing_root node found, clean up your scene" ) return objSetLs[0] def get_surfacing_projects(): """ Get all surfacing Projects under the root. Returns: list. surfacing projects PyNodes list. """ objSetLs = [ item for item in pm.ls(type="objectSet") if item.hasAttr(ATTR_SURFACING_PROJECT) ] return objSetLs def get_surfacing_project_by_name(name=None): """ Get surfacing Project by name. Kwargs: name (str): surfacing project name. Returns: PyNode. Returns first found hit, we are assuming the objectSet name is equal to surfacing_project attr value. """ projects_list = get_surfacing_projects() for each in projects_list: if name == each.name(): return each return None def get_surfacing_object_by_name(name=None): """ Get surfacing Object by name. Kwargs: name (str): surfacing object name. Returns: PyNode. Returns first found hit, we are assuming the objectSet name is equal to surfacing_object attr value. """ projects_list = get_surfacing_projects() for prj in projects_list: objs = get_surfacing_objects(prj) for obj in objs: if name == obj.name(): return obj return None def delete_surfacing_project(project): """ Delete a surfacing_project, and its members. Args: project (PyNode): surfacing project. """ if is_surfacing_project(project): pm.delete(project.members()) def get_surfacing_objects(project): """ Get all surfacing Objects under the given surfacing project Args: project (PyNode): surfacing project """ if is_surfacing_project(project): return project.members() else: return [] def is_surfacing_project(project): """ Check if the node is a surfacing project Args: project (PyNode): surfacing project Returns: bool. True if it is. """ if project.hasAttr(ATTR_SURFACING_PROJECT): return True else: return False def is_surfacing_object(surfacing_object): """ Check if node is surfacing Object Args: surfacing_object (PyNode): surfacing_object """ if surfacing_object.hasAttr(ATTR_SURFACING_OBJECT): return True else: return False def remove_surfacing_invalid_members(): """ Pops all not-allowd member types from surfacing projects and objects. Only Allowed types: objectSets (surfacing_projects) inside the surfacing projects root objectSets (surfacing_object) inside surfacing projects transforms (that have a mesh) inside surfacing_object """ project_root = get_surfacing_root() for project in project_root.members(): if ( not project.type() == "objectSet" ): # TODO (eze) add check for attr project_root.removeMembers([project]) for project in get_surfacing_projects(): for object in get_surfacing_objects( project ): # TODO (eze) add check for attr if not object.type() == "objectSet": project.removeMembers([object]) else: for member in object.members(): if not member.type() == "transform": logger.info( "removing invalid member: %s" % member ) object.removeMembers([member]) elif not member.listRelatives( type="mesh" ): logger.info( "removing invalid member: %s" % member ) object.removeMembers([member]) def get_mesh_transforms(object_list): # TODO move to common """ Get all the mesh shapes transforms. Includes all descendants in hierarchy. Args: object_list (list): PyNode list of nodes. """ shapes_in_hierarchy = pm.listRelatives( object_list, allDescendents=True, path=True, f=True, type="mesh", ) shapes_transforms = pm.listRelatives( shapes_in_hierarchy, p=True, path=True, f=True ) return shapes_transforms def add_member(surfacing_object, transform): # TODO move to common """ Add transform to surfacing Object Args: surfacing_object (PyNode): surfacing object transform (PyNode): transform node """ pm.sets(surfacing_object, transform, fe=True) def add_mesh_transforms_to_surfacing_object( surfacing_object, object_list ): """ Add all mesh shape transforms -and descendants- from the list to a surfacing Object. Args: surfacing_object (PyNode): surfacing object object_list (list): object list """ pm.select() if is_surfacing_object(surfacing_object): for item in object_list: # Disconnect the objects from other Surf proj and obj for c in item.instObjGroups.listConnections(c=True, p=True): if is_surfacing_object(c[1].node()) or is_surfacing_project(c[1].node()): logger.info( "disconnecting from Surf project or obj: %s" % c[1].node() ) pm.disconnectAttr("%s"%c[0], "%s"%c[1]) for transform in get_mesh_transforms(item): pm.select(transform) add_member(surfacing_object, transform) def update_surfacing_partition(): """Recreate the partition node, and reconnects to all the surfacing objects objectSets.""" partitions = [ item for item in pm.ls(type="partition") if item.hasAttr("surfacing_partition") ] for each in partitions: logger.info( "disconnecting existing partition: %s" % each ) each.sets.disconnect() pm.delete(each) logger.info("deleted partition") surfacing_partition = pm.createNode( "partition", name="surfacing_partition" ) logger.info( "partition created: %s" % surfacing_partition ) surfacing_partition.setAttr( "surfacing_partition", "", force=True ) for project in get_surfacing_projects(): for object in get_surfacing_objects(project): pm.connectAttr( "%s.partition" % object, surfacing_partition.sets, na=True, ) logger.info( "partition connected: %s " % object ) def remove_invalid_characters(): """Remove not allowed characters from surfacing projects and names like '_'.""" project_root = get_surfacing_root() surfacing_projects = get_surfacing_projects() #invalid_character = '_' invalid_character = '*' for project in surfacing_projects: if invalid_character in project.name(): project.rename(project.name().replace(invalid_character, '')) logger.info( 'Invalid character removed from surfacing_project,' 'new name: %s' % project) for surfacing_object in get_surfacing_objects(project): if invalid_character in surfacing_object.name(): surfacing_object.rename( surfacing_object.name().replace(invalid_character, '')) logger.info( 'Invalid characters removed from surfacing_object,' 'new name: %s' % surfacing_object) def validate_surfacing(): """ Validate the scene. Removes invalidad characters and members, updates the partition, and mesh attributes. """ remove_invalid_characters() remove_surfacing_invalid_members() update_surfacing_partition() update_surfacing_attributes() def export_alembic(geo_list, file_path): """ Export alembic file from the object list. Args: geo_list (list): list of geometry to export file_path (str): export file path """ if geo_list and file_path: roots = " -root |" + " -root |".join( [str(x) for x in geo_list] ) cmd = ( r'-frameRange 0 0 -uvWrite -dataFormat ogawa ' r'-userAttrPrefix surfacing' + roots + ' -file ' + (file_path) ) logger.info("AbcExport: %s" % cmd) mc.AbcExport(j=cmd) logger.info( "Succesful Alembic export to: %s" % file_path ) def merge_surfacing_object_meshes(surfacing_object): """ Merge all the meshs assigned to a surfacing Object. Args: surfacing_object (PyNode): surfacing object Raises: BaseException. Could not merge member meshes. """ try: members = surfacing_object.members() logger.info("Merging members: %s" % members) geo_name = "%s_geo" % str(surfacing_object) if len(members) > 1: geo = pm.polyUnite(*members, n=geo_name) return geo[0] else: logger.info( "single object found, skipping merge: %s" % members[0] ) members[0].rename(geo_name) pm.parent(members[0], world=True) return members[0] except BaseException: logger.error( "Could not merge members of: %s" % surfacing_object ) return False def export_surfacing_project(project, subdiv_level=0, single_export=True, folder_path=False): """ Export surfacing Project to Alembic. Args: project (PyNode): surfacing project Kwargs: single_export (bool): is single export folder_path (str): Export folder path """ current_file = pm.sceneName() if single_export: save_unsaved_scene_() if not folder_path: folder_path = qtutils.get_folder_path() project_geo_list = [] if ldtutils.is_directory(folder_path) and is_surfacing_project(project): for each in get_surfacing_objects(project): merged_geo = merge_surfacing_object_meshes(each) if merged_geo: project_geo_list.append(merged_geo) if project_geo_list: if subdiv_level: for geo in project_geo_list: logger.info( "subdivision level: %s" % subdiv_level ) logger.info( "subdividing merged members: %s" % geo ) # -mth 0 -sdt 2 -ovb 1 -ofb 3 -ofc 0 -ost 0 -ocr 0 -dv 3 # -bnr 1 -c 1 -kb 1 -ksb 1 -khe 0 -kt 1 -kmb 1 -suv 1 # -peh 0 -sl 1 -dpe 1 -ps 0.1 -ro 1 -ch 1 pm.polySmooth( geo, mth=0, sdt=2, ovb=1, dv=subdiv_level ) export_file_path = os.path.join( folder_path, str(project) + ".abc" ) export_alembic(project_geo_list, export_file_path) export_surfacing_object_dir = os.path.join( folder_path, str(project) ) ldtutils.create_directoy(export_surfacing_object_dir) for geo in project_geo_list: export_root = " -root |" + geo export_surfacing_object_path = os.path.join( export_surfacing_object_dir + "/" + geo + ".abc" ) export_alembic( [geo], export_surfacing_object_path ) if single_export: pm.openFile(current_file, force=True) def export_all_surfacing_projects(folder_path=None, subdiv_level=0): """ Export all surfacing Projects. Kwargs: folder_path (str): folder to export files. """ save_unsaved_scene_() if not folder_path: folder_path = qtutils.get_folder_path() current_file = pm.sceneName() for project in get_surfacing_projects(): export_surfacing_project( project, subdiv_level, single_export=False, folder_path=folder_path ) pm.openFile(current_file, force=True) return True def save_unsaved_scene_(): # TODO move to common """Check the scene state, if modified, will ask the user to save it.""" if unsaved_scene(): if save_scene_dialog(): pm.saveFile(force=True) else: raise ValueError("Unsaved changes") def update_surfacing_attributes(): """ Create attributes on meshes of what surfacing object they are assigned to. Adds the attributes to all the shapes transforms assigned to surfacing objects. This will be used later for quick shader/material creation and assignment. """ for project in get_surfacing_projects(): project.setAttr(ATTR_SURFACING_PROJECT, project) logger.info( "Updating attributes for project: %s" % project ) for surfacing_object_set in get_surfacing_objects(project): logger.info( "\tUpdating attributes for object texture set: %s" % surfacing_object_set ) surfacing_object_set.setAttr( ATTR_SURFACING_OBJECT, surfacing_object_set ) members = surfacing_object_set.members() logger.info( "\t\tUpdating attr for meshes: %s" % members ) for member in members: member.setAttr( ATTR_SURFACING_PROJECT, project.name(), force=True, ) member.setAttr( ATTR_SURFACING_OBJECT, surfacing_object_set.name(), force=True, ) def set_wifreframe_color_black(): """Set the wireframe color to black in all mesh objects.""" transforms = pm.ls(type="transform") shape_transforms = get_mesh_transforms(transforms) for mesh in shape_transforms: mesh_shape = mesh.getShape() mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(0) mesh_shape.overrideColor.set(1) def set_wifreframe_color_none(): """Remove the wireframe color in all mesh objects.""" transforms = pm.ls(type="transform") shape_transforms = get_mesh_transforms(transforms) for mesh in shape_transforms: mesh_shape = mesh.getShape() mesh_shape.overrideEnabled.set(0) def set_wireframe_colors_per_project(): """ Set the wireframe color per surfacing project. For all meshes, sets it to black to start with, this implies that the mesh has not be assigned to any surfacing object yet will show black in the VP """ set_wifreframe_color_black() projects = get_surfacing_projects() for project in projects: random.seed(project) wire_color = random.randint(1, 31) for surfacingObject in get_surfacing_objects(project): for mesh in surfacingObject.members(): mesh_shape = mesh.getShape() try: mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(0) mesh_shape.overrideColor.set(wire_color) except: logger.error('Could not set override color for: %s, might ' 'belong to a display layer' % mesh ) def set_wireframe_colors_per_object(): """ Set the wireframe color per surfacing object. For all meshes, sets it to black to start with, this implies that the mesh has not be assigned to any surfacing object yet will show black in the VP """ set_wifreframe_color_black() projects = get_surfacing_projects() print projects for project in projects: for surfacingObject in get_surfacing_objects(project): for mesh in surfacingObject.members(): mesh_shape = mesh.getShape() try: mesh_shape.overrideEnabled.set(1) mesh_shape.overrideRGBColors.set(1) mesh_shape.overrideColorRGB.set( ldtutils.get_random_color(surfacingObject) ) except: logger.error('Could not set override color for: %s, might ' 'belong to a display layer' % mesh ) def set_materials_per_object(shader_type): """Create a material per surfacing project and assigns it""" delete_materials() projects = get_surfacing_projects() for project in projects: for obj in get_surfacing_objects(project): shader, shading_group = create_shader( type=shader_type) pm.select(obj) meshes = pm.ls(sl=True) pm.sets(shading_group, forceElement=meshes) pm.select(None) if shader_type == 'aiStandardSurface': shader.baseColor.set( ldtutils.get_random_color(obj) ) else: shader.color.set( ldtutils.get_random_color(obj) ) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), 'obj', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), obj.name(), force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), 'color', force=True) def set_materials_per_project(shader_type): """Create a material per surfacing project and assigns it""" delete_materials() projects = get_surfacing_projects() for project in projects: shader, shading_group = create_shader( type=shader_type) pm.select(project) meshes = pm.ls(sl=True) pm.sets(shading_group, forceElement=meshes) pm.select(None) if shader_type == 'aiStandardSurface': shader.baseColor.set( ldtutils.get_random_color(project) ) else: shader.color.set( ldtutils.get_random_color(project) ) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), 'project', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), project.name(), force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), 'color', force=True) def delete_materials(): """delete all material networks that have surfacing attributes""" all_shading_groups = pm.ls(type="shadingEngine") to_delete = [] for shading_group in all_shading_groups: if pm.hasAttr(shading_group, ATTR_MATERIAL): to_delete.append(shading_group) pm.delete(to_delete) def delete_materials_viewport(type=None): """ delete all material networks that have surfacing attributes. Kwargs: type (str): type of vp material to delete, usually 'color', or 'pattern' """ all_shading_groups = pm.ls(type="shadingEngine") to_delete = [] for shading_group in all_shading_groups: if pm.hasAttr(shading_group, ATTR_MATERIAL_VP): to_delete.append(shading_group) pm.delete(to_delete) def unsaved_scene(): """Check for unsaved changes.""" import maya.cmds as cmds return cmds.file(q=True, modified=True) def save_scene_dialog(): """ Ask the user to go ahead save or cancel the operation. Returns: bool. True is Ok clicked, false otherwise. """ msg = QtWidgets.QMessageBox() msg.setIcon(QtWidgets.QMessageBox.Information) msg.setText("Your scene has unsaved changes") msg.setInformativeText("") msg.setWindowTitle("Warning") msg.setDetailedText( "This tool will do undoable changes. It requires you to save your scene, and reopen it after its finished" ) msg.setStandardButtons( QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Cancel ) retval = msg.exec_() if retval == QtWidgets.QMessageBox.Ok: return True else: return False def create_file_node(name=None): """ Create a file node, and its 2dPlacement Node. Kwargs: name (str): file node name Returns: PyNode. Image file node """ file_node = pm.shadingNode( 'file', name=name, asTexture=True, isColorManaged=True) placement_name = '%s_place2dfile_nodeture' % name placement_node = pm.shadingNode( 'place2dTexture', name=placement_name, asUtility=True) file_node.filterType.set(0) pm.connectAttr(placement_node.outUV, file_node.uvCoord) pm.connectAttr(placement_node.outUvFilterSize, file_node.uvFilterSize) pm.connectAttr(placement_node.coverage, file_node.coverage) pm.connectAttr(placement_node.mirrorU, file_node.mirrorU) pm.connectAttr(placement_node.mirrorV, file_node.mirrorV) pm.connectAttr(placement_node.noiseUV, file_node.noiseUV) pm.connectAttr(placement_node.offset, file_node.offset) pm.connectAttr(placement_node.repeatUV, file_node.repeatUV) pm.connectAttr(placement_node.rotateFrame, file_node.rotateFrame) pm.connectAttr(placement_node.rotateUV, file_node.rotateUV) pm.connectAttr(placement_node.stagger, file_node.stagger) pm.connectAttr(placement_node.translateFrame, file_node.translateFrame) pm.connectAttr(placement_node.wrapU, file_node.wrapU) pm.connectAttr(placement_node.wrapV, file_node.wrapV) return file_node def create_shader(type='PxrSurface'): """ Create shaders and shading groups. Kwargs: type (str): type of material shader to create, for ie 'blinn' tag (str): tag to set in ATTR_MATERIAL, usually the surfacing project or surfacing object Returns: tuple. PyNode shader, and PyNode shading_group """ shader, shading_group = pm.createSurfaceShader(type) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL), '', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_ASSIGN), '', force=True) pm.setAttr('%s.%s' % (shading_group, ATTR_MATERIAL_VP), '', force=True) return shader, shading_group def import_surfacing_textures(): """ TODO WE DONT REALLY NEED THIS DONT WE TODO THIS IS A WORKING HARDCODED IMPORT, IMPLEMENT CORRECTLY Import textures to surfacing objects or projects. Kwargs: parsed_files (list): list of lucidity parsed files with 'filepath' key key (str): surfacing attr to use for import, surfacing project or surfacing object shaders (list): a list of shaders, where the keys match the parsed files key to use for import """ import pymel.core as pm from ldtcommon import TEXTURE_FILE_PATTERN import ldtmaya import ldtutils import ldttextures textures_folder = '/run/media/ezequielm/misc/wrk/current/cabinPixar/textures' texture_list = ldtutils.get_files_in_folder( textures_folder, recursive=True, pattern='.tex') texture_finder = ldttextures.TextureFinder( TEXTURE_FILE_PATTERN, texture_list) texture_finder.get_channel_plug(texture_list[0]) for surfPrj in ldtmaya.get_surfacing_projects(): for surfObj in ldtmaya.get_surfacing_objects(surfPrj): # Find texture files with a matching surfacing_project, get udim paths texture_files = texture_finder.find_key_values( surfacing_project=surfPrj, merge_udims=True) if texture_files: print surfObj # create and assign the material to each surfacing_object pm.select(surfObj) meshes = pm.ls(sl=True) shader, shading_group = ldtmaya.create_shader() shader.rename('%s_%s' % (surfObj, 'material')) pm.sets(shading_group, forceElement=meshes) pm.select(None) for texture_file in texture_files: # get the shader plug input, to connect the texture to shader_plug = texture_finder.get_channel_plug(texture_file) if shader_plug: shader_plug = pm.PyNode( '%s.%s' % (shader.name(), shader_plug)) # create image nodes file_node = ldtmaya.create_file_node( '%s_%s' % (surfObj, shader_plug)) # paths come with 'udim', replac this with the prman <UDIM> file_node.fileTextureName.set( texture_file.replace('udim', '<UDIM>')) pm.setAttr('%s.%s' % (file_node, 'uvTilingMode'), 3) pm.setAttr('%s.%s' % (file_node, 'alphaIsLuminance'), 1) # TODO Query the shader plug, connect RGB or single channel # TODO if a normal or bump, create the inbetween node # if shader_plug == "normal" or shader_plug == "bump" print "plug %s -->%s" % (texture_file, shader_plug) if len(shader_plug.elements()) == 4: if "bump" in shader_plug.name(): bump_node = pm.shadingNode( 'bump2d', asTexture=True) bump_node.rename('%s_%s' % (surfObj, 'bump')) file_node.outAlpha.connect(bump_node.bumpValue) bump_node.outNormal.connect(shader_plug) else: file_node.outColor.connect(shader_plug) else: file_node.outAlpha.connect(shader_plug) ldtui代码:init_.py: """ .. module:: ldtui :synopsis: Main tools UI. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import sys import imp import os import logging from functools import partial from ldtui import qtutils import ldt logger = logging.getLogger(__name__) class LDTWindow(QMainWindow): '''Main Tools UI Window. Loads the plugInfo.plugin_object.plugin_layout QWidget from all loaded plugins as tabs''' def __init__(self, plugins): super(LDTWindow, self).__init__() self.setWindowTitle("Look Dev Tool Set") self.setGeometry(0, 0, 650, 600) layout = QtWidgets.QGridLayout() self.setLayout(layout) tabwidget = QtWidgets.QTabWidget() tabwidget.setTabBar(qtutils.HTabWidget(width=150, height=50)) tabwidget.setTabPosition(QtWidgets.QTabWidget.West) # Stylesheet fix for Katana # With default colors, the tab text is almost the # same as the tab background stylesheet = """ QTabBar::tab:unselected {background: #222222;} QTabWidget>QWidget>QWidget{background: #222222;} QTabBar::tab:selected {background: #303030;} QTabWidget>QWidget>QWidget{background: #303030;} """ tabwidget.setStyleSheet(stylesheet) layout.addWidget(tabwidget, 0, 0) plugins_ui = {} plugins_buttons = {} for pluginInfo in plugins.getAllPlugins(): tabwidget.addTab( pluginInfo.plugin_object.plugin_layout, pluginInfo.name) self.setCentralWidget(tabwidget) qtutils.py: """ .. module:: qtutils :synopsis: small qt utilies and custom widgets. .. moduleauthor:: Ezequiel Mastrasso """ from Qt import QtGui, QtWidgets, QtCore from Qt.QtWidgets import QApplication, QWidget, QLabel, QMainWindow import logging logger = logging.getLogger(__name__) def get_folder_path(): """Gets a folder path from the user""" file_dialog = QtWidgets.QFileDialog() file_dialog.setFileMode(QtWidgets.QFileDialog.Directory) if file_dialog.exec_(): path = str(file_dialog.selectedFiles()[0]) return path else: return None class HTabWidget(QtWidgets.QTabBar): ''' QPaint event to draw the QTabWidget titles horizontally ''' def __init__(self, *args, **kwargs): self.tabSize = QtCore.QSize(kwargs.pop('width'), kwargs.pop('height')) super(HTabWidget, self).__init__(*args, **kwargs) def paintEvent(self, event): painter = QtWidgets.QStylePainter(self) option = QtWidgets.QStyleOptionTab() for index in range(self.count()): self.initStyleOption(option, index) tabRect = self.tabRect(index) tabRect.moveLeft(10) painter.drawControl(QtWidgets.QStyle.CE_TabBarTabShape, option) painter.drawText(tabRect, QtCore.Qt.AlignVCenter | QtCore.Qt.TextDontClip, self.tabText(index)) def tabSizeHint(self, index): return self.tabSize ldtutils代码: """ .. module:: ldtutils :synopsis: general non dcc specific utils. .. moduleauthor:: Ezequiel Mastrasso """ from ldtcommon import CONFIG_MATERIALS_JSON from ldtcommon import TEXTURE_CHANNEL_MATCHING_RATIO from ldtcommon import TEXTURE_MATCHING_RATIO from fuzzywuzzy import fuzz import subprocess import multiprocessing import sys import json import logging import os import random import lucidity logger = logging.getLogger(__name__) def create_commands(texture_mapping): default_command = "maya -batch -file someMayaFile.mb -command " commands = [] for key, value in texture_mapping: dir, filename = os.path.split(key) filename, ext = os.path.splitext(filename) filename = os.path.join(dir, filename + "WithTexture." + ext) commands.append("".format(default_command, "abc_file", key, "texture", value, "file -save ", filename)) return commands def launch_function(func, args): commands = create_commands(args) launch_multiprocess(launch_subprocess, commands) def launch_subprocess(command): try: subprocess_output = subprocess.check_output(command) logger.info("Subprocess launched\t{out}\nrunning the command\t{command}", out=subprocess_output, command=command) except subprocess.CalledProcessError as e: logger.exception( "Error while trying to launch subprocess: {}".format(e.output)) raise return command def launch_multiprocess(function, args): """ :param function: Function to be called inside the multiprocess -- Takes only 1 arg :param args: list/tuple of arguments for above function :return: """ multiprocessing.freeze_support() pool = multiprocessing.Pool(processes=int(multiprocessing.cpu_count()/2)) logger.debug("Pool created") try: pool_process = pool.map_async(function, (args)) except Exception as e: msg = "Error while trying to launch process in pool: {}".format(e) logger.exception(msg) raise finally: pool.close() pool.join() try: logger.info("output from the process: {pool_out}", out=pool_process.successful()) return 0 except AssertionError: logger.exception("Couldn't communicate with the process poll created; \ No output from processes fetched") return 1 def map_textures_to_alembic(texture_mapping): launch_subprocess(command) def load_json(file_path): """ Load a json an returns a dict. Args: file_path (str): Json file path to open. """ with open(file_path) as handle: dictdump = json.loads(handle.read()) return dictdump def save_json(file_path, data): """ Dump a dict into a json file. Args: file_path (str): Json file path to save. data (dict): Data to save into the json file. """ # TODO (eze) pass def get_random_color(seed): """ Return a random color using a seed. Used by all material creating, and viewport color functions that do not use textures, to have a common color accross dccs Args: seed (str): Returns: tuple, R,G,B colors. """ random.seed(seed + "_r") color_red = random.uniform(0, 1) random.seed(seed + "_g") color_green = random.uniform(0, 1) random.seed(seed + "_b") color_blue = random.uniform(0, 1) return color_red, color_green, color_blue def create_directoy(path): """ Create a folder. Args: path (str): Directory path to create. """ os.mkdir(path) logger.info("Directory created: %s" % path) def is_directory(path): """ Check if the given path exists, and is a directory. Args: path (str): Directory to check. """ if os.path.exists(path) and os.path.isdir(path): return True else: return False def get_files_in_folder(path, recursive=False, pattern=None): """ Search files in a folder. Args: path (str): Path to search. Kwards: recursive (bool): Search files recursively in folder. pattern (str): pattern to match, for ie '.exr'. Returns: array. File list """ logger.info("Searching for files in: %s" % path) logger.info("Searching options: Recursive %s, pattern: %s" % (recursive, pattern)) file_list = [] for path, subdirs, files in os.walk(path): for file in files: # skip .mayaswatchs stuff if ".maya" not in file: if pattern: if pattern in file: file_list.append(os.path.join(path, file)) logger.debug( "File with pattern found, added to the list: %s" % file) else: file_list.append(os.path.join(path, file)) logger.debug("File added to the list: %s" % file) if not recursive: break return file_list def string_matching_ratio(stringA, stringB): """ Compare two strings and returns a fuzzy string matching ratio. In general ratio, partial_ratio, token_sort_ratio and token_set_ratio did not give different results given that we are comparin a single. TODO: Try bitap algorithm for fuzzy matching, partial substring matching might be better for our cases. Different channels fuzzy ratio comparission ('baseColor','diffusecolor') = 67 ('base','diffusecolor') = 25 ('specular','specularColor') = 76 ('specular','specularcolor') = 76 ('specular_color', 'specular_bump') = 67 ('coat_color', 'coat_ior') = 78 ('secondary_specular_color', 'secondary_specular_ior') = 91 ('subsurface_weight', 'subsurface_Color') = 67 ('emission', 'emission_weight') = 70 Same channel diferent naming ratio comparission ('diffuse_weight','diffuseGain') = 64 Args: stringA (str): string to compare against. stringB (str): string to compare. Returns: int. Ratio, from 0 to 100 according to fuzzy matching. """ return fuzz.token_set_ratio(stringA, stringB) def get_config_materials(): """ Gets the CONFIG_MATERIALS_JSON as a dict Returns: dict. CONFIG_MATERIALS_JSON """ return load_json(CONFIG_MATERIALS_JSON)
08-21
单向双向V2G 环境下分布式电源与电动汽车充电站联合配置方法(Matlab代码实现)内容概要:本文介绍了在单向和双向V2G(Vehicle-to-Grid)环境下,分布式电源与电动汽车充电站的联合配置方法,并提供了基于Matlab的代码实现。研究涵盖电力系统优化、可再生能源接入、电动汽车充放电调度、储能配置及微电网经济调度等多个关键技术领域,重点探讨了在不同电价机制和需求响应策略下,如何通过智能优化算法实现充电站与分布式电源的协同规划与运行优化。文中还展示了多种应用场景,如有序充电调度、鲁棒优化模型、多目标优化算法(如NSGA-II、粒子群算法)在电力系统中的实际应用,体现了较强的工程实践价值和技术综合性。; 适合人群:具备电力系统、新能源、智能优化算法等相关背景的科研人员、研究生及从事能源系统规划与优化的工程技术人员;熟悉Matlab/Simulink仿真工具者更佳。; 使用场景及目标:①用于科研项目中关于电动汽车与分布式电源协同配置的模型构建与仿真验证;②支持毕业论文、期刊投稿中的案例分析与算法对比;③指导实际电力系统中充电站布局与能源调度的优化设计。; 阅读建议:建议结合文中提供的Matlab代码与具体案例进行同步实践,重点关注优化模型的数学建模过程与算法实现细节,同时可参考文末网盘资源获取完整代码与数据集以提升学习效率。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值