Lightroom 5 SDK: folder children don't recognize their parent.

  • 2
  • Problem
  • Updated 5 years ago
Test code:

local function folderTest2( call )
local p1 = catalog:getFolders()[1]
local c = p1:getChildren()
for i, f in ipairs( c ) do
local p2 = f:getParent()
if p1 == p2 then
Debug.pause( "found parent" ) -- never happens
return
end
end
Debug.pause( "no children recognize their parent" ) -- happens
end
Photo of Rob Cole

Rob Cole

  • 4831 Posts
  • 385 Reply Likes

Posted 5 years ago

  • 2
Photo of John R. Ellis

John R. Ellis, Champion

  • 3895 Posts
  • 1035 Reply Likes
Interesting. This problem only appears to occur when the parent is a top-level folder. It seems related to the fact that in that case, folder:getPath() and child:getParent():getPath() return different values. Here's an example:

f:getParent(),f:getPath(), f:getChildren()[1]:getParent():getPath(), f == f:getChildren()[1]:getParent()
=>
nil
"C:\Users"
"C:\Users\"
false


f:getParent(),f:getPath(), f:getChildren()[1]:getParent():getPath(), f == f:getChildren()[1]:getParent()
=>
LrFolder
"C:\Users\Ellis"
"C:\Users\Ellis"
true

Just tested it, and this problem occurred in LR 4.4 as well.
Photo of Rob Cole

Rob Cole

  • 4831 Posts
  • 385 Reply Likes
So just correcting the path of top-level folders would fix the problem, I presume.
Photo of Rob Cole

Rob Cole

  • 4831 Posts
  • 385 Reply Likes
Work-around functions for the mean time (they need to be translated if you don't use Elare Plugin Framework):

--- Get folder path, normalized (without the trailing -slash which accompanies top-level folders).
--
function Catalog:getFolderPath( f )
local fPath = f:getPath()
if fPath:sub( -1 ):find( "[\\/]" ) then -- trailing slash or backslash (probably would manifest as OS path sep, but cheap insurance to check both..).
return fPath:sub( 1, #fPath - 1 ) -- assuming only one.
else
return fPath
end
end

--- Determine if two folders are equal.
-- @usage object equality only valid if both folders are top-level, or neither is.
function Catalog:isEqualFolders( f1, f2 )
if f1 == f2 then -- if equal, then equal.
return true
else -- equality based on normalized paths.
return self:getFolderPath( f1 ) == self:getFolderPath( f2 )
end
end
Catalog.foldersAreEqual = Catalog.isEqualFolders -- function Catalog:foldersAreEqual( ... ) -- "natural" syntax / synonym.