### Install Pymeshio from archive Source: https://github.com/ousttrue/pymeshio/blob/master/README.rst Install pymeshio by downloading and extracting the archive. Navigate to the extracted directory and run the setup script. ```bash unzip pymeshio-x.x.x.zip cd pymeshio-x.x.x python setup.py install ``` -------------------------------- ### Install Pymeshio using pip Source: https://github.com/ousttrue/pymeshio/blob/master/README.rst Install the pymeshio package from PyPI using pip. This is the recommended method for installing the library. ```bash $ pip install pymeshio ``` -------------------------------- ### Writing PMX Files Source: https://context7.com/ousttrue/pymeshio/llms.txt Demonstrates creating a PMX 2.0 model, adding vertices with bone weights, defining bones with IK, and configuring materials before writing to a file. ```python import io import pymeshio.pmx import pymeshio.pmx.writer from pymeshio.common import Vector3, Vector2, RGB, RGBA # Create a new PMX model model = pymeshio.pmx.Model(version=2.0) model.name = 'テストモデル' model.english_name = 'Test Model' model.comment = 'Created with pymeshio' model.english_comment = 'Created with pymeshio' # Add vertices with bone deformation vertex = pymeshio.pmx.Vertex( position=Vector3(0, 0, 0), normal=Vector3(0, 1, 0), uv=Vector2(0, 0), deform=pymeshio.pmx.Bdef1(0), # Single bone weight edge_factor=1.0 ) model.vertices.append(vertex) # Add vertex with two-bone weight vertex2 = pymeshio.pmx.Vertex( position=Vector3(1, 0, 0), normal=Vector3(0, 1, 0), uv=Vector2(1, 0), deform=pymeshio.pmx.Bdef2(0, 1, 0.5), # Two bones, 50% weight edge_factor=1.0 ) model.vertices.append(vertex2) # Add indices model.indices = [0, 1, 2] # Add textures model.textures = ['texture.png', 'normal.png'] # Add a bone with IK bone = pymeshio.pmx.Bone( name='センター', english_name='center', position=Vector3(0, 0, 0), parent_index=-1, layer=0, flag=0x0002 | 0x0004 | 0x0008 | 0x0010 # rotatable, translatable, visible, manipulatable ) model.bones = [bone] # Add a material material = pymeshio.pmx.Material( name='マテリアル', english_name='material', diffuse_color=RGB(0.8, 0.8, 0.8), alpha=1.0, specular_factor=5.0, specular_color=RGB(1.0, 1.0, 1.0), ambient_color=RGB(0.2, 0.2, 0.2), flag=0x01 | 0x02, # both face + ground shadow edge_color=RGBA(0, 0, 0, 1), edge_size=1.0, texture_index=0, sphere_texture_index=-1, sphere_mode=0, toon_sharing_flag=1, toon_texture_index=0, comment='', vertex_count=3 ) model.materials = [material] # Write to file (UTF-16 encoding) pymeshio.pmx.writer.write_to_file(model, 'output.pmx') print("PMX file written successfully") ``` -------------------------------- ### Upload to PyPI Source: https://github.com/ousttrue/pymeshio/blob/master/README.rst Commands to check and upload the project to PyPI. Ensure you have the necessary build tools and credentials. ```bash $ python setup.py check -r $ python setup.py sdist --formats=zip upload ``` -------------------------------- ### Accessing Model Data Source: https://context7.com/ousttrue/pymeshio/llms.txt Demonstrates how to iterate through UVs, normals, materials, and faces of a loaded model, and how to retrieve specific vertex data by reference. ```python print(f"UVs: {len(model.uv)}") for uv in model.uv[:5]: print(f" UV: ({uv.x}, {uv.y})") # Access normals print(f"Normals: {len(model.normals)}") for n in model.normals[:5]: print(f" Normal: ({n.x}, {n.y}, {n.z})") # Access materials and faces for material in model.materials: print(f"Material: {material.name}") print(f" Kd (diffuse): {material.Kd}") print(f" Ka (ambient): {material.Ka}") print(f" Ks (specular): {material.Ks}") print(f" Faces: {len(material.faces)}") for face in material.faces[:3]: print(f" Face vertices: {len(face.vertex_references)}") for vref in face.vertex_references: print(f" v={vref.v}, vt={vref.vt}, vn={vref.vn}") # Get vertex data by reference ref = model.materials[0].faces[0].vertex_references[0] vertex_data = model.get_vertex(ref) print(f"Vertex position: {vertex_data[0]}") print(f"Vertex UV: {vertex_data[1]}") print(f"Vertex normal: {vertex_data[2]}") ``` -------------------------------- ### Reading from IO Streams Source: https://context7.com/ousttrue/pymeshio/llms.txt Demonstrates reading model files from byte streams or file-like objects. ```python import io import pymeshio.pmd.reader import pymeshio.pmx.reader import pymeshio.common # Read PMD from bytes with open('model.pmd', 'rb') as f: data = f.read() model = pymeshio.pmd.reader.read(io.BytesIO(data)) print(f"PMD from stream: {model}") # Read PMX from stream with io.open('model.pmx', 'rb') as f: model = pymeshio.pmx.reader.read(f) print(f"PMX from stream: {model}") # Use readall helper data = pymeshio.common.readall('model.pmd') model = pymeshio.pmd.reader.read(io.BytesIO(data)) ``` -------------------------------- ### Reading MQO Files Source: https://context7.com/ousttrue/pymeshio/llms.txt Parses Metasequoia 3D model files to extract material properties, object hierarchies, vertices, and face data. ```python import pymeshio.mqo.reader # Read MQO file model = pymeshio.mqo.reader.read_from_file('model.mqo') # Access materials for material in model.materials: print(f"Material: {material.name}") print(f"Color: RGBA({material.color.r}, {material.color.g}, {material.color.b}, {material.color.a})") print(f"Diffuse: {material.diffuse}") print(f"Ambient: {material.ambient}") print(f"Specular: {material.specular}") print(f"Texture: {material.tex}") # Access objects for obj in model.objects: print(f"Object: {obj.name}") print(f"Vertices: {len(obj.vertices)}") print(f"Faces: {len(obj.faces)}") print(f"Depth: {obj.depth}") print(f"Visible: {obj.visible}") # Access vertices for vertex in obj.vertices[:3]: print(f" Vertex: ({vertex.x}, {vertex.y}, {vertex.z})") # Access faces for face in obj.faces[:3]: print(f" Face indices: {face.indices}") print(f" Material index: {face.material_index}") print(f" UVs: {[str(uv) for uv in face.uv]}") ``` -------------------------------- ### Reading OBJ Files Source: https://context7.com/ousttrue/pymeshio/llms.txt Loads Wavefront OBJ files, including automatic parsing of associated MTL material files. ```python import pymeshio.obj.reader # Read OBJ file (automatically loads .mtl file if present) model = pymeshio.obj.reader.read_from_file('model.obj') # Access vertices print(f"Vertices: {len(model.vertices)}") for v in model.vertices[:5]: print(f" Position: ({v.x}, {v.y}, {v.z})") ``` -------------------------------- ### Write PMD File with Pymeshio Source: https://context7.com/ousttrue/pymeshio/llms.txt Creates a new PMD model programmatically by adding vertices, indices, bones, and materials, then writes it to a file. Requires importing common data types like Vector3, Vector2, and RGB. ```python import io import pymeshio.pmd import pymeshio.pmd.writer from pymeshio.common import Vector3, Vector2, RGB # Create a new PMD model model = pymeshio.pmd.Model(version=1.0) model.name = b'Test Model' model.comment = b'Created with pymeshio' model.english_name = b'Test Model' model.english_comment = b'Created with pymeshio' # Add vertices vertex = pymeshio.pmd.Vertex( pos=Vector3(0, 0, 0), normal=Vector3(0, 1, 0), uv=Vector2(0, 0), bone0=0, bone1=0, weight0=100, edge_flag=0 ) model.vertices.append(vertex) # Add indices (triangles) model.indices = [0, 1, 2] # Add a bone bone = pymeshio.pmd.Bone_RotateMove(b'center') bone.pos = Vector3(0, 0, 0) model.bones.append(bone) # Add a material material = pymeshio.pmd.Material( diffuse_color=RGB(0.8, 0.8, 0.8), alpha=1.0, specular_factor=5.0, specular_color=RGB(1.0, 1.0, 1.0), ambient_color=RGB(0.2, 0.2, 0.2), toon_index=0, edge_flag=1, vertex_count=3, texture_file=b'' ) model.materials.append(material) # Write to file with io.open('output.pmd', 'wb') as f: pymeshio.pmd.writer.write(f, model) print("PMD file written successfully") ``` -------------------------------- ### Read PMX File with Pymeshio Source: https://context7.com/ousttrue/pymeshio/llms.txt Loads an extended MikuMikuDance model file (PMX) and accesses its properties including version, names, comments, vertices with deformation data, bones with IK information, materials with extended properties, and morphs. ```python import pymeshio.pmx.reader # Read a PMX model model = pymeshio.pmx.reader.read_from_file('model.pmx') # Access model info print(f"Version: {model.version}") print(f"Name: {model.name}") print(f"English name: {model.english_name}") print(f"Comment: {model.comment}") # Access vertices with deformation data for vertex in model.vertices[:3]: print(f"Position: {vertex.position}") print(f"Normal: {vertex.normal}") print(f"UV: {vertex.uv}") print(f"Deform type: {type(vertex.deform).__name__}") print(f"Edge factor: {vertex.edge_factor}") # Access bone data with IK information for bone in model.bones[:5]: print(f"Bone: {bone.name}") print(f"Position: {bone.position}") print(f"Parent index: {bone.parent_index}") print(f"Has IK: {bone.getIkFlag()}") print(f"Rotatable: {bone.getRotatable()}") print(f"Translatable: {bone.getTranslatable()}") if bone.ik: print(f"IK target: {bone.ik.target_index}, loops: {bone.ik.loop}") # Access materials with extended properties for material in model.materials: print(f"Material: {material.name}") print(f"Diffuse: {material.diffuse_color}, Alpha: {material.alpha}") print(f"Edge color: {material.edge_color}, Edge size: {material.edge_size}") print(f"Texture index: {material.texture_index}") # Access morphs for morph in model.morphs: print(f"Morph: {morph.name}, type={morph.morph_type}, offsets={len(morph.offsets)}") ``` -------------------------------- ### Using Common Data Structures Source: https://context7.com/ousttrue/pymeshio/llms.txt Utilizes Pymeshio's built-in vector, quaternion, and color classes for 3D operations. ```python from pymeshio.common import Vector2, Vector3, Vector4, Quaternion, RGB, RGBA # Vector2 for UV coordinates uv = Vector2(0.5, 0.5) print(f"UV: {uv.x}, {uv.y}") print(f"As tuple: {uv.to_tuple()}") # Vector3 for positions and normals position = Vector3(1.0, 2.0, 3.0) normal = Vector3(0.0, 1.0, 0.0) print(f"Position: {position}") print(f"Norm: {position.getNorm()}") print(f"Normalized: {position.normalize()}") print(f"Dot product: {position.dot(normal)}") print(f"Cross product: {position.cross(normal)}") # Vector arithmetic v1 = Vector3(1, 0, 0) v2 = Vector3(0, 1, 0) v3 = v1 + v2 v4 = v1 - v2 v5 = v1 * 2.0 print(f"Add: {v3}") print(f"Sub: {v4}") print(f"Scale: {v5}") # Quaternion for rotations q = Quaternion(0, 0, 0, 1) # Identity quaternion print(f"Quaternion: {q}") roll, pitch, yaw = q.getRollPitchYaw() print(f"Roll, Pitch, Yaw: {roll}, {pitch}, {yaw}") # Create rotation from axis-angle import math axis = Vector3(0, 1, 0) angle = math.pi / 4 # 45 degrees rotation = Quaternion.createFromAxisAngle([axis.x, axis.y, axis.z], angle) print(f"Rotation quaternion: {rotation}") # RGB and RGBA colors diffuse = RGB(0.8, 0.2, 0.2) edge_color = RGBA(0, 0, 0, 1) print(f"Diffuse: R={diffuse.r}, G={diffuse.g}, B={diffuse.b}") print(f"Edge: R={edge_color.r}, G={edge_color.g}, B={edge_color.b}, A={edge_color.a}") ``` -------------------------------- ### Read PMD File with Pymeshio Source: https://context7.com/ousttrue/pymeshio/llms.txt Loads a MikuMikuDance model file (PMD) and accesses its properties like name, vertices, faces, materials, bones, IK chains, and morphs. Also demonstrates accessing individual vertex and bone data. ```python import pymeshio.pmd.reader # Read a PMD model from file model = pymeshio.pmd.reader.read_from_file('model.pmd') # Access model properties print(f"Model name: {model.name}") print(f"Vertices: {len(model.vertices)}") print(f"Faces: {len(model.indices)}") print(f"Materials: {len(model.materials)}") print(f"Bones: {len(model.bones)}") print(f"IK chains: {len(model.ik_list)}") print(f"Morphs: {len(model.morphs)}") # Access vertex data for vertex in model.vertices[:3]: print(f"Position: {vertex.pos}") print(f"Normal: {vertex.normal}") print(f"UV: {vertex.uv}") print(f"Bone weights: {vertex.bone0}, {vertex.bone1}, weight={vertex.weight0}") # Access bone hierarchy for bone in model.bones[:5]: print(f"Bone: {bone.name}, type={bone.type}, parent={bone.parent_index}") # Access materials for material in model.materials: print(f"Material: diffuse={material.diffuse_color}, alpha={material.alpha}") print(f"Texture: {material.texture_file}") # Output: ``` -------------------------------- ### Converting OBJ to PMX Source: https://context7.com/ousttrue/pymeshio/llms.txt Transforms a Wavefront OBJ model into the PMX format. ```python import pymeshio.obj.reader import pymeshio.pmx.writer import pymeshio.converter # Read OBJ model obj_model = pymeshio.obj.reader.read_from_file('input.obj') # Convert to PMX pmx_model = pymeshio.converter.obj_to_pmx( obj_model, name='Converted Model', scale=1.0 ) # Access converted data print(f"Vertices: {len(pmx_model.vertices)}") print(f"Indices: {len(pmx_model.indices)}") print(f"Materials: {len(pmx_model.materials)}") # Save as PMX pymeshio.pmx.writer.write_to_file(pmx_model, 'output.pmx') print("OBJ to PMX conversion complete!") ``` -------------------------------- ### Read PMD model from file Source: https://github.com/ousttrue/pymeshio/blob/master/README.rst Reads a MikuMikuDance PMD model from a specified file path. Requires importing the pmd reader module. ```python import pymeshio.pmd.reader m=pymeshio.pmd.reader.read_from_file('resources/初音ミクVer2.pmd') print(m) ``` -------------------------------- ### Write PMX model to file Source: https://github.com/ousttrue/pymeshio/blob/master/README.rst Writes a PMX model object to a specified file path. Returns True upon successful writing. ```python import pymeshio.pmx.writer pymeshio.pmx.writer.write_to_file(pmx_model, "out.pmx") ``` -------------------------------- ### Reading VMD Files Source: https://context7.com/ousttrue/pymeshio/llms.txt Loads motion animation data from VMD files, providing access to bone, morph, camera, and light keyframes. ```python import pymeshio.vmd.reader # Read VMD motion file motion = pymeshio.vmd.reader.read_from_file('motion.vmd') # Access motion info print(f"Model name: {motion.model_name}") print(f"Bone keyframes: {len(motion.motions)}") print(f"Morph keyframes: {len(motion.shapes)}") print(f"Camera keyframes: {len(motion.cameras)}") print(f"Light keyframes: {len(motion.lights)}") # Access bone animation frames for frame in motion.motions[:10]: print(f"Bone: {frame.name}, Frame: {frame.frame}") print(f"Position: {frame.pos}") print(f"Rotation (quaternion): {frame.q}") # Access morph/shape animation for shape in motion.shapes[:5]: print(f"Morph: {shape.name}, Frame: {shape.frame}, Ratio: {shape.ratio}") # Access camera animation for camera in motion.cameras[:5]: print(f"Camera frame: {camera.frame}") print(f"Position: {camera.pos}") print(f"Euler angles: {camera.euler}") print(f"Length: {camera.length}, Angle: {camera.angle}") ``` -------------------------------- ### Converting PMD to PMX Source: https://context7.com/ousttrue/pymeshio/llms.txt Converts a PMD model to the PMX format and verifies the preservation of model data. ```python import pymeshio.pmd.reader import pymeshio.pmx.writer import pymeshio.converter # Read PMD model pmd_model = pymeshio.pmd.reader.read_from_file('input.pmd') print(f"Original PMD: {pmd_model}") # Convert to PMX pmx_model = pymeshio.converter.pmd_to_pmx(pmd_model) print(f"Converted PMX: {pmx_model}") # Verify conversion preserved data print(f"Vertices: {len(pmx_model.vertices)}") print(f"Indices: {len(pmx_model.indices)}") print(f"Materials: {len(pmx_model.materials)}") print(f"Bones: {len(pmx_model.bones)}") print(f"Morphs: {len(pmx_model.morphs)}") print(f"Rigid bodies: {len(pmx_model.rigidbodies)}") print(f"Joints: {len(pmx_model.joints)}") # Save converted model pymeshio.pmx.writer.write_to_file(pmx_model, 'output.pmx') print("Conversion complete!") ``` -------------------------------- ### Convert PMD model to PMX format Source: https://github.com/ousttrue/pymeshio/blob/master/README.rst Converts a loaded PMD model object to the PMX format using the pymeshio converter module. The converted model can then be written to a file. ```python import pymeshio.converter pmx_model=pymeshio.converter.pmd_to_pmx(m) print(pmx_model) ``` -------------------------------- ### Read VMD motion data from file Source: https://github.com/ousttrue/pymeshio/blob/master/README.rst Reads MikuMikuDance VMD motion data from a specified file path. This function loads motion, shape, and camera data if present. ```python import pymeshio.vmd.reader pymeshio.vmd.reader.read_from_file('resources/motion.vmd') ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.