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
//! Rendering material textures to memory or disk
//!
use crate::ffi::{raw::HAPI_MaterialInfo, ImageInfo};
use crate::node::{HoudiniNode, NodeHandle};
use crate::parameter::ParmHandle;
use crate::session::Session;
use crate::Result;
use std::ffi::CString;
use std::path::Path;

#[derive(Debug, Clone)]
/// Represents a material node (SHOP) with methods for texture baking
pub struct Material {
    pub(crate) session: Session,
    pub(crate) info: HAPI_MaterialInfo,
}

impl Material {
    #[inline]
    pub fn node(&self) -> Result<HoudiniNode> {
        HoudiniNode::new(self.session.clone(), self.node_handle(), None)
    }

    #[inline]
    fn node_handle(&self) -> NodeHandle {
        NodeHandle(self.info.nodeId)
    }

    #[inline]
    pub fn has_changed(&self) -> bool {
        self.info.hasChanged > 0
    }

    pub fn render_texture(&self, parm_name: &str) -> Result<()> {
        debug_assert!(self.session.is_valid());
        let name = CString::new(parm_name)?;
        let id = crate::ffi::get_parm_id_from_name(&name, self.node_handle(), &self.session)?;
        crate::ffi::render_texture_to_image(&self.session, self.node_handle(), ParmHandle(id))
    }

    pub fn extract_image_to_file(
        &self,
        image_planes: impl AsRef<str>,
        path: impl AsRef<Path>,
    ) -> Result<String> {
        debug_assert!(self.session.is_valid());
        extract_image_to_file(&self.session, self.node_handle(), image_planes, path)
    }

    pub fn extract_image_to_memory(
        &self,
        buffer: &mut Vec<u8>,
        image_planes: impl AsRef<str>,
        format: impl AsRef<str>,
    ) -> Result<()> {
        debug_assert!(self.session.is_valid());
        extract_image_to_memory(
            &self.session,
            self.node_handle(),
            buffer,
            image_planes,
            format,
        )
    }

    pub fn set_image_info(&self, info: &ImageInfo) -> Result<()> {
        debug_assert!(self.session.is_valid());
        crate::ffi::set_image_info(&self.session, self.node_handle(), info)
    }

    pub fn get_image_info(&self) -> Result<ImageInfo> {
        debug_assert!(self.session.is_valid());
        crate::ffi::get_image_info(&self.session, self.node_handle())
            .map(|inner| ImageInfo { inner })
    }

    pub fn get_image_planes(&self) -> Result<Vec<String>> {
        debug_assert!(self.session.is_valid());
        crate::ffi::get_image_planes(&self.session, self.node_handle())
            .map(|a| a.into_iter().collect())
    }
}

pub(crate) fn extract_image_to_file(
    session: &Session,
    node: NodeHandle,
    image_planes: impl AsRef<str>,
    path: impl AsRef<Path>,
) -> Result<String> {
    debug_assert!(session.is_valid());
    let path = path.as_ref();
    let format = CString::new(
        path.extension()
            .expect("extension")
            .to_string_lossy()
            .to_string()
            .to_uppercase(),
    )?;
    let image_planes = CString::new(image_planes.as_ref())?;
    let dest_folder = CString::new(path.parent().expect("parent").to_string_lossy().to_string())?;
    let dest_file = CString::new(
        path.file_stem()
            .expect("extension")
            .to_string_lossy()
            .to_string(),
    )?;
    crate::ffi::extract_image_to_file(
        session,
        node,
        &format,
        &image_planes,
        &dest_folder,
        &dest_file,
    )
}

pub(crate) fn extract_image_to_memory(
    session: &Session,
    node: NodeHandle,
    buffer: &mut Vec<u8>,
    image_planes: impl AsRef<str>,
    format: impl AsRef<str>,
) -> Result<()> {
    let format = CString::new(format.as_ref())?;
    let image_planes = CString::new(image_planes.as_ref())?;
    crate::ffi::extract_image_to_memory(session, node, buffer, &format, &image_planes)
}