tag:blogger.com,1999:blog-58439772564135304902024-02-20T17:57:20.290-06:00Tech Art TikiIf it ain't broke, it will be soon.Unknownnoreply@blogger.comBlogger28125tag:blogger.com,1999:blog-5843977256413530490.post-34800806618685647492013-02-20T15:32:00.000-06:002013-02-20T15:32:59.755-06:00Why retarget missing bitmaps in 3ds Max?I was just reading a <a href="http://enemcee.com/blog/?p=157">blog post by Nick Covington</a> about retargeting missing bitmaps in 3ds Max scenes. He has a perfectly clever idea for doing so, but I wondered why I've never needed to write something similar for my users. Or rather, why so many people feel that bitmap retargeting is necessary?<br><br>
3ds Max has an "External File Paths" feature found under Customize/Configure User Paths. It allows you to list folders in which bitmaps can be found on your system, regardless of where they were when first assigned. For instance, if you load a Max scene containing a reference to "C:\my_killer_art\foo.tga", but that file or folder doesn't exist on your PC, it will automatically look for "foo.tga" in each of the External File folders until it's found.<br><br>
It also works for shader files. As long as you have paths in that list that contain all bitmaps your scenes could possibly use, you'll never see another "missing files" error again.<br><br>
So am I missing something? Do people just prefer to retarget in the scenes themselves, rather than maintain that list of external file folders? I don't, but mileage might vary.<br><br>
Admittedly, one limitation of that external file paths is that they don't automatically recurse into subdirectories. If you have a folder below one listed there, it will not look in that folder, it needs to be listed explicitly. Rather dumb.Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-5843977256413530490.post-90355868632693930752012-03-01T10:20:00.000-06:002013-03-06T09:48:33.344-06:00Perforce Python API BasicsMany Python users working with Perforce believe that calling out to "p4.exe" with subprocess is the only method available. Perforce actually maintains <a href="http://www.perforce.com/product/components/apis">free, native API packages</a> for several languages, including Python. The Perforce Python API is fast, fully-featured and easy to work with. It lets you interact with Perforce in a familiar Python manner, without having to capture and parse command-line output. Parsing output is one of my least favorite things to do, and I doubt I'm alone there.<br><br>
Here is a dead-simple example, showing how to use the Perforce Python API to sync all files in a certain depot folder.
<pre class="brush:python">
# Sync contents of a folder
import P4
p4_api = P4.P4( )
p4_api.connect( )
results = p4_api.run_sync( '//project_x/...' )
p4_api.disconnect( )</pre>
Some notes on connections... You'll notice above I first "connected" before issuing any commands with the API. Typically you do this once in your tool/script, run any Perforce commands you need, then disconnect when you're finished or the tool closes. It will also disconnect when the API object falls out of scope and gets destroyed. There's no need to open and close the connection all the time.<br><br>
You can also use the "with" statement to easily manage the connection, automatically disconnecting when that block of code is completed:
<pre class="brush:python">
with p4_api.connect( ):
# connected here
results = p4_api.run_sync( '//project_x/...' )
# disconnected here</pre>
Going back to the top example... as written it will simply use the default Perforce port, client and user. If you want to explicitly set this and not use the default, call the "set_env" function prior to your connect call (new in version 2011.1):
<pre class="brush:python">
p4_api.set_env = ( 'P4CLIENT', 'my_workspace' )
</pre>
Next, take a look at the "run_sync" command we issued. One cool thing about the Perforce Python API is that the general syntax for everything is "<api_object>.run_<command>( args )", where "command" is literally the command string you would pass to p4.exe when using the command-line interface. Examples: "run_sync", "run_edit", "run_add", "run_fstat", etc. If you know how to use Python from the command-line you already know how to use the Python API.<br><br>
Above you'll see I captured the return value of our sync as "results". Calls like this all return a single list of dictionaries, one dict for each file the operation was run on. In the sync example above, it only has to update two files in my workspace, so the results object returned looks like this:
<pre class="brush:python">
[
{
'totalFileSize': '5299712',
'rev': '319',
'totalFileCount': '2',
'clientFile': 'D:\\projects\\project_x\\stuff.dll',
'fileSize': '4865024',
'action': 'updated',
'depotFile': '//project_x/stuff.dll',
'change': '969310'
},
{
'action': 'updated',
'clientFile': 'D:\\projects\\project_x\\foo.txt',
'rev': '134',
'depotFile': '//project_x/foo.txt',
'fileSize': '434688'
}
]</pre>
Looking at the second dictionary at the bottom, you'll notice several keys indicating data from the sync operation for that file, including the action ("updated", "added", etc.), both the client and depot paths to the file, and its new revision number.<br><br>
For some operations the first dictionary returned contains some extra keys related to the overall operation, such as the total number of files acted on, and their total sizes on disk.<br><br>
These returned results are full of any data you need to present friendly messages to your users. Being in simple dictionary form means they're flexible and easy to work with.Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-5843977256413530490.post-36780339408946475592012-01-29T13:54:00.000-06:002012-01-29T13:55:29.186-06:00Old is the new newIf you're seeing old posts of mine on the RSS feed, I apologize. The code syntax highlighter I use stopped working and I had to modify several posts to get it working again. For unknown reasons that causes the feed to treat them as new.
<br />
<br />
If anyone else uses Blogger and knows how to avoid that, please enlighten me.Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-5843977256413530490.post-75699040092847368832012-01-09T06:49:00.001-06:002012-01-09T06:51:11.668-06:00GDC 2012 Tech Artist Boot Camp AnnouncementI'm organizing and MCing the <a href="http://schedule.gdconf.com/session/6473/Technical_Artist_Boot_Camp" target="_blank"><span style="color: #ff9900;">Tech Artist Boot Camp at GDC 2012</span></a> in March. The TABC is an
all-day Tutorial-format session on Tuesday, March 6, from 10 AM-6
PM.<br />
<br />
Below is the session description and list of speakers &
topics. We also plan to do a group panel-style Q&A session at the end of the
day.<br />
<br />
I spoke at the <a href="http://tech-artists.org/forum/showthread.php?t=1500">TABC last year</a>, and it was an excellent way to reach out to and share with other industry TAs. I hope to see you
there!<br />
<br />
<b>Description</b><br />
Technical Art is evolving rapidly. In many
studios TAs play key roles in developing efficient tools pipelines and ensuring
art content is visually striking and optimized for performance. TAs bridge
content and engineering helping make both more successful. However, many studios
have still not fully embraced the TA role. Their TAs are smart and eager to make
an impact, but are not sure how to best prove their value, and be given key
roles in development.<br />
<br />
A group of experienced, respected technical artists
from across the industry would like to invite you to sit with them for a day and
learn how to be a more effective TA. Focus on the tools and skills TAs can use
to demonstrate their value, and further integrate technical art into their
studios' pipelines and cultures. Find the worst development problems at your
studio and show them what a TA can do!<br />
<br />
<b>Intended Audience</b><br />
This
all-day tutorial is for technical artists and other developers of any experience
level. A light focus will be placed on techniques and skills useful to TAs at
studios with little-to-no tech art integration and
culture.<br />
<br />
<b>Takeaway</b><br />
At the end of this all-day event, attendees
will understand key techniques to help them take technical art to the next level
at their studios. Learn how to effectively work within constraints, integrate
into your teams, communicate with other disciplines, design better code and
pipelines, and master new shader techniques.<br />
<br />
<b>Speakers & Topics
---</b><br />
<br />
<b>Welcome, Introduction</b><br />
<b>Adam Pletcher, Technical Art
Director, Volition, Inc.</b><br />
<br />
<b>You Have to Start Somewhere... Defining
the Tech Art Role and Building Their Team</b><br />
<b>Arthur Shek, Technical Art
Director, Microsoft Studios (Turn 10)</b><br />
This session will go over the
trials of moving from a job in film/animation to a studio with a minimal Tech
Art presence and the ensuing panic of change. The Tech Art role has a soft
definition and differs at every studio – our common quality is that we are
problem solvers, and to problem solve, you must have experience, wide knowledge
and the ability to scramble on your feet. At times, what we may feel pressure to
know can be overwhelming. Relax - you have to start somewhere.<br />
<br />
<b>Better,
Faster Stronger: Teaching Tech Artists to Build Technology</b><br />
<b>Rob
Galanakis, Lead Technical Artist, CCP Games</b><br />
The success of Tech Art has
caused a complexity of projects and tools for which our traditional skill set is
under-equipped. Tech Artists are now building technology, not just scripts, and
our essential growth must be as a cohesive team, not just trained individuals.
In this session, attendees will learn how to apply a few key practices of
professional software development, such as code review, support processes, and
collaborative coding, to the unique environment of Tech Art. <br />
<br />
<b>Build it
on Stone: Best Practices for Developing A Tech Art Infrastructure</b><br />
<b>Seth
Gibson, Senior Technical Artist, Crystal Dynamics</b><br />
In this session we
present a set of best practices for building Tech Art tools and pipelines in a
stable, maintainable, and scalable fashion through the establishment of a solid
tools development infrastructure geared toward the specific needs of Technical
Artists.<br />
<br />
<b>Joining the Dark Side: How Embedded Tech Artists Can Unite
Artists and Programmers</b><br />
<b>Ben Cloward, Senior Technical Artist, Bioware
Austin</b><br />
Technical Artists can be a powerful force to unify teams and
ensure that productions run smoothly. In this case study, I’ll show how the
simple act of moving two technical artists into the programmers’ working area
helped to improve the relationship between art and programming and resulted in a
better-looking, more efficient game.<br />
<br />
<b>Lessons in Tool
Development</b><br />
<b>Jason Hayes, Technical Art Director, Volition,
Inc.</b><br />
All too often, the importance of planning the architecture of tools
and pipelines in game development is overlooked. In most cases, project
pressures often give us the false impression that we don’t have time to plan, or
worse, we actually save time by “just getting it done”. Nothing could be further
from the truth. This session explains why up front planning is important, when
to recognize over-engineering and offers architectural design principles for
effective tools development-- such as program organization, data design,
scalability and user interface design. Internal tools developed at Volition will
be used to demonstrate these topics.<br />
<br />
<b>Shady Situations: Real-time
Rendering Tips & Techniques</b><br />
<b>Wes Grandmont III, Senior Technical
Art Director, Microsoft Studios (343 Industries)</b><br />
This tutorial session
will cover a variety of techniques that can be used individually or combined to
solve a variety of game related real-time shading problems. It will begin with a
brief overview of the current generation GPU pipeline, followed by some HLSL
basics. The rest of the talk will dive into a range of techniques with a
complete overview of how each one is implemented.<br />
<br />
<b>Unusual UVs:
Illuminating Night Windows in Saints Row The Third</b><br />
<b>Will Smith,
Technical Artist, Volition, Inc.</b><br />
This session presents a holistic case
study involving HLSL shader development. Included is not only the problem and
its resolution, but perhaps more importantly, an insight into the Technical
Artist’s problem-solving mindset throughout its resolution.<br />
<br />
<b>Group
Q&A, Conclusion</b>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-5843977256413530490.post-55628672404653590872011-12-05T06:40:00.003-06:002012-01-29T10:03:00.973-06:00py2exe, Windows 7 & VistaI don't use <a href="http://www.py2exe.org/">py2exe</a> very often, but it can be a useful tool for environments that may not have an existing Python installation.<br />
<br />
I recently used py2exe on my Windows 7 PC to build a small tasktray tool. The resulting executable ran fine on my PC (doesn't it always?), but threw an exception on any Vista PC it was run on.<br />
<pre>File "win32com\__init__.pyo", line 5, in <module>File "win32api.pyo", line 12, in <module>
File "win32api.pyo", line 10, in __load
ImportError: DLL load failed: The specified module could not be found.</pre>
After more online searching than I'd like to admit, I found a post that said py2exe may be including W7-specific DLLs, when instead it should be leaving those out, forcing Vista to go find its native builds of those DLLs.<br />
<br />
I was able to fix the problem by adding two DLLs to the "dll_excludes" list in my py2exe setup script:<br />
<pre class="brush:python">options = {
"bundle_files": 3,
"compressed": 1,
"optimize": 1,
"excludes": excludes,
"packages": packages,
'dll_excludes': [ 'mswsock.dll', 'powrprof.dll' ]
}</pre>
The tool now runs on both Vista and Windows 7.
<pre class="python:nogutter" name="code">Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-5843977256413530490.post-21440596594240840742011-02-27T12:36:00.004-06:002011-03-02T02:08:38.293-06:00GDC 2011 Wrap-up, downloadThe <a href="http://www.gdconf.com/conference/tutorials.html#214">Technical Artist Boot Camp</a> went really well today. I personally learned a great deal from both the attendees and fellow presenters. Thanks to everyone that turned out, and thanks for all the great questions!<br /><br />Here is the sample script file I promised in my talk on databases. It's a simple, working illustration of how to use SQLAlchemy ORM to map a Python class to a database table. Please let me know if you have any questions.<br /><br /><a href="http://sites.google.com/site/adampletcher/GDC2011_AdamPletcher_PythonSamples.zip">GDC2011_AdamPletcher_PythonSamples.zip</a> (2 KB)Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-5843977256413530490.post-44045952352740743422011-02-20T15:00:00.014-06:002011-02-24T19:26:53.195-06:00GDC 2011 - Technical Artist Boot Camp<a href="http://www.gdconf.com/"><img style="MARGIN: 0px 10px 10px 0px; WIDTH: 100px; FLOAT: left; HEIGHT: 100px; CURSOR: hand" border="0" alt="" src="http://sites.google.com/site/adampletcher/GDC11_speakerbutton-100px.gif" /></a><br /><div><a href="http://www.gdconf.com/">Game Developers Conference 2011</a> is a week away. I'm co-presenting at the <a href="http://www.gdconf.com/conference/tutorials.html#214">Technical Artist Boot Camp</a>, a special all-day Tutorial session on Tuesday March 1.<br /><br />My portion of the Boot Camp is called "Embrace the Database." Here's my summary:<br /><br /><em>They may have a mystical aura about them, but databases are far easier to use than you may think. They can be a Technical Artist’s greatest ally in game development, powering your most important tools, gathering usage and error data you’ve never had access to, enabling new workflows and revealing hidden weaknesses (and strengths) in your content pipelines.<br /><br />This session will also explore how to use Python to unlock the power of databases at your studio. We will look at what databases do best, using practical examples to get you started. We'll discover how Object-Relational Mapping lets you interact with a database in a simple manner that any Python user will already understand</em>.<br /><br />I'm really excited to be presenting alongside so many talented TAs from our industry. It's going to be packed with great ideas and techniques. If you plan to be in SF for GDC, definitely stop by for our session.</div>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-5843977256413530490.post-68201213220071814682010-09-25T17:53:00.012-05:002012-01-29T10:55:50.476-06:00Using Sharepoint Lists with PythonContinuing my tradition of shoving Python into new and unusual places, I recently worked out how to use Python to post items to a Sharepoint List.<br />
<br />
We use Sharepoint for some of our intranet needs, and I was experimenting with error-reporting workflows. While I ultimately didn't stick with Sharepoint for this purpose, the Python code worked fine and I wanted to share.<br />
<br />
First, a few things to note. Sharepoint uses several SOAP-based webservices as a means of exposing functionality to other tools/languages. One of these is for manipulating Lists, which are Sharepoint's basic storehouse for items that hold arbitrary columns/fields of data.<br />
<br />
I was unfamiliar with SOAP prior to this, and ended up using the "suds" extension for Python to help with the formatting. Suds is necessary to run the examples below, and can be downloaded on the <a href="http://sourceforge.net/projects/python-suds/">Suds SourceForge page</a>.<br />
<br />
The "sharepoint" module I'm posting below has basic usage like this:<br />
<pre class="brush:python">import sharepoint
item_data = {
'item_id' : 32,
'Log Time' : datetime.datetime.now( ),
'Message' : 'Sharepoint is rather obtuse',
'User Name' : 'adam.pletcher',
}
sp_list = sharepoint.Sharepoint_List( 'http://sp_server/some_site/', 'Sharepoint List Name', 'domain_name', 'user_name', 'rot13_encoded_password' )
result = sp_list.add_item( item_data )</pre>
Walking through the above, first we create a simple dictionary of key/value pairs. The keys are the Sharepoint names for the columns in your list, and the values are the values you wish to submit for your new list item. Then, we construct a <span style="font-family: courier new;">Sharepoint_List</span> instance, passing it the site URL, List Name, domain, user and password. Finally, call our <span style="font-family: courier new;">add_item</span> method on that object, passing it the data dictionary we made.<br />
<br />
The <span style="font-family: courier new;">add_item</span> method accepts most data types, including Python datetime objects, as shown. You can easily extend <span style="font-family: courier new;">add_item</span> to do something more elaborate.<br />
<br />
One thing to watch out for is the list's column/field names used in your dictionary. These must be strings matching the true Sharepoint names of your fields. Even if you rename a column later, it's true name will not change once the list has been created. The only flexibility I currently provide in <span style="font-family: courier new;">add_item</span> is the ability to leave spaces in the field names.<br />
<br />
I experimented with NTLM as a means of using the current user's Windows Authentication instead of requiring the username/pass each time. I was not successful in that, however... if anyone has success there I'd love to hear about it. As it stands, the <span style="font-family: courier new;">Sharepoint_List</span> class requires the username and a ROT-13 encoded password. If you've never encoded anything, here's how to do that from a Python prompt:<br />
<pre class="brush:python">>>> 'my_password'.encode( 'rot-13' )
'zl_cnffjbeq'</pre>
The complete "sharepoint" module described here can be downloaded below. Adding an item isn't the only thing the Sharepoint lists webservice offers. You can use the framework in this module to do more including edits, deletes and other actions.<br />
<br />
<a href="http://sites.google.com/site/adampletcher/Python_Sharepoint.zip">Python_Sharepoint.zip</a> (3 KB)Unknownnoreply@blogger.com6tag:blogger.com,1999:blog-5843977256413530490.post-38370514114480599072009-12-06T20:44:00.014-06:002012-01-29T10:54:58.666-06:00MaxScript DotNet Sockets with PythonI create and work with several Python tools that manipulate scenes in 3ds Max. These are usually floating dialogs linked to the main 3ds Max window that send MaxScript commands via COM. This works well until I need the tool's UI to update when something happens in 3ds Max. Like refresh an object list when the selection in Max changes.<br />
<br />
You would think doing a COM connection the other way would work. However, since Python-registered COM servers run separately in their own instance of the interpreter, there's no native connection to the original tool.<br />
<br />
Nathaniel Albright, a fellow TA at <a href="http://www.volition-inc.com/">Volition</a>, recently created a Python COM server that communicated to his Python tool via TCP/IP socket. So it went 3ds Max -> Python COM server -> TCP/IP -> Python tool. This works well, but I wondered if the DotNet facilities in MaxScript offered a direct way to use sockets.<br />
<br />
I had yet to touch DotNet in MaxScript, so this seemed like a good opportunity to learn a few things. After a lot of searching online I only turned up a few scraps of info, no complete recipe. However, I did find enough to get MXS DotNet sockets working, and assemble a comprehensive example.<br />
<br />
I created a little Python tool that displays the names of all selected objects in the 3ds Max scene. As the scene selection changes, the list of names automatically updates. I won't go over all the code in this post, but the full working example tool is included in the zipfile below.<br />
<br />
There's three main points of interest in the example:<br />
<br />
1. Using DotNet in MaxScript to communicate via TCP/IP socket<br />
2. Listening on a socket in a background thread in Python<br />
3. Creating and posting custom wxPython events<br />
<br />
<strong>The MaxScript Client</strong><br />
<br />
I made a MaxScript struct called "mxs_socket". The code follows, and also included in the zipfile below.<br />
<pre class="brush:maxscript">struct mxs_socket (
ip_address = "127.0.0.1", -- "localhost" also valid
port = 2323, -- default port
-- <dotnet>connect <string>ip_string <int>port_int
--
-- Description:
-- Takes IP address, port and connects to socket listener at that
-- address
fn connect ip_string port_int = (
socket = dotNetObject "System.Net.Sockets.Socket" ( dotnetclass "System.Net.Sockets.AddressFamily" ).InterNetwork ( dotnetclass "System.Net.Sockets.SocketType" ).Stream ( dotnetclass "System.Net.Sockets.ProtocolType" ).Tcp
socket.Connect ip_string port_int
socket -- return
),
-- <int>send <string>data
--
-- Description:
-- Converts a string (or any object that can be converted
-- to a string) to dotnet ASCII-encoded byte sequence and
-- sends it via socket. Uses ip_address and port defined
-- in struct above, or set by client.
-- Returns integer of how many bytes were sent.
fn send data = (
-- Convert string to bytes
ascii_encoder = dotNetObject "System.Text.ASCIIEncoding"
bytes = ascii_encoder.GetBytes ( data as string )
-- Connect, send bytes, then close
socket = connect ip_address port
-- result is # of bytes sent
result = socket.Send bytes
socket.Close()
result -- return # of bytes sent
)
)</pre>
Using this, I can send bytes to any socket listener on port 5432 by doing the following:<br />
<pre class="brush:maxscript">socket = mxs_socket port:5432
socket.send "Hello, World!"</pre>
The <span style="font-family: courier new;">connect</span> method was pretty simple in the end. The only twist turned out to be converting the socket integer into a DotNet socket object.<br />
<br />
The <span style="font-family: courier new;">send</span> method converts the string into an ASCII-encoded DotNet bytes object, connects to the listener, sends the bytes, then closes the connection. The value returned is the number of bytes sent.<br />
<br />
The last lines of the above code sets up a MaxScript callback that fires when the object selection changes in the scene. That uses <span style="font-family: courier new;">mxs_socket</span> to send a string containing the names of all the selected objects to any tool that's listening on that port.<br />
<br />
Now I just need to make my Python tool listen.<br />
<br />
<strong>The Python Server</strong><br />
<br />
My Python server/listener (also in the zipfile below) is a typical wxPython frame, but with two added qualities... It uses a background thread to listen on a socket, and posts a custom <span style="font-family: courier new;">wx.Event</span> when data is received. I had never used either of these techniques before, but it was fun getting it working.<br />
<br />
Since a typical wxPython app sits in its main loop waiting for user input, I created a <span style="font-family: courier new;">Socket_Listen_Thread</span> class, a subclass of <span style="font-family: courier new;">threading.Thread</span>. This does the listening in a background thread while the main UI thread waits on the user. The <span style="font-family: courier new;">run</span> method here does the real work:<br />
<pre class="brush:python">def run( self ):
self.running = True
while ( self.running ):
# Starting server...
# Listen for connection. We're in non-blocking mode so it can
# check for the signal to shut down from the main thread.
try:
client_socket, clientaddr = self.socket.accept( )
data_received = True
except socket.error:
data_received = False
if ( data_received ):
# Set new client socket to block. Otherwise it will
# inherit the non-blocking mode of the server socket.
client_socket.setblocking( True )
# Connection found, read its data then close
data = client_socket.recv( self.buffer_size )
client_socket.close( )
# Create wx event and post it to our app window
event = self.event_class( data = data )
wx.PostEvent( self.window, event )</pre>
This listening thread runs quietly in the background until it receives data. At that point I need a way to break into the main UI thread. There's other ways to do this, but using a custom wx.Event seemed to be the best fit here.<br />
<br />
First, when the wx.Frame is opened, I create a custom wxPython event.<br />
<pre class="brush:python">(Max_Update_Event, EVT_3DS_MAX_UPDATE) = wx.lib.newevent.NewEvent()</pre>
Calling <span style="font-family: courier new;">NewEvent</span> returns both a new Event class and an object to bind the event handler to. I pass the event class to the listener thread, bind an event handler to it, and that's all.<br />
<br />
When data comes in over that TCP/IP port from our MaxScript tool, the listening thread receives it and posts our custom event to the main wx.Frame. That in turn fires the event handler to update the UI.<br />
<br />
My example MaxScript client and Python listener described above can be found in the following ZIP file. Drop me a line if you do something useful with them, I'd love to hear about it.<br />
<br />
<a href="http://sites.google.com/site/adampletcher/MaxScript_DotNet_Sockets_Python.zip">MaxScript_DotNet_Sockets_Python.zip</a> (4 KB)<br />
<br />
<i>Thanks to Nate Albright and everyone contributing to the</i> <a href="http://forums.cgsociety.org/showthread.php?f=98&t=551473">"dotNet + MXS"</a> and <a href="http://forums.cgsociety.org/showthread.php?f=98&t=509631">"Python + MXS"</a><i> threads on the CGSociety forums. Those long-running threads have been very inspiring, and contain several tips that were key in getting the MaxScript DotNet socket stuff hammered out.</i>Unknownnoreply@blogger.com11tag:blogger.com,1999:blog-5843977256413530490.post-89076375940858671882009-08-06T06:57:00.010-05:002009-08-06T07:45:56.240-05:00Regular Expressions CoachingTry as I might, I've never had instant recall on the details of regular expressions. For whatever reason (infrequent use, old age) the syntax just slides out of my head and onto the floor mere minutes after using it.<br /><br />Awhile back a co-worker introduced me to <a href="http://weitz.de/regex-coach/">The Regex Coach</a>, and I've used it regularly ever since. Paste in a snippet of text, type a regex pattern, and it highlights matches in the text as you type. Super easy way to (re)learn or explore regex.<br /><br /><img src="http://adam.pletcher.googlepages.com/regexcoach.png" /><br />It's targeted at Perl-style regex, which for me has proved completely compatible with Python's re module. If you like it, don't forget to donate.<br /><br />A bonus tip for the regex-challenged like myself...<br />If you memorize only one thing: <span style="font-family:courier new;"><strong>(.*)</strong></span>Unknownnoreply@blogger.com11tag:blogger.com,1999:blog-5843977256413530490.post-31959916747797259082009-06-27T07:34:00.007-05:002012-01-29T10:53:39.186-06:00Hidden HiddenDOSCommand detailsTwice in recent months I've been bitten by MaxScript's HiddenDOSCommand.<br />
<br />
It was added in 3ds Max 2008 as a way to issue DOS commands without bringing up an ugly command prompt. Sounds great but what the docs don't tell you is that the optional "startpath:<path>" argument is actually not optional at all. If you leave it out you'll receive a cryptic error like this:.</path><br />
<pre class="brush:maxscript">HiddenDOSCommand "notepad %temp%\\cmdout.tmp" prompt:"Waiting..."
-- Error! CreateProcess(cmd /e:on /d /c "notepad %temp%\hiddencmdout.tmp") failed!</pre>
Note, that command was pasted from Example Usage in the MaxScript docs for HiddenDOSCommand. It will not work, nor will the other examples listed there <em>unless</em> you include "startpath"....<pre class="brush:maxscript">HiddenDOSCommand "notepad %temp%\\cmdout.tmp" prompt:"Waiting..." startpath:"C:\\"
true</pre>
This may have been addressed in the helpfile for 3ds Max 2010, I haven't checked. This can be Google fodder in the meantime.Unknownnoreply@blogger.com4tag:blogger.com,1999:blog-5843977256413530490.post-19091986472204801572009-05-10T09:14:00.005-05:002009-05-10T09:51:08.266-05:00What we do with PythonThere's a great thread going at <a href="http://www.tech-artists.org/">tech-artists.org</a> called <a href="http://tech-artists.org/forum/showthread.php?t=359">What do you do with Python?</a> The other day I posted a few of the things our studio has done with Python in the past year or two...<br /><br /><ul><li>Measure start/stop times of various processes, logging data to SQL database. For instance, how long it takes 3ds Max to start up, so we can spot bad trends when new tools are published. </li><li>System for logging errors and tools usage data to central database, with optional emailing of errors/callstack. Works for Python tools as well as MaxScript (via COM). </li><li>A non-linear GUI editor for an otherwise complex/table-driven cutscene pipeline.</li><li>Build graphical user interfaces (generally with wxPython) that integrate with in-house and off-the-shelf C applications. For example, floating Python dialogs that link to app windows as children, or as docking task panes. </li><li>Tool that communicates with game C code (via socket) running on consoles to do in-game realtime lighting. </li><li>Embed Python interpreter into editor framework for next-gen development tools. This is the one I spend lots of time on these days... works like MaxScript in 3ds Max, but for our custom editors. </li><li>One Exporter that writes out various data files from 3ds Max, Photoshop, and imports/categorizes them in our asset system. </li><li>Logs me into Outlook's webmail without manually entering my creds every time. I guess that was a home project. :)</li><li>At 3ds Max startup, scan folders for MaxScripts, building a MacroScript .mcr file for all of them. </li><li>At 3ds Max startup, builds list of texture map folders for a given project, sorts them by user's discipline and adds them to Max's bitmap paths list. </li><li>Profile rendering performance of art assets recently submitted to Perforce, recording data to SQL database.</li><li>Searches web-based bug tracker database for entries assigned to you and displays data in a Vista Sidebar gadget.</li><li>Creates makefiles with dependencies, for distributed build processes in Incredibuild/XGE.</li><li>Wavelet transform calculations for content-based image comparison tools. For finding textures that are too similar, or comparing rendered output of one shader vs. another.</li><li>Takes zipcode or lat/long as input, gathers geo-survey data from various online sources and creates the road/terrain network inside our world editor.</li><li>Tons of data mining uses. Like searching various exported XML files for instances of X material, mesh, etc. in game world.</li><li>Tool for bridging various apps with COM interfaces in other tools. Like firing MaxScripts in 3ds Max from Ultraedit, or taking current Python script in Wing and running it in our editor's embedded interpreter. </li><li>Custom scripts for integrating our tools/processes into Wing (the Python IDE we use).</li></ul><p>P.S. Call your mom today. </p>Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-5843977256413530490.post-63904022882002768372009-03-24T21:49:00.006-05:002009-03-24T22:13:08.278-05:00Volition at GDC 2009Hey if you're at this year's <a href="http://www.gdconf.com/">Game Developers Conference</a>, be sure to check out the talks from my co-workers. <a href="http://www.volition-inc.com/">Volition</a> has a strong showing this year, with plenty of great tech artist material.<br /><br /><a href="https://www.cmpevents.com/GD09/a.asp?option=C&V=11&SessID=8541"><strong>Blowing Up the Outside World: Destruction Done the Next Gen Way</strong></a><br /><strong>by Eric Arnold and Jeff Hanna</strong><br />This session presents an in-depth look at the tools and technologies used to make a truly destructible world for <a href="http://www.redfaction.com/">RED FACTION: GUERRILLA</a>. The presenters will share the lessons they learned and the problems they had to overcome in order to have destruction in their game.<br /><br /><a href="https://www.cmpevents.com/GD09/a.asp?option=C&V=11&SessID=8574"><strong>Technical Artist Roundtable</strong></a><br /><strong>by Jeff Hanna</strong><br />This roundtable will be an animated group discussion about being an effective technical artist. Topics of discussion will include what skills a technical artist should possess, how the role differs from company to company, scripting content creation applications, shader development, asset management, and improving production pipelines.<br /><br /><strong><a href="https://www.cmpevents.com/GD09/a.asp?option=C&V=11&SessID=8694">Technical Art Techniques Panel: Tools and Pipeline</a></strong><br /><strong>Robert Galanakis, Jeff Hanna, Seth Gibson, Christopher Evans and Ross Patel</strong><br />As game pipelines, their tools, and content become more complex, technical artists have become the developers of choice for much of the planning, overseeing, and implementation of pipelines. Technical artists from BioWare, Bungie, Microsoft, and Volition discuss their solutions and practices for tools and pipeline.<br /><br /><strong><a href="https://www.cmpevents.com/GD09/a.asp?option=C&V=11&SessID=8677">Breathing LIFE into an Open World</a></strong><br /><strong>by Scott Phillips</strong><br />Examine the history of populating open worlds and a detailed description and post-mortem of the LIFE system developed by Volition for SAINTS ROW 2. Attendees will learn about the inspiration, organization, methodology, successes and failures of the LIFE system used to add life to the open world city of Stilwater.<br /><br /><strong><a href="https://www.cmpevents.com/GD09/a.asp?option=C&V=11&SessID=8567">Universal Character System in SAINTS ROW 2</a></strong><br /><strong>by Chris Fortier</strong><br />Attendees will learn how SAINTS ROW 2's character customization and random NPC generator work. Some things that will be discussed include the universal body mesh, character morphing, normal map blending, layered clothing, shader-based customization features, how we assemble NPCs and how all this character variation affects animation.Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-5843977256413530490.post-13313314304926240532009-03-17T21:29:00.011-05:002012-01-29T10:52:48.067-06:00Python cheat sheetsI'm out of hibernation, time I posted something.<br />
<br />
As much as I use Python these days, there's a few things I find myself looking up regularly. At one point I just made a small crib sheet and stuck it to my monitor. I have examples on it for <strong>list comprehensions</strong>, <strong>filter</strong>, and <strong>map</strong>.<br />
<br />
<strong>List Comprehensions</strong><br />
These are useful for creating modified lists from existing data without a lot of fuss. They aren't all that hard to remember, but the syntax was a bit alien to me for awhile. They're basically an expression followed by a <span style="font-family: courier new;">for</span> clause.<br />
<br />
The below example takes an existing list, <span style="font-family: courier new;">my_list</span> and builds a new list with only the elements that are greater than 2. In this case the result is assigned right back to <span style="font-family: courier new;">my_list.</span><br />
<pre class="brush:python">my_list = [x for x in my_list if x > 2]</pre>
<strong>Filter</strong><br />
Using <span style="font-family: courier new;">filter</span> is a powerful way to remove undesired elements from a list. You pass a function as the first argument, which generally returns True/False based on some criteria. The second argument is the sequence to be filtered (or any iterable object). Only the elements that return True when passed to that function will remain in the newly returned list.<br />
<br />
Filtering is often done with a <span style="font-family: courier new;">lambda</span> as the first function argument. A lambda is a one-off function that's defined and used in the same place. Since it's only used once, it doesn't need a name. It's so common to see <span style="font-family: courier new;">filter</span> and <span style="font-family: courier new;">lambda</span> together, the fact they were seperate didn't occur to me when I was learning the language.<br />
<br />
In this example, we have a list of filenames, my_files, and we want to remove any that aren't Python scripts, ending in '.py'.<br />
<pre class="brush:python">my_files = filter(lambda f: f.endswith('.py'), my_files)</pre>
That is the shorter equivalent of:<br />
<pre class="brush:python">def is_py_filename(filename):
return filename.endswith('.py')
my_files = filter(is_py_filename, my_files)</pre>
With <span style="font-family: courier new;">filter</span>, passing None as the first argument instead of a function automatically removes any elements that don't evaluate to True. That includes integers or floats that are zero, as well as occurrences of <span style="font-family: courier new;">False</span> or <span style="font-family: courier new;">None</span>.<br />
<br />
<pre class="brush:python">my_files = filter(None, my_files)</pre>
<strong>Map</strong><br />
Mapped functions let you apply a function to every element in a sequence.<br />
<pre class="brush:python">def add_ten(x):
return x+10
result = map(add_ten, [1,2,3,4,5])</pre>
The value of <span style="font-family: courier new;">result</span> would be <span style="font-family: courier new;">[11, 12, 13, 14, 15]</span>. Of course you could also use a lambda here, too:<br />
<pre class="brush:python">result = map(lambda x: x+10, [1,2,3,4,5])</pre>
So what's on your cheat sheet?Unknownnoreply@blogger.com5tag:blogger.com,1999:blog-5843977256413530490.post-91774724605599864692008-08-29T08:29:00.011-05:002012-01-29T10:50:33.871-06:00Read-only Windows files with PythonHow do you use Python to get or change read-only/writeable access on files in Windows? The Python docs don't answer this in a direct manner. Here's one option using only the standard library.<br />
<pre class="brush:python">import os, stat
myFile = r'C:\stuff\grail.txt'
fileAtt = os.stat(myFile)[0]
if (not fileAtt & stat.S_IWRITE):
# File is read-only, so make it writeable
os.chmod(myFile, stat.S_IWRITE)
else:
# File is writeable, so make it read-only
os.chmod(myFile, stat.S_IREAD)</pre>
You may prefer the <a href="http://python.net/crew/mhammond/win32/">pywin32 extensions</a> for this sort of thing...<br />
<pre class="brush:python">import win32api, win32con
myFile = r'C:\stuff\grail.txt'
fileAtt = win32api.GetFileAttributes(myFile)
if (fileAtt & win32con.FILE_ATTRIBUTE_READONLY):
# File is read-only, so make it writeable
win32api.SetFileAttributes(myFile, ~win32con.FILE_ATTRIBUTE_READONLY)
else:
# File is writeable, so make it read-only
win32api.SetFileAttributes(myFile, win32con.FILE_ATTRIBUTE_READONLY)</pre>
Or, more concisely with win32:<br />
<pre class="brush:python">roAtt = win32api.GetFileAttributes(myFile) & win32con.FILE_ATTRIBUTE_READONLY
win32api.SetFileAttributes(myFile, ~roAtt)</pre>
Using win32 you can also set other Windows file attributes (unlike <span style="font-family: courier new;">os.chmod</span>), but read/write is usually all I care about.Unknownnoreply@blogger.com6tag:blogger.com,1999:blog-5843977256413530490.post-34651646483045741012008-08-22T14:43:00.007-05:002009-02-03T08:31:53.915-06:00ColladaMax for 3ds Max 2009 64-bitI was unable to find the <a href="http://sourceforge.net/projects/colladamaya/">ColladaMax</a> importer/exporter plugin for 3ds Max 2009 64-bit, so I built one. It's from the 3.05b source.<br /><br /><strong>Update 02/03/09</strong><em> - I made a new build that depends on an older version of the DirectX SDK. It should fix the "failed to initialize" error some of you were getting, without the need to install anything else.</em><br /><br />Feel free to grab it if you like:<br /><a href="http://adam.pletcher.googlepages.com/ColladaMax_2009x64.rar">ColladaMax_2009x64.rar</a> (709 KB)Unknownnoreply@blogger.com22tag:blogger.com,1999:blog-5843977256413530490.post-85918841948285758862008-08-05T22:30:00.009-05:002012-01-29T10:49:30.326-06:00Photoshop scripting with PythonPhotoshop natively supports scripting with AppleScript, JavaScript and VBScript. While Python is notably absent from that list, it can still be used to automate nearly anything in Photoshop. This is thanks to the extensive COM interface Photoshop provides.<br />
<br />
The methods here are similar to those used in my <a href="http://www.volition-inc.com/gdc">GDC 2008 Python lecture</a>, about driving 3ds Max via Python. You start by dispatching the Photoshop COM server, using Python as the client:<br />
<pre class="brush:python">import win32com.client
psApp = win32com.client.Dispatch("Photoshop.Application")</pre>
This connects to your already-opened Photoshop session, or opens one if none are running. The root COM object is then assigned to <span style="font-family: courier new;">psApp</span>, and you're ready to do some cool stuff. Here's a quick example:<br />
<pre class="brush:python">psApp.Open(r"D:\temp\blah.psd") # Opens a PSD file
doc = psApp.Application.ActiveDocument # Get active document object
layer = doc.ArtLayers[0] # Get the bottom-most layer
layer.AdjustBrightnessContrast(20,-15) # Bright +20, Contrast -15
doc.Save() # Save the modified PSD</pre>
Here's a more complex example. This script recursively scans a folder for PSD files, exporting various textures contained inside. One PSD can have specifically-named Layer Groups, each of which is written to a separate PNG file with a specific suffix. If a Group contains several layers, they're flattened when exported, allowing you to keep all your layered effects intact in the PSD.<br />
<br />
In the example below, a group named "diffuse" is exported as "psdname_D.png", the "normal" group as "psdname_N.png", and so on. The <span style="font-family: courier new;">exportType</span> dictionary determines the name/suffix pairs.<br />
<pre class="brush:python"># Recursively scans a folder (psdRoot) for Photoshop PSD files.
# For each, exports various 24-bit PNG textures based on layer
# groups found in the PSD.
# Requires the Win32 Extensions:
# http://python.net/crew/mhammond/win32/
import win32com.client
import os
# Change to match your root folder
psdRoot = r'C:\ArtFiles\PSD'
# Map of layer group names and the suffixes to use when exporting
exportTypes = {'diffuse':'_D', 'normal':'_N', 'specular':'_S'}
if (__name__ == '__main__'):
# COM dispatch for Photoshop
psApp = win32com.client.Dispatch('Photoshop.Application')
# Photoshop actually exposes several different COM interfaces,
# including one specifically for classes defining export options.
options = win32com.client.Dispatch('Photoshop.ExportOptionsSaveForWeb')
options.Format = 13 # PNG
options.PNG8 = False # Sets it to PNG-24 bit
# Get all PSDs under root dir
psdFiles = []
for root, dir, files in os.walk(psdRoot):
for thisFile in files:
if (thisFile.lower().endswith('.psd')):
fullFilename = os.path.join(root, thisFile)
psdFiles.append(fullFilename)
# Loop through PSDs we found
for psdFile in psdFiles:
doc = psApp.Open(psdFile)
layerSets = doc.LayerSets
if (len(layerSets) > 0):
# First hide all root-level layers
for layer in doc.Layers:
layer.Visible = False
# ... and layerSets
for layerSet in layerSets:
layerSet.Visible = False
# Loop through each LayerSet (aka Group)
for layerSet in layerSets:
lsName = layerSet.Name.lower()
if (lsName in exportTypes):
layerSet.Visible = True # make visible again
# Make our export filename
pngFile = os.path.splitext(psdFile)[0] + exportTypes[lsName] + '.png'
# If PNG exists but older than PSD, delete it.
if (os.path.exists(pngFile)):
psdTime = os.stat(psdFile)[8]
pngTime = os.stat(pngFile)[8]
if (psdTime > pngTime):
os.remove(pngFile)
# Export PNG for this layer Group
if (not os.path.exists(pngFile)):
doc = psApp.Open(psdFile)
doc.Export(ExportIn=pngFile, ExportAs=2, Options=options)
print 'exporting:', pngFile
else:
print 'skipping newer file:', psdFile
# Make LayerSet invisible again
layerSet.Visible = False
# Close PSD without saving
doc.Close(2)</pre>
It only exports when the PNG is missing or older than the PSD. This makes it good for running a batch texture export on your project's entire texture tree.<br />
<br />
Here is a ZIP containing the above script and a sample PSD file to try it on: <a href="http://adam.pletcher.googlepages.com/exportTextureLayers.zip">exportTextureLayers.zip</a> (143 KB)<br />
<br />
I imagine you can do all of the above with the native Photoshop scripting. I just think it's cool being able to use Python instead of rooting through a language I'm less familiar with. Dinosaurs were roaming the earth the last time I tried anything in VB.<br />
<br />
If you dig this, I'd recommend reading the <a href="http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/photoshop_cs5_scripting_guide.pdf">Photoshop CS5 Scripting Guide</a> and <a href="http://wwwimages.adobe.com/www.adobe.com/content/dam/Adobe/en/devnet/photoshop/pdfs/photoshop_cs5_vbscript_ref.pdf">Photoshop CS5 VBScript Reference</a> found in the <a href="http://www.adobe.com/devnet/photoshop/scripting/">Adobe Photoshop Developer Center</a>. While the above wasn't VBScript, the COM interface we used is nearly identical.Unknownnoreply@blogger.com73tag:blogger.com,1999:blog-5843977256413530490.post-20509004718380746972008-07-16T18:50:00.011-05:002012-01-29T10:48:36.193-06:00Checksums in 3ds Max (part 2 of 2)In <a href="http://techarttiki.blogspot.com/2008/03/checksums.html">Part 1</a> I showed how to calculate checksums inside 3ds Max. Here's how to do something useful with them.<br />
<br />
Any TA that's crossed paths with 3ds Max can tell you it doesn't do the best job of managing scene materials. Due to scene object merges/imports and other typical operations, it's common for a given material to be copied several times in one Max scene. Meaning, it's not instanced across several objects, but actually copied several times in memory. This can lead to increased memory usage and potentially inefficiencies in your game engine (depending how your exporter deals with this).<br />
<br />
What's worse, you usually can't rely on similar material names to find duplicates by hand. To do a thorough search with MaxScript, you would need to loop through every material in the scene and compare every property in it to every other material's property. This would be a slow process in C, and a complete horror-show with MaxScript.<br />
<br />
Enough grim talk. Here's a walkthrough of a MaxScript that uses checksums to make short work of this. To summarize, the script loops through the materials in your scene, creating a checksum for each as it goes. It uses that checksum to do a quick compare on previous material checksums it found, to see if they're actually property-identical. If it finds a dupe, that object is given the original material instead, effectively deleting the duplicated material.<br />
<br />
The script is divided into three functions and a short bit of main code.<br />
<br />
<b><span style="font-family: courier new;">getChecksum()</span></b> is the first function, taken from my <a href="http://techarttiki.blogspot.com/2008/03/checksums.html">previous post</a>. It calls the Python COM object we registered, which returns a checksum to the MaxScript. If you can't (or don't want to) set-up the COM object, you can use the MaxScript implementation I listed in that blog post instead... it's just less robust than the MD5 checksums used by the Python method.<br />
<br />
Next is the <b><span style="font-family: courier new;">getPropsString()</span></b> and <b><span style="font-family: courier new;">getMaterialChecksum()</span></b> functions:<br />
<pre class="brush:maxscript">------------------------------------------------------------
-- (str)getPropsString (material)mat
--
-- Description:
-- Builds a string representing the property names/values
-- of the supplied Max material.
------------------------------------------------------------
fn getPropsString mat = (
myStr = "" as stringStream
if (mat == undefined) then (
format "undefined" to:myStr
) else (
-- Start our string w/the classname
format (classOf mat as string) to:myStr
if (classof mat == ArrayParameter) then (
-- Array, so recursively add strings for each element
for element in mat do (
format (getPropsString element) to:myStr
)
) else (
-- Not an array, so see if it has properties
propNames = undefined
try (
propNames = getPropNames mat
) catch ()
if (classOf mat == BitMap) then (
try ( -- Add bitmap's filename
format mat.filename to:myStr
) catch ()
) else if (propNames == undefined) then (
format (mat as string) to:myStr
) else (
format (propNames as string) to:myStr
-- Loop through properties, adding their names
-- and values to our string to be checksummed
for i in 1 to propNames.count do (
format (i as string) to:myStr
p = propNames[i]
val = getProperty mat p
format (i as string) to:myStr
format (getPropsString val) to:myStr
)
)
)
)
(myStr as string)
)
------------------------------------------------------------
-- (str)getMaterialChecksum (material)mat
--
-- Description:
-- Takes a Max material (or multi-sub material) and
-- calculates a checksum value from it, for use as a
-- hashtable key, or whatever you like.
------------------------------------------------------------
fn getMaterialChecksum mat = (
str = ""
if (classof mat == Multimaterial) then (
for id in mat.materialIDList do (
-- Add material IDs as factors
str += id as string
)
for subMat in mat.materialList do (
-- Get string representing each submaterial
str += (getPropsString subMat)
)
) else (
-- Get string representing this material
str += getPropsString mat
)
-- Add string length as a factor
str += str.count as string
-- Get checksum from our base string
-- 99991 = largest prime number under 10k
(getChecksum str)
)</pre>
The above functions work together to generate a string of data representing the supplied 3ds Max material (or Multi-Sub material). Once it has that string, it's passed to <b><span style="font-family: courier new;">getChecksum()</span></b>.<br />
<br />
The main code block loops through the entire 3ds Max scene, doing the above for every material found on geometry objects:<br />
<pre class="brush:maxscript">----------
-- MAIN
----------
-- Set up a few things first.
timeStart = timestamp() -- Time we started process
removedCount = 0 -- Counters for printing info below
uniqueCount = 0
-- Array of two synced arrays, first with the material
-- checksums, second with materials themselves.
-- Basically a poor-man's hashtable.
csMatArr = #(#(), #())
format "Scanning scene materials...\n"
-- Loop through all geometry
for obj in geometry do (
mat = obj.material
alreadyDone = (findItem csMatArr[2] mat) != 0
if (not alreadyDone) and (mat != undefined) then (
-- First get this material's checksum
csum = getMaterialChecksum mat
idx = findItem csMatArr[1] csum
if (idx != 0) then (
-- Dupe material found, so remove it by
-- assigning the first mat to this object
format "Replacing material '%' with '%'\n" mat.name csMatArr[2][idx].name
obj.material = csMatArr[2][idx]
removedCount += 1
) else (
-- New checksum, so add it to our table,
-- along with the material itself.
append csMatArr[1] csum
append csMatArr[2] mat
uniqueCount += 1
)
)
)
gc() -- Remind Max to take out the trash
-- Done, print some results
format "-- DONE in % secs --\n" ((timestamp() - timeStart) / 1000.0)
format "Old material count = %\n" (uniqueCount + removedCount)
format "New material count = %\n" uniqueCount
format "Duplicates removed = %\n" removedCount</pre>
That's it. At the end a summary is printed to the MaxScript Listener.<br />
<br />
In the Max scene I was working with today this script cut the root material count from 533 to 261. That's <strong>51%</strong> fewer materials! It also reduced the file load time from 136 seconds to 102 seconds.<br />
<br />
You can download the complete script above here: <a href="http://adam.pletcher.googlepages.com/RemoveDupeMaterials.zip">RemoveDupeMaterials.zip</a> (4 KB)<br />
It includes the Python script to register the COM server, and the alternate MaxScript checksum method.<br />
<br />
<b>Update 7/25/08:</b> I modified <b><span style="font-family: courier new;">getPropsString()</span></b> to better handle bitmap values, and generally run faster. The ZIP file above is updated as well. Thanks to MoonDoggie/Colin on <a href="http://forums.cgsociety.org/showthread.php?f=98&t=656126">CGTalk</a> for the feedback!Unknownnoreply@blogger.com2tag:blogger.com,1999:blog-5843977256413530490.post-6830766519415698772008-06-18T22:28:00.002-05:002012-01-29T10:47:28.636-06:00Checksums in 3ds Max (part 1 of 2)In my <a href="http://techarttiki.blogspot.com/2008/03/calling-python-from-maxscript.html">Calling Python from MaxScript post</a> I mentioned the usefulness of checksums in Tech Art work. I was hoping to elaborate on that a bit.<br />
<br />
In short, a checksum is a number computed from a larger piece of data. The checksum is (ideally) guaranteed to be unique for that data. Let's say that data is this string: <span style="font-family: courier new;">"Tech Art is A-#1 Supar"</span>, and the checksum you've computed is <span style="font-family: courier new;">"30532"</span>. If any character in that string changes, the computed checksum for it will be different, like <span style="font-family: courier new;">"18835"</span> or <span style="font-family: courier new;">"1335"</span>... basically anything other than <span style="font-family: courier new;">"30532"</span>.<br />
<br />
Checksums are most useful in cases where you need to compare two sets of data to see if they differ, but don't care <em>where</em> or <em>how</em> they differ. If you have a short number that uniquely identifies a huge piece of data, you can compare it to other data sets much faster/easier than comparing every element of the original data. If you're hip to how slow n-squared searches can be (especially in languages like MEL or MaxScript), this is a classic method for avoiding them.<br />
<br />
Here's a MaxScript function that takes a string of any length and returns a checksum for it.<br />
<pre class="brush:maxscript">
------------------------------------------------------------
-- (str)getChecksum (string)val (int)size:256
--
-- Description:
-- Calculates simple checksum value from supplied string (or
-- any value convertable to a string). Default size is 256,
-- but can be changed with the optional "size" parameter.
------------------------------------------------------------
fn getChecksum val size:256 = (
if (classof val != String) then (
try (
val = val as String
) catch (
return false
)
)
alphaKey = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 !@#$%^&*()[]\\{};':\",./<>?"
total = 0
for i in 1 to val.count do (
thisVal = findString alphaKey val[i]
if (thisVal == undefined) then (
thisVal = 0
)
-- Multiply the alphanumeric value by its position in
-- the input string, add to running total
total += (thisVal * i)
)
-- make sure divisor is smaller than dividend
while (total < size) do (
total = total * 2
)
-- Return final checksum value
checksum = mod total size
return (checksum as string)
)</pre>
We used the above function in several of the Saints Row tools, primarily to help remove identical materials in 3ds Max scenes. It's very unscientific, however, and can generate collisions in rare cases (two different input strings that generate the same output checksum) **.<br />
<br />
If you don't mind a little more setup, I would recommend an alternate checksum method. Python natively offers more robust checksum tools, and can be set up to be called directly from MaxScript. The steps for doing this, and the actual MD5 checksum function, are covered in my earlier blog, <a href="http://techarttiki.blogspot.com/2008/03/calling-python-from-maxscript.html">Calling Python from MaxScript</a>.<br />
<br />
Start with the Python script from that blog that defines and registers the COM server. Then you're able to use a far-shorter MaxScript function to get checksums:<br />
<br />
<pre class="brush:maxscript">fn getChecksum val = (
comObj = createOLEObject "PythonCom.Utilities"
checksum = comObj.checksumMD5 val
return checksum
)</pre>
That's it. The checksums you get from this function will create fewer collisions than the pure-MaxScript one above, and can be made to use any alternative method available in Python.<br />
<br />
Now you know more about checksums, and how to generate them in 3ds Max. Next time (in <a href="http://techarttiki.blogspot.com/2008/07/checksums2.html">Part 2</a>) I'll discuss how you can use them to save memory in both Max and your game engine.Unknownnoreply@blogger.com11tag:blogger.com,1999:blog-5843977256413530490.post-88987855378435287852008-05-26T06:09:00.001-05:002012-01-29T10:46:22.313-06:00Python String TemplatesThe Template class, found in the standard Python <span style="font-family: courier new;">string</span> module, is extremely useful. Start with a template string containing keys you want to replace. By default keys start with "$".<br />
<br />
<pre class="brush:python">>>> import string
>>> thisTmp = string.Template("The $speed $color $thing1")
>>> thisTmp.substitute(speed='quick', color='brown', thing1='fox')
'The quick brown fox'</pre>
You can also pass the <span style="font-family: courier new;">substitute</span> method a dictionary with key/value pairs for your template:<br />
<pre class="brush:python">>>> strDict = {'speed':'slow', 'color':'toupe', 'thing1':'mango'}
>>> thisTmp.substitute(strDict)
'The slow toupe mango'</pre>
This makes it easy to insert variable parts in an otherwise fixed string or file. I use it all the time for generating table-based HTML reports.<br />
<br />
Create a file like "report.html" with string keys like "$rowValue1" or "$user" in the appropriate places, and have your script read in the contents as a string Template and do the substitutions. This also allows the report layout/appearance to be altered later without touching the script code.Unknownnoreply@blogger.com5tag:blogger.com,1999:blog-5843977256413530490.post-13209345473410842592008-05-06T22:39:00.008-05:002012-01-29T10:44:09.616-06:00Calling Python from MaxScriptUnlike Maya, 3ds Max does not have internal support for Python. But that shouldn't stop you from calling useful Python code in your MaxScripts! Here's the basics of how to do that using COM.<br />
<br />
COM is a Windows system that supports, among other arcane things, interprocess communication. You can use a language like Python, Visual Basic, or C to define a COM "server". This is a class or function, defined by a unique identifier (GUID) and a name. Here's some <a href="http://en.wikipedia.org/wiki/Component_Object_Model">gory details on COM</a> if you're curious.<br />
<br />
Here's a simple COM server using Python:<br />
<i>Requires the <a href="http://python.net/crew/mhammond/win32/">Python Win32 Extensions</a> (which no TA should be without)</i><br />
<pre class="brush:python"># A simple Python COM server.
class PythonComUtilities:
# These tell win32 what/how to register with COM
_public_methods_ = ['checksumMD5']
_reg_progid_ = 'PythonCom.Utilities'
# Class ID must be new/unique for every server you create
_reg_clsid_ = '{48dd4b8f-f35e-11dc-a4fd-0013029ef248}'
def checksumMD5(self, string):
"""Creates MD5 checksum from string"""
import hashlib
m = hashlib.md5()
m.update(str(string))
return m.hexdigest()
if (__name__ == '__main__'):
print 'Registering COM server...'
import win32com.server.register as comReg
comReg.UseCommandLine(PythonComUtilities)</pre>
This defines a function, <code>checksumMD5</code> that takes a string as input, and returns the MD5 checksum for that string.<br />
<br />
To register the COM server on a PC, simply run the Python script. Windows records it in registry, noting which script/application it uses.<br />
<br />
Now that's done, another application (3ds Max, in this case) can connect to that COM server's interface and call it like any other function. Here's an example of doing that from MaxScript:<br />
<pre class="brush:maxscript">
-- Connect to the COM server by name
comObj = createOLEObject "PythonCom.Utilities"
-- Call the function it exposes, with a sample string
checksum = comObj.checksumMD5 "The quick brown fox."</pre>
It's that simple. The checkum value returned for our sample string is <code>"2e87284d245c2aae1c74fa4c50a74c77"</code>.<br />
<br />
You might be wondering what a checksum is, or what it's good for. Stay tuned and I'll show you some slick stuff you can do with them in 3ds Max. See <a href="http://techarttiki.blogspot.com/2008/03/checksums.html">Checksums in 3ds Max, Part 1</a> and <a href="http://techarttiki.blogspot.com/2008/07/checksums2.html">Part 2</a>.<br />
<br />
<i>Python COM server example adapted from code appearing in </i><a href="http://www.oreilly.com/catalog/pythonwin32/">Python Programming in Win32</a><i> by Mark Hammond and Andy Robinson... a great book for getting more out of Windows with Python.</i>Unknownnoreply@blogger.com12tag:blogger.com,1999:blog-5843977256413530490.post-12454046447767765912008-04-05T23:18:00.000-05:002012-01-29T10:40:24.867-06:00Logging (I'm no lumberjack, but I'm okay)Exploring the Python standard library is fun.<br /><br />Not long ago I found how great the logging module is. Use it to create a logging channel, attach different handlers to it (for logging events to a file, through email or HTTP, etc), then make one-line log events of different types.<br /><pre class="brush:python"><br />import logging<br />import logging.handlers<br /><br /># Sets up a basic textfile log, with formatting<br />logging.basicConfig(level=logging.DEBUG,<br /> format='%(asctime)s %(levelname)-8s %(message)s',<br /> datefmt='%m/%d/%y %H:%M:%S',<br /> filename=r'C:\temp\mylog.log',<br /> filemode='a')<br /><br /># Log a few different events<br />logging.info('Just testing the water.')<br />logging.warning('Hmm, something is not right here')<br />logging.error("Oh no, now you're in for it")<br /></pre>The resulting text log:<br /><pre>02/14/08 22:19:03 INFO Just testing the water.<br />02/14/08 22:19:03 WARNING Hmm, something is not right here<br />02/14/08 22:19:03 ERROR Oh no, now you're in for it</pre>Add a few more lines and it sends you an email for any logs that are level "ERROR" or above:<br /><pre class="brush:python;firstline:15"><br />email = logging.handlers.SMTPHandler('smtp.foo.com',<br /> 'script@foo.com',('techart@bar.com'),'Error Report')<br />email.setLevel(logging.ERROR)<br /><br />logging.getLogger('').addHandler(email)<br /></pre>There's several other handler types as well. Very useful!Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-5843977256413530490.post-83457763545363443382008-03-15T20:20:00.000-05:002008-07-05T06:18:25.228-05:00Excel as a Database?Normally I don't pass stuff like this on, but this one still kills me every time. And if you're reading my blog you've probably heard at least one conversation like this.<br /><br /><a href="http://www.neopoleon.com/home/blogs/neo/archive/2003/09/29/5458.aspx">Rory - Neopoleon: Excel as a Database</a>Unknownnoreply@blogger.com0tag:blogger.com,1999:blog-5843977256413530490.post-12065282374564503482008-03-01T21:02:00.001-06:002008-07-05T08:03:02.824-05:00On FarmingAbout a year ago I took a look at <a href="http://pyro.sourceforge.net/">Pyro</a>, aka Python Remote Objects. It's an extension package that lets you define code on one machine, and essentially run that code remotely on another machine.<br /><br />The cool bit is how it hides all the socket and TCP/IP baloney under the hood. It handles connects, disconnects, name lookups, all that. Harnessing something powerful and not needing to understand the gory details is always something I appreciate daily as a Tech Artist. Part of me wants to learn all the inner workings someday. But seriously, I'll probably live a full life and never know.<br /><br />Anyway, my first Pyro tool was a simple client/server batching utility. A server script would keep track of the slave machines connected and a little job queue, and assign the jobs as slaves became available. I didn't have time to make a GUI for it, but it worked. DOS commands or Python script jobs could be dished out to remote machines and it took very little code to do it. Then I got busy at work again and put the tool aside.<br /><br />So one of the many GDC 2008 lectures I wasn't able to attend was <a href="https://www.cmpevents.com/GD08/a.asp?option=C&V=11&SessID=6455">Life on the Bungie Farm: Fun Things to Do with 180 Servers</a> by Luis Villegas and Sean Shypula (<a href="http://www.bungie.net/images/Inside/publications/presentations/Life_on_the_Bungie_Farm.pptx">lecture slides</a>). It outlined a system used at Bungie to slave-out time-consuming or annoying processing tasks to a big set of rack-mounted PCs. It could do lighting processing for level artists, regular game builds and verification, whatever.<br /><br />My first thought was "DO WANT". My next thought was, my little slave tool could make a great start for something much bigger and better. I've done nothing notable on it yet, but I've been turning over details in my head, and hope to find time to expand on it soon.<br /><br />And to Luis and Sean, thanks for sharing!Unknownnoreply@blogger.com1tag:blogger.com,1999:blog-5843977256413530490.post-33649855454383331912008-02-24T23:23:00.007-06:002008-12-09T22:50:30.537-06:00GDC wrap-up<a href="http://www.gdconf.com/">GDC 2008</a> was quite a trip. The two lectures I gave appeared to go extremely well. High attendance and lots of great questions, reactions. Thanks to everyone that took the time to attend, we really appreciated it!<br /><br />I've posted our slides on the <a href="http://www.volition-inc.com/gdc">Volition GDC Archive</a>, along with the promised Python Example Files (<a href="http://www.volition-inc.com/gdc/GDC2008_AdamPletcher_PythonSamples.zip">zipfile</a>) from my Python for Technical Artists lecture. If you're a TA and enjoy scripting, definitely download them, pick 'em apart and do something cool with them.<br /><br />I wish I'd been able to attend more sessions myself, but between meetings and lecture prep I never hit as many as I'd like.Unknownnoreply@blogger.com0