Current Showreel

Showing posts with label Python Cheat Sheets. Show all posts
Showing posts with label Python Cheat Sheets. Show all posts

Sunday, 7 March 2021

Learning Houdini - Part 1

Started learning Houdini properly this weekend. Some notes below on various discoveries.

5 Hour free course on Houdini basics: 

Shout out to Denis J-F for putting me onto this one. So far so good. Well explained and has playback speed controls which is always a godsend when watching tutorials so you can fast-forward on the real basic stuff that's easy to digest and then slow down again for the bits that need a bit more concentration.

Key things to remember:

Houdini is like a UNIX style OS. 
Nodes organised like folders and are the address to themselves within the DCC
eg "/" is the root of a houdini file
"/obj/geo1" is the 'path' to the node "geo1"

Houdini Python Cheatsheet:
(Formatting generated with http://hilite.me/ using the "fruity" style)

# Houdini python reference:
# https://www.sidefx.com/docs/houdini/hom/index.html

# hou is the name of the python houdini commands. 
# It's already imported in a python shell that's loaded from Houdini

#### FILE OPERATIONS ####
# open a file
file_name = "/my/file/path.hip"
hou.hipFile.load(file_name)

# save a file
hou.hipFile.save(file_name)



#### WORKING WITH NODES ####
# GETTING/SELECTING
path_to_node = "/obj/geo1"
node = hou.node(path_to_node)
# set a node to be selected in the GUI
new_geo_node.setSelected(True)

# TYPES
# getting a node's type (this get's a NodeType class)
node_type = node.type()
node_type_name = note_type.name()
print node_type_name
>>> "geo"

# PARAMETERS
# setting parameters on a node 
#(NB: this works much the same way that Nuke does it's knobs with python)
parameter_name = "tx"
# first get the parameter
parameter = node.parm(parameter_name)
value = 2
# then set the value you want.
parameter.set(value)

# SET FLAGS ON NODES
# Some settings on nodes are known as flags (not sure what the technical difference 
# is yet between parameters and flags)
# Some notable flags display and selectable in viewport can be modified as shown below

# set the display of the geo to off
new_geo_node.setDisplayFlag(False)
# set the select-ablity of the geo to off
new_geo_node.setSelectableInViewport(False)
# set the lock state of the node
new_geo_node.setHardLocked(True)

# MULTIPLE NODES
# There's almost certainly a better way of doing this, but a basic
# way to list nodes in a scene looks like this:
# get the root
root = hou.node("/")
# get the children of that node 
# (which are the different houdini contexts (eg: obj, mat, cop, chop, etc)
contexts = root.children()
# for each context 
for cxt in contexts:
    print cxt.name()
    # get the children of that context
    nodes = cxt.children()
    if nodes:
        # for each node in that context print the name of the node
        for n in nodes:
            print n.name()


#### CREATING NEW NODES ####
# to create new nodes you've got to do so from and class instance of the context in 
# which that node can be created.
# eg: for a Geometry node you need create it from an instance of the "/obj" context
# eg: for a Shader node (principledShader) you need to create it from an instance of
# the "/mat" context

# setup the instance of the obj context
obj_context = hou.node("/obj")
new_node_type = "geo"
new_node_name = "my_geo"
# create the geometry container (essentially the transform node to use Maya terms)
new_geo_node = obj_context.createNode(new_node_type, new_node_name)
primitive_type = "box"
primitive_name = "my_geo_box"
# now create the primitive inside the geo container (ie the shape node in Maya terms)
box = new_geo_node.createNode(primitive_type, primitive_name)


#### NODE CONNECTIONS AND INPUTS ####

# setting the input of one node to the output of another

# create a new polybevel node so we've got something to connect up
polybevel = new_geo_node.createNode("polybevel")
# set the 1st input to the box created above. 
# NB: The first arg is the index of the input 'slot'
# The second arg must be a class object. A string to the path of the object is not sufficient
polybevel.setInput(0, box)

# break/disconnect inputs
# the way to do this is to just set a None input.
polybevel.setInput(0, None)

#### MISC ####
# POSITIONING NODES IN THE GRAPH
# get the current position of the node in the graph. 
# This appears as a list but is actually and instance of the hou.Vector2 class.
current_position = new_geo_node.position()
>>> [-1,1]
# set the position of a node in the graph by passing a list with the x and y position 
# you want to node to go to.
new_geo_node.setPosition([0,2])

Wednesday, 21 February 2018

List all vray nodes in maya

It's a bit dumb but I didn't know how to do this.

Turns out the best way to is use the maya.cmds.pluginInfo command with some flags. See below for example:

-------------------------------------------------------------------------------

import maya.cmds as mc
vray_node_list = mc.pluginInfo("vrayformaya", q=True, dn=True)

Thursday, 8 December 2016

Mayabatch and Python - Introduction & Part 1

Introduction 

I wasn't able to find much information about running scripts within mayabatch when I first started looking into how to do this myself. There's loads of info out there about running renders via mayabatch but not much about how to do other things with it (ie run scripts on a file rather than just rendering a file).

I hope to lay out some basic tutorials over the next few month on how to do this, as much for myself as anything else. For anyone else reading this please note the following:

This will only be relevant to Windows and possibly with specific reference to Windows 10. Sorry to Mac and Linux users out there but I've not got experience of using those systems so I have nothing to offer about how to do this on those OS.

These tutorials will also assume at least a basic understanding of Python. If you're not familiar with Python, then I would recommend checking out Jeremy Ernst's Vimeo channel, specifically his Building Maya Interfaces with Python series which starts with this video: https://vimeo.com/42848594

First I should start with how to actually run mayabatch.exe via the command line in windows, a very basic step but one that seems to be overlooked by others when teaching about this kind of thing.

Part 1 - Running mayabatch for the first time

There are two basic ways to get mayabatch up and running.
  1. Run cmd directly in the Maya bin
  2. Define where the mayabatch.exe actually is on the system before you try and run it.

Option #1 - Run cmd in the Maya bin

  • Navigate to your maya bin folder. This is the program files of your windows machine. Most likely it will look like this:
C:\Program Files\Autodesk\Maya2015\bin 
(obviously change the maya version to the one you're using eg 'Maya2016')
  • Type cmd into the address bar of the explorer once you're here:

This is where you want to type 'cmd'

  • This will open a cmd terminal looking like this:

This means that the cmd terminal will understand what and where mayabatch is. Note that this is different from running cmd from anywhere else in Windows. (ie from the start menu, the Run command, or the same process but in another folder)
  • type in mayabatch.exe and hit enter, this will open a version of maya without a UI. 

It'll take a second or two to start but and once it's loaded it it'll close itself down again given that it's got nothing to do. You'll basically see something like this:




I'm not really sure what the error about Bifrost is about, something for me to look into another time perhaps.

Option #2 - Define where mayabatch is on the system


  • Open a normal command prompt terminal. Type 'cmd' into the start menu and hit enter.

  • This will bring up a command terminal much like the other but notice that the folder path is different. It should default to the current user's path.

  • This time we need to specify where the mayabatch.exe actually is for it to run. Type in the file path for mayabatch (or copy and paste it). This will most likely be: 
C:\Program Files\Autodesk\Maya2015\bin\mayabatch.exe
(again change the version number to the version of maya you have installed)

Here's the error you get when you try to run mayabatch with a space in program files

  •  The thing is this will cause errors in the command prompt because it can't resolve the space in 'Program Files' part of the path. The way around this is to change the path we're using slightly. Changing 'Program Files' to 'Progra~1' allows command prompt to read the path properly.
C:\Progra~1\Autodesk\Maya2015\bin\mayabatch.exe

Here we see mayabatch running. Note that where you put the '~1' is important as if it's in the wrong place you'll get an error.


That's the end of this part of the tutorial, next time I'll go into more details about how to open files in maya batch followed by how to run scripts on opend files.







Wednesday, 9 September 2015

Python Cheat Sheets - String Concatenation

String Concatenation

Here's some notes I've built up on Python for my own reference.You should be able to copy and paste these into python without too much trouble.If you've found this from a far corner of the 'tinter-webs then leave a comment if any of it doesn't make sense.


### BASICS ###
# set up some variables
xNum = 0
xStr = "MyString"

# basic concatenation of two strings. use a + sign
MyNewVar = xStr + "_IsVeryCool"
print MyNewVar
# python would print "MyString_IsVeryCool"

### MAKE A NUMBER A STRING ###
# will make a number/integer (xNum) into a string. The number 6 becomes the character "6"

str(xNum)

### MAKE A STRING A NUMBER ###
# will make a string into a number (the reverse of above)
int(xStr)



### CONCATENATE MULTIPLE THINGS ###
# to concatenate a string into another string.:
#    1. use %s where you want the string variable to go
#    2. put %[your variable here] after the string
#    use %d to concatenate a number into a string
#    see the examples below

VarText = "sentence"
VarWord =  "words"

# Eg1. -  Basic
VarSentence = "My long %s needs something" %VarText
# python would print "My long sentence needs something"

# Eg.2 - Multiple. Use brackets. Strings will be added in the order you put them in the brackets
VarSentence = "My long %s needs some %s" %(VarText, VarWord)
print VarSentence
# python would print "My long sentence needs some words"