maya api : while importing prompts error

218 views
Skip to first unread message

blossom ghuntla

unread,
Nov 19, 2019, 5:06:58 AM11/19/19
to Python Programming for Autodesk Maya
Hello All,

I autodidact basic python, basic maya.cmds, basic qt just for the sake of learning to create a deformer and i am not doing good at all



# defTwist.py

import sys
import maya.OpenMayaMPx as OpenMayaMPx
import maya.OpenMaya as OpenMaya
import maya.cmds as cmds
import math

# Plug-in information:
kPluginNodeName = "twistDeformer"     # The name of the node.
kPluginNodeId = OpenMaya.MTypeId( 0xBEEF8 ) # A unique ID associated to this node type.

# Some global variables were moved from MPxDeformerNode to MPxGeometryFilter.
# Set som constants to the proper C++ cvars based on the API version
kApiVersion = cmds.about(apiVersion=True)
if kApiVersion < 201600:
kInput = OpenMayaMPx.cvar.MPxDeformerNode_input
kInputGeom = OpenMayaMPx.cvar.MPxDeformerNode_inputGeom
kOutputGeom = OpenMayaMPx.cvar.MPxDeformerNode_outputGeom
kEnvelope = OpenMayaMPx.cvar.MPxDeformerNode_envelope
else:
kInput = OpenMayaMPx.cvar.MPxGeometryFilter_input
kInputGeom = OpenMayaMPx.cvar.MPxGeometryFilter_inputGeom
kOutputGeom = OpenMayaMPx.cvar.MPxGeometryFilter_outputGeom
kEnvelope = OpenMayaMPx.cvar.MPxGeometryFilter_envelope



class MyDeformerNode(OpenMayaMPx.MPxDeformerNode):
    
# Static variable(s) which will later be replaced by the node's attribute(s).
twistYAttr = OpenMaya.MObject()

def __init__(self):
''' Constructor. '''
# (!) Make sure you call the base class's constructor.
OpenMayaMPx.MPxDeformerNode.__init__(self)

def deform(self, pDataBlock, pGeometryIterator, pLocalToWorldMatrix, pGeometryIndex):
''' Deform each vertex using the geometry iterator. '''

# The envelope determines the overall weight of the deformer on the mesh.
# The envelope is obtained via the OpenMayaMPx.cvar.MPxDeformerNode_envelope (pre Maya 2016) or
# OpenMayaMPx.cvar.MPxGeometryFilter_envelope (Maya 2016) variable.
# This variable and others like it are generated by SWIG to expose variables or constants declared in C++ header files. 
envelopeAttribute = kEnvelope
envelopeValue = pDataBlock.inputValue( envelopeAttribute ).asFloat()

# Get the value of the mesh twist node attribute.
twistYAttribute = pDataBlock.inputValue( MyDeformerNode.twistYAttr )
twistYAttributeType = meshInflationHandle.asDouble()

# Get the input mesh from the datablock using our getDeformerInputGeometry() helper function.     
inputGeometryObject = self.getDeformerInputGeometry(pDataBlock, pGeometryIndex)

# Obtain the list of normals for each vertex in the mesh.
normals = OpenMaya.MFloatVectorArray()
meshFn = OpenMaya.MFnMesh( inputGeometryObject )
meshFn.getVertexNormals( True, normals, OpenMaya.MSpace.kObject )


def getDeformerInputGeometry(self, pDataBlock, pGeometryIndex):
'''
Obtain a reference to the input mesh. This mesh will be used to compute our bounding box, and we will also require its normals.

We use MDataBlock.outputArrayValue() to avoid having to recompute the mesh and propagate this recomputation throughout the 
Dependency Graph.

OpenMayaMPx.cvar.MPxDeformerNode_input and OpenMayaMPx.cvar.MPxDeformerNode_inputGeom (for pre Maya 2016) and 
OpenMayaMPx.cvar.MPxGeometryFilter_input and OpenMayaMPx.cvar.MPxGeometryFilter_inputGeom (Maya 2016) are SWIG-generated 
variables which respectively contain references to the deformer's 'input' attribute and 'inputGeom' attribute.   
'''
inputAttribute = OpenMayaMPx.cvar.MPxGeometryFilter_input
inputGeometryAttribute = OpenMayaMPx.cvar.MPxGeometryFilter_inputGeom

inputHandle = pDataBlock.outputArrayValue( inputAttribute )
inputHandle.jumpToElement( pGeometryIndex )
inputGeometryObject = inputHandle.outputValue().child( inputGeometryAttribute ).asMesh()

return inputGeometryObject


##########################################################
# Plug-in initialization.
##########################################################
def nodeCreator():
''' Creates an instance of our node class and delivers it to Maya as a pointer. '''
return OpenMayaMPx.asMPxPtr( MyDeformerNode() )

def nodeInitializer():
''' Defines the input and output attributes as static variables in our plug-in class. '''
# The following MFnNumericAttribute function set will allow us to create our attributes.
numericAttributeFn = OpenMaya.MFnNumericAttribute()

#==================================
# INPUT NODE ATTRIBUTE(S)
#==================================

# Define a mesh inflation attribute, responsible for actually moving the vertices in the direction of their normals.
MyDeformerNode.meshInflationAttribute = numericAttributeFn.create( 'ytwist', 'yt', OpenMaya.MFnNumericData.kDouble, 10.0 )

numericAttributeFn.setDefault(0.0)
numericAttributeFn.setKeyable(True)
MyDeformerNode.addAttribute( MyDeformerNode.twistYAttribute )

''' The input geometry node attribute is already declared in OpenMayaMPx.cvar.MPxGeometryFilter_inputGeom '''

#==================================
# OUTPUT NODE ATTRIBUTE(S)
#==================================

''' The output geometry node attribute is already declared in OpenMayaMPx.cvar.MPxGeometryFilter_outputGeom '''

#==================================
# NODE ATTRIBUTE DEPENDENCIES
#==================================
# If any of the inputs change, the output mesh will be recomputed.

print dir(OpenMayaMPx.cvar)

MyDeformerNode.attributeAffects( MyDeformerNode.twistYAttr, kOutputGeom )


    
def initializePlugin( mobject ):
''' Initialize the plug-in '''
mplugin = OpenMayaMPx.MFnPlugin( mobject )
try:
    mplugin.registerNode( kPluginNodeName, kPluginNodeId, nodeCreator,
                          nodeInitializer, OpenMayaMPx.MPxNode.kDeformerNode )
except:
    sys.stderr.write( 'Failed to register node: ' + kPluginNodeName )
    raise
    
def uninitializePlugin( mobject ):
''' Uninitializes the plug-in '''
mplugin = OpenMayaMPx.MFnPlugin( mobject )
try:
    mplugin.deregisterNode( kPluginNodeId )
except:
    sys.stderr.write( 'Failed to deregister node: ' + kPluginNodeName )
    raise

and tried translating losing all hope i searched online for twist python reference below is the link 

I want this so badly i dont get it why its not executing and when i try executing 
works like a charm

let me know if any correction are needed in the script or anything i need to study, as i am interested in learning api
// Error: file: /builds/maya_runtime/6b3b073194/scripts/others/pluginWin.mel line 909: type object 'MyDeformerNode' has no attribute 'twistYAttribute'
# Traceback (most recent call last):
#   File "/u/bghu/PycharmProjects/scripts/twistDeformer.py", line 104, in nodeInitializer
#     MyDeformerNode.addAttribute( MyDeformerNode.twistYAttribute )
# AttributeError: type object 'MyDeformerNode' has no attribute 'twistYAttribute' // 
// Warning: file: /builds/maya_runtime/6b3b073194/scripts/others/pluginWin.mel line 909: Failed to call script creator function // 

Robert White

unread,
Nov 20, 2019, 10:50:37 AM11/20/19
to Python Programming for Autodesk Maya
It looks like in some of your code you're referencing it as `twistYAttribute` but in other places as `twistYAttr` which appears to be the correct spelling.

blossom ghuntla

unread,
Nov 21, 2019, 3:44:32 AM11/21/19
to Python Programming for Autodesk Maya
# Warning: Skipped polyCylinder1 since it was not deformable. # 
# Error: (kInvalidParameter): Object is incompatible with this method
# Traceback (most recent call last):
#   File "/u/bghu/PycharmProjects/scripts/twistDeformer.py", line 50, in deform
#     twistYAttribute = pDataBlock.inputValue( MyDeformerNode.twistYAttr )
# RuntimeError: (kInvalidParameter): Object is incompatible with this method # 
# Result: [u'twistDeformer1'] # 
// Error: (kInvalidParameter): Object is incompatible with this method
# Traceback (most recent call last):
#   File "/u/bghu/PycharmProjects/scripts/twistDeformer.py", line 50, in deform
#     twistYAttribute = pDataBlock.inputValue( MyDeformerNode.twistYAttr )
# RuntimeError: (kInvalidParameter): Object is incompatible with this method // 

Thank you that fixed the crashing issue
But now i am facing a new error

blossom ghuntla

unread,
Nov 29, 2019, 10:24:47 PM11/29/19
to python_in...@googlegroups.com
fixed the issue

while not pGeometryIterator.isDone():
point = pGeometryIterator.position()
ff = magnitude*point.y*envelopeValue
if ff != 0.0:
cct = math.cos(ff)
cst = math.sin(ff)
tt = point.x*cct - point.z*cst
point.z = point.x*cst +point.z*cct
point.x=tt
pGeometryIterator.setPosition(point)
pGeometryIterator.next()

why it wasn't working for me was the last 2 lines causing the while loop to iterate over and over again(the popular infinite loop)
so its done now i can proceed further by working on new deformers
Thank you for you help and i have even attached the code for checking purpose


--
You received this message because you are subscribed to a topic in the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/python_inside_maya/-gIdP7BFhbE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to python_inside_m...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/python_inside_maya/4fd22e6d-2b24-4614-9b09-a3609969d3f3%40googlegroups.com.
twistDeformer.py
Reply all
Reply to author
Forward
0 new messages