3333// ////////////////////////////////////////////////////////////////////////
3434
3535#include " IECoreNuke/ToNukeGeometryConverter.h"
36+ #include " IECoreNuke/Convert.h"
3637#include " IECoreNuke/LiveScene.h"
3738
3839#include " IECore/CompoundData.h"
40+ #include " IECore/MessageHandler.h"
41+ #include " IECore/CompoundObject.h"
3942#include " IECore/CompoundParameter.h"
43+ #include " IECore/SimpleTypedData.h"
4044
4145#include < cassert>
4246
47+ #include " boost/format.hpp"
48+
4349using namespace IECoreNuke ;
4450using namespace IECore ;
4551using namespace DD ::Image;
4652
53+ namespace
54+ {
55+
56+ void writeAttribute ( DD ::Image::GeometryList &geoList, int objIndex, const char *name, const IECore::Object *value )
57+ {
58+ switch ( value->typeId () )
59+ {
60+ case IECore::FloatDataTypeId :
61+ {
62+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::FLOAT_ATTRIB );
63+ attr->flt () = static_cast <const IECore::FloatData *>( value )->readable ();
64+ break ;
65+ }
66+ case IECore::IntDataTypeId :
67+ {
68+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::INT_ATTRIB );
69+ attr->integer () = static_cast <const IECore::IntData *>( value )->readable ();
70+ break ;
71+ }
72+ case IECore::BoolDataTypeId :
73+ {
74+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::INT_ATTRIB );
75+ attr->integer () = static_cast <const IECore::BoolData *>( value )->readable () ? 1 : 0 ;
76+ break ;
77+ }
78+ case IECore::StringDataTypeId :
79+ {
80+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::STD_STRING_ATTRIB );
81+ attr->stdstring () = static_cast <const IECore::StringData *>( value )->readable ();
82+ break ;
83+ }
84+ case IECore::V2fDataTypeId :
85+ {
86+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::VECTOR2_ATTRIB );
87+ const auto &v = static_cast <const IECore::V2fData *>( value )->readable ();
88+ attr->vector2 () = DD::Image::Vector2 ( v.x , v.y );
89+ break ;
90+ }
91+ case IECore::V3fDataTypeId :
92+ {
93+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::VECTOR3_ATTRIB );
94+ attr->vector3 () = IECore::convert<DD ::Image::Vector3>( static_cast <const IECore::V3fData *>( value )->readable () );
95+ break ;
96+ }
97+ case IECore::Color4fDataTypeId :
98+ {
99+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::VECTOR4_ATTRIB );
100+ const auto &c = static_cast <const IECore::Color4fData *>( value )->readable ();
101+ attr->vector4 () = DD::Image::Vector4 ( c.r , c.g , c.b , c.a );
102+ break ;
103+ }
104+ case IECore::M33fDataTypeId :
105+ {
106+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::MATRIX3_ATTRIB );
107+ const auto &m = static_cast <const IECore::M33fData *>( value )->readable ();
108+ DD ::Image::Matrix3 result;
109+ for ( int i = 0 ; i < 3 ; i++ )
110+ {
111+ for ( int j = 0 ; j < 3 ; j++ )
112+ {
113+ result[j][i] = m[j][i];
114+ }
115+ }
116+ attr->matrix3 () = result;
117+ break ;
118+ }
119+ case IECore::M44fDataTypeId :
120+ {
121+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::MATRIX4_ATTRIB );
122+ attr->matrix4 () = IECore::convert<DD ::Image::Matrix4>( Imath::M44d ( static_cast <const IECore::M44fData *>( value )->readable () ) );
123+ break ;
124+ }
125+ case IECore::M44dDataTypeId :
126+ {
127+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::MATRIX4_ATTRIB );
128+ attr->matrix4 () = IECore::convert<DD ::Image::Matrix4>( static_cast <const IECore::M44dData *>( value )->readable () );
129+ break ;
130+ }
131+ case IECore::M33dDataTypeId :
132+ {
133+ auto attr = geoList.writable_attribute ( objIndex, GroupType::Group_Object, name, AttribType::MATRIX3_ATTRIB );
134+ const auto &m = static_cast <const IECore::M33dData *>( value )->readable ();
135+ DD ::Image::Matrix3 result;
136+ for ( int i = 0 ; i < 3 ; i++ )
137+ {
138+ for ( int j = 0 ; j < 3 ; j++ )
139+ {
140+ result[j][i] = m[j][i];
141+ }
142+ }
143+ attr->matrix3 () = result;
144+ break ;
145+ }
146+ default :
147+ IECore::msg ( IECore::Msg::Warning, " ToNukeGeometryConverter" , boost::format ( " Unsupported attribute type \" %s\" for \" %s\" " ) % value->typeName () % name );
148+ break ;
149+ }
150+ }
151+
152+ } // namespace
153+
47154IE_CORE_DEFINERUNTIMETYPED ( ToNukeGeometryConverter );
48155
49156ToNukeGeometryConverter::ToNukeGeometryConverter ( const std::string &description, IECore::TypeId fromType, ConstObjectPtr object )
@@ -57,6 +164,9 @@ ToNukeGeometryConverter::ToNukeGeometryConverter( const std::string &description
57164 m_pathParameter = new StringParameter ( " path" , " The object path in the hierarchy." , new StringData () );
58165 parameters ()->addParameter ( m_pathParameter );
59166
167+ m_attributesParameter = new CompoundObjectParameter ( " attributes" , " Scene attributes to write as Group_Object attributes." , new CompoundObject () );
168+ parameters ()->addParameter ( m_attributesParameter );
169+
60170}
61171
62172void ToNukeGeometryConverter::convert ( GeometryList &geoList ) const
@@ -72,6 +182,13 @@ void ToNukeGeometryConverter::convert( GeometryList &geoList ) const
72182 auto nameAttribute = geoList.writable_attribute ( objIndex, GroupType::Group_Object, IECoreNuke::LiveScene::nameAttribute.data (), AttribType::STD_STRING_ATTRIB );
73183 nameAttribute->stdstring () = m_pathParameter->getTypedValue ();
74184
185+ // add scene attributes as Group_Object attributes
186+ ConstCompoundObjectPtr attributes = m_attributesParameter->getTypedValue <CompoundObject>();
187+ for ( const auto &attr : attributes->members () )
188+ {
189+ writeAttribute ( geoList, objIndex, attr.first .c_str (), attr.second .get () );
190+ }
191+
75192 ConstCompoundObjectPtr operands = parameters ()->getTypedValidatedValue <CompoundObject>();
76193 doConversion ( srcParameter ()->getValidatedValue (), geoList, objIndex, operands.get () );
77194}
0 commit comments