Maya Python query P4 workspaces

Hello,

I’m learning to use P4 within Maya but having problems checking out files. I can checkout files if my workspace name is manually determined. But I have 3 workspaces used by a number of Maya files. How can I query my workspaces or better query an opened Maya file’s workspace name.

Thanks

The way I used to do it (10 years ago mind).
On startup run a p4 clients query for the current user and host, sorted by access date.

For each client I got the view and run a where (or a replace on the client mapping using the client root) on each path to end up with each client, in access order and its view in depot and local path.
So;

clients = {
  "ldunham_pa_data": {
    "root": r"c:\pa",
    "view": [
      ("//depot/pa/main/data", r"c:\pa\data")
    ]
  },
  "ldunham_pa_offline": {
    "root": r"c:\pa",
    "view": [
      ("//depot/pa/main/offline", r"c:\pa\offline")
    ]
  }
}

Whenever a file or bunch of files needed a p4 op, it would test each filepath to each client’s view’s local path until it found a match. Then, I’d do p4 ops on each mapping of client to its mapped files.
This generally was meant to do as few p4 calls as possible.

This worked pretty well at the time (it might still do?), but I’m pretty sure smarter/easier solutions exist.

E: Added poor data example.

2 Likes

Worth noting for performance, I did the startup p4 calls using cmd via subprocess using -ztag -F piped into FINDSTR. That way I did minimal string parsing in python on the output.

1 Like

most p4 file ops operate within a client, either implicit or specified. This makes “which client is this file in?” queries hard.

from the command line
p4 clients -u username will list your clients
p4 -c clientName where Path/to/file.ext will tell you if and how the file is mapped in that client’s depot

There are no ‘client independant’ queries, as far as I know

p4 command results are annoyingly and inconsistently verbose, so using -ztag and piping to FINDSTR as @ldunham1 suggests is wise.

I made gui that allows users to select which perforce client they want Maya to use.
It saves to an optionVar, persisting accross Maya sessions until the user changes it.

If your pipeline is lucky/wise enough to map the various client roots to separate local directories you could just query the client root, and check the Maya file path for the root of each client.

1 Like

My company has a high workspace per user ratio so for this problem I wrote logic to compare the root of the path being operated on to the root of the workspace - if it matches it runs the given function if it doesn’t it moves on to the next workspace associated with the the user and host:

OpenPype/openpype/modules/version_control/backends/perforce/api at d58899f8ab9c573e97dbc3ef55ea67065ce05e77 · sharkmob/OpenPype · GitHubinit.py#L634

This is done with P4Python - but the same general approach can be applied to any interface with P4.
This way peeps don’t have to know what workspace a file is associated with, they don’t have to pre-configure anything it just works as if they have a single workspace.

1 Like

Hello,
Many thanks for the useful feedback everyone. I managed to finally get some time to work on this and was successful in getting it to work. That’s thanks to you folks!

I went with @Munkybutt approach to compare the root of the path to the root of the workspace.
I did try to use @Mambo4 command to (p4 -c clientName where Path/to/file.ext) but that failed

So here’s a break down of those commands:

p4_instant = P4()
p4_instant.connect()
user_name = p4_instant.user

# This got my clients from the below command which created a dic to which I used ["clients"] key to access the client names. Below shows this 

client_list = p4_instant.run("clients", "-u", user_name):

# Tried to check my maya scene path with my clients but it failed. I must be using the run command with the wrong console command layout. Below shows my failed attempt
p4_instant.run( "-c", f"{clientName} where",  maya_scene_filepath):  
# Note: clientName is a arg taken from the dic key["client"]

As I mentioned, I eventually went with querying the client root path to the maya filepath! Wrapped it all up in class methods! Job done.

Thanks once again to you all!

Note: To whoever is planning to use P4python in maya. A great help is to get a list of all the p4 console commands by using “p4 help commands” in the command prompt. You can then use these commands in Maya with p4 .run() command.

Also, Jeremy Ernst show’s a good tutorial on the subject.