Load Expression

MakeHuman python API, python plugins, etc

Moderator: joepal

Load Expression

Postby Problematicproblem » Mon Apr 08, 2024 9:33 pm

Code: Select all
import os
import gui3d
import animation

class PoseLoader:
    def __init__(self):
        self.human = gui3d.app.selectedHuman
       
    def load_pose(self, filename):
        if not os.path.exists(filename):
            print(f"Error: File '{filename}' does not exist.")
            return
       
        pose = self._load_pose_from_file(filename)
        if pose:
            self._apply_pose_to_character(pose)
        else:
            print(f"Error: Failed to load pose from file '{filename}'.")

    def _load_pose_from_file(self, filename):
        pose_data = self._read_file(filename)
        if pose_data:
            return animation.Pose.fromDict(pose_data)
        else:
            return None

    def _read_file(self, filename):
        pose_data = {}
        with open(filename, 'r', encoding='utf-8') as file:
            for line in file:
                # Split each line into key-value pairs using the colon as separator
                parts = line.strip().split(':', 1)
                if len(parts) == 2:
                    key, value = parts
                    pose_data[key.strip()] = value.strip()
        return pose_data

    def _apply_pose_to_character(self, pose):
        self.human.setActiveAnimation(None)  # Clear existing animations
        self.human.addAnimation(pose)
        self.human.setActiveAnimation(pose.name)
        self.human.setPosed(True)
        self.human.refreshPose()

# Example usage:
pose_loader = PoseLoader()
pose_file_path = r"C:\Users\Michael\AppData\Local\makehuman-community\makehuman\data\expressions\Shapes\browDownLeft.mhpose"  # Replace with the actual file path
pose_loader.load_pose(pose_file_path)


That is the code that i have written to import an Expression (C:\Users\Michael\AppData\Local\makehuman-community\makehuman\data\expressions\Shapes\browDownLeft.mhpose) that i created via code. I get the following error:

name 'animation' is not defined
Traceback (most recent call last):
File "C:\Users\Michael\AppData\Local\makehuman-community\makehuman\plugins\7_scripting.py", line 830, in executeScript
exec(scriptSource)
File "<string>", line 48, in <module>
File "<string>", line 14, in load_pose
File "<string>", line 23, in _load_pose_from_file
NameError: name 'animation' is not defined


I am no programmer, i got no idea how to fix it

I would be glad if someone could help
Problematicproblem
 
Posts: 3
Joined: Sat Apr 06, 2024 9:50 am

Return to Python scripts

Who is online

Users browsing this forum: No registered users and 1 guest