Profile


Name
trim
ID
108773
Shared with
Public
Parent
None
Children
None
Created on
July 19, 2024 at 15:53 PM UTC
Updated on
July 19, 2024 at 15:54 PM UTC
Description

Quick demo of structure.TrimPose function. The function can throw errors, so the demo uses pcall () to catch them.

Best for


Code


--[[ trim - quick demo of structure.TrimPose use pcall to catch errors which otherwise end the recipe LociOiling 20240719 ]]-- -- -- ParseError -- common routine used by safe functions -- function ParseError ( errmsg ) local reason local start, stop, line, msg start, stop, line, msg = errmsg:find ( ":(%d+):%s()" ) if msg ~= nil then errmsg = errmsg:sub ( msg, #errmsg ) end return errmsg end -- -- SafeTrim uses pcall -- to call structure.TrimPose, returning -- a numeric return code and an error message. -- -- The return codes are: -- -- 0 - successful, error message is nil -- -1 - pose was already trimmed -- -2 - nothing selected -- -999 - other error -- function SafeTrim () -- local BADTRIM = "Cannot trim already trimmed pose." local NOSEL = "Cannot trim pose with no residues selected." -- local good, errmsg = pcall ( structure.TrimPose ) if good then return 0, nil else local err2 = ParseError ( errmsg ) local errp = err2:find ( BADTRIM ) if errp ~= nil then return -1, err2 end local errp = err2:find ( NOSEL ) if errp ~= nil then return -2, err2 end return -999, err2 end end print ( "trim demo" ) startsegs = structure.GetCount () print ( "before trim, segment count = " .. startsegs ) selcnt = selection.GetCount () print ( "selected segments = " .. selcnt ) local rc,trimerr = SafeTrim () if rc ~= 0 then print ( "trim error: \"" .. trimerr .. "\"" ) end trimsegs = structure.GetCount () print ( "after trim, segment count = " .. trimsegs ) if trimsegs == startsegs then print ( "nothing trimmed" ) if rc == -1 then print ( "pose was already trimmed" ) elseif rc == -2 then print ( "nothing was selected" ) elseif rc ~= 0 then print ( "unknown error" ) end end

Comments