temp(houdini)
<?xml version="1.0" encoding="UTF-8" ?>
<menuDocument>
<menu>
<separatorItem />
<scriptItem id="bake_key_id">
<label>Bake Key</label>
<scriptCode>
<![CDATA[
parm = kwargs["parms"][0]
framerange = range(int(hou.expandString('$RFSTART')), int(hou.expandString('$RFEND')) + 1 )
keyVals = {}
for kf in framerange:
keyVals[kf] = parm.evalAtFrame(kf)
parm.deleteAllKeyframes()
for kf in framerange:
setKey = hou.Keyframe()
setKey.setFrame(kf)
setKey.setValue( keyVals[kf] )
parm.setKeyframe(setKey)
]]>
</scriptCode>
</scriptItem>
</menu>
</menuDocument>
import sys
import os
import cStringIO
import tempfile
import traceback
def getComponentStart(node):
return None
def getSpecializedProperties(node):
return ()
def getValueInitializations(node):
return ()
def getNeedsBraces(node):
return False
def setUpCallbacksForNode(node, dialog):
pass
##############################################################################
def viewSource(node):
import hdefereval
source = node.node("common").hdaModule().buildUISource(
node, add_window=False)
# Houdini doesn't like it when button callbacks block waiting for popup
# windows, so defer displaying the message dialog.
hdefereval.executeDeferred(lambda: hou.ui.displayMessage(
"UI File Source Code:", details=source, details_expanded=True))
def showWindow(node):
try:
is_window = node.type().name() == "ui_window"
source = node.node("common").hdaModule().buildUISource(
node, add_window=not is_window)
ui_file_path = os.path.join(tempfile.gettempdir(), "ui_file.ui")
ui_file = open(ui_file_path, "wb")
ui_file.write(source + "\n")
ui_file.close()
dialog = hou.ui.createDialog(ui_file_path)
_setUpCallbacks(node, dialog)
if is_window:
value_name = node.evalParm("value_name")
else:
value_name = "show_test_window"
dialog.setValue(value_name, 1)
except:
hou.ui.displayMessage(traceback.format_exc())
def buildUISource(node, add_window):
output_file = cStringIO.StringIO()
output = _UIFileOutput(output_file)
outputValueInitializations(output, node)
if add_window:
_outputTestWindowStart(output)
outputComponent(output, node)
if add_window:
_outputTestWindowEnd(output)
output_file.reset()
return output_file.read()
def _outputTestWindowStart(output):
output.startLine('test_window = DIALOG "Preview Components"')
output.startLine("{")
output.indent_level += 1
output.startLine("VALUE(show_test_window) LOOK(plain) STRETCH")
def _outputTestWindowEnd(output):
output.indent_level -= 1
output.startLine("}")
def outputValueInitializations(output, node):
for line in nodeModule(node).getValueInitializations(node):
output.startLine(line + "\n")
for output_node in _outputsWithoutSubnets(node):
outputValueInitializations(output, output_node)
def outputComponent(output, node):
outputComponentStart(output, node)
actual_outputs = _outputsWithoutSubnets(node)
needs_braces = (len(actual_outputs) != 0 or
nodeModule(node).getNeedsBraces(node))
if needs_braces:
output.startLine("{\n")
output.indent_level += 1
outputProperties(output, node)
if needs_braces:
output.startLine("\n")
for output_node in actual_outputs:
outputComponent(output, output_node)
output.indent_level -= 1
if needs_braces:
output.startLine("}\n")
else:
output.appendToLine(";\n")
def outputComponentStart(output, node):
# Each type of component must implement outputComponentStart.
output.startLine(nodeModule(node).getComponentStart(node) + " ")
def outputProperties(output, node):
properties = list(getCommonProperties(node))
properties.extend(nodeModule(node).getSpecializedProperties(node))
output.appendToLine(" ".join(properties))
def getCommonProperties(node):
properties = []
if node.evalParm("use_width"):
if node.evalParm("hstretch"):
properties.append("MIN_WIDTH(%g)" % node.evalParm("width"))
else:
properties.append("WIDTH(%g)" % node.evalParm("width"))
if node.evalParm("use_height"):
if node.evalParm("vstretch"):
properties.append("MIN_HEIGHT(%g)" % node.evalParm("height"))
else:
properties.append("HEIGHT(%g)" % node.evalParm("height"))
if node.evalParm("hstretch") and node.evalParm("vstretch"):
properties.append("STRETCH")
else:
if node.evalParm("hstretch"):
properties.append("HSTRETCH")
if node.evalParm("vstretch"):
properties.append("VSTRETCH")
if node.evalParm("margin") != 0.0:
properties.append("MARGIN(%g)" % node.evalParm("margin"))
if node.evalParm("spacing") != 0.0:
properties.append("SPACING(%g)" % node.evalParm("spacing"))
return properties
def _setUpCallbacks(node, dialog):
nodeModule(node).setUpCallbacksForNode(node, dialog)
for output_node in _outputsWithoutSubnets(node):
_setUpCallbacks(output_node, dialog)
def _outputsWithoutSubnets(node):
result = []
for output_node in _sortedNodes(node.outputs()):
# Don't dive into components, since they're subnets with the common
# node inside.
if _isLowLevelComponent(output_node):
result.append(output_node)
else:
result.extend(_followIntoSubnet(output_node))
return result
def _isLowLevelComponent(node):
return (len(node.children()) == 1 and
node.children()[0].type().name() == "ui_common")
def _followIntoSubnet(node):
"""If a node is a subnet, return a sequence containing just the node.
Otherwise, return all the nodes connected to the first indirect input
in the subnet.
"""
try:
indirect_input = node.indirectInputs()[0]
except hou.InvalidNodeType:
return (node,)
# Return the nodes inside the subnet that are outputs of indirect_input.
if hasattr(indirect_input, "outputs"):
return _sortedNodes(indirect_input.outputs())
# Find the node inside the subnet that's connected to this indirect
# input, if any.
subnet = indirect_input.parent()
subnet_first_input = subnet.inputs()[0]
return list(_sortedNodes(child_node for child_node in subnet.children()
if subnet_first_input in child_node.inputs()))
def _sortedNodes(nodes):
# Sort outputs by their x position in the network, since the
# order is otherwise random.
return sorted(nodes,
lambda n0, n1: cmp(n0.position()[0], n1.position()[0]))
def doubleQuotedString(s):
return '"' + "".join(_escape_dict.get(char, char) for char in s) + '"'
_escape_dict = {
"\\": "\\\\",
"\n": "\\n",
"\t": "\\t",
'"': '\\"'
}
class FallbackHDAModule(object):
def __init__(self, overriding_hda_module, base_hda_module):
self.overriding_hda_module = overriding_hda_module
self.base_hda_module = base_hda_module
def __getattr__(self, attrib_name):
try:
return getattr(self.overriding_hda_module, attrib_name)
except:
return getattr(self.base_hda_module, attrib_name)
def nodeModule(node):
return FallbackHDAModule(
node.hdaModule(), node.node("common").hdaModule())
class _UIFileOutput(object):
def __init__(self, output_file):
self.output_file = output_file
self.indent_level = 0
self.ended_line = True
def startLine(self, text):
if not self.ended_line:
self.output_file.write("\n")
self.output_file.write(" " * self.indent_level + text)
self.ended_line = text.endswith("\n")
def appendToLine(self, text):
if self.ended_line:
self.output_file.write(" " * self.indent_level)
self.output_file.write(text)
self.ended_line = text.endswith("\n")
import sys
import os
import cStringIO
import tempfile
import traceback
def getComponentStart(node):
return None
def getSpecializedProperties(node):
return ()
def getValueInitializations(node):
return ()
def getNeedsBraces(node):
return False
def setUpCallbacksForNode(node, dialog):
pass
##############################################################################
def viewSource(node):
import hdefereval
source = node.node("common").hdaModule().buildUISource(
node, add_window=False)
# Houdini doesn't like it when button callbacks block waiting for popup
# windows, so defer displaying the message dialog.
hdefereval.executeDeferred(lambda: hou.ui.displayMessage(
"UI File Source Code:", details=source, details_expanded=True))
def showWindow(node):
try:
is_window = node.type().name() == "ui_window"
source = node.node("common").hdaModule().buildUISource(
node, add_window=not is_window)
ui_file_path = os.path.join(tempfile.gettempdir(), "ui_file.ui")
ui_file = open(ui_file_path, "wb")
ui_file.write(source + "\n")
ui_file.close()
dialog = hou.ui.createDialog(ui_file_path)
_setUpCallbacks(node, dialog)
if is_window:
value_name = node.evalParm("value_name")
else:
value_name = "show_test_window"
dialog.setValue(value_name, 1)
except:
hou.ui.displayMessage(traceback.format_exc())
def buildUISource(node, add_window):
output_file = cStringIO.StringIO()
output = _UIFileOutput(output_file)
outputValueInitializations(output, node)
if add_window:
_outputTestWindowStart(output)
outputComponent(output, node)
if add_window:
_outputTestWindowEnd(output)
output_file.reset()
return output_file.read()
def _outputTestWindowStart(output):
output.startLine('test_window = DIALOG "Preview Components"')
output.startLine("{")
output.indent_level += 1
output.startLine("VALUE(show_test_window) LOOK(plain) STRETCH")
def _outputTestWindowEnd(output):
output.indent_level -= 1
output.startLine("}")
def outputValueInitializations(output, node):
for line in nodeModule(node).getValueInitializations(node):
output.startLine(line + "\n")
for output_node in _outputsWithoutSubnets(node):
outputValueInitializations(output, output_node)
def outputComponent(output, node):
outputComponentStart(output, node)
actual_outputs = _outputsWithoutSubnets(node)
needs_braces = (len(actual_outputs) != 0 or
nodeModule(node).getNeedsBraces(node))
if needs_braces:
output.startLine("{\n")
output.indent_level += 1
outputProperties(output, node)
if needs_braces:
output.startLine("\n")
for output_node in actual_outputs:
outputComponent(output, output_node)
output.indent_level -= 1
if needs_braces:
output.startLine("}\n")
else:
output.appendToLine(";\n")
def outputComponentStart(output, node):
# Each type of component must implement outputComponentStart.
output.startLine(nodeModule(node).getComponentStart(node) + " ")
def outputProperties(output, node):
properties = list(getCommonProperties(node))
properties.extend(nodeModule(node).getSpecializedProperties(node))
output.appendToLine(" ".join(properties))
def getCommonProperties(node):
properties = []
if node.evalParm("use_width"):
if node.evalParm("hstretch"):
properties.append("MIN_WIDTH(%g)" % node.evalParm("width"))
else:
properties.append("WIDTH(%g)" % node.evalParm("width"))
if node.evalParm("use_height"):
if node.evalParm("vstretch"):
properties.append("MIN_HEIGHT(%g)" % node.evalParm("height"))
else:
properties.append("HEIGHT(%g)" % node.evalParm("height"))
if node.evalParm("hstretch") and node.evalParm("vstretch"):
properties.append("STRETCH")
else:
if node.evalParm("hstretch"):
properties.append("HSTRETCH")
if node.evalParm("vstretch"):
properties.append("VSTRETCH")
if node.evalParm("margin") != 0.0:
properties.append("MARGIN(%g)" % node.evalParm("margin"))
if node.evalParm("spacing") != 0.0:
properties.append("SPACING(%g)" % node.evalParm("spacing"))
return properties
def _setUpCallbacks(node, dialog):
nodeModule(node).setUpCallbacksForNode(node, dialog)
for output_node in _outputsWithoutSubnets(node):
_setUpCallbacks(output_node, dialog)
def _outputsWithoutSubnets(node):
result = []
for output_node in _sortedNodes(node.outputs()):
# Don't dive into components, since they're subnets with the common
# node inside.
if _isLowLevelComponent(output_node):
result.append(output_node)
else:
result.extend(_followIntoSubnet(output_node))
return result
def _isLowLevelComponent(node):
return (len(node.children()) == 1 and
node.children()[0].type().name() == "ui_common")
def _followIntoSubnet(node):
"""If a node is a subnet, return a sequence containing just the node.
Otherwise, return all the nodes connected to the first indirect input
in the subnet.
"""
try:
indirect_input = node.indirectInputs()[0]
except hou.InvalidNodeType:
return (node,)
# Return the nodes inside the subnet that are outputs of indirect_input.
if hasattr(indirect_input, "outputs"):
return _sortedNodes(indirect_input.outputs())
# Find the node inside the subnet that's connected to this indirect
# input, if any.
subnet = indirect_input.parent()
subnet_first_input = subnet.inputs()[0]
return list(_sortedNodes(child_node for child_node in subnet.children()
if subnet_first_input in child_node.inputs()))
def _sortedNodes(nodes):
# Sort outputs by their x position in the network, since the
# order is otherwise random.
return sorted(nodes,
lambda n0, n1: cmp(n0.position()[0], n1.position()[0]))
def doubleQuotedString(s):
return '"' + "".join(_escape_dict.get(char, char) for char in s) + '"'
_escape_dict = {
"\\": "\\\\",
"\n": "\\n",
"\t": "\\t",
'"': '\\"'
}
class FallbackHDAModule(object):
def __init__(self, overriding_hda_module, base_hda_module):
self.overriding_hda_module = overriding_hda_module
self.base_hda_module = base_hda_module
def __getattr__(self, attrib_name):
try:
return getattr(self.overriding_hda_module, attrib_name)
except:
return getattr(self.base_hda_module, attrib_name)
def nodeModule(node):
return FallbackHDAModule(
node.hdaModule(), node.node("common").hdaModule())
class _UIFileOutput(object):
def __init__(self, output_file):
self.output_file = output_file
self.indent_level = 0
self.ended_line = True
def startLine(self, text):
if not self.ended_line:
self.output_file.write("\n")
self.output_file.write(" " * self.indent_level + text)
self.ended_line = text.endswith("\n")
def appendToLine(self, text):
if self.ended_line:
self.output_file.write(" " * self.indent_level)
self.output_file.write(text)
self.ended_line = text.endswith("\n")