misterbk
05-08-2010, 09:20 PM
Hello,
I am writing a node using the Maya Python API. I have a question whether it's "OK" to do something when writing a Python Scripted Plugin for Maya. I already know it works, but want to know if it is valid or stable.
What I want to do is hold a large amount of data, arranged into a special structure that I have written, within the instance of the Python class and not within the node attributes and not accessed using Maya datablocks.
The data I want to store is loaded from a file when the filename is typed into a string attribute in the node. My node does special processing on this data, and sets its output plugs based on that processing and the current time in Maya. I specifically do not want to or need to store any of the internal node data to disk when the Maya scene gets saved.
The reason I need to store it in a structure in RAM is the calculation is fairly heavy and computes a whole range of data for the current time all at once. It's expected that after one value is requested then many many other requests will be made, and if I store the results in RAM this becomes a lookup instead of a recalculation.
Reason I'm wondering is this method may conflict with David Gould's often-repeated phrase "Maya owns all of the data, you own none of it".
So given that info is it considered "OK" to do this:
## The usual imports and definitions
## i.e. Node ID etc
## Scroll down and find stuff in Yellow, that is what I'm wondering about
## Define my own custom class that Maya will not know about
class MyProcessingNode:
def __init__(self,theFileName):
myFileName = theFileName
# Reads file and creates basic structure
def getResult(self, time, index):
# Calculates result at time, caches the calculation in the structure
# Returns a result from calculating a function at Time and Index.
class bkSomethingNode(OpenMayaMPx.MPxNode):
# Input Plugs
something = OpenMaya.MObject()
theFileName = OpenMaya.MObject()
# Output Plugs
someOutput = OpenMaya.MObject()
def __init__(self):
OpenMayaMPx.MPxNode.__init__(self)
def compute(self, plug, DataBlock)
if plug == bkSomethingNode.theFileName or plug.parent() == bkSomethingNode.theFileName:
fileString = #code to get filename as a string from Maya
# THIS RIGHT HERE. IS THIS OK?
self.internalData = myProcessingNode(fileString)
# Since there is significant calculation if the file changes, I will not be defining
# any output plug dependencies on the FileName plug. Instead I will check
# to make sure the file has actually changed and manually set plugs as dirty.
if plug == bkSomethingNode.someOutput or plug.parent() == bkSomethingNode.someOutput:
theTime = #get current time from Maya
theIndex = #get from Maya the index at which the calculation should be requested
# Can I reliably retrieve data from a class instance variable like this in a Maya plugin?
# Or does Maya do something funny and ALL data the node needs must be in Data Blocks?
result = self.internalData.getResult(theTime, theIndex)
# set the someOutput plug based on result.
## etc. etc. required plug-in functions etc.
I am writing a node using the Maya Python API. I have a question whether it's "OK" to do something when writing a Python Scripted Plugin for Maya. I already know it works, but want to know if it is valid or stable.
What I want to do is hold a large amount of data, arranged into a special structure that I have written, within the instance of the Python class and not within the node attributes and not accessed using Maya datablocks.
The data I want to store is loaded from a file when the filename is typed into a string attribute in the node. My node does special processing on this data, and sets its output plugs based on that processing and the current time in Maya. I specifically do not want to or need to store any of the internal node data to disk when the Maya scene gets saved.
The reason I need to store it in a structure in RAM is the calculation is fairly heavy and computes a whole range of data for the current time all at once. It's expected that after one value is requested then many many other requests will be made, and if I store the results in RAM this becomes a lookup instead of a recalculation.
Reason I'm wondering is this method may conflict with David Gould's often-repeated phrase "Maya owns all of the data, you own none of it".
So given that info is it considered "OK" to do this:
## The usual imports and definitions
## i.e. Node ID etc
## Scroll down and find stuff in Yellow, that is what I'm wondering about
## Define my own custom class that Maya will not know about
class MyProcessingNode:
def __init__(self,theFileName):
myFileName = theFileName
# Reads file and creates basic structure
def getResult(self, time, index):
# Calculates result at time, caches the calculation in the structure
# Returns a result from calculating a function at Time and Index.
class bkSomethingNode(OpenMayaMPx.MPxNode):
# Input Plugs
something = OpenMaya.MObject()
theFileName = OpenMaya.MObject()
# Output Plugs
someOutput = OpenMaya.MObject()
def __init__(self):
OpenMayaMPx.MPxNode.__init__(self)
def compute(self, plug, DataBlock)
if plug == bkSomethingNode.theFileName or plug.parent() == bkSomethingNode.theFileName:
fileString = #code to get filename as a string from Maya
# THIS RIGHT HERE. IS THIS OK?
self.internalData = myProcessingNode(fileString)
# Since there is significant calculation if the file changes, I will not be defining
# any output plug dependencies on the FileName plug. Instead I will check
# to make sure the file has actually changed and manually set plugs as dirty.
if plug == bkSomethingNode.someOutput or plug.parent() == bkSomethingNode.someOutput:
theTime = #get current time from Maya
theIndex = #get from Maya the index at which the calculation should be requested
# Can I reliably retrieve data from a class instance variable like this in a Maya plugin?
# Or does Maya do something funny and ALL data the node needs must be in Data Blocks?
result = self.internalData.getResult(theTime, theIndex)
# set the someOutput plug based on result.
## etc. etc. required plug-in functions etc.
