Skip to content

Commit 4b5fbef

Browse files
committed
IECoreMaya : Added Path/MDagPath conversion utility to LiveScene.
1 parent b387925 commit 4b5fbef

File tree

4 files changed

+141
-0
lines changed

4 files changed

+141
-0
lines changed

include/IECoreMaya/LiveScene.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,12 @@ class IECOREMAYA_API LiveScene : public IECoreScene::SceneInterface
192192
/// Returns the MDagPath object to the scene node
193193
MDagPath dagPath() const;
194194

195+
/// Returns the scene path corresponding to the maya dag path
196+
static void dagPathToPath( MDagPath dagPath, IECoreScene::SceneInterface::Path &path );
197+
198+
/// Returns the maya dag path corresponding to the scene path
199+
static void pathToDagPath( const IECoreScene::SceneInterface::Path &path, MDagPath &dagPath );
200+
195201
/// Translates cortex attribute name to maya attribute name
196202
/// Returns an empty string if there are no valid mappings
197203
static Name toMayaAttributeName( const Name &name );

src/IECoreMaya/LiveScene.cpp

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,66 @@ void LiveScene::hash( HashType hashType, double time, MurmurHash &h ) const
11221122
throw Exception( "Hashes currently not supported in IECoreMaya::LiveScene objects." );
11231123
}
11241124

1125+
void LiveScene::dagPathToPath( MDagPath dagPath, IECoreScene::SceneInterface::Path &path )
1126+
{
1127+
tbb::recursive_mutex::scoped_lock l( g_mutex );
1128+
path.clear();
1129+
1130+
if( dagPath.isValid() )
1131+
{
1132+
// Only transforms can be part of the path
1133+
if( !dagPath.hasFn( MFn::kTransform ) )
1134+
{
1135+
dagPath.pop();
1136+
}
1137+
1138+
const std::string pathStr( dagPath.fullPathName().asChar() );
1139+
const boost::tokenizer< boost::char_separator<char> > tokens( pathStr, boost::char_separator<char>( "|" ) );
1140+
for ( const auto &token : tokens )
1141+
{
1142+
path.push_back( token );
1143+
}
1144+
1145+
return;
1146+
}
1147+
1148+
throw Exception( "IECoreMaya::LiveScene::dagPathToPath invalid dag path." );
1149+
}
1150+
1151+
void LiveScene::pathToDagPath( const IECoreScene::SceneInterface::Path &path, MDagPath &dagPath )
1152+
{
1153+
tbb::recursive_mutex::scoped_lock l( g_mutex );
1154+
1155+
if( path.empty() )
1156+
{
1157+
MItDag itDag;
1158+
itDag.getPath( dagPath );
1159+
return;
1160+
}
1161+
1162+
std::string dagPathStr;
1163+
for( const auto &name : path )
1164+
{
1165+
dagPathStr += "|";
1166+
dagPathStr += name;
1167+
}
1168+
1169+
MSelectionList sel;
1170+
if( sel.add( dagPathStr.c_str() ) && sel.getDagPath( 0, dagPath ) )
1171+
{
1172+
return;
1173+
}
1174+
1175+
// Invalid dag path
1176+
std::string pathStr;
1177+
IECoreScene::SceneInterface::pathToString( path, pathStr );
1178+
throw Exception(
1179+
boost::str(
1180+
boost::format( "IECoreMaya::LiveScene::pathToDagPath invalid conversion to dag path from \"%1%\"." ) % pathStr
1181+
)
1182+
);
1183+
}
1184+
11251185
void LiveScene::registerCustomObject( HasFn hasFn, ReadFn readFn )
11261186
{
11271187
CustomReader r;

src/IECoreMaya/bindings/LiveSceneBinding.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,39 @@ inline std::string dagPath( object liveSceneObject )
204204
return liveScene.dagPath().fullPathName().asChar();
205205
}
206206

207+
inline list dagPathToPath( object dagObj )
208+
{
209+
list pathList;
210+
extract<MDagPath> dagPath( dagObj );
211+
212+
// Return the root path (an empty list) if we are given an empty string
213+
// Maya doesn't really have a "root" dag path, so this is as close as we can get while simultaneously
214+
// ensuring dagPathToPath( pathToDagPath( [] ) ) == []
215+
if( dagPath.check() && len( dagObj ) == 0 )
216+
{
217+
return pathList;
218+
}
219+
220+
// Get the path, or throw if the dag path object is invalid
221+
IECoreScene::SceneInterface::Path path;
222+
LiveScene::dagPathToPath( dagPath(), path );
223+
for(const auto &name : path )
224+
{
225+
pathList.append( name.string() );
226+
}
227+
return pathList;
228+
}
229+
230+
inline std::string pathToDagPath( list pathList )
231+
{
232+
IECoreScene::SceneInterface::Path path;
233+
container_utils::extend_container( path, pathList );
234+
235+
MDagPath dagPath;
236+
LiveScene::pathToDagPath( path, dagPath );
237+
return dagPath.fullPathName().asChar();
238+
}
239+
207240
} // namespace
208241

209242
void IECoreMaya::bindLiveScene()
@@ -213,6 +246,8 @@ void IECoreMaya::bindLiveScene()
213246
.def( "registerCustomTags", registerCustomTags, ( arg_( "hasFn" ), arg_( "readFn" ) ) ).staticmethod( "registerCustomTags" )
214247
.def( "registerCustomAttributes", registerCustomAttributes, ( arg_( "namesFn" ), arg_( "readFn" ), arg_( "mightHaveFn" ) = object() ) ).staticmethod( "registerCustomAttributes" )
215248
.def( "dagPath", ::dagPath )
249+
.def( "dagPathToPath", ::dagPathToPath ).staticmethod( "dagPathToPath" )
250+
.def( "pathToDagPath", ::pathToDagPath ).staticmethod( "pathToDagPath" )
216251
.def( "toMayaAttributeName", LiveScene::toMayaAttributeName ).staticmethod( "toMayaAttributeName" )
217252
.def( "fromMayaAttributeName", LiveScene::fromMayaAttributeName ).staticmethod( "fromMayaAttributeName" )
218253
.def_readonly("visibilityOverrideName", LiveScene::visibilityOverrideName )

test/IECoreMaya/LiveSceneTest.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1458,6 +1458,46 @@ def testDagPath( self ):
14581458
cubeScene = liveScene.scene( [group, cube] )
14591459
self.assertEqual( cubeScene.dagPath(), '|{}|{}'.format( group, cube ) )
14601460

1461+
def testDagPathToPath( self ):
1462+
group = str( maya.cmds.group( empty=True ) )
1463+
cubeTransform = maya.cmds.polyCube( constructionHistory=False )[0]
1464+
maya.cmds.parent( cubeTransform, group )
1465+
cubeTransform = str( maya.cmds.ls( cubeTransform, long=True )[0] )
1466+
cubeShape = str( maya.cmds.listRelatives( cubeTransform, fullPath=True )[0] )
1467+
1468+
dagTransform = OpenMaya.MDagPath()
1469+
dagShape = OpenMaya.MDagPath()
1470+
sel = OpenMaya.MSelectionList()
1471+
sel.add( cubeTransform )
1472+
sel.add( cubeShape )
1473+
sel.getDagPath( 0, dagTransform )
1474+
sel.getDagPath( 0, dagShape )
1475+
1476+
pathTransform = IECoreMaya.LiveScene.dagPathToPath( dagTransform.fullPathName() )
1477+
self.assertEqual( pathTransform, cubeTransform[1:].split('|') )
1478+
1479+
pathShape = IECoreMaya.LiveScene.dagPathToPath( dagShape.fullPathName() )
1480+
self.assertEqual( pathShape, cubeTransform[1:].split('|') )
1481+
1482+
pathRoot = IECoreMaya.LiveScene.dagPathToPath( '' )
1483+
self.assertEqual( pathRoot, [] )
1484+
1485+
self.assertRaises( Exception, IECoreMaya.LiveScene.dagPathToPath, '|invalid' )
1486+
1487+
def testPathToDagPath( self ):
1488+
group = str( maya.cmds.group( empty=True ) )
1489+
cube = str( maya.cmds.polyCube( constructionHistory=False )[0] )
1490+
maya.cmds.parent( cube, group )
1491+
1492+
path = [group, cube]
1493+
dagTransform = IECoreMaya.LiveScene.pathToDagPath( path )
1494+
self.assertEqual( dagTransform, '|{}|{}'.format( group, cube ) )
1495+
1496+
dagRoot = IECoreMaya.LiveScene.pathToDagPath( [] )
1497+
self.assertEqual( dagRoot, '' )
1498+
1499+
self.assertRaises( Exception, IECoreMaya.LiveScene.pathToDagPath, ['invalid'] )
1500+
14611501

14621502
if __name__ == "__main__":
14631503
IECoreMaya.TestProgram( plugins = [ "ieCore" ] )

0 commit comments

Comments
 (0)