Is there a way to retrieve the current handles of a picked object

Moderator: Kathy_9

mangurian
Posts: 122
Joined: Wed Sep 01, 2010 4:54 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Dell Inc 0K3CM7 A00
processor: Intel I7 11th Gen
ram: 64 Gig
Video Card: NVIDIA GeForce RTX 3060 [Display adapter]
Hard_Drive_Capacity: 3 T
Corel programs: VSP, PSP
Location: Connecticut USA

Is there a way to retrieve the current handles of a picked object

Post by mangurian »

I want to make scaled copies by computationally changing the handles.
LeviFiction
Advisor
Posts: 6472
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: Is there a way to retrieve the current handles of a picked object

Post by LeviFiction »

Yes and no.

You cannot grab the active handles of a pick. You can either calculate the handles from the Rect of the layer/image/selection/vector selection. Or you can grab the last used pick handles. The latter option can be forced to work by selecting a different tool programmatically, then grabbing the last used nodes from the LastUsed Parameters. But not what is active on the screen at the moment. So which method you use depends on your situation.

If the user does not have the pick tool currently active (SelectTool returns the name of the tool that was active before you called it) then you can only grab the rectangle ( (x,y), Width, Height) of the layer, selection, vector selection.

So here is how I would do it. First select a tool "Eyedropper" for example using the SelectTool option. Then check if the pick tool was previously active. If it was, get the LastUsed Parameters to figure out where the handles are. If not, then call ReturnLayerProperties or "GetSelectionRect" or "GetVectorSelectionRect". And use the returned rectangle to work out the handles. Of course if you know for a fact that the pick tool's last used handles are not what you want, just grab the rectangles and do the math.

Then you can call the Pick command the parameters for the pick tool are Handle 1 = bottom left, handle 2 = top left, handl3 = top right, handle 4 = bottom right. Or any of the other values, like scale, position, pivot point, etc.

First the commands you might think about using depending on your method. I'm including links directly to the API documentation for each.

GetRasterSelectionRect - Returns a dictionary with 2 keys 'Type' and 'Rect'. Type will be zero if there is no raster selection. Rect is a tuple in the form of ((x,y), width, height). The first item the rect tuple (index 0) is also a tuple.

ReturnLayerProperties - Returns a dictionary that contains a 'LayerRect' key in the same format as the selection rectangle above.

GetCommandInfo - If you tell it to return the LastUsed parameters they will be included in the made up of the Library key and Name Keys combined with an escaped backslash character. Like this LastUsedKey = info['Library'] + "\\" + info['Name']. info[LastUsedKey] will return a dictionary of the last used parameters. Corel's own scripts save us time with this by creating a function called to grab the Last Used Parameters for any command quickly.

Code: Select all

def getLastUsed(Environment, CommandName):
  info = App.Do(Environment, 'GetCommandInfo', {'TargetCmd': CommandName, 'LastUsed': 1})
  return info[info['Library'] + "\\" + info['Name']]
  
Pick - Command for calling the pick tool. It's technically easier to just copy the command from the history palette in PSP as you don't have to type everything out. Then just put in your parameters.

Select Tool - This lets you select another tool by name 'Pick" 'Eyedropper' etc.

And here's a rough idea of what you would do

Code: Select all

def Do(Environment):
    PrevTool = App.Do(Environment, 'SelectTool', {'Tool': 'Eyedropper'})
    if PrevTool == 'Pick':
       lastUsed = getLastUsed(Environment, 'Pick')
       Handle1 = lastUsed['Handle1']
       Handle2 = lastUsed['Handle2']
       Handle3 = lastUsed['Handle3']
       Handle4 = lastUsed['Handle4']
    else:
       # Get rectangle.  I'll ignore selection rectangles here as I don't think that's useful for what you're doing
       rect = App.Do(Environment, 'ReturnLayerProperties',{})['LayerRect']
       Handle1 = ( rect[0][0], rect[0][1] + rect[2] )
       Handle2 = ( rect[0][0], rect[0][1] )
       Handle3 = ( rect[0][0] + rect[1], rect[0][1] )
       Handle4 = ( rect[0][0] + rect[1], rect[0][1] + rect[2] )
    
    # Do your math here to figure out where you want the new handle parameters to be
    # ....
    # End your math and call Pick with the new handle locations
    App.Do(Environment, 'Pick', {'Handle1': Handle1,
                                               'Handle2': Handle2,
                                               'Handle3': Handle3,
                                               'Handle4': Handle4})
                                               
def getLastUsed(Environment, CommandName):
    # Utility function to get the lastUsed parameters from any command.
    info = App.Do(Environment, 'GetCommandInfo', {'TargetCmd': CommandName, 'LastUsed': 1})
    return info[info['Library'] + "\\" + info['Name']]
mangurian
Posts: 122
Joined: Wed Sep 01, 2010 4:54 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Dell Inc 0K3CM7 A00
processor: Intel I7 11th Gen
ram: 64 Gig
Video Card: NVIDIA GeForce RTX 3060 [Display adapter]
Hard_Drive_Capacity: 3 T
Corel programs: VSP, PSP
Location: Connecticut USA

Re: Is there a way to retrieve the current handles of a picked object

Post by mangurian »

Thanks.........I will try to work with this.
mangurian
Posts: 122
Joined: Wed Sep 01, 2010 4:54 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Dell Inc 0K3CM7 A00
processor: Intel I7 11th Gen
ram: 64 Gig
Video Card: NVIDIA GeForce RTX 3060 [Display adapter]
Hard_Drive_Capacity: 3 T
Corel programs: VSP, PSP
Location: Connecticut USA

Re: Is there a way to retrieve the current handles of a picked object

Post by mangurian »

LeviFiction - If you have time, can you run this script? I am just trying to see if I can get the Handles after the pick.
I suspect (hope) that my error is trivial.

Code: Select all

from PSPApp import *

def getLastUsed(Environment, CommandName):
    # Utility function to get the lastUsed parameters from any command.
    info = App.Do(Environment, 'GetCommandInfo', {'TargetCmd': CommandName, 'LastUsed': 1})
    return info[info['Library'] + "\\" + info['Name']]
def ScriptProperties():
    return {
        'Author': u'',
        'Copyright': u'',
        'Description': u'',
        'Host': u'P',
        'Host Version': u'24.00'
        }
    # Begin Loop ++++++++++++++++++++++++++++++++
def Do(Environment):
    numtimes = 1
    while numtimes < 5: 
        numtimes += 1
        print numtimes  
              # LayerDuplicate
        App.Do( Environment, 'LayerDuplicate', {
          'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((24,0,0),1)
                }
            })
    # Pick
        App.Do( Environment, 'Pick', {
            'Type': App.Constants.ObjectSelection.Select, 
            'X': 0, 
            'Y': 0, 
            'Width': 1, 
            'Height': 1, 
            'Group': True, 
            'Handle1': (1035,2786.4), 
            'Handle2': (1035,1881), 
            'Handle3': (2725.2,1881), 
            'Handle4': (2725.2,2786.4), 
            'Pivot': (1974,2384), 
            'EarlyX4Script': False, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Default, 
                'AutoActionMode': App.Constants.AutoActionMode.Match, 
                'Version': ((24,0,0),1)
                }
            })
            
        PrevTool = App.Do(Environment, 'SelectTool', {'Tool': 'Eyedropper'})
        lastUsed = getLastUsed(Environment, 'Pick')
        Handle1 = lastUsed['Handle1']
        Handle2 = lastUsed['Handle2']
        Handle3 = lastUsed['Handle3']
        Handle4 = lastUsed['Handle4'] 
        print Handle3                  
    # Vector Selection Update
        App.Do( Environment, 'VectorSelectionUpdate', {
            'Path': (0,-1,[],False), 
            'Type': App.Constants.ObjectSelection.Select, 
            'GeneralSettings': {
                'ExecutionMode': App.Constants.ExecutionMode.Silent, 
                'AutoActionMode': App.Constants.AutoActionMode.Default, 
                'Version': ((24,0,0),1)
                }
            })     

 


LeviFiction
Advisor
Posts: 6472
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: Is there a way to retrieve the current handles of a picked object

Post by LeviFiction »

I ran the script, it duplicated the background layer, ran pick with specific handles, then grabbed the handles, printed handle 3 and it matched the last used pick tool as executed by the script, (2725.2, 1881.0) . Then selected the background layer again, and repeated.

No errors, retrieved the handle, it seems to work just fine.
mangurian
Posts: 122
Joined: Wed Sep 01, 2010 4:54 am
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Dell Inc 0K3CM7 A00
processor: Intel I7 11th Gen
ram: 64 Gig
Video Card: NVIDIA GeForce RTX 3060 [Display adapter]
Hard_Drive_Capacity: 3 T
Corel programs: VSP, PSP
Location: Connecticut USA

help again? Re: Is there a way to retrieve the current handles of a picked object

Post by mangurian »

If you would help one more time: I am getting a different result.
The script that runs for you, errors out when I run it - as shown below.
I use: PaintShop Pro 2022 (64-bit) on a Dell 8940 PC

Executing RunScript
2
Executing LayerDuplicate
Executing Pick
Executing SelectTool
Executing GetCommandInfo
Traceback (most recent call last):
File "<string>", line 51, in Do
File "<string>", line 6, in getLastUsed
KeyError: u'ToolObject\\Pick'

Script '00000' has completed with an error.
LeviFiction
Advisor
Posts: 6472
Joined: Thu Oct 02, 2008 1:07 pm
operating_system: Windows 10
System_Drive: C
32bit or 64bit: 64 Bit
motherboard: Alienware M17xR4
processor: Intel Core i7-3630QM CPU - 2_40GH
ram: 6 GB
Video Card: NVIDIA GeForce GTX 660M
sound_card: Sound Blaster Recon3Di
Hard_Drive_Capacity: 500GB
Corel programs: PSP: 8-2023
Location: USA

Re: Is there a way to retrieve the current handles of a picked object

Post by LeviFiction »

No idea, PSP should return that key. I have been unable to get PSP to fail on this point no matter which tool I put in there. Including ones I haven't used.

Here is another version of the getLastUsed function that will print out all keys in the dictionary returned by the GetCommandInfo command. If PSP isn't returning the toolobject\\pick key it'll still fail, but it'll print out a list of dictionary keys that were returned first.

Code: Select all

def getLastUsed(Environment, CommandName):
    # Utility function to get the lastUsed parameters from any command.
    info = App.Do(Environment, 'GetCommandInfo', {'TargetCmd': CommandName, 'LastUsed': 1})
    print info.keys()
    return info[info['Library'] + "\\" + info['Name']]