Assorted Python questions
-
On 14/12/2017 at 17:57, xxxxxxxx wrote:
Hi!
Beginning with Python on C4D, I'm find quite a few obstacles that slow me down or make things a little more complicated.
- I have a very simple package with basic functions, but it does not recompile when I edit the source. If I edit and save Tools/Tools.py in Script Manager and execute SomeScript.py, it will not be recompiled, I have to exit C4D and start again to use any the changes in it.
Here's a simplified version of what i have...
scripts\SomeScript.py scripts\Tools\__init__.py scripts\Tools\Tools.py
import sys import os def HelloWorld() : print ("Hello World")
#---------------- import c4d sys.path.insert(0, os.path.dirname(__file__)) from Tools import Tools sys.path.pop(0) #---------------- def main() : Tools.HelloWorld() if __name__=='__main__': result = main()
- Functions from Tools.py don't recognize imported c4d and doc, I have to pass them from the main executing script as a parameter. Why? How do I avoid that?
import c4d def ListAllOk(doc) : objs = doc.GetObjects() for node in objs: print ("*+[%s]" % (node.GetName())) def ListAllNotOk() : objs = doc.GetObjects() for node in objs: print ("*+[%s]" % (node.GetName()))
#---------------- import c4d sys.path.insert(0, os.path.dirname(__file__)) from Tools import Tools sys.path.pop(0) #---------------- def main() : Tools.ListAllOk(doc) Tools.ListAllNotOk() if __name__=='__main__': result = main()
- Print to standard error? There's too much noise on stdout when running from command line without gui, so I'm trying to use stderr. None of these actually output to stderr, but I can see them on the console and stdout. Is there a way to print to stderr?
print(*args, file=sys.stderr, **kwargs) sys.stderr.write('STDERR: sys.stderr.write') logging.warn('STDERR: logging.warn') print >> sys.stderr, 'STDERR: print >> sys.stderr'
-
On 15/12/2017 at 01:56, xxxxxxxx wrote:
Hi, I recommand you to read Best Practise for Import? and to use py localimport made by
Niklas
[URL-REMOVED]It will carry all your problems. In term of implementation you just have to load it into your main file (your .pyp) then in other file make normal import, it will work.
If you want to take a look at how it's look like into a project, please look at my project Refractive Index Importer, main file is RefractiveIndex.pyp and other source file are in "res/src/"I hope it's help !
[URL-REMOVED] @maxon: This section contained a non-resolving link which has been removed.
-
On 15/12/2017 at 07:03, xxxxxxxx wrote:
I'm not developing a plugin, I just have a bunch of scripts that I execute some times.
I find pasting all that locaimport blob into every single script kind of foolish.
When you say 'carry all your problems', does it mean my problems will continue or be solved?I don't understand why Maxon won't just add the scripts folder to the path as default and fix all our import problems.
-
On 15/12/2017 at 07:14, xxxxxxxx wrote:
localImport blob are only needed in the main file.
But anyway sorry, I thought you spoke about plugin while about script.
As It's design, script that are stored inside script folder are not imported into python and script folder is jsut the default location where script are saved. More important as I understand a script is normally design to be just a OneCallExecution.
If you want to make some functions available from everyone (or at least to you) you have to put your module/script inside site-package.
Window%AppData%\MAXON\CINEMA 4D RXX\library\python\packages\win64
Mac
/Users/"YOURUSERNAME"/Library/Preferences/MAXON/CINEMA 4D RXX/library/python/packages/win64
I guess it's how Cinema4D and python is designed.
-
On 15/12/2017 at 08:54, xxxxxxxx wrote:
Hi,
About the second question, this is how Python and programming languages work in general with variable scope. Variables aren't automatically passed over to called functions.
Python in Cinema 4D adds some special variables like doc when a script is executed. These variables are only passed to the script's scope and aren't global to the execution.Regarding the third question, I'm afraid Python embedded in Cinema 4D changes both stdout/stderr and these print to the same location.