Convert python-script

Become a member of the CGSociety

Connect, Share, and Learn with our Large Growing CG Art Community. It's Free!

THREAD CLOSED
 
Thread Tools Search this Thread Display Modes
Old 06 June 2013   #1
Convert python-script

Hi guys,

does anyone know how to convert this script into python:

 global proc GeoDeform() 
{
string $sel[] = `ls -sl -tr`;
if (size($sel)==2)
{
string $collider = $sel[0];
string $target = $sel[1];
string $collidershape[] = `listRelatives -s $collider`;
string $collisiondeformer[] = `deformer -typ "GeoDeform" -n "collisionDeformer" $target`;
connectAttr -f ($collidershape[0]+".worldMesh[0]") ($collisiondeformer[0]+".collider");
connectAttr -f ($collider+".matrix") ($collisiondeformer[0]+".colliderMatrix");
connectAttr -f ($collider+".boundingBox.boundingBoxSize") ($collisiondeformer[0]+".colliderBBoxSize");
}
else
{
error "please select two meshes: first the collider mesh then the mesh that should be deformed.";
}
}
 
 
global proc AEGeoDeformNew( string $attributeName1, string $attributeName2) {
checkBoxGrp -numberOfCheckBoxes 1 -label "Backface Culling" culling;
checkBoxGrp -numberOfCheckBoxes 1 -label "Sculpt Mode" sculpt;
 
connectControl -index 2 culling ($attributeName1);
connectControl -index 2 sculpt ($attributeName2);
}
 
global proc AEGeoDeformReplace( string $attributeName1, string $attributeName2) {
connectControl -index 2 culling ($attributeName1);
connectControl -index 2 sculpt ($attributeName2);
}
 
global proc AEGeoDeformTemplate( string $nodeName )
{
// the following controls will be in a scrollable layout
editorTemplate -beginScrollLayout;
 
// add a bunch of common properties
editorTemplate -beginLayout "Collision Deformer Attributes" -collapse 0;
editorTemplate -callCustom "AEGeoDeformNew" "AEGeoDeformReplace" "backface_culling" "sculpt_mode";
editorTemplate -addSeparator;
editorTemplate -addControl "bulge" ;
editorTemplate -addControl "bulgeextend" ;
editorTemplate -addControl "offset" ;
editorTemplate -addControl "envelope" ;
AEaddRampControl "bulgeshape" ;
 
editorTemplate -endLayout;
 
// include/call base class/node attributes
AEdependNodeTemplate $nodeName;
 
// add any extra attributes that have been added
editorTemplate -addExtraControls;
 
editorTemplate -endScrollLayout;
}
'''
meval( mel )
 
Old 06 June 2013   #2
For every mel command there is an equivalent python command.

mel:
ls -sl -tr;

python:
cmds.ls(sl=True, tr=True)


So to convert the script you'll just need to learn a bit of python syntax, and then follow some simple rules to convert the commands.
My suggestion is google a few python getting started tutes, then have a go at converting...
When it fails... post your python version of the lines giving the error (plus the error message) and we'll help you with the syntax.

David
__________________
http://www.djx.com.au
 
Old 06 June 2013   #3
Thanks djx!

I found the opportunity to work with sth called mel2py.py
Is this advisable and do you know how to work with this?
 
Old 06 June 2013   #4
I think it depends on why you want to convert the script.
You can still run mel scripts from python, so if you have a working script and you dont need new features, then you can just keep it as mel.
Quote: import maya.mel
maya.mel.eval('GeoDeform()')

or in pymel, even more directly
import pymel.core as pm
pm.mel.GeoDeform()


If you want to convert it as a way of learning python (or python in maya), then there is not much advantage to doing that with a script like mel2py.py It would be much better to learn how to do it manually and at the same time learn how things can be structured differently to take advantage of python being a more advanced language.

David
__________________
http://www.djx.com.au
 
Old 07 July 2013   #5
I keep an SE Tab with this code:
import pymel.tools.mel2py as mel2py # Beware escaped chars - \t needs to be \\t
        mel2py.mel2py("path to mel file")
        IE:
        mel2py.mel2py("C:/Program Files/Autodesk/Maya2012/scripts/others/advanceToNextDrivenKey.mel")
        
    pythonCode = mel2py.mel2pyStr( """
        put mel here
        """)
   IE:
        pythonCode = mel2py.mel2pyStr( """
        instance; for ($i=1; $i<13; ++$i) instance -st; group sphere7 sphere8 sphere9 sphere10;
        """)
        print pythonCode
        
mel2py.mel2py (path to mel file) will create a .py version of a mel script in the same dir in which the mel script resides.
The 2nd example will convert a mel cmd into python and put in a var.
The returned code is pymel and often needs to cleaned up, but it gives a great head start.
__________________
If animation couldn't change the world, it wouldn't be such a Micky-Mouse place.
 
Old 07 July 2013   #6
Thread automatically closed

This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.
__________________
CGTalk Policy/Legalities
Note that as CGTalk Members, you agree to the terms and conditions of using this website.
 
Thread Closed share thread



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
CGSociety
Society of Digital Artists
www.cgsociety.org

Powered by vBulletin
Copyright 2000 - 2006,
Jelsoft Enterprises Ltd.
Minimize Ads
Forum Jump
Miscellaneous

All times are GMT. The time now is 11:22 PM.


Powered by vBulletin
Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.