0

I am trying to create a geoprocessing tool using ModelBuilder that will create 3 folders with geodatabases inside for the current ArcGIS Pro project I am in.

I saw you can use inline variable substitution to call the scratch geodatabase and the scratch folder, but can you write to your current project folder?

I plan to assign this tool to a custom ribbon so I can use it in new projects.

If anything, I can create a variable called "Workspace" and use it as a model parameter every time I run the tool. I would then need to navigate to the current project's project folder to run the rest of the tool. I was just wondering if I could do that automatically without Python.

Edit to add draft of model: enter image description here

6
  • 1
    What does your test model look like so far? I think you should include a picture of it.
    – PolyGeo
    Sep 23 '21 at 19:09
  • Since you're aware of %scratchFolder%,, your only real solution is the one you identified (creating a variable), or the idea from @Son of a beach, using a Calculate Value tool. However, I'd suggest using arcpy.env.workspace (not a scratch), as this will give you the GDB of your project, not project/scratch. Basically one level up from scratch.
    – KHibma
    Sep 24 '21 at 13:26
  • @KHibma - in ArcGIS Pro, the scratch workspace and the current workspace are both the project default GDB (unless they have been explicitly changed to something else). At least that's my understanding of the ESRI doco at: pro.arcgis.com/en/pro-app/latest/tool-reference/… Sep 27 '21 at 0:51
  • @SonofaBeach You appear to be right. That's not how I remember scratchWorkspace working... but I've been out of the GP team for a few years now; my knowledge must be getting stale.
    – KHibma
    Sep 27 '21 at 11:47
  • It was different with ArcMap, I think. Sep 27 '21 at 11:48
1

Note that this solution does work, but it does not use variable substitution and it does use two lines of Python (but all within a model-builder model). It's quite simple...

Suggested Solution

Add a 'Calculate Value' utility tool to the model with the following parameters:

  • Expression: os.path.dirname(arcpy.env.scratchWorkspace)

  • Code Block: import os

  • Data Type: Folder

(For readability, rename the output item to Project Folder or similar).

You can then use this (eg, 'Project Folder') output item of the 'Calculate Value) as the input of other model tools where you need the project folder as input (eg, as the 'Output Location' of another tool). I tested it with the 'Create Folder' tool and it worked to create a new folder within the project folder.

Explanation

In ArcGIS Pro, the scratch and current workspaces default to the default project database. The directory of the default project database is the project directory. So this uses Python to get the directory in which the scratch database resides, ie, the project directory.

Caveat

This will fail if you have your scratchWorkspace set to anything other than the default. If this is the case, use workspace instead - unless that is also set to something other than the default, in which case, this solution won't work at all.

4
  • I'd suggest using either scratchGDB or scratchFolder variables. These variables will ONLY be a GDB or a Folder. You, nor the code needs to assume what scratchWorkspace is pointing at.
    – KHibma
    Sep 24 '21 at 13:28
  • I tried both currentWorkspace and scratchWorkspace. currentWorkspace returned the error: ERROR 000539: Traceback (most recent call last): File "<expression>", line 1, in <module> AttributeError: 'GPEnvironment' object has no attribute 'currentWorkspace' Failed to execute (Calculate Value). I only changed the word "current" to "scratch" and it worked like a charm. It put all 3 folders within my current project by using it as the folder location for the Create Folder tools. I am new to using Calculate Value in Modelbuilder. I will have to read up on it.
    – Smithw1
    Sep 28 '21 at 13:39
  • 1
    @Smithw1 - Sorry, my mistake. It should have been just “workspace”, not “currentWorkspace” to get the current workspace. I have now fixed the post. Glad it worked for you, anyhow. Sep 28 '21 at 20:11
  • @SonofaBeach Thanks for the help! Much appreciated.
    – Smithw1
    Nov 30 '21 at 19:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.