top of page
  • Writer's pictureStephen Andri

Model Name Script (python)

Updated: Nov 14, 2023

The goal of the script was to build an app that could take in a list of item types and translate them into the final mesh name using an external JSON reference Dict while also improving the usability and ease of the initial script received. As well as using it as a platform to learn and try exercising my learnings.


The initial script I had to build upon was very manual, there was an internal dictionary (naming_dict) that was a broken down JSON from another path that was the source of truth and used a replace method on "update" before copying the terminal output list back into Excel. The problem is that "from_excel" and "update" needed to be updated in the file every run, and there was a dependency on "naming_dict" to update it from the external JSON file occasionally as it was a live file.


Early on I knew I wanted to change some aspects of the script:

  • no longer change the script internally with hard-coded values for the use case

  • abstract the "naming_dict" to reference in the source JSON

  • create a smarter function that doesn't require pre-writing the naming schema

  • a smarter way to execute the "from_excel" list

  • refactor the script to 'run' like a program/app

  • return the end list to the clipboard removing steps from the user

All of these points were a new challenge to problem solve as this would be the most holistic program I've built. I tackled each point when it became more relevant there were plenty of test py files to experiment with each aspect that I needed to figure out.


Main Program Loop

As it is currently only working in the terminal I've decided to a while loop dependency based on 'state' to allow the terminal to persist and run through some logic steps;

  • Start script.

  • Get input from the user to start.

  • Get "update" name.

  • Run through the automation loop (the 'try' on line 58 contains the data logic aspect of the project that will be explained later on).

  • Request to continue with a new list from the user and continue.

  • Start again with a new "update" name.

  • Otherwise, close the script.

Some things in the future I want to correct/clean up, are the user input condition loops, I don't think it's very glamorous but it works.


For some context, the "updateSuffix" gets used in conjunction with some functions for the schema, and there are cases where in one session there are multiple suffix changes hence why I decided to add the loop structure to allow that without reloading the program.


I did not do a full deep dive into the best practices on how to do program loops in python as the current method serves its purpose for now as I planned to eventually build a GUI much later down the track instead


Referencing External JSON data

This was a new challenge for me as I have worked with JSON and XML files in work before but never tried to read and use the data within a script, so I needed to do some research and experimentation to use the data how I was planning to use it.

import json

As I'm reading in JSON I Respectively use the json package and used the documentation to use it accordingly to get the data into a internal dict in python

with open(desiredPath) as file:
    data= json.load(file)

Using with keeps the data in memory while it loads the Var data with the contents of the JSON file and closes it after it is done. This is just a shortcut so I can compress it to a few lines rather than having to file.close() the resource.


Core Logic Loop

The JSON data contains Key-Value pairs that are a little contrived as it is part of a larger existing tools system.

"ContentStringSearch": "FunctionalityInGame",

The Key is the expected partial name of the asset and the Value is what the game aligns the functionality with.

So the use of the data is a little backward as we use the value to find the Key data that gets used at the name, this stumped me for a little to figure out how to do it in that way

This is the method I found that gave me the data I needed from the incoming list


"*_reception_desk_*": "desk_reception", "indprop_desk_*": "desk", "*_fan_ceiling_*": "fan_ceiling",


After getting the Key value which contained the way the asset should be named from the JSON.

The formatting and data were premade in the past and cannot be changed, so interestingly the way it works as the value is the desired functions-like and the key is the Model name that will be applied to the string, and the input data that gets put into the program is the desired functionsLikeName. Also note there are discrepancies between the lines some start with indprop and others surrounded by _* and *_.

These minor differences needed to be removed so that the schema was applied to a cleaned-up naming standard, so it strips away the * _ as well as the string indprop as it leaves the rest of the string in the right format for the model name


Dynamic Schema application

There's an existing schema for the names which needs all assets to be named and I'm using some simple string concatenation to achieve it ( there are edge-cases that are currently caught in the "exception_list" dict for now Future goal is to find a solution)

"indprop_" + assetBaseName + "_" + update

The function loops over the build list of assets and cross-checks the contents filtering the action required on the incoming list. Firstly new items come in as **** for the user to manually check later, then it checks the exception list first as a priority catch, followed by applying the above schema which catches all instances at the moment.


Clipboard functionality

I liked the idea of removing steps from the user and one of the solutions I liked the idea of was to remove the copy and pasting the user did throughout the process, and decided to automate it. I found the pyperclip library that allowed me access to the windows clipboard to be able to read and write to the clipboard with some modifications to work as expected

import pyperclip

There was probably some os library to do a similar thing but I found it outside my scope of effort.


The function above breaks down the string from the clipboard as it will always be copied from an Excel sheet and needed to be split, I believe I could condense it down to a single-line argument, but haven't explored it just as yet

pyperclip.copy("\n".join(rename))

Later on I need to be able to put the result back on the clipboard, this was fairly simple and happened to also work pasting back into Excel as it respects the new line cells pasting in the correct position


8 views0 comments

Recent Posts

See All

Comments


bottom of page