Plugin Cafe Homepage
Forum Home Forum Home > Plugin Cafe > PYTHON Development
  New Posts New Posts
  FAQ FAQ  Forum Search   Register Register  Login Login

storing data in memory

 Post Reply Post Reply
Author
Message
MohamedSakr View Drop Down
Member
Member
Avatar

Joined: 2012 Sep 17
Location: Egypt
Online Status: Offline
Posts: 577
Post Options Post Options   Quote MohamedSakr Quote  Post ReplyReply Direct Link To This Post Topic: storing data in memory
    Posted: 2015 Jun 23 at 10:08pm
Hi,

I'm new to Cinema 4D Python SDK "but familiar with the C++ SDK".
I have a Python Generator , I'm reading data from files, I need to read data only once "storing it in memory", then use this data in the document "in animation".

I know that this question is kinda noob, but bare with me.
what confuses me:
* the generator will get executed at any frame, so how to make sure that the data is cached in memory so I can safely read it? i.e if it will run every frame, data will get erased after execution right?

so the behavior is something like this:
at first run, read files and cache data into memory, at other runs I will handle these data, I ask this question because in C++ this needs a class with Init and possibly some command buttons "too easy to do in C++".

offtopic:
in C++ plugin, there is a dll ".cdl64 for example" , and res folder, what is the equivalent in Python plugin?
cheers,
Mohamed Sakr
Back to Top
bonsak View Drop Down
Member
Member
Avatar

Joined: 2006 May 13
Location: Oslo, Norway
Online Status: Offline
Posts: 130
Post Options Post Options   Quote bonsak Quote  Post ReplyReply Direct Link To This Post Posted: 2015 Jun 25 at 10:18am
Global variables seems to carry over from frame to frame. Then you just need some kind of test or flag to decide when to execute the read.

import c4d
#Welcome to the world of Python

inputData = -1

def main():
    global inputData
    fps = doc.GetFps()
    time = doc.GetTime()
    frame = time.GetFrame(fps)
    if frame == 0:
        print "read"
        inputData = 2

    print inputData

An other option is to use an interaction tag and set its language to python. Check out the different events in the example code you get when you set the language. You could use the "message" function to check for document load and execute your read.

Python plugins are named .pyp

Cheers
Bonsak
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2573
Post Options Post Options   Quote NiklasR Quote  Post ReplyReply Direct Link To This Post Posted: 2015 Jun 25 at 10:37am
You shouldn't use global variables in a Python Plugin. A second
instance of your Python Tag Plugin would use the same global
variable, interfering with the first tag. Global variables in a Python
Scripting Tag, on the other side, are for visible to that tag only.

In a Python Plugin, use attributes instead.

  def __init__(self):
    super(MyTag, c4d.plugins.TagData).__init__()
    self.counter = 0
  def Execute(self, tag, doc, op, bt, priority, flags):
    print self.counter
    self.counter += 1
    return c4d.EXECUTIONRESULT_OK

-Niklas
Back to Top
MohamedSakr View Drop Down
Member
Member
Avatar

Joined: 2012 Sep 17
Location: Egypt
Online Status: Offline
Posts: 577
Post Options Post Options   Quote MohamedSakr Quote  Post ReplyReply Direct Link To This Post Posted: 2015 Jun 25 at 10:57am
thanks NirkalsR and bosnak.
@NiklasR , your approach is what I will do "pretty much similar to C++ class members" , but what I'm missing here is: how does Cinema4D see a Python plugin? "for C++, it requires .cdl64 and res, what does it require for Python?"
cheers,
Mohamed Sakr
Back to Top
bonsak View Drop Down
Member
Member
Avatar

Joined: 2006 May 13
Location: Oslo, Norway
Online Status: Offline
Posts: 130
Post Options Post Options   Quote bonsak Quote  Post ReplyReply Direct Link To This Post Posted: 2015 Jun 25 at 11:19am
@Niklas. Didn't know that. Always thought that global in one tag was contained to that tags scope.
@Mohammed Check out the file and directory structure of some of the examples: https://developers.maxon.net/docs/Cinema4DPythonSDK/help/examples/plugins/Py-LookAtCamera/

-b
Back to Top
NiklasR View Drop Down
Member
Member


Joined: 2010 Dec 13
Location: Germany
Online Status: Offline
Posts: 2573
Post Options Post Options   Quote NiklasR Quote  Post ReplyReply Direct Link To This Post Posted: 2015 Jun 25 at 11:32am
@bonask: That is true for a Python Scripting Tag (the one built-in into Cinema where you can edit
the code live from Cinema 4D) but not for Python Tag Plugins
@MohamedSakr: A .pyp file which contains the Python source code, or .pypv for a protected Python plugin
Back to Top
S_Bach View Drop Down
Forum Moderator
Forum Moderator
Avatar

Joined: 2011 Jun 27
Online Status: Offline
Posts: 1364
Post Options Post Options   Quote S_Bach Quote  Post ReplyReply Direct Link To This Post Posted: 2015 Jun 29 at 3:04am
Hello,

you find information on the plugin structure and PYP files in the documentation:
best wishes,
Sebastian

SDK Support Engineer
Back to Top
MohamedSakr View Drop Down
Member
Member
Avatar

Joined: 2012 Sep 17
Location: Egypt
Online Status: Offline
Posts: 577
Post Options Post Options   Quote MohamedSakr Quote  Post ReplyReply Direct Link To This Post Posted: 2015 Jun 29 at 4:55pm
thanks a lot Niklas and Sebastian
cheers,
Mohamed Sakr
Back to Top
 Post Reply Post Reply

Forum Jump Forum Permissions View Drop Down

Bulletin Board Software by Web Wiz Forums® version 9.61 [Free Express Edition]
Copyright ©2001-2009 Web Wiz

This page was generated in 0.094 seconds.