Objects with tags inside python generator

On 17/09/2015 at 11:52, xxxxxxxx wrote:

Hi, I'm very newbie with python so I hope you can help me with this little problem.

I'm trying to make python generator object where you can link object (spline, primitive, polygon, cloner, matrix) and python generator object will make clone to every vertex point to that linked object. Clones are spline text objects so you can visualize vertex points' or clones' id's.

My setup is now pretty complicated. I have python generator object that takes object link input via userdata. Python generator makes cloner object and cloner is set to object mode and it uses linked object as object. There is also dummy cloner object and xpresso tag which I'm using to get count of clones in cloner. Python generator gets total count via userdata. This setup is working actually pretty well but I'm trying to optimize it that one python generator could do everything.

TL;DR
I have no idea how can insert xpresso tag to object with python generator and put some nodes and connections to that xpresso tag. Also I don't know how can I add or edit userdata input with python generator. Is that even possible? Python documentation is very confusing with this section.

Second way to do this what I tried was to import mograph module and try to get clones count with GeGetMoData(op) command. But looks like it works only with python effector not with python generator. Am I right?

I can give link to my current file if needed.

Any help will be appreciated. :thumbsup:

On 18/09/2015 at 02:17, xxxxxxxx wrote:

Hello and welcome,

please post only one question per thread. This keeps the forum organized and makes it more easy to find existing answers.

Please notice that you should use a Python Generator only to create new, virtual objects. A Generator should not manipulate existing objects.

If you want to create a Xpresso tag you can do this using MakeTag() and the ID of the tag, which is Texpresso. From this Xpresso tag you can get the node master with GetNodeMaster(). Using that node master you can create new nodes. You will find some example code in the forum like here:

You can add user data to an object using AddUserData(). You find some example here:

But please remember that a Python Generator typically should not edit existing objects. In what context do you want to use GeGetMoData() in your generator?

If you need more information on a specific topic please open a new thread. Thanks.

Best wishes,
Sebastian

On 18/09/2015 at 06:16, xxxxxxxx wrote:

Hi and thank you very much!

Now I can create a xpresso tag to cloner object inside python generator, but where can i find xpresso nodes port ids so I can add them to the node? Can you only use nodes which are in this list (Graph View Node Types)? I can't find any Motion Graphics nodes.

# create a xpresso tag  
tag = c4d.BaseTag(c4d.Texpresso)  
  
# insert xpresso tag to cloner  
clr.InsertTag(tag)  
  
nodeMaster = tag.GetNodeMaster()  
  
# create xpresso node (New Node > Motion Graphics > Data)  
modataNode = nodeMaster.CreateNode(nodeMaster.GetRoot(),c4d.ID_OPERATOR_MOGRAPH_DATA?, None, 0, 0)  
  
# add position port  
modataNode.AddPort(c4d.GV_PORT_OUTPUT, c4d.ID_POSITION? ,c4d.GV_PORT_FLAG_IS_VISIBLE,True)  

Edit:
Looks like it is not possible to do it this way. When I convert python generator (with c key) and open xpresso Cinema 4D will crash.

On 21/09/2015 at 00:35, xxxxxxxx wrote:

Hello,

the Mograph Xpresso Nodes are no core nodes so they are not documented. You can get the ID of a given node by creating it manually and getting its ID with GetOperatorID(). In the case of the Data node the ID is 1019010.

The IDs or symbols of the ports can be found in the corresponding header file or in the C++ documentation.

Best wishes,
Sebastian

On 21/09/2015 at 07:15, xxxxxxxx wrote:

Thanks again Sebastian,

I feel really stupid to ask this, but how can I call manually made xpresso node when I don't know its ID? There is no any command like GetFirstNode(). As far I can get is to call the main XGroup with this code:

nm = x.GetNodeMaster()  
root = nm.GetRoot()  
owner = nm.GetOwner()  
print root

I'm sorry that I keep asking different questions in this same topic. Should I make everytime new topic for every question?

On 21/09/2015 at 09:42, xxxxxxxx wrote:

Hello,

like most classes of the API, GvNode ist based on BaseList2D and GeListNode. This means you can get the child objects of the root node with GeListNode.GetDown(). Then you can loop through the nodes using GeListNode. GetNext().

As long as your questions are related to the topic of creating Xpresso nodes you can continue in this threads. For other questions please open a new thread.

Best wishes,
Sebastian

On 21/09/2015 at 13:31, xxxxxxxx wrote:

Nice! Thank you.

I'm able to get node's ID with this script. I leave it here so it might be helpful for those who are struggling with same problem.

(Run in Script Manager)

import c4d  
  
# make xpresso tag and node manually  
# select xpresso tag and run the script  
  
def main() :  
  selection = doc.GetSelection()  
    
  for x in selection:  
        
      nm = x.GetNodeMaster()  
      root = nm.GetRoot()  
    
      firstNode = root.GetDown()          
      firstNodeID = firstNode.GetOperatorID()  
        
      print firstNodeID  
    
  c4d.EventAdd()  
  
if __name__=='__main__':  
  main()  

I'm almost there. Messing with ports are now giving me a headache.

nm = x.GetNodeMaster()  
root = nm.GetRoot()  
  
objectNode = nm.CreateNode(nm.GetRoot(), 400001000, None, 0, 0)  
objectIndexNode = nm.CreateNode(nm.GetRoot(), 400001152, None, 0, 0)  
  
# Check it is OK to add port?  
print objectNode.AddPortIsOK(c4d.GV_PORT_INPUT, c4d.GV_OBJECT_OPERATOR_OBJECT_IN)  
 **# prints True - everything should be fine**  
  
# Try to add port  
objectNode.AddPort(c4d.GV_PORT_INPUT, c4d.GV_OBJECT_OPERATOR_OBJECT_IN)  
 **# Nothing happens - port does not appear**  
  
# Try to connect two ports  
portA_IN = objectNode.GetInPort(0)  
portB_OUT = objectIndexNode.GetOutPort(0)         
portA_IN.Connect(portB_OUT)  
 **# AttributeError: 'NoneType' object has no attribute 'Connect'** 

I'm not sure what is wrong.

On 22/09/2015 at 00:16, xxxxxxxx wrote:

Hello,

currently there is a bug with creating object ports. See "OBJECT_OPERATOR_OBJECT_OUT".

best wishes,
Sebastian

On 22/09/2015 at 07:14, xxxxxxxx wrote:

Hi and thanks,

So there is way to go around that object port bug with user data field. Very nice!

I'm also able to add other kind of ports to nodes, but when I try to connect two ports together, no matter what nodes type is, I get error message: **AttributeError: 'NoneTpye' object has no attribute 'Connect'.
**
Sample code

formulaNode = nm.CreateNode(nm.GetRoot(), 400001133, None, 0, 0)  
mathNode = nm.CreateNode(nm.GetRoot(), 400001121, None, 0, 0)  
    
portA = mathNode.GetPort(c4d.GV_MATH_INPUT)  
portB = formulaNode.GetPort(c4d.GV_FORMULA_OUTPUT)  
        
portA.Connect(portB)

On 22/09/2015 at 10:21, xxxxxxxx wrote:

Hello,

the problem is that the ports could not be found. Access the ports by the index:

  
portA = mathNode.GetInPort(0)  
portB = formulaNode.GetOutPort(0)  
  
portA.Connect(portB)  

best wishes,
Sebastian

On 23/09/2015 at 00:28, xxxxxxxx wrote:

Thank you so much!

Now everything makes sense. Now I'm gonna work with this my thing and I will share the final product when and if I get it done.

On 23/09/2015 at 12:48, xxxxxxxx wrote:

Hi,

I'm having now weird problems. Adding object port with DescID is not working. I'm using command:

basenode.AddPort(c4d.GV_PORT_OUTPUT, c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA, c4d.DTYPE_SUBCONTAINER, 0), c4d.DescLevel(1)), message = True)

I "emulated" the problem with manually made objects and script manager.

Null
-->Cloner

This code works perfectly:

import c4d  
  
def main() :  
  #doc = c4d.documents.GetActiveDocument()  
  #pointobject = doc.GetActiveObject()  
     
  userdata = c4d.GetCustomDatatypeDefault(c4d.DTYPE_BASELISTLINK)  
  userdata[c4d.DESC_NAME] = "Object Data"  
  
  child = op.GetDown()  
    
  linkobject = child.AddUserData(userdata)  
  child[linkobject] = child  
  
  xpressotag = c4d.BaseTag(c4d.Texpresso)  
  xpressotag.SetName("PointLock XPresso")  
    
  child.InsertTag(xpressotag)  
    
  nodemaster = xpressotag.GetNodeMaster()  
  basenode = nodemaster.CreateNode(nodemaster.GetRoot(), c4d.ID_OPERATOR_OBJECT, None, 100, 100)  
    
  basenodeobjectoutput = basenode.AddPort(c4d.GV_PORT_OUTPUT, c4d.DescID(c4d.DescLevel(c4d.ID_USERDATA, c4d.DTYPE_SUBCONTAINER, 0),\  
c4d.DescLevel(1)), message = True)  
    
  #basenodepositionoutput = basenode.AddPort(c4d.GV_PORT_OUTPUT, c4d.ID_BASEOBJECT_REL_POSITION)  
  #c4d.modules.graphview.RedrawMaster(nodemaster)  
  
    
  c4d.EventAdd()  
  
if __name__=='__main__':  
  main()

But when I try to run same kind of code with python generator it is not making the object port. I also first need to insert cloner under null object and return the null to get user data field to cloner. If I try to return only the cloner it comes without user data. Is it the bug or do I need to play with DescID?

On 24/09/2015 at 00:53, xxxxxxxx wrote:

Hello,

in a Script Manager script "op" refers to the currently selected object. In a Python Generator it refers to the Generator Object. Maybe this is the difference.

Best wishes,
Sebastian

On 24/09/2015 at 07:21, xxxxxxxx wrote:

Hi,

I know that. I'm not using op variable in Python Generator.

On 24/09/2015 at 09:40, xxxxxxxx wrote:

Hello,

what code do you use in your Python Generator?

Best wishes,
Sebastian

On 24/09/2015 at 14:01, xxxxxxxx wrote:

Hello,

I'm using this code in Python Generator.

import c4d  
  
def main() :  
  nul = c4d.BaseObject(5140)                                      # create a null  
  clr = c4d.BaseObject(1018544)                                   # create a cloner  
  link = op[c4d.ID_USERDATA,2]                                    # object  
  num = 5                                                         # number  
  userdata = c4d.GetCustomDatatypeDefault(c4d.DTYPE_BASELISTLINK) # user data type  
  userdata[c4d.DESC_NAME] = "Object Data"                         # user data name  
  clr.InsertUnder(nul)                                            # insert cloner under the null  
  clr[c4d.ID_MG_MOTIONGENERATOR_MODE] = 0                         # set cloner to object mode  
  clr[c4d.MG_OBJECT_LINK] = link                                  # link object to cloner  
   
  ###     
   
  # create user data field  
  linkobject = clr.AddUserData(userdata)  
  clr[linkobject] = clr  
  
  # xpresso stuff  
  xpressotag = c4d.BaseTag(c4d.Texpresso)  
  clr.InsertTag(xpressotag)  
  nodemaster = xpressotag.GetNodeMaster()  
    
  # create nodes  
  basenode = nodemaster.CreateNode(nodemaster.GetRoot(), c4d.ID_OPERATOR_OBJECT, None, 100, 100)  
  datanode = nodemaster.CreateNode(nodemaster.GetRoot(), 1019010, None, 0, 0)  
    
  # create ports  
  basenode.AddPort(c4d.GV_PORT_OUTPUT, c4d.DescID\  
  (c4d.DescLevel(c4d.ID_USERDATA, c4d.DTYPE_SUBCONTAINER, 0), c4d.DescLevel(1)), message = True)  
    
  # create connections  
  basenodeobjectoutput = basenode.GetOutPort(0)  
    
  datanodeinput = datanode.GetInPort(0)  
  datanodeoutput = datanode.GetOutPort(0)  
    
  basenodeobjectoutput.Connect(datanodeinput)                     # << error >>  
  
  ####  
  
  for i in xrange(num) :  
      obj = c4d.BaseObject(5159)                                  # base spline text  
      obj.InsertUnderLast(clr)                                    # insert clone under the cloner  
  
  # return final object  
  return nul  

If I don't insert cloner under the null object I don't get user data tab to the cloner.

On 25/09/2015 at 09:05, xxxxxxxx wrote:

Hello,

it seems that you are using the "op" variable in line 6 of your code. In the code you provided "basenodeobjectoutput" is not defined.

You are creating a Object Node without defining what object this node should reference. You can define the referenced object by setting the GV_OBJECT_OBJECT_ID parameter. Something like this:

  
objectNode.SetParameter(c4d.GV_OBJECT_OBJECT_ID, linkedObject, c4d.DESCFLAGS_SET_0)  

best wishes,
Sebastian