mmd-mpl 0.3.4

MPL is a rule-based Domain-Specific Language for creating MMD poses and animations using natural semantic syntax
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use crate::{
    animation::MPLAnimation, mpl::MPLKeyFrame, pose::MPLPose, VMDReader, VMDWriter, VPDReader,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MPLScript {
    pub poses: HashMap<String, MPLPose>,
    pub animations: HashMap<String, MPLAnimation>,
    pub main: Vec<String>,
}

impl MPLScript {
    pub fn new() -> Self {
        Self {
            poses: HashMap::new(),
            animations: HashMap::new(),
            main: vec![],
        }
    }
    pub fn to_key_frames(&self) -> Vec<MPLKeyFrame> {
        let mut key_frames = vec![];
        for name in &self.main {
            if self.animations.contains_key(name) {
                let anim = self.animations.get(name).unwrap();
                for statement in &anim.statements {
                    let mut bone_frames = vec![];
                    if statement.poses.len() == 1 {
                        let pose_name = statement.poses[0].clone();
                        let pose = self.poses.get(&pose_name).unwrap();
                        bone_frames.extend(pose.to_bone_frames());
                    } else {
                        let mut pose_statements = vec![];
                        for pose_name in &statement.poses {
                            let pose = self.poses.get(pose_name).unwrap();
                            pose_statements.extend(pose.statements.clone());
                        }
                        let pose = MPLPose::new("composite".to_string(), pose_statements);
                        bone_frames.extend(pose.to_bone_frames());
                    }
                    key_frames.push(MPLKeyFrame::new(statement.time, bone_frames, vec![]));
                }
            } else if self.poses.contains_key(name) {
                let pose = self.poses.get(name).unwrap();
                key_frames.push(MPLKeyFrame::new(0.0, pose.to_bone_frames(), vec![]));
            }
        }
        key_frames
    }
}

enum BlockType {
    None,
    Pose,
    Animation,
    Main,
}

pub struct MPLCompiler {}

impl MPLCompiler {
    pub fn new() -> Self {
        Self {}
    }

    pub fn compile(&self, text: &str) -> Result<Vec<u8>, String> {
        let mut in_block = false;
        let mut brace_count = 0;
        let mut current_block = String::new();
        let mut script = MPLScript::new();
        let mut block_type = BlockType::None;

        for (line_number, line) in text.lines().enumerate() {
            let trimmed = line.trim();
            if trimmed.is_empty() {
                continue;
            }
            if trimmed.starts_with("@pose")
                || trimmed.starts_with("@animation")
                || trimmed.starts_with("main")
            {
                if in_block {
                    return Err(format!(
                        "Line {}: Nested block is not allowed",
                        line_number + 1
                    ));
                }
                in_block = true;
                match trimmed.split_whitespace().next().unwrap() {
                    "@pose" => block_type = BlockType::Pose,
                    "@animation" => block_type = BlockType::Animation,
                    "main" => block_type = BlockType::Main,
                    _ => return Err(format!("Line {}: Invalid block type", line_number + 1)),
                }
            }

            if !in_block {
                return Err(format!(
                    "Line {}: Invalid text outside of block",
                    line_number + 1
                ));
            }
            current_block.push_str(line);
            current_block.push('\n');

            brace_count += line.chars().filter(|&c| c == '{').count() as i32;
            brace_count -= line.chars().filter(|&c| c == '}').count() as i32;

            if brace_count < 0 {
                return Err(format!(
                    "Line {}: Unexpected closing brace",
                    line_number + 1
                ));
            }
            if brace_count == 0 {
                match block_type {
                    BlockType::Pose => {
                        let pose = self.parse_pose(&current_block)?;

                        // Check for duplicate pose name
                        if script.poses.contains_key(&pose.name) {
                            return Err(format!("Duplicate pose name: '{}'", pose.name));
                        }
                        if script.animations.contains_key(&pose.name)
                            || script.poses.contains_key(&pose.name)
                        {
                            return Err(format!(
                                "Name '{}' already used by an animation",
                                pose.name
                            ));
                        }

                        script.poses.insert(pose.name.clone(), pose);
                    }
                    BlockType::Animation => {
                        let animation = self.parse_animation(&current_block)?;

                        // Check for duplicate animation name
                        if script.animations.contains_key(&animation.name)
                            || script.poses.contains_key(&animation.name)
                        {
                            return Err(format!("Duplicate animation name: '{}'", animation.name));
                        }

                        if script.poses.contains_key(&animation.name) {
                            return Err(format!(
                                "Name '{}' already used by a pose",
                                animation.name
                            ));
                        }

                        // Validate that all referenced poses exist
                        for statement in &animation.statements {
                            for pose_name in &statement.poses {
                                if !script.poses.contains_key(pose_name) {
                                    return Err(format!(
                                        "Animation '{}' references unknown pose '{}'",
                                        animation.name, pose_name
                                    ));
                                }
                            }
                        }

                        script.animations.insert(animation.name.clone(), animation);
                    }
                    BlockType::Main => {
                        let main = self.parse_main(&current_block)?;

                        // Validate that all referenced animations/poses exist
                        for reference in &main {
                            if !script.animations.contains_key(reference)
                                && !script.poses.contains_key(reference)
                            {
                                return Err(format!(
                                    "Main references unknown animation or pose '{}'",
                                    reference
                                ));
                            }
                        }

                        script.main = main;
                    }
                    BlockType::None => {}
                }
                current_block.clear();
                in_block = false;
                block_type = BlockType::None;
            }
        }

        if in_block {
            return Err("Unclosed block".to_string());
        }

        let key_frames = script.to_key_frames();
        let vmd_bytes = VMDWriter::new(key_frames);
        match vmd_bytes.write() {
            Ok(bytes) => Ok(bytes),
            Err(e) => Err(e.to_string()),
        }
    }

    pub fn from_vmd(&self, vmd_data: &[u8]) -> Result<String, String> {
        let mut script = String::new();
        let reader = VMDReader::new();
        let read_key_frames = reader.read(vmd_data).map_err(|e| e.to_string())?;

        let mut pose_map: std::collections::HashMap<String, MPLPose> =
            std::collections::HashMap::new(); // statements_content -> pose
        let mut animation_statements = Vec::new();
        let mut pose_counter = 0;
        let mut total_poses_processed = 0;

        for (i, keyframe) in read_key_frames.iter().enumerate() {
            let pose =
                MPLPose::from_bone_frames(&format!("pose_{}", i), keyframe.bone_frames.clone());

            total_poses_processed += 1;

            // Skip empty poses
            if pose.statements.is_empty() {
                continue;
            }

            let statements_content = pose.to_string(); // Just the statements, no pose name
            let pose_name = if let Some(existing_pose) = pose_map.get(&statements_content) {
                existing_pose.name.clone()
            } else {
                pose_counter += 1;
                let new_pose_name = format!("pose_{}", pose_counter - 1);
                let new_pose = MPLPose::new(new_pose_name.clone(), pose.statements.clone());
                pose_map.insert(statements_content, new_pose);
                new_pose_name
            };

            // Create animation statement
            animation_statements.push(format!("    {:.2}: {};\n", keyframe.time, pose_name));
        }

        // Add unique poses to script using to_block()
        let unique_poses_count = pose_map.len();
        for (_, pose) in pose_map {
            script.push_str(&pose.to_block());
            script.push_str("\n");
        }

        script.push_str("@animation extracted_animation {\n");
        for statement in animation_statements {
            script.push_str(&statement);
        }
        script.push_str("}\n\n");

        script.push_str("main {\n");
        script.push_str("    extracted_animation;\n");
        script.push_str("}\n");

        println!("Read {} keyframes", total_poses_processed);
        println!("Reversed to {} poses", unique_poses_count);

        Ok(script)
    }

    pub fn from_vpd(&self, vpd_data: &[u8]) -> Result<String, String> {
        let reader = VPDReader::new();
        match reader.read(vpd_data) {
            Ok(bone_frames) => {
                let pose = MPLPose::from_bone_frames(&"pose_1", bone_frames.clone());
                Ok(pose.to_script())
            }
            Err(e) => Err(e.to_string()),
        }
    }

    fn parse_pose(&self, text: &str) -> Result<MPLPose, String> {
        let mut pose_name = String::new();
        let mut statements = Vec::new();

        for (line_number, line) in text.lines().enumerate() {
            let trimmed = line.trim();
            if trimmed.is_empty() || trimmed == "{" || trimmed == "}" {
                continue;
            }

            if trimmed.starts_with("@pose") {
                let name_part = trimmed.trim_end_matches('{').trim();
                pose_name = name_part
                    .split_whitespace()
                    .nth(1)
                    .ok_or(format!("Line {}: Missing pose name", line_number + 1))?
                    .to_string();
                continue;
            }

            if trimmed.ends_with(';') {
                let stmt_text = trimmed.trim_end_matches(';').trim();
                if !stmt_text.is_empty() {
                    let parts: Vec<&str> = stmt_text.split(',').collect();

                    if parts.len() > 1 {
                        // Compound statement - extract bone from first part
                        let first_parts: Vec<&str> = parts[0].trim().split_whitespace().collect();
                        let bone_name = if first_parts.len() == 4 {
                            first_parts[0]
                        } else if first_parts.len() == 2 && first_parts[1] == "reset" {
                            first_parts[0]
                        } else {
                            return Err(format!(
                                "Line {} (statement 1): Invalid statement",
                                line_number + 1
                            ));
                        };

                        // Parse each part
                        for (i, part) in parts.iter().enumerate() {
                            let trimmed_part = part.trim();
                            if !trimmed_part.is_empty() {
                                let full_stmt = if i == 0 {
                                    trimmed_part.to_string()
                                } else {
                                    format!("{} {}", bone_name, trimmed_part)
                                };

                                match crate::pose::MPLPoseStatement::from_str(&full_stmt) {
                                    Ok(stmt) => statements.push(stmt),
                                    Err(e) => {
                                        return Err(format!(
                                            "Line {} (statement {}): {}",
                                            line_number + 1,
                                            i + 1,
                                            e
                                        ))
                                    }
                                }
                            }
                        }
                    } else {
                        // Single statement
                        match crate::pose::MPLPoseStatement::from_str(stmt_text) {
                            Ok(stmt) => statements.push(stmt),
                            Err(e) => return Err(format!("Line {}: {}", line_number + 1, e)),
                        }
                    }
                }
            } else {
                return Err(format!(
                    "Line {}: Statement must end with semicolon",
                    line_number + 1
                ));
            }
        }

        if pose_name.is_empty() {
            return Err("No pose declaration found".to_string());
        }

        // if statements.is_empty() {
        //     return Err("Pose must contain at least one statement".to_string());
        // }

        Ok(MPLPose::new(pose_name, statements))
    }

    fn parse_animation(&self, text: &str) -> Result<MPLAnimation, String> {
        let mut animation_name = String::new();
        let mut statements = Vec::new();

        for (line_number, line) in text.lines().enumerate() {
            let trimmed = line.trim();
            if trimmed.is_empty() || trimmed == "{" || trimmed == "}" {
                continue;
            }

            if trimmed.starts_with("@animation") {
                let name_part = trimmed.trim_end_matches('{').trim();
                animation_name = name_part
                    .split_whitespace()
                    .nth(1)
                    .ok_or(format!("Line {}: Missing animation name", line_number + 1))?
                    .to_string();
                continue;
            }

            if trimmed.ends_with(';') {
                let stmt_text = trimmed.trim_end_matches(';').trim();
                if !stmt_text.is_empty() {
                    match crate::animation::MPLAnimationStatement::from_str(stmt_text) {
                        Ok(stmt) => statements.push(stmt),
                        Err(e) => return Err(format!("Line {}: {}", line_number + 1, e)),
                    }
                }
            } else {
                return Err(format!(
                    "Line {}: Statement must end with semicolon",
                    line_number + 1
                ));
            }
        }

        if animation_name.is_empty() {
            return Err("No animation declaration found".to_string());
        }

        if statements.is_empty() {
            return Err("Animation must contain at least one statement".to_string());
        }

        Ok(MPLAnimation::new(animation_name, statements))
    }

    fn parse_main(&self, text: &str) -> Result<Vec<String>, String> {
        let mut animations = Vec::new();

        for (line_number, line) in text.lines().enumerate() {
            let trimmed = line.trim();
            if trimmed.is_empty() || trimmed == "{" || trimmed == "}" {
                continue;
            }

            // Skip main declaration
            if trimmed == "main" || trimmed.starts_with("main") {
                continue;
            }

            // Parse animation/pose reference (must end with semicolon)
            if trimmed.ends_with(';') {
                let name = trimmed.trim_end_matches(';').trim();
                if !name.is_empty() {
                    animations.push(name.to_string());
                }
            } else {
                return Err(format!(
                    "Line {}: Animation reference must end with semicolon",
                    line_number + 1
                ));
            }
        }

        if animations.is_empty() {
            return Err("Main block must contain at least one animation reference".to_string());
        }

        Ok(animations)
    }
}