need some help with passing values

42 views
Skip to first unread message

yann19

unread,
Jul 6, 2016, 9:10:34 PM7/6/16
to Python Programming for Autodesk Maya
I am editing this file where it has 2 files - one for UI and the other (lets call it the Function script, not sure what is the correct term is) is the functions that process the user selections etc.

Currently in this UI, there is this text field in which I am trying to pass its string values to the other script.
However, the only way I can think of is creating another file with this attribute called itemName and import into this Function script and called it but sometimes it seems to and seems not to work..

All in all, what it does is upon the user selection, when running this particular function, the object created should be parented under the object's/node's name which is reflected in this text field.

This is the main UI code
import meshPaintCon as meshPaintCon

class meshPaintWin (object):
...
self.uitxtField = mc.textFieldButtonGrp( label='Parent to Group', text='', buttonLabel='SELECT', bc=lambda * args:self.uiButtonCallback("uitxtField", args))
...
    
def uiButtonCallback(self, *args):
        button = args[0]
if (button == 'uitxtField'):
import foo as foo
foo.itemName = mc.ls(l=True, sl=True);
print(foo.itemName)
print(foo.itemName[0])
mc.textFieldButtonGrp(self.uitxtField, edit=True, text=str(foo.itemName[0]) )
...


This is the portion that executes the parenting.. (From the Function script)
class paintSur(object):
 
...
   
...
   
 
def fetchObject(self):
       
...
       
if(mc.nodeType(sourceDAG) != 'transform'):
            tempDAG
= mc.listRelatives(sourceDAG, parent=True)
            sourceDAG
= tempDAG[0]
           
       
print(sourceDAG)
       
        newObjectDAG
= None
       
if (self.uiValues.instance):
                newObjectDAG
= mc.instance(sourceDAG)
       
       
# string for self.tempgroup is : newItemCreation
       
import foo as foo
       
if (foo.itemName != ""):
            newObjectDAG
= mc.parent(newObjectDAG[0], foo.itemName, relative=True)
       
else:
            newObjectDAG
= mc.parent(newObjectDAG[0], self.tempgroup, relative=True)
   
   
return sourceDAG, newObjectDAG[0]


In case if you are curious about how i run my code:
import sys
sys
.path.insert(0, '/Desktop/paintMesh')
import paintMeshTool
reload
(paintMeshTool)

paintMeshTool
.meshPaintWin()

Any help is greatly appreciated!

Justin Israel

unread,
Jul 6, 2016, 11:50:27 PM7/6/16
to Python Programming for Autodesk Maya
If I am reading your examples correct, it would appear you are using your foo module as a kind of global scratch pad for storing and sharing data between unrelated function calls in your UI and your business logic modules. You might try making your business logic capable of storing everything it needs to do its work. Your UI should be creating an instance of it and managing it over time. That is, setting item name should be something you do on an instance of your business logic class. Not a global in a module. 

Justin


--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, 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/9a9f33b6-2b32-42c8-8a95-3feb94b23420%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

yann19

unread,
Jul 7, 2016, 12:22:51 PM7/7/16
to Python Programming for Autodesk Maya
Hi, do you have an example in which I can refer to, if possible?

Truth be told, I am still pretty much of an amateur in scripting. And this script that I am editing was not my code to begin with and I am still trying to grasp how it works in certain parts

Justin Israel

unread,
Jul 7, 2016, 6:08:50 PM7/7/16
to python_in...@googlegroups.com
On Fri, Jul 8, 2016 at 4:22 AM yann19 <yang...@gmail.com> wrote:
Hi, do you have an example in which I can refer to, if possible?

Truth be told, I am still pretty much of an amateur in scripting. And this script that I am editing was not my code to begin with and I am still trying to grasp how it works in certain parts

This may be an incorrect interpretation of your modules, but something like this?

## Module foo
class Context(object):

  def __init__(self):
    self.itemName = ""

## Module meshPaintCon
class PaintSurfacer(object):

  def __init__(self, context):
    self.context = context

  def fetchObject(self):
    if self.context.itemName:
      # use it

## Module ui
import foo
import meshPaintCon 

class MeshPaintWin(object):

  def __init__(self):
    self._context = foo.Context()
    self._painter = meshPaintCon.PaintSurfacer(self._context)

  def uiButtonCallback(self, *args):
    self._context.itemName = mc.ls(l=True, sl=True)[0]

See how your UI creates a context that is then given to your business logic? You no longer have global variables if you do this. And it means you could have multiple instances of the window running without values colliding.

You may not even need to use a Context, if you can just set values directly on your painter instance:

## Module meshPaintCon
class PaintSurfacer(object):

  def __init__(self):
    self.itemName = ""

  def fetchObject(self):
    if self.itemName:
      # use it

## Module ui
import meshPaintCon 

class MeshPaintWin(object):

  def __init__(self):
    self._painter = meshPaintCon.PaintSurfacer()

  def uiButtonCallback(self, *args):
    self._painter.itemName = mc.ls(l=True, sl=True)[0]

Justin

 


On Wednesday, July 6, 2016 at 8:50:27 PM UTC-7, Justin Israel wrote:
If I am reading your examples correct, it would appear you are using your foo module as a kind of global scratch pad for storing and sharing data between unrelated function calls in your UI and your business logic modules. You might try making your business logic capable of storing everything it needs to do its work. Your UI should be creating an instance of it and managing it over time. That is, setting item name should be something you do on an instance of your business logic class. Not a global in a module. 

Justin

--
You received this message because you are subscribed to the Google Groups "Python Programming for Autodesk Maya" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python_inside_m...@googlegroups.com.

yann19

unread,
Jul 11, 2016, 6:11:50 PM7/11/16
to Python Programming for Autodesk Maya
Thanks! I will check it out.

Thank you for the examples!
Reply all
Reply to author
Forward
0 new messages