Jump to content

accessing hda "extra files" using python.


TomRaynor

Recommended Posts

Hi everyone,

I have an hda which has a python module inside it that needs to access a '.json' (jscript) file.

Basically there is a line of code in my python module that says something along the lines of:

BLAH = 'C:\Users\Me\Desktop\blah.json'

This string variable then gets passed to a function that does other shit...

As is, this works fine, and executes correctly, however I want to have the '.json' file embedded in the extra files section of my otl instead of being loose on my desktop.

My question is can anyone help me with the pythonic syntax to access the path to the file within the extra files section of the otl?

I currently can't get it to work pick up the file.

I have tried things like:

BLAH = hou.readFile('opdef:./?client_secrets.json')

and

BLAH = hou.hscript('opdef:./?client_secrets.json')

Either I have got these wrong syntactically or they don't work as I have expected.

Any help would be much appreciated!

Cheers

-T

Link to comment
Share on other sites

I'd access the it using the hou.HDADefintion's sections() method.

definition = asset.type().definition()
secrets = definition.sections()['client_secrets.json'].contents()

The hou.readFile method does work, however it doesn't understand the relative '.' part for the opdef: call. You need to do the full operator name:

hou.readFile("opdef:/Sop/something?client_secrets.json")
hou.readFile("opdef:/{0}?client_secrets.json".format(asset.type().nameWithCategory()))

Link to comment
Share on other sites

Hey Graham,

Thanks for the reply, that is really great stuff. However I believe that the python method that I am using requires the path to the '.json' file rather than the actual contents of it to work...

I believe this is why it worked when I used the explicit path:

BLAH = 'C:\Users\Me\Desktop\blah.json'

Is it possible to get some sort of path to the extra file within the otl so that I can feed it into this method?

Thanks again!

Link to comment
Share on other sites

Whilst you ponder on that one, I have a couple of another questions I was meaning to ask...

It seems you can only have one "pythonModule" in the scripts section of your otl. Is this true or can we access and run multiple python modules from this location?

If I have some extra python files in either the scripts section of my otl or the extra files section of my otl, is it possible for me to import those modules into the "pythonModule" within my scripts directory so that I can have access to the code contained within them?

Cheers!

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...